Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActivityRecorderActivity.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.activityrecorder;
  23. import android.app.Activity;
  24. import android.app.ProgressDialog;
  25. import android.content.ComponentName;
  26. import android.content.Intent;
  27. import android.content.ServiceConnection;
  28. import android.content.pm.PackageManager.NameNotFoundException;
  29. import android.os.Bundle;
  30. import android.os.Handler;
  31. import android.os.IBinder;
  32. import android.os.RemoteException;
  33. import android.telephony.TelephonyManager;
  34. import android.util.Log;
  35. import android.view.View;
  36. import android.view.View.OnClickListener;
  37. import android.widget.ArrayAdapter;
  38. import android.widget.Button;
  39. import android.widget.ListView;
  40. import com.flurry.android.FlurryAgent;
  41. import java.util.List;
  42. import uk.co.md87.android.activityrecorder.rpc.ActivityRecorderBinder;
  43. import uk.co.md87.android.activityrecorder.rpc.Classification;
  44. import uk.co.md87.android.common.ExceptionHandler;
  45. /**
  46. *
  47. * @author chris
  48. */
  49. public class ActivityRecorderActivity extends Activity {
  50. ActivityRecorderBinder service = null;
  51. ProgressDialog dialog;
  52. final Handler handler = new Handler();
  53. private final ServiceConnection connection = new ServiceConnection() {
  54. public void onServiceConnected(ComponentName arg0, IBinder arg1) {
  55. service = ActivityRecorderBinder.Stub.asInterface(arg1);
  56. handler.postDelayed(updateRunnable, 500);
  57. }
  58. public void onServiceDisconnected(ComponentName arg0) {
  59. service = null;
  60. handler.removeCallbacks(updateRunnable);
  61. ((Button) findViewById(R.id.togglebutton)).setEnabled(false);
  62. }
  63. };
  64. private final Runnable updateRunnable = new Runnable() {
  65. public void run() {
  66. updateButton();
  67. handler.postDelayed(updateRunnable, 500);
  68. }
  69. };
  70. private final Runnable startRunnable = new Runnable() {
  71. public void run() {
  72. startService(new Intent(ActivityRecorderActivity.this,
  73. RecorderService.class));
  74. updateRunnable.run();
  75. }
  76. };
  77. private OnClickListener clickListener = new OnClickListener() {
  78. public void onClick(View arg0) {
  79. try {
  80. if (service.isRunning()) {
  81. FlurryAgent.onEvent("recording_stop");
  82. stopService(new Intent(ActivityRecorderActivity.this,
  83. RecorderService.class));
  84. unbindService(connection);
  85. bindService(new Intent(ActivityRecorderActivity.this, RecorderService.class),
  86. connection, BIND_AUTO_CREATE);
  87. } else {
  88. FlurryAgent.onEvent("recording_start");
  89. handler.removeCallbacks(updateRunnable);
  90. ((Button) findViewById(R.id.togglebutton)).setEnabled(false);
  91. dialog = ProgressDialog.show(ActivityRecorderActivity.this, "Starting service",
  92. "Please wait...", true);
  93. handler.postDelayed(startRunnable, 500);
  94. }
  95. } catch (RemoteException ex) {
  96. Log.e(getClass().getName(), "Unable to get service state", ex);
  97. }
  98. }
  99. };
  100. /** {@inheritDoc} */
  101. @Override
  102. public void onCreate(Bundle icicle) {
  103. super.onCreate(icicle);
  104. Thread.setDefaultUncaughtExceptionHandler(
  105. new ExceptionHandler("ActivityRecorder",
  106. "http://chris.smith.name/android/upload", getVersionName(), getIMEI()));
  107. bindService(new Intent(this, RecorderService.class), connection, BIND_AUTO_CREATE);
  108. setContentView(R.layout.main);
  109. ((Button) findViewById(R.id.togglebutton)).setEnabled(false);
  110. ((Button) findViewById(R.id.togglebutton)).setOnClickListener(clickListener);
  111. ((ListView) findViewById(R.id.list)).setAdapter(
  112. new ArrayAdapter<Classification>(this, R.layout.item));
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. protected void onDestroy() {
  117. super.onDestroy();
  118. unbindService(connection);
  119. }
  120. @SuppressWarnings("unchecked")
  121. void updateButton() {
  122. try {
  123. ((Button) findViewById(R.id.togglebutton)).setText(service.isRunning()
  124. ? R.string.service_enabled : R.string.service_disabled);
  125. ((Button) findViewById(R.id.togglebutton)).setEnabled(true);
  126. if (dialog != null) {
  127. dialog.dismiss();
  128. dialog = null;
  129. }
  130. final List<Classification> classifications = service.getClassifications();
  131. final ArrayAdapter<Classification> adapter = (ArrayAdapter<Classification>)
  132. ((ListView) findViewById(R.id.list)).getAdapter();
  133. if (classifications.isEmpty()) {
  134. adapter.clear();
  135. } else if (!adapter.isEmpty()) {
  136. final Classification myLast = adapter.getItem(adapter.getCount() - 1);
  137. final Classification expected = classifications.get(adapter.getCount() - 1);
  138. if (myLast.getClassification().equals(expected.getClassification())) {
  139. // Just update the end time
  140. myLast.updateEnd(expected.getEnd());
  141. adapter.notifyDataSetChanged();
  142. } else {
  143. // Something's gone wrong - the entries should match
  144. adapter.clear();
  145. }
  146. }
  147. for (int i = adapter.getCount(); i < classifications.size(); i++) {
  148. adapter.add(classifications.get(i).withContext(this));
  149. }
  150. } catch (RemoteException ex) {
  151. Log.e(getClass().getName(), "Unable to get service state", ex);
  152. }
  153. }
  154. public String getVersionName() {
  155. try {
  156. return getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
  157. } catch (NameNotFoundException ex) {
  158. return "Unknown";
  159. }
  160. }
  161. public String getIMEI() {
  162. return ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();
  163. }
  164. /** {@inheritDoc} */
  165. @Override
  166. protected void onStart() {
  167. super.onStart();
  168. FlurryAgent.onStartSession(this, "EMKSQFUWSCW51AKBL2JJ");
  169. }
  170. /** {@inheritDoc} */
  171. @Override
  172. protected void onStop() {
  173. super.onStop();
  174. FlurryAgent.onEndSession(this);
  175. }
  176. }