Advertisement
tariq786

Main_Activity

Sep 22nd, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.07 KB | None | 0 0
  1. package edu.umass.cs.client;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.content.ComponentName;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.ServiceConnection;
  9. import android.hardware.Sensor;
  10. import android.hardware.SensorEvent;
  11. import android.hardware.SensorEventListener;
  12. import android.hardware.SensorManager;
  13. import android.os.Bundle;
  14. import android.os.Handler;
  15. import android.os.IBinder;
  16. import android.os.Message;
  17. import android.os.Messenger;
  18. import android.os.RemoteException;
  19. import android.util.Log;
  20. import android.widget.CompoundButton;
  21. import android.widget.CompoundButton.OnCheckedChangeListener;
  22. import android.widget.TextView;
  23. import android.widget.ToggleButton;
  24.  
  25. public class MainActivity extends Activity{
  26.  
  27.  
  28.  
  29.     /**
  30.      * Variable to check if accelerometer is running
  31.      */
  32.     private boolean accelStarted = false;
  33.    
  34.     /**
  35.      * Instance of this activity
  36.      */
  37.     private MainActivity activity;
  38.    
  39.    
  40.     /*
  41.      * Various UI components
  42.      */
  43.     private TextView accelXView, accelYView, accelZView;
  44.     private TextView statusView;
  45.     private CompoundButton accelButton;
  46.    
  47.     /**
  48.      * Messenger service for exchanging messages with the background service
  49.      */
  50.     private Messenger mService = null;
  51.     /**
  52.      * Variable indicating if this activity is connected to the service
  53.      */
  54.     private boolean mIsBound;
  55.     /**
  56.      * Messenger receiving messages from the background service to update UI
  57.      */
  58.     private final Messenger mMessenger = new Messenger(new IncomingHandler());
  59.    
  60.     /**
  61.      * Handler to handle incoming messages
  62.      */
  63.     @SuppressLint("HandlerLeak")
  64.     class IncomingHandler extends Handler {
  65.  
  66.         @Override
  67.         public void handleMessage(Message msg) {
  68.             switch (msg.what) {
  69.             case Context_Service.MSG_ACCEL_VALUES:
  70.             {
  71.                 float accX = msg.getData().getFloat("accx");
  72.                 float accY = msg.getData().getFloat("accy");
  73.                 float accZ = msg.getData().getFloat("accz");
  74.                 activity.setAccelValues(accX,accY,accZ);
  75.                 break;
  76.             }
  77.             case Context_Service.MSG_ACCELEROMETER_STARTED:
  78.             {
  79.                 if(accelButton!=null) {
  80.                     accelButton.setChecked(true);
  81.                     accelStarted = true;
  82.                     statusView.setText("Accelerometer Started");
  83.                 }
  84.  
  85.  
  86.                 break;
  87.             }
  88.             case Context_Service.MSG_ACCELEROMETER_STOPPED:
  89.             {
  90.                 if(accelButton!=null) {
  91.                     accelButton.setChecked(false);
  92.                     accelStarted = false;
  93.                     statusView.setText("Accelerometer Stopped");
  94.                 }
  95.  
  96.                 break;
  97.             }
  98.             default:
  99.                 super.handleMessage(msg);
  100.             }
  101.         }
  102.     }
  103.    
  104.     /**
  105.      * Connection with the service
  106.      */
  107.     private ServiceConnection mConnection = new ServiceConnection() {
  108.         public void onServiceConnected(ComponentName className, IBinder service) {
  109.             mService = new Messenger(service);
  110.             statusView.setText("Attached to Service");
  111.             mIsBound = true;
  112.             try {
  113.                 Message msg = Message.obtain(null, Context_Service.MSG_REGISTER_CLIENT);
  114.                 msg.replyTo = mMessenger;
  115.                 mService.send(msg);
  116.             } catch (RemoteException e) {
  117.                 // In this case the service has crashed before we could even do anything with it
  118.             }
  119.         }
  120.  
  121.         public void onServiceDisconnected(ComponentName className) {
  122.             // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
  123.             mIsBound = false;
  124.             mService = null;
  125.             statusView.setText("Disconnected from Service");
  126.         }
  127.     };
  128.    
  129.     /* Invoked when an activity is created
  130.      * @see android.app.Activity#onCreate(android.os.Bundle)
  131.      */
  132.     @Override
  133.     public void onCreate(Bundle savedInstanceState) {
  134.        
  135.         activity = this;
  136.         super.onCreate(savedInstanceState);
  137.         //Set Layout
  138.         setContentView(R.layout.main);
  139.        
  140.         //Setting up text views
  141.         statusView = (TextView) findViewById(R.id.StatusView);
  142.         accelXView = (TextView) findViewById(R.id.AccelXView);
  143.         accelYView = (TextView) findViewById(R.id.AccelYView);
  144.         accelZView = (TextView) findViewById(R.id.AccelZView);
  145.         statusView.setText("Service Not Bound");
  146.  
  147.  
  148.         //Start Background Service if not already started
  149.         if(!Context_Service.isRunning()) {
  150.             Intent cssBg = new Intent(activity,Context_Service.class);
  151.             startService(cssBg);
  152.         }
  153.        
  154.        
  155.         //Bind to the service if it is already running
  156.         bindToServiceIfIsRunning();
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.         //Determine if the accelerometer is on
  164.         accelStarted = false;
  165.         if(Context_Service.isAccelerometerRunning())
  166.             accelStarted = true;
  167.        
  168.         //Set the buttons and the text accordingly
  169.         accelButton = (ToggleButton) findViewById(R.id.start_button);
  170.         accelButton.setChecked(accelStarted);
  171.         accelButton.setOnCheckedChangeListener(
  172.                 new OnCheckedChangeListener() {
  173.                     public void onCheckedChanged(CompoundButton btn,boolean isChecked) {
  174.                         accelStarted = Context_Service.isAccelerometerRunning();
  175.                         if(!accelStarted)
  176.                             startAccelerometer();
  177.                         else
  178.                             stopAccelerometer();
  179.                     }
  180.                 }
  181.         );
  182.        
  183.        
  184.     }
  185.    
  186.     /**
  187.      * Binds this activity to the service if the service is already running
  188.      */
  189.     private void bindToServiceIfIsRunning() {
  190.         //If the service is running when the activity starts, we want to automatically bind to it.
  191.         if (Context_Service.isRunning()) {
  192.             doBindService();//
  193.             statusView.setText("Request to bind service");
  194.         }
  195.     }
  196.    
  197.    
  198.     /**
  199.      * This method is required to send a request to the background service.
  200.      * In current application, we are not sending any message yet.
  201.      * @param message
  202.      */
  203.     private void sendMessageToService(int message) {
  204.         if (mIsBound) {
  205.             if (mService != null) {
  206.                 try {
  207.                     Message msg = Message.obtain(null, message);
  208.                     msg.replyTo = mMessenger;
  209.                     mService.send(msg);
  210.                 } catch (RemoteException e) {
  211.                 }
  212.             }
  213.         }
  214.     }
  215.    
  216.     /**
  217.      * Display accelerometer values in UI
  218.      * @param accX
  219.      * @param accY
  220.      * @param accZ
  221.      */
  222.     public void setAccelValues(float accX, float accY, float accZ) {
  223.         String text = String.format("%2.2f", accX);
  224.         accelXView.setText(text);
  225.         text = String.format("%2.2f", accY);
  226.         accelYView.setText(text);
  227.         text = String.format("%2.2f", accZ);
  228.         accelZView.setText(text);
  229.     }
  230.    
  231.    
  232.     @Override
  233.     public void onBackPressed() {
  234.         super.onBackPressed();
  235.     }
  236.    
  237.     @Override
  238.     protected void onDestroy() {
  239.         super.onDestroy();
  240.         try {
  241.             doUnbindService();
  242.         } catch (Throwable t) {
  243.             Log.e("MainActivity", "Failed to unbind from the service", t);
  244.         }
  245.     }
  246.    
  247.     /**
  248.      * Binds the activity to the background service
  249.      */
  250.     void doBindService() {
  251.         bindService(new Intent(this, Context_Service.class), mConnection, Context.BIND_AUTO_CREATE);
  252.         statusView.setText("Binding to Service");
  253.     }
  254.    
  255.     /**
  256.      * Unbind this activity from the background service
  257.      */
  258.     void doUnbindService() {
  259.         if (mIsBound) {
  260.             // If we have received the service, and hence registered with it, then now is the time to unregister.
  261.             if (mService != null) {
  262.                 try {
  263.                     Message msg = Message.obtain(null, Context_Service.MSG_UNREGISTER_CLIENT);
  264.                     msg.replyTo = mMessenger;
  265.                     mService.send(msg);
  266.                 } catch (RemoteException e) {
  267.                     // There is nothing special we need to do if the service has crashed.
  268.                 }
  269.             }
  270.             // Detach our existing connection.
  271.             unbindService(mConnection);
  272.             statusView.setText("Unbinding from Service");
  273.         }
  274.     }
  275.    
  276.     /**
  277.      * Sends Accelerometer Start Request
  278.      */
  279.     private void startAccelerometer() {
  280.         if(!mIsBound) {
  281.             doBindService();
  282.             //In this case, start accelerometer won't work because service is not bound
  283.             accelButton.setChecked(false);
  284.         }
  285.         if(mIsBound) {
  286.             sendMessageToService(Context_Service.MSG_ACCELEROMETER_STARTED);
  287.         }
  288.     }
  289.    
  290.     /**
  291.      * Sends Accelerometer Stop Request
  292.      */
  293.     private void stopAccelerometer() {
  294.         if(!mIsBound) {
  295.             doBindService();
  296.         }
  297.         if(mIsBound) {
  298.             sendMessageToService(Context_Service.MSG_ACCELEROMETER_STOPPED);
  299.         }
  300.     }
  301.  
  302.  
  303.  
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement