PageRenderTime 70ms CodeModel.GetById 18ms RepoModel.GetById 2ms app.codeStats 0ms

/src/edu/umich/PowerTutor/ui/UMLogger.java

https://github.com/msg555/PowerTutor
Java | 355 lines | 303 code | 31 blank | 21 comment | 16 complexity | 3ffda58d1a9de34218f6fda417ce6e37 MD5 | raw file
  1. /*
  2. Copyright (C) 2011 The University of Michigan
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. Please send inquiries to powertutor@umich.edu
  14. */
  15. package edu.umich.PowerTutor.ui;
  16. import edu.umich.PowerTutor.R;
  17. import edu.umich.PowerTutor.phone.PhoneSelector;
  18. import edu.umich.PowerTutor.service.ICounterService;
  19. import edu.umich.PowerTutor.service.UMLoggerService;
  20. import android.app.Activity;
  21. import android.app.AlertDialog;
  22. import android.app.Dialog;
  23. import android.content.ComponentName;
  24. import android.content.Context;
  25. import android.content.DialogInterface;
  26. import android.content.Intent;
  27. import android.content.ServiceConnection;
  28. import android.content.SharedPreferences;
  29. import android.content.res.Configuration;
  30. import android.os.Bundle;
  31. import android.os.Environment;
  32. import android.os.IBinder;
  33. import android.os.RemoteException;
  34. import android.preference.PreferenceManager;
  35. import android.util.Log;
  36. import android.view.Menu;
  37. import android.view.MenuItem;
  38. import android.view.View;
  39. import android.widget.AdapterView;
  40. import android.widget.AdapterView.OnItemSelectedListener;
  41. import android.widget.ArrayAdapter;
  42. import android.widget.Button;
  43. import android.widget.SeekBar;
  44. import android.widget.SeekBar.OnSeekBarChangeListener;
  45. import android.widget.Spinner;
  46. import android.widget.TextView;
  47. import android.widget.Toast;
  48. import java.io.File;
  49. import java.io.FileOutputStream;
  50. import java.util.zip.InflaterInputStream;
  51. import java.io.BufferedOutputStream;
  52. import java.io.IOException;
  53. import java.io.OutputStreamWriter;
  54. import java.net.DatagramPacket;
  55. import java.net.DatagramSocket;
  56. import java.net.InetAddress;
  57. import java.net.UnknownHostException;
  58. /** The main view activity for PowerTutor*/
  59. public class UMLogger extends Activity {
  60. private static final String TAG = "UMLogger";
  61. public static final String CURRENT_VERSION = "1.2"; // Don't change this...
  62. public static final String SERVER_IP = "spidermonkey.eecs.umich.edu";
  63. public static final int SERVER_PORT = 5204;
  64. private SharedPreferences prefs;
  65. private Intent serviceIntent;
  66. private ICounterService counterService;
  67. private CounterServiceConnection conn;
  68. private Button serviceStartButton;
  69. private Button appViewerButton;
  70. private Button sysViewerButton;
  71. private Button helpButton;
  72. private TextView scaleText;
  73. /** Called when the activity is first created. */
  74. @Override
  75. public void onCreate(Bundle savedInstanceState) {
  76. super.onCreate(savedInstanceState);
  77. prefs = PreferenceManager.getDefaultSharedPreferences(this);
  78. serviceIntent = new Intent(this, UMLoggerService.class);
  79. conn = new CounterServiceConnection();
  80. setContentView(R.layout.main);
  81. ArrayAdapter<?> adapterxaxis = ArrayAdapter.createFromResource(
  82. this, R.array.xaxis, android.R.layout.simple_spinner_item);
  83. adapterxaxis.setDropDownViewResource(
  84. android.R.layout.simple_spinner_dropdown_item);
  85. serviceStartButton = (Button)findViewById(R.id.servicestartbutton);
  86. appViewerButton = (Button)findViewById(R.id.appviewerbutton);
  87. sysViewerButton = (Button)findViewById(R.id.sysviewerbutton);
  88. helpButton= (Button)findViewById(R.id.helpbutton);
  89. serviceStartButton.setOnClickListener(serviceStartButtonListener);
  90. sysViewerButton.setOnClickListener(sysViewerButtonListener);
  91. appViewerButton.setOnClickListener(appViewerButtonListener);
  92. helpButton.setOnClickListener(helpButtonListener);
  93. if(counterService != null) {
  94. serviceStartButton.setText("Stop Profiler");
  95. appViewerButton.setEnabled(true);
  96. sysViewerButton.setEnabled(true);
  97. } else {
  98. serviceStartButton.setText("Start Profiler");
  99. appViewerButton.setEnabled(false);
  100. sysViewerButton.setEnabled(false);
  101. }
  102. }
  103. @Override
  104. public void onResume() {
  105. super.onResume();
  106. getApplicationContext().bindService(serviceIntent, conn, 0);
  107. if(prefs.getBoolean("firstRun", true)) {
  108. if(PhoneSelector.getPhoneType() == PhoneSelector.PHONE_UNKNOWN) {
  109. showDialog(DIALOG_UNKNOWN_PHONE);
  110. } else {
  111. showDialog(DIALOG_TOS);
  112. }
  113. }
  114. Intent startingIntent = getIntent();
  115. if(startingIntent.getBooleanExtra("isFromIcon", false)) {
  116. Intent copyIntent = (Intent)getIntent().clone();
  117. copyIntent.putExtra("isFromIcon", false);
  118. setIntent(copyIntent);
  119. Intent intent = new Intent(this, PowerTabs.class);
  120. startActivity(intent);
  121. }
  122. }
  123. @Override
  124. public void onPause() {
  125. super.onPause();
  126. getApplicationContext().unbindService(conn);
  127. }
  128. private static final int MENU_PREFERENCES = 0;
  129. private static final int MENU_SAVE_LOG = 1;
  130. private static final int DIALOG_START_SENDING = 0;
  131. private static final int DIALOG_STOP_SENDING = 1;
  132. private static final int DIALOG_TOS = 2;
  133. private static final int DIALOG_RUNNING_ON_STARTUP = 3;
  134. private static final int DIALOG_NOT_RUNNING_ON_STARTUP = 4;
  135. private static final int DIALOG_UNKNOWN_PHONE = 5;
  136. @Override
  137. public boolean onCreateOptionsMenu(Menu menu) {
  138. menu.add(0, MENU_PREFERENCES, 0, "Options");
  139. menu.add(0, MENU_SAVE_LOG, 0, "Save log");
  140. return true;
  141. }
  142. @Override
  143. public boolean onOptionsItemSelected(MenuItem item) {
  144. switch(item.getItemId()) {
  145. case MENU_PREFERENCES:
  146. startActivity(new Intent(this, EditPreferences.class));
  147. return true;
  148. case MENU_SAVE_LOG:
  149. new Thread() {
  150. public void start() {
  151. File writeFile = new File(
  152. Environment.getExternalStorageDirectory(), "PowerTrace" +
  153. System.currentTimeMillis() + ".log");
  154. try {
  155. InflaterInputStream logIn = new InflaterInputStream(
  156. openFileInput("PowerTrace.log"));
  157. BufferedOutputStream logOut = new BufferedOutputStream(
  158. new FileOutputStream(writeFile));
  159. byte[] buffer = new byte[20480];
  160. for(int ln = logIn.read(buffer); ln != -1;
  161. ln = logIn.read(buffer)) {
  162. logOut.write(buffer, 0, ln);
  163. }
  164. logIn.close();
  165. logOut.close();
  166. Toast.makeText(UMLogger.this, "Wrote log to " +
  167. writeFile.getAbsolutePath(),
  168. Toast.LENGTH_SHORT).show();
  169. return;
  170. } catch(java.io.EOFException e) {
  171. Toast.makeText(UMLogger.this, "Wrote log to " +
  172. writeFile.getAbsolutePath(),
  173. Toast.LENGTH_SHORT).show();
  174. return;
  175. } catch(IOException e) {
  176. }
  177. Toast.makeText(UMLogger.this, "Failed to write log to sdcard",
  178. Toast.LENGTH_SHORT).show();
  179. }
  180. }.start();
  181. return true;
  182. }
  183. return super.onOptionsItemSelected(item);
  184. }
  185. /**This function includes all the dialog constructor*/
  186. @Override
  187. protected Dialog onCreateDialog(int id) {
  188. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  189. switch(id) {
  190. case DIALOG_TOS:
  191. builder.setMessage(R.string.term)
  192. .setCancelable(false)
  193. .setPositiveButton("Agree", new DialogInterface.OnClickListener() {
  194. public void onClick(DialogInterface dialog, int id) {
  195. prefs.edit().putBoolean("firstRun", false)
  196. .putBoolean("runOnStartup", true)
  197. .putBoolean("sendPermission", true).commit();
  198. dialog.dismiss();
  199. }
  200. })
  201. .setNegativeButton("Do not agree",
  202. new DialogInterface.OnClickListener() {
  203. public void onClick(DialogInterface dialog, int id) {
  204. prefs.edit().putBoolean("firstRun", true).commit();
  205. finish();
  206. }
  207. });
  208. return builder.create();
  209. case DIALOG_STOP_SENDING:
  210. builder.setMessage(R.string.stop_sending_text)
  211. .setCancelable(true)
  212. .setPositiveButton("Stop", new DialogInterface.OnClickListener() {
  213. public void onClick(DialogInterface dialog, int id) {
  214. prefs.edit().putBoolean("sendPermission", false).commit();
  215. dialog.dismiss();
  216. }
  217. })
  218. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  219. public void onClick(DialogInterface dialog, int id) {
  220. dialog.cancel();
  221. }
  222. });
  223. return builder.create();
  224. case DIALOG_START_SENDING:
  225. builder.setMessage(R.string.start_sending_text)
  226. .setCancelable(true)
  227. .setPositiveButton("Start", new DialogInterface.OnClickListener() {
  228. public void onClick(DialogInterface dialog, int id) {
  229. prefs.edit().putBoolean("sendPermission", true).commit();
  230. dialog.dismiss();
  231. }
  232. })
  233. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  234. public void onClick(DialogInterface dialog, int id) {
  235. dialog.cancel();
  236. }
  237. });
  238. return builder.create();
  239. case DIALOG_RUNNING_ON_STARTUP:
  240. builder.setMessage(R.string.running_on_startup)
  241. .setCancelable(true)
  242. .setNeutralButton("Ok", null);
  243. return builder.create();
  244. case DIALOG_NOT_RUNNING_ON_STARTUP:
  245. builder.setMessage(R.string.not_running_on_startup)
  246. .setCancelable(true)
  247. .setNeutralButton("Ok", null);
  248. return builder.create();
  249. case DIALOG_UNKNOWN_PHONE:
  250. builder.setMessage(R.string.unknown_phone)
  251. .setCancelable(false)
  252. .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
  253. public void onClick(DialogInterface dialog, int id) {
  254. dialog.dismiss();
  255. showDialog(DIALOG_TOS);
  256. }
  257. });
  258. return builder.create();
  259. }
  260. return null;
  261. }
  262. private Button.OnClickListener appViewerButtonListener =
  263. new Button.OnClickListener() {
  264. public void onClick(View v) {
  265. Intent intent = new Intent(v.getContext(), PowerTop.class);
  266. startActivityForResult(intent, 0);
  267. }
  268. };
  269. private Button.OnClickListener sysViewerButtonListener =
  270. new Button.OnClickListener() {
  271. public void onClick(View v) {
  272. Intent intent = new Intent(v.getContext(), PowerTabs.class);
  273. startActivityForResult(intent, 0);
  274. }
  275. };
  276. private Button.OnClickListener serviceStartButtonListener =
  277. new Button.OnClickListener() {
  278. public void onClick(View v) {
  279. serviceStartButton.setEnabled(false);
  280. if(counterService != null) {
  281. stopService(serviceIntent);
  282. } else {
  283. if(conn == null) {
  284. Toast.makeText(UMLogger.this, "Profiler failed to start",
  285. Toast.LENGTH_SHORT).show();
  286. } else {
  287. startService(serviceIntent);
  288. }
  289. }
  290. }
  291. };
  292. private class CounterServiceConnection implements ServiceConnection {
  293. public void onServiceConnected(ComponentName className,
  294. IBinder boundService) {
  295. counterService = ICounterService.Stub.asInterface((IBinder)boundService);
  296. serviceStartButton.setText("Stop Profiler");
  297. serviceStartButton.setEnabled(true);
  298. appViewerButton.setEnabled(true);
  299. sysViewerButton.setEnabled(true);
  300. }
  301. public void onServiceDisconnected(ComponentName className) {
  302. counterService = null;
  303. getApplicationContext().unbindService(conn);
  304. getApplicationContext().bindService(serviceIntent, conn, 0);
  305. Toast.makeText(UMLogger.this, "Profiler stopped",
  306. Toast.LENGTH_SHORT).show();
  307. serviceStartButton.setText("Start Profiler");
  308. serviceStartButton.setEnabled(true);
  309. appViewerButton.setEnabled(false);
  310. sysViewerButton.setEnabled(false);
  311. }
  312. }
  313. private Button.OnClickListener helpButtonListener =
  314. new Button.OnClickListener() {
  315. public void onClick(View v) {
  316. Intent myIntent = new Intent(v.getContext(), Help.class);
  317. startActivityForResult(myIntent, 0);
  318. }
  319. };
  320. }