OSDN Git Service

t25722 t25722
authorYuji Konishi <yuji.k64613@gmail.com>
Mon, 18 Jul 2011 05:28:00 +0000 (14:28 +0900)
committerYuji Konishi <yuji.k64613@gmail.com>
Mon, 18 Jul 2011 05:28:00 +0000 (14:28 +0900)
workspace/TrainDelayed/AndroidManifest.xml
workspace/TrainDelayed/src/com/td/TrainDelayedActivity.java
workspace/TrainDelayed/src/com/td/broadcast/DelayReceiver.java
workspace/TrainDelayed/src/com/td/service/SchedulerTest.java
workspace/TrainDelayed/src/com/td/service/TrainDelayedServiceImpl.java
workspace/TrainDelayed/src/com/td/utility/DateUtil.java

index f770ab1..62dd025 100644 (file)
@@ -28,6 +28,8 @@
                <receiver android:name=".broadcast.DelayReceiver">
                        <intent-filter>
                                <action android:name="com.td.broadcast.DelayReceiver.DEBUG" />
+                               <action android:name="com.td.broadcast.DelayReceiver.START_SERVICE" />
+                               <action android:name="com.td.broadcast.DelayReceiver.STOP_SERVICE" />
                        </intent-filter>
                </receiver>
     </application>
index ab5a209..cc4c7ba 100644 (file)
@@ -5,9 +5,11 @@ import java.util.List;
 import android.app.AlarmManager;
 import android.app.AlertDialog;
 import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.graphics.Color;
 import android.os.Bundle;
 import android.view.View;
@@ -16,18 +18,17 @@ import android.widget.Button;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import com.td.broadcast.DelayReceiver;
 import com.td.db.Monitor;
 import com.td.db.MonitorDao;
 import com.td.db.Train;
 import com.td.db.TrainDao;
+import com.td.service.SchedulerTest;
 import com.td.service.TrainDelayedServiceImpl;
 import com.td.utility.BaseActivity;
 import com.td.utility.EditPrefUtil;
-import com.td.utility.Util;
 
 public class TrainDelayedActivity extends BaseActivity {
-       public static final String PREF_TRD_SFLG = "TRD_SFLG";
-       
        private TextView textView1;
        private Button button1;
        private Button button2;
@@ -47,13 +48,34 @@ public class TrainDelayedActivity extends BaseActivity {
        private LinearLayout linearLayout9;
 
        private EditPrefUtil pref = new EditPrefUtil(this);
-       private int prefSFlag;
+       private int prefSvsStatus;
+       private boolean isClick = false;
 
+       //private DelayReceiver receiver = new DelayReceiver(this);
+       private BroadcastReceiver receiver = new BroadcastReceiver(){
+               @Override
+               public void onReceive(Context context, Intent intent) {
+                       String action = intent.getAction();
+                       
+                       if (action.compareTo(DelayReceiver.START_SERVICE) == 0){
+                               setStatus();
+                       }
+                       else if (action.compareTo(DelayReceiver.STOP_SERVICE) == 0){
+                               // \96¢\8eg\97p
+                       }
+               }               
+       };
+       
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
 
+               IntentFilter intentFilter = new IntentFilter();
+               intentFilter.addAction(DelayReceiver.START_SERVICE);
+               intentFilter.addAction(DelayReceiver.STOP_SERVICE);
+               registerReceiver(receiver, intentFilter);
+             
                button1 = (Button) this.findViewById(R.id.button01);
                button1.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
@@ -61,11 +83,8 @@ public class TrainDelayedActivity extends BaseActivity {
                                intent.putExtra("TEXT", "\83T\83\93\83v\83\8b\83\81\83b\83Z\81[\83W");
                                sendBroadcast(intent);
                                
-                               //pref.put(TrainDelayedServiceImpl.PREF_SVS_DBFLG, 0);
-                               //pref.update();
-
-                               //SchedulerTest.repeat(TrainDelayedActivity.this);
-                               //SchedulerTest.main();
+                               SchedulerTest.repeat(TrainDelayedActivity.this);
+                               SchedulerTest.main();
                        }});
 
                button2 = (Button) this.findViewById(R.id.button02);
@@ -89,6 +108,10 @@ public class TrainDelayedActivity extends BaseActivity {
                button4 = (Button) this.findViewById(R.id.button04);
                button4.setOnClickListener(new OnClickListener() {
                        public void onClick(View view) {
+                               if (isClick){
+                                       return;
+                               }
+                               isClick = true;
                                new AlertDialog.Builder(TrainDelayedActivity.this)
                                        .setTitle(getString(R.string.warn_dialog_title))
                                        .setMessage(getString(R.string.start_service_qmsg))
@@ -97,17 +120,19 @@ public class TrainDelayedActivity extends BaseActivity {
                                                        // \83T\81[\83r\83X\82ð\8aJ\8en
                                                        Intent intent = new Intent(TrainDelayedActivity.this,
                                                                        TrainDelayedServiceImpl.class);
+                                                       intent.setAction(TrainDelayedServiceImpl.START_ACTION);
                                                        startService(intent);
                                                        
-                                                       // TODO
-                                                       pref.put(PREF_TRD_SFLG, 1);
+                                                       pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 1);
                                                        pref.update();
                                                        setStatus();
+                                                       
+                                                       isClick = false;
                                                }
                                        })
                                        .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int whichButton) {
-                                                       ;
+                                                       isClick = false;
                                                }
                                        })
                                        .create().show();
@@ -117,6 +142,10 @@ public class TrainDelayedActivity extends BaseActivity {
                button5 = (Button) this.findViewById(R.id.button05);
                button5.setOnClickListener(new OnClickListener() {
                        public void onClick(View view) {
+                               if (isClick){
+                                       return;
+                               }
+                               isClick = true;
                                // TODO
                                //HistoryDao.getInstance().delete(TrainDelayedActivity.this);
 
@@ -132,15 +161,16 @@ public class TrainDelayedActivity extends BaseActivity {
                                                AlarmManager alerm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                                                alerm.cancel(alarmSender);
                                                
-                                               // TODO
-                                               pref.put(PREF_TRD_SFLG, 0);
+                                               pref.put(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
                                                pref.update();
                                                setStatus();
+
+                                               isClick = false;
                                        }
                                })
                                .setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
-                                               ;
+                                               isClick = false;
                                        }
                                })
                                .create().show();
@@ -194,7 +224,7 @@ public class TrainDelayedActivity extends BaseActivity {
 
        private void setStatus() {
                String text = "";
-               prefSFlag = pref.getInt(PREF_TRD_SFLG, 0);
+               prefSvsStatus = pref.getInt(TrainDelayedServiceImpl.PREF_SVS_STATUS, 0);
                
                boolean enabled1 = true;
                boolean enabled2 = true;
@@ -232,12 +262,18 @@ public class TrainDelayedActivity extends BaseActivity {
                        msgNo = (msgNo < 0)? 3 : msgNo;
                }
                
-               if (Util.toBoolean(prefSFlag)){
-                       enabled4 = false;                       
-               }
-               else {
+               switch (prefSvsStatus){
+               case 0:
                        msgNo = (msgNo < 0)? 4 : msgNo;
                        enabled5 = false;                                               
+                       break;
+               case 1:
+                       enabled4 = false;                       
+                       enabled5 = false;                                               
+                       break;
+               case 2:
+                       enabled4 = false;                       
+                       break;
                }
                
                button1.setEnabled(enabled1);
index ce75348..c4dcee5 100644 (file)
@@ -8,6 +8,8 @@ import android.widget.Toast;
 \r
 public class DelayReceiver extends BroadcastReceiver {\r
        public static String DEBUG = "com.td.broadcast.DelayReceiver.DEBUG";\r
+       public static String START_SERVICE = "com.td.broadcast.DelayReceiver.START_SERVICE";\r
+       public static String STOP_SERVICE = "com.td.broadcast.DelayReceiver.STOP_SERVICE";\r
        \r
        @Override\r
        public void onReceive(Context context, Intent intent) {\r
@@ -17,5 +19,11 @@ public class DelayReceiver extends BroadcastReceiver {
                        String text = bundle.getString("TEXT");\r
                        Toast.makeText(context, text, Toast.LENGTH_LONG).show();                        \r
                }\r
+               else if (action.equals(START_SERVICE)){\r
+                       ;\r
+               }\r
+               else if (action.equals(STOP_SERVICE)){\r
+                       ;\r
+               }\r
        }\r
 }\r
index 83a2e56..8da4e73 100644 (file)
@@ -20,6 +20,8 @@ public class SchedulerTest {
                Scheduler sc = new Scheduler();\r
                long t = cal.getTimeInMillis();\r
                \r
+               sc.makeSchedule();\r
+               \r
                for (int i = 0; i < 5; i++){\r
                        t = sc.calcSchedule(t);\r
                        Date d = DateUtil.getDate(t);\r
index b601fb2..60a8e57 100644 (file)
@@ -29,10 +29,10 @@ import com.td.utility.EditPrefUtil;
 import com.td.utility.Util;\r
 \r
 public class TrainDelayedServiceImpl extends Service {\r
+       public static final String PREF_SVS_STATUS = "SVS_STATUS";\r
        public static final String PREF_SVS_DBFLG = "SVS_DBFLG";\r
-       public static final String ACTION = TrainDelayedServiceImpl.class\r
-                       .getSimpleName();\r
-       \r
+       public static final String START_ACTION = "com.td.START";\r
+\r
        private final IBinder binder = new Binder();\r
        private static Scheduler scheduler = null;\r
 \r
@@ -41,6 +41,7 @@ public class TrainDelayedServiceImpl extends Service {
        private static AsyncTwitterFactory factory = new AsyncTwitterFactory();\r
        private AsyncTwitter twitter = factory.getInstance();\r
        private TrainTwitterAdapter listener = new TrainTwitterAdapter(this);\r
+       private String action = null;\r
 \r
        private static int id = 0;\r
 \r
@@ -53,11 +54,11 @@ public class TrainDelayedServiceImpl extends Service {
                try {\r
                        Debug.d(this, "run() " + id);\r
 \r
-                       if (scheduler == null){\r
+                       if (scheduler == null) {\r
                                return;\r
                        }\r
                        List<Train> list = scheduler.getTrains();\r
-                       if (list == null){\r
+                       if (list == null) {\r
                                return;\r
                        }\r
 \r
@@ -75,8 +76,8 @@ public class TrainDelayedServiceImpl extends Service {
                                twitter.search(query);\r
                        }\r
                        // TODO\r
-                       //notificate("id = " + id);\r
-                       \r
+                       // notificate("id = " + id);\r
+\r
                        id = (id + 1) % 1000000;\r
                } catch (Exception e) {\r
                        Debug.d(this, null, e);\r
@@ -120,56 +121,77 @@ public class TrainDelayedServiceImpl extends Service {
 \r
        private Runnable task = new Runnable() {\r
                public void run() {\r
+                       boolean flag = false;\r
                        try {\r
-                               long now = System.currentTimeMillis();\r
-\r
-                               Debug.d(this, "id=" + id);\r
-                               synchronized (binder) {\r
-                                       try {\r
-                                               int prefDBFlag = pref.getInt(PREF_SVS_DBFLG, 0);\r
-                                               if (!Util.toBoolean(prefDBFlag)){\r
-                                                       pref.put(PREF_SVS_DBFLG, 1);\r
-                                                       pref.update();\r
-                                                       scheduler = null;\r
+                               try {\r
+                                       long now = System.currentTimeMillis();\r
+\r
+                                       Debug.d(this, "id=" + id);\r
+                                       synchronized (binder) {\r
+                                               try {\r
+                                                       int prefDBFlag = pref.getInt(PREF_SVS_DBFLG, 0);\r
+                                                       if (!Util.toBoolean(prefDBFlag)) {\r
+                                                               pref.put(PREF_SVS_DBFLG, 1);\r
+                                                               pref.update();\r
+                                                               scheduler = null;\r
+                                                       }\r
+\r
+                                                       if (scheduler == null) {\r
+                                                               scheduler = new Scheduler();\r
+                                                               DatabaseHelper.init(getApplicationContext());\r
+                                                       }\r
+                                                       scheduler.init();\r
+                                                       schedule(-1);\r
+                                               } catch (Exception e) {\r
+                                                       // TODO\r
+                                                       Debug.t(TrainDelayedServiceImpl.this,\r
+                                                                       e.getMessage());\r
+                                                       Debug.d(this, null, e);\r
                                                }\r
+                                       }\r
 \r
-                                               if (scheduler == null){\r
-                                                       scheduler = new Scheduler();\r
-                                                       DatabaseHelper.init(getApplicationContext());\r
-                                               }\r
-                                               scheduler.init();\r
-                                               schedule(-1);\r
-                                       } catch (Exception e) {\r
-                                               // TODO\r
-                                               Debug.t(TrainDelayedServiceImpl.this, e.getMessage());\r
-                                               Debug.d(this, null, e);\r
+                                       long next = scheduler.calcSchedule(now);\r
+                                       String nextStr = DateUtil.formatted(next);\r
+                                       Debug.t(TrainDelayedServiceImpl.this, nextStr);\r
+                                       Debug.d(this, "calcSchedule " + nextStr);\r
+\r
+                                       // \8e\9f\89ñ\8bN\93®\93o\98^\r
+                                       PendingIntent alarmSender = PendingIntent.getService(\r
+                                                       TrainDelayedServiceImpl.this, 0, new Intent(\r
+                                                                       TrainDelayedServiceImpl.this,\r
+                                                                       TrainDelayedServiceImpl.class), 0);\r
+                                       AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\r
+                                       alarm.set(AlarmManager.RTC, next, alarmSender);\r
+                                       flag = true;\r
+\r
+                                       if (action.compareTo(START_ACTION) == 0) {\r
+                                               pref.put(PREF_SVS_STATUS, 2);\r
+                                               pref.update();\r
+                                               \r
+                                               Intent intent = new Intent(DelayReceiver.START_SERVICE);\r
+                                               TrainDelayedServiceImpl.this.sendBroadcast(intent);                                             \r
+\r
+                                               Debug.t(TrainDelayedServiceImpl.this, START_ACTION);\r
                                        }\r
+\r
+                                       // \83T\81[\83r\83X\8fI\97¹\r
+                                       TrainDelayedServiceImpl.this.stopSelf();\r
+\r
+                                       id = (id + 1) % 1000000;\r
+                               } catch (Exception e) {\r
+                                       Debug.t(TrainDelayedServiceImpl.this, e.getMessage());\r
+                                       Debug.d(this, null, e);\r
+                               }\r
+                       } finally {\r
+                               if (!flag){\r
+                                       pref.put(PREF_SVS_STATUS, 0);\r
+                                       pref.update();                                  \r
+\r
+                                       Intent intent = new Intent(DelayReceiver.STOP_SERVICE);\r
+                                       TrainDelayedServiceImpl.this.sendBroadcast(intent);                                             \r
                                }\r
-                               \r
-                               long next = scheduler.calcSchedule(now);\r
-                               String nextStr = DateUtil.formatted(next);\r
-                               Debug.t(TrainDelayedServiceImpl.this, nextStr);\r
-                               Debug.d(this, "calcSchedule " + nextStr);\r
-                               \r
-                               // \8e\9f\89ñ\8bN\93®\93o\98^\r
-                               PendingIntent alarmSender = PendingIntent.getService(\r
-                                               TrainDelayedServiceImpl.this, 0, new Intent(\r
-                                                               TrainDelayedServiceImpl.this,\r
-                                                               TrainDelayedServiceImpl.class), 0);\r
-                               AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\r
-                               alarm.set(AlarmManager.RTC, next,\r
-                                               alarmSender);\r
-\r
-                               // \83T\81[\83r\83X\8fI\97¹\r
-                               TrainDelayedServiceImpl.this.stopSelf();\r
-                               \r
-                               id = (id + 1) % 1000000;\r
-                       } catch (Exception e) {\r
-                               Debug.t(TrainDelayedServiceImpl.this, e.getMessage());\r
-                               Debug.d(this, null, e);\r
                        }\r
                }\r
-\r
        };\r
 \r
        @Override\r
@@ -178,6 +200,9 @@ public class TrainDelayedServiceImpl extends Service {
 \r
                Debug.d(this, Debug.ON_START);\r
 \r
+               action = intent.getAction();\r
+               action = (action == null) ? "" : action;\r
+\r
                Thread thread = new Thread(null, task,\r
                                TrainDelayedServiceImpl.class.getSimpleName());\r
                thread.start();\r
@@ -207,7 +232,7 @@ public class TrainDelayedServiceImpl extends Service {
        }\r
 \r
        public List<Keyword> getKeywords() {\r
-               if (scheduler == null){\r
+               if (scheduler == null) {\r
                        return null;\r
                }\r
                return scheduler.getKeywords();\r
index 118ccb0..2bc4fcf 100644 (file)
@@ -8,7 +8,7 @@ public class DateUtil {
        public static final int WEEK = 7;\r
        public static final int HM_MIN = 0;\r
        public static final int HM_MAX = 9999;\r
-       public static final int LEN = 10;\r
+       public static final int LEN = 1;\r
        private static DecimalFormat fmt2 = new DecimalFormat("00");\r
        \r
        public static int getYmd(Date date){\r