Guest User

launcheractivity

a guest
Apr 2nd, 2015
257
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright (C) 2011 Google Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5.  * use this file except in compliance with the License. You may obtain a copy of
  6.  * the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13.  * License for the specific language governing permissions and limitations under
  14.  * the License.
  15.  */
  16.  
  17. package com.cellbots.logger;
  18.  
  19. import android.app.Activity;
  20. import android.bluetooth.BluetoothDevice;
  21. import android.bluetooth.BluetoothManager;
  22. import android.content.ComponentName;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.content.ServiceConnection;
  26. import android.hardware.Camera;
  27. import android.os.Build;
  28. import android.os.Bundle;
  29. import android.os.IBinder;
  30. import android.util.Log;
  31. import android.view.View;
  32. import android.view.View.OnClickListener;
  33. import android.widget.Button;
  34. import android.widget.CheckBox;
  35. import android.widget.EditText;
  36. import android.widget.Toast;
  37.  
  38. import com.mbientlab.metawear.api.MetaWearBleService;
  39. import com.mbientlab.metawear.api.MetaWearController;
  40. import com.mbientlab.metawear.api.Module;
  41. import com.mbientlab.metawear.api.controller.Accelerometer;
  42. import com.mbientlab.metawear.api.controller.Accelerometer.Axis;
  43. import com.mbientlab.metawear.api.controller.Accelerometer.MovementData;
  44. import com.mbientlab.metawear.api.controller.MechanicalSwitch;
  45. //import com.cellbots.logger.SettingsFragment.SettingsState;
  46.  
  47. import com.cellbots.logger.localServer.ServerControlActivity;
  48.  
  49. /**
  50.  * A simple Activity for choosing which mode to launch the data logger in.
  51.  *
  52.  * @author clchen@google.com (Charles L. Chen)
  53.  */
  54. public class LauncherActivity extends Activity  implements ServiceConnection {
  55.     private MetaWearBleService mwService= null;
  56.     private CheckBox useZipCheckbox;
  57.     private MetaWearController mwCtrllr;
  58.     private final string MW_MAC_ADDRESS= "EC:2C:09:81:22:AC";
  59.  
  60.     @Override
  61.     public void onCreate(Bundle savedInstanceState) {
  62.         super.onCreate(savedInstanceState);
  63.         ///< Bind the MetaWear service when the activity is created
  64.         getApplicationContext().bindService(new Intent(this, MetaWearBleService.class),
  65.                 this, Context.BIND_AUTO_CREATE);
  66.         setContentView(R.layout.main);
  67.  
  68.         useZipCheckbox = (CheckBox) findViewById(R.id.useZip);
  69.  
  70.         final Activity self = this;
  71.         Button launchLocalServerButton = (Button) findViewById(R.id.launchLocalServer);
  72.         launchLocalServerButton.setOnClickListener(new OnClickListener() {
  73.             @Override
  74.             public void onClick(View v) {
  75.                 Intent i = new Intent(LauncherActivity.this, ServerControlActivity.class);
  76.                 startActivity(i);
  77.                 finish();
  78.             }
  79.         });
  80.         Button launchVideoFrontButton = (Button) findViewById(R.id.launchVideoFront);
  81.         launchVideoFrontButton.setOnClickListener(new OnClickListener() {
  82.             @Override
  83.             public void onClick(View v) {
  84.                 launchLoggingActivity(LoggerActivity.MODE_VIDEO_FRONT, useZipCheckbox.isChecked());
  85.             }
  86.         });
  87.         Button launchVideoBackButton = (Button) findViewById(R.id.launchVideoBack);
  88.         launchVideoBackButton.setOnClickListener(new OnClickListener() {
  89.             @Override
  90.             public void onClick(View v) {
  91.                 launchLoggingActivity(LoggerActivity.MODE_VIDEO_BACK, useZipCheckbox.isChecked());
  92.             }
  93.         });
  94.         final EditText pictureDelayEditText = (EditText) findViewById(R.id.pictureDelay);
  95.         Button launchPictureButton = (Button) findViewById(R.id.launchPicture);
  96.         launchPictureButton.setOnClickListener(new OnClickListener() {
  97.             @Override
  98.             public void onClick(View v) {
  99.                 Intent i = new Intent(self, LoggerActivity.class);
  100.                 i.putExtra(LoggerActivity.EXTRA_MODE, LoggerActivity.MODE_PICTURES);
  101.                 i.putExtra(LoggerActivity.EXTRA_USE_ZIP, useZipCheckbox.isChecked());
  102.                 int delay = 30;
  103.                 try {
  104.                     delay = Integer.parseInt(pictureDelayEditText.getText().toString());
  105.                 } catch (Exception e) {
  106.                     Toast.makeText(self,
  107.                             "Error parsing picture delay time. Using default delay of 30 seconds.",
  108.                             Toast.LENGTH_LONG).show();
  109.                 }
  110.                 i.putExtra(LoggerActivity.EXTRA_PICTURE_DELAY, delay);
  111.                 startActivity(i);
  112.                 finish();
  113.             }
  114.         });
  115.         // The code we are using for taking video through the front camera
  116.         // relies on APIs added in SDK 9. Don't offer the front video option to
  117.         // users on devices older than that OR to devices who have only one
  118.         // camera. Currently assume that if only one camera is present, it is
  119.         // the back camera.
  120.         if (Build.VERSION.SDK_INT < 9 || Camera.getNumberOfCameras() == 1) {
  121.             launchVideoFrontButton.setVisibility(View.GONE);
  122.         }
  123.     }
  124.     @Override
  125.     public void onDestroy() {
  126.         super.onDestroy();
  127.  
  128.         if (mwService != null) {
  129.             ///< Don't forget to unregister the MetaWear receiver
  130.             mwService.unregisterReceiver(MetaWearBleService.getMetaWearBroadcastReceiver());
  131.         }
  132.         getApplicationContext().unbindService(this);
  133.     }
  134.  
  135.     @Override
  136.     public void onServiceConnected(ComponentName name, IBinder service) {
  137.         mwService= ((MetaWearBleService.LocalBinder) service).getService();
  138.  
  139.         final BluetoothManager btManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  140.         final mwBoard = btManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS);
  141.         mwCtrllr= mwService.getMetaWearController(mwService);
  142.         ///< Register the callback, log message will appear when connected
  143.         mwCtrllr.addDeviceCallback(dCallbacks);
  144.  
  145. ///< Remove the callback, no feedback for when a ble connection is made
  146.         mwCtrllr.removeDeviceCallback(dCallbacks);
  147.         mwCtrllr.connect();
  148.     }
  149.  
  150.     private MetaWearController.DeviceCallbacks dCallbacks= new MetaWearController.DeviceCallbacks() {
  151.         @Override
  152.         public void connected() {
  153.             Log.i("ExampleActivity", "A Bluetooth LE connection has been established!");
  154.         }
  155.  
  156.         @Override
  157.         public void disconnected() {
  158.             Log.i("ExampleActivity", "Lost the Bluetooth LE connection!");
  159.         }
  160.     };
  161.  
  162.     ///< Don't need this callback method but we must implement it
  163.     @Override
  164.     public void onServiceDisconnected(ComponentName name) { }
  165.  
  166.     private void launchLoggingActivity(int mode, boolean useZip) {
  167.         Intent i = new Intent(LauncherActivity.this, LoggerActivity.class);
  168.         i.putExtra(LoggerActivity.EXTRA_MODE, mode);
  169.         i.putExtra(LoggerActivity.EXTRA_USE_ZIP, useZip);
  170.         startActivity(i);
  171.         finish();
  172.     }
  173. }
RAW Paste Data