From 109cdabfbbbe2ebfb77f37e2a5ecf02e553a1106 Mon Sep 17 00:00:00 2001 From: Yuji Konishi Date: Sat, 30 Jul 2011 16:33:19 +0900 Subject: [PATCH] =?utf8?q?=E3=81=8A=E5=BE=85=E3=81=A1=E3=81=8F=E3=81=A0?= =?utf8?q?=E3=81=95=E3=81=84=E7=94=BB=E9=9D=A2=E3=81=AE=E6=B1=8E=E7=94=A8?= =?utf8?q?=E5=8C=96=20Activity=E3=81=AE=E3=83=A9=E3=82=A4=E3=83=95?= =?utf8?q?=E3=82=B5=E3=82=A4=E3=82=AF=E3=83=AB=E3=81=AE=E8=80=83=E6=85=AE?= =?utf8?q?=E3=81=8C=E5=BF=85=E8=A6=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../src/com/td/TrainDelayedActivity.java | 55 ++++-- .../TrainDelayed/src/com/td/WaitActivity.java | 184 ++++++++++----------- .../TrainDelayed/src/com/td/wait/IWating.java | 11 ++ .../src/com/td/wait/InitializeWating.java | 31 ++++ .../src/com/td/wait/TrainServiceWaiting.java | 66 ++++++++ .../TrainDelayed/src/com/td/wait/WatingImpl.java | 17 ++ 6 files changed, 252 insertions(+), 112 deletions(-) create mode 100644 workspace/TrainDelayed/src/com/td/wait/IWating.java create mode 100644 workspace/TrainDelayed/src/com/td/wait/InitializeWating.java create mode 100644 workspace/TrainDelayed/src/com/td/wait/TrainServiceWaiting.java create mode 100644 workspace/TrainDelayed/src/com/td/wait/WatingImpl.java diff --git a/workspace/TrainDelayed/src/com/td/TrainDelayedActivity.java b/workspace/TrainDelayed/src/com/td/TrainDelayedActivity.java index 1cae341..0d7632d 100644 --- a/workspace/TrainDelayed/src/com/td/TrainDelayedActivity.java +++ b/workspace/TrainDelayed/src/com/td/TrainDelayedActivity.java @@ -23,10 +23,13 @@ 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.Debug; import com.td.utility.EditPrefUtil; +import com.td.wait.InitializeWating; +import com.td.wait.TrainServiceWaiting; public class TrainDelayedActivity extends BaseActivity { private TextView textView1; @@ -82,21 +85,17 @@ public class TrainDelayedActivity extends BaseActivity { public void onClick(View v) { Intent intent = new Intent(TrainDelayedActivity.this, (Class) WaitActivity.class); - startActivityForResult(intent, -1); - -// RouteUpdater up = new RouteUpdater(); -// up.update(); - -// TrainDao.getInstance().search(1); + intent.putExtra(WaitActivity.CLASS, + TrainServiceWaiting.class.getName()); + intent.putExtra(WaitActivity.MESSAGE1, + getString(R.string.start_rouote_msg)); + intent.putExtra(WaitActivity.MESSAGE2, + getString(R.string.end_rouote_msg)); + intent.putExtra(WaitActivity.MESSAGE3, + ""); -// HistoryDao.getInstance().delete(TrainDelayedActivity.this); - -// Intent intent = new Intent("com.td.broadcast.DelayReceiver"); -// intent.putExtra("TEXT", "ƒTƒ“ƒvƒ‹ƒƒbƒZ[ƒW"); -// sendBroadcast(intent); -// -// SchedulerTest.repeat(TrainDelayedActivity.this); -// SchedulerTest.main(); + WaitActivity.init(TrainDelayedActivity.this); + startActivityForResult(intent, -1); }}); button2 = (Button) this.findViewById(R.id.button02); @@ -387,8 +386,19 @@ public class TrainDelayedActivity extends BaseActivity { } private void initizlize(){ - // TODO - // WaitActivity + Intent intent = new Intent(this, + (Class) WaitActivity.class); + intent.putExtra(WaitActivity.CLASS, + InitializeWating.class.getName()); + intent.putExtra(WaitActivity.MESSAGE1, + getString(R.string.start_rouote_msg)); + intent.putExtra(WaitActivity.MESSAGE2, + getString(R.string.end_rouote_msg)); + intent.putExtra(WaitActivity.MESSAGE3, + ""); + + WaitActivity.init(this); + startActivityForResult(intent, -1); } private void debug(){ @@ -401,6 +411,19 @@ public class TrainDelayedActivity extends BaseActivity { pref.update(); setStatus(); break; + case 2: + // ƒXƒPƒWƒ…[ƒ‹‚̃eƒXƒg + SchedulerTest.repeat(TrainDelayedActivity.this); + SchedulerTest.main(); + break; + case 3: + // ƒuƒ[ƒhƒLƒƒƒXƒg + Intent intent = new Intent("com.td.broadcast.DelayReceiver"); + intent.putExtra("TEXT", "ƒTƒ“ƒvƒ‹ƒƒbƒZ[ƒW"); + sendBroadcast(intent); + break; + default: + break; } } } \ No newline at end of file diff --git a/workspace/TrainDelayed/src/com/td/WaitActivity.java b/workspace/TrainDelayed/src/com/td/WaitActivity.java index d71ce2a..d9d7b70 100644 --- a/workspace/TrainDelayed/src/com/td/WaitActivity.java +++ b/workspace/TrainDelayed/src/com/td/WaitActivity.java @@ -1,132 +1,121 @@ package com.td; -import android.content.ComponentName; import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; -import android.os.IBinder; -import android.os.RemoteException; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; -import android.widget.Toast; -import com.td.service.ITrainService; -import com.td.service.ITrainServiceCallback; -import com.td.service.TrainService; import com.td.utility.BaseActivity; +import com.td.utility.EditPrefUtil; +import com.td.wait.IWating; + +public class WaitActivity extends BaseActivity { + public static final String PREF_WIT_FLAG = "WIT_FLAG"; + + public static final String CLASS = "CLASS"; + public static final String MESSAGE1 = "MESSAGE1"; + public static final String MESSAGE2 = "MESSAGE2"; + public static final String MESSAGE3 = "MESSAGE3"; + + public static final int ID_OK = 0; + public static final int ID_NG = -1; -public class WaitActivity extends BaseActivity implements ServiceConnection { - private static final int ID = 100; - private ITrainService serviceImpl; private TextView textView; - private WaitView waitView; - private Button button; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.wait); - - textView = (TextView)findViewById(R.id.textViewa1); - waitView = (WaitView)findViewById(R.id.waitViewa1); - button = (Button)findViewById(R.id.buttona1); - - button.setOnClickListener(new OnClickListener() { + private WaitView waitView; + private Button button; + + private IWating waiting = null; + private String clazzName; + private String message1; + private String message2; + private String message3; + + private EditPrefUtil pref = new EditPrefUtil(this); + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.wait); + + textView = (TextView) findViewById(R.id.textViewa1); + waitView = (WaitView) findViewById(R.id.waitViewa1); + button = (Button) findViewById(R.id.buttona1); + + button.setOnClickListener(new OnClickListener() { public void onClick(View v) { WaitActivity.this.finish(); } }); button.setEnabled(false); - } - - @Override - protected void onStart() { - super.onStart(); - - // TODO -// Intent intent = new Intent(); -// intent.setClassName(this, "com.td.SampleService"); - Intent intent = new Intent(this, TrainService.class); - - this.bindService(intent, this, Context.BIND_AUTO_CREATE); - - // Activity‚ªŽ€‚ñ‚Å‚àƒT[ƒrƒX‚ðI—¹‚³‚¹‚È‚¢‚½‚ß - startService(intent); - } - + + Bundle extras = getIntent().getExtras(); + clazzName = extras.getString(CLASS); + message1 = extras.getString(MESSAGE1); + message2 = extras.getString(MESSAGE2); + message3 = extras.getString(MESSAGE3); + + textView.setText(message1); + + int flag = pref.getInt(PREF_WIT_FLAG, 0); + if (flag == 0){ + Class clazz; + try { + clazz = (Class) Class.forName(clazzName); + IWating obj = clazz.newInstance(); + waiting = obj; + waiting.setContext(this); + waiting.setHandler(handler); + } catch (Exception e) { + this.setResult(ID_NG); + this.finish(); + } + } + pref.put(PREF_WIT_FLAG, 1); + } + + @Override + protected void onStart() { + super.onStart(); + + if (waiting != null) { + waiting.onStart(); + } + } + @Override protected void onStop() { - unbindService(this); - - // ƒT[ƒrƒX‚́A’âŽ~‚³‚¹‚È‚¢(stopService‚ðŒÄ‚΂Ȃ¢) - //stopService(new Intent(this, TrainService.class)); + if (waiting != null) { + waiting.onStop(); + } super.onStop(); } - @Override protected void onDestroy() { super.onDestroy(); } - - public void onServiceConnected(ComponentName name, IBinder service) { - serviceImpl = ITrainService.Stub.asInterface(service); - - try { - serviceImpl.setListener(interfaceImpl); - - doService(); - } catch (RemoteException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public void onServiceDisconnected(ComponentName name) { - // Service‚Ƃ̐ڑ±‚ª—\Šú‚¹‚¸Ø‚ꂽiKILL‚³‚ê‚é‚Ȃǁjê‡‚ɌĂ΂ê‚é - serviceImpl = null; - } - - private void doService(){ - if (serviceImpl == null){ - return; - } - try { - int val = serviceImpl.doExecute("123"); - Toast.makeText(WaitActivity.this, "v=" + val, Toast.LENGTH_LONG).show(); - } catch (RemoteException e) { - // TODO - e.printStackTrace(); - } - } - - private ITrainServiceCallback.Stub interfaceImpl = new ITrainServiceCallback.Stub() { - public void notify(String msg) throws RemoteException { - //ƒnƒ“ƒhƒ‰‚ɃƒbƒZ[ƒW‚𑗐M - //ƒƒbƒZ[ƒW‚Í‚¢‚¿‚¢‚¿ì¬‚·‚é‚ƃRƒXƒg‚ª‚©‚©‚邽‚߁AƒƒbƒZ[ƒWƒv[ƒ‹‚É‚ ‚éƒCƒ“ƒXƒ^ƒ“ƒX‚ðŽg—p‚·‚é(obtainMessage) - //‘—M“à—e‚̓ƒbƒZ[ƒWŽí•Ê‚ƈø”(ˆø”‚ɉž‚¶‚ăI[ƒo[ƒ[ƒh‚³‚ê‚Ä‚¢‚éobtainMessage‚̃oƒŠƒG[ƒVƒ‡ƒ“‚ªŽg‚¦‚é) - handler.sendMessage(handler.obtainMessage(ID)); - } - }; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { - switch(msg.what) { - case ID: -// //’Ê’m‚³‚ꂽŽžŠÔ‚ð•\Ž¦‚·‚éB -// final TextView text = (TextView)findViewById(R.id.play_time); -// final String minute = Integer.toString(msg.arg1/60000); -// final String second = Integer.toString(msg.arg1%60000/1000); -// text.setText(minute+":"+second); + int code = msg.what; + + switch (code) { + case ID_OK: + case ID_NG: // TODO ƒvƒƒZƒX‚̍ċN“®‚ɂ͑Ήž‚Å‚«‚È‚¢ waitView.stop(); - textView.setText(getString(R.string.end_rouote_msg)); + textView.setText((code == ID_OK)? message2 : message3); button.setEnabled(true); + + if (waiting != null) { + waiting.setContext(null); + waiting.setHandler(null); + waiting = null; + } break; default: super.handleMessage(msg); @@ -135,5 +124,8 @@ public class WaitActivity extends BaseActivity implements ServiceConnection { } }; - + public static void init(Context context){ + EditPrefUtil pref = new EditPrefUtil(context); + pref.remove(WaitActivity.PREF_WIT_FLAG); + } } diff --git a/workspace/TrainDelayed/src/com/td/wait/IWating.java b/workspace/TrainDelayed/src/com/td/wait/IWating.java new file mode 100644 index 0000000..6f438f5 --- /dev/null +++ b/workspace/TrainDelayed/src/com/td/wait/IWating.java @@ -0,0 +1,11 @@ +package com.td.wait; + +import android.content.Context; +import android.os.Handler; + +public interface IWating { + void setContext(Context context); + void setHandler(Handler handler); + void onStart(); + void onStop(); +} \ No newline at end of file diff --git a/workspace/TrainDelayed/src/com/td/wait/InitializeWating.java b/workspace/TrainDelayed/src/com/td/wait/InitializeWating.java new file mode 100644 index 0000000..8d00501 --- /dev/null +++ b/workspace/TrainDelayed/src/com/td/wait/InitializeWating.java @@ -0,0 +1,31 @@ +package com.td.wait; + +import com.td.WaitActivity; + +public class InitializeWating extends WatingImpl { + private Thread t = null; + + public void onStart() { + if (t != null){ + return; + } + t = new Thread() { + public void run() { + try { + Thread.sleep(10 * 1000); + handler.sendMessage(handler + .obtainMessage(WaitActivity.ID_OK)); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }; + t.start(); + } + + public void onStop() { + + } + +} diff --git a/workspace/TrainDelayed/src/com/td/wait/TrainServiceWaiting.java b/workspace/TrainDelayed/src/com/td/wait/TrainServiceWaiting.java new file mode 100644 index 0000000..993df30 --- /dev/null +++ b/workspace/TrainDelayed/src/com/td/wait/TrainServiceWaiting.java @@ -0,0 +1,66 @@ +package com.td.wait; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.os.RemoteException; +import android.widget.Toast; + +import com.td.WaitActivity; +import com.td.service.ITrainService; +import com.td.service.ITrainServiceCallback; +import com.td.service.TrainService; + +public class TrainServiceWaiting extends WatingImpl implements ServiceConnection { + private ITrainService serviceImpl; + + public void onStart(){ + Intent intent = new Intent(context, TrainService.class); + + context.bindService(intent, this, Context.BIND_AUTO_CREATE); + + // Activity‚ªŽ€‚ñ‚Å‚àƒT[ƒrƒX‚ðI—¹‚³‚¹‚È‚¢‚½‚ß + context.startService(intent); + } + + public void onStop(){ + context.unbindService(this); + context = null; + } + + public void onServiceConnected(ComponentName name, IBinder service) { + serviceImpl = ITrainService.Stub.asInterface(service); + + try { + serviceImpl.setListener(interfaceImpl); + + doService(); + } catch (RemoteException e) { + handler.sendMessage(handler.obtainMessage(WaitActivity.ID_NG)); + } + } + + public void onServiceDisconnected(ComponentName name) { + // Service‚Ƃ̐ڑ±‚ª—\Šú‚¹‚¸Ø‚ꂽiKILL‚³‚ê‚é‚Ȃǁjê‡‚ɌĂ΂ê‚é + serviceImpl = null; + } + + private void doService() throws RemoteException{ + if (serviceImpl == null){ + return; + } + int val = serviceImpl.doExecute("123"); + Toast.makeText(context, "v=" + val, Toast.LENGTH_LONG).show(); + } + + private ITrainServiceCallback.Stub interfaceImpl = new ITrainServiceCallback.Stub() { + public void notify(String msg) throws RemoteException { + //ƒnƒ“ƒhƒ‰‚ɃƒbƒZ[ƒW‚𑗐M + //ƒƒbƒZ[ƒW‚Í‚¢‚¿‚¢‚¿ì¬‚·‚é‚ƃRƒXƒg‚ª‚©‚©‚邽‚߁AƒƒbƒZ[ƒWƒv[ƒ‹‚É‚ ‚éƒCƒ“ƒXƒ^ƒ“ƒX‚ðŽg—p‚·‚é(obtainMessage) + //‘—M“à—e‚̓ƒbƒZ[ƒWŽí•Ê‚ƈø”(ˆø”‚ɉž‚¶‚ăI[ƒo[ƒ[ƒh‚³‚ê‚Ä‚¢‚éobtainMessage‚̃oƒŠƒG[ƒVƒ‡ƒ“‚ªŽg‚¦‚é) + handler.sendMessage(handler.obtainMessage(WaitActivity.ID_OK)); + } + }; +} diff --git a/workspace/TrainDelayed/src/com/td/wait/WatingImpl.java b/workspace/TrainDelayed/src/com/td/wait/WatingImpl.java new file mode 100644 index 0000000..79ba033 --- /dev/null +++ b/workspace/TrainDelayed/src/com/td/wait/WatingImpl.java @@ -0,0 +1,17 @@ +package com.td.wait; + +import android.content.Context; +import android.os.Handler; + +public abstract class WatingImpl implements IWating { + protected Context context; + protected Handler handler; + + public void setContext(Context context){ + this.context = context; + } + + public void setHandler(Handler handler) { + this.handler = handler; + } +} -- 2.11.0