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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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(new ExceptionHandler(this));
  105. bindService(new Intent(this, RecorderService.class), connection, BIND_AUTO_CREATE);
  106. setContentView(R.layout.main);
  107. ((Button) findViewById(R.id.togglebutton)).setEnabled(false);
  108. ((Button) findViewById(R.id.togglebutton)).setOnClickListener(clickListener);
  109. ((ListView) findViewById(R.id.list)).setAdapter(
  110. new ArrayAdapter<Classification>(this, R.layout.item));
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. protected void onDestroy() {
  115. super.onDestroy();
  116. unbindService(connection);
  117. }
  118. @SuppressWarnings("unchecked")
  119. void updateButton() {
  120. try {
  121. ((Button) findViewById(R.id.togglebutton)).setText(service.isRunning()
  122. ? R.string.service_enabled : R.string.service_disabled);
  123. ((Button) findViewById(R.id.togglebutton)).setEnabled(true);
  124. if (dialog != null) {
  125. dialog.dismiss();
  126. dialog = null;
  127. }
  128. final List<Classification> classifications = service.getClassifications();
  129. final ArrayAdapter<Classification> adapter = (ArrayAdapter<Classification>)
  130. ((ListView) findViewById(R.id.list)).getAdapter();
  131. if (classifications.isEmpty()) {
  132. adapter.clear();
  133. } else if (!adapter.isEmpty()) {
  134. final Classification myLast = adapter.getItem(adapter.getCount() - 1);
  135. final Classification expected = classifications.get(adapter.getCount() - 1);
  136. if (myLast.getClassification().equals(expected.getClassification())) {
  137. // Just update the end time
  138. myLast.updateEnd(expected.getEnd());
  139. adapter.notifyDataSetChanged();
  140. } else {
  141. // Something's gone wrong - the entries should match
  142. adapter.clear();
  143. }
  144. }
  145. for (int i = adapter.getCount(); i < classifications.size(); i++) {
  146. adapter.add(classifications.get(i).withContext(this));
  147. }
  148. } catch (RemoteException ex) {
  149. Log.e(getClass().getName(), "Unable to get service state", ex);
  150. }
  151. }
  152. /** {@inheritDoc} */
  153. @Override
  154. protected void onStart() {
  155. super.onStart();
  156. FlurryAgent.onStartSession(this, "EMKSQFUWSCW51AKBL2JJ");
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. protected void onStop() {
  161. super.onStop();
  162. FlurryAgent.onEndSession(this);
  163. }
  164. }