MenuActivity.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 The Android Open Source Project
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 /*
00017 copyright 2014 UNL Nimbus Lab 
00018 
00019   Licensed under the Apache License, Version 2.0 (the "License");
00020      you may not use this file except in compliance with the License.
00021     You may obtain a copy of the License at
00022   
00023         http://www.apache.org/licenses/LICENSE-2.0
00024   
00025     Unless required by applicable law or agreed to in writing, software
00026   distributed under the License is distributed on an "AS IS" BASIS,
00027    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00028    See the License for the specific language governing permissions and
00029   limitations under the License.
00030 */
00031 
00032 package edu.nimbus.glass;
00033 
00034 
00035 import android.app.Activity;
00036 import android.content.ComponentName;
00037 import android.content.Intent;
00038 import android.content.ServiceConnection;
00039 import android.os.Bundle;
00040 import android.os.IBinder;
00041 import android.view.Menu;
00042 import android.view.MenuItem;
00043 
00048 public class MenuActivity extends Activity {
00049 
00051     private ROSMonitorService.ROSMonitorBinder mService;
00052     private boolean mResumed;
00053 
00054     
00055     
00056     private ServiceConnection mConnection = new ServiceConnection() {
00057         @Override
00058         public void onServiceConnected(ComponentName name, IBinder service) {
00059             if (service instanceof ROSMonitorService.ROSMonitorBinder) {
00060                 mService = (ROSMonitorService.ROSMonitorBinder) service;
00061                 openOptionsMenu();
00062             }
00063         }
00064 
00065         @Override
00066         public void onServiceDisconnected(ComponentName name) {
00067             // Do nothing.
00068         }
00069     };
00070 
00071     @Override
00072     protected void onCreate(Bundle savedInstanceState) {
00073         super.onCreate(savedInstanceState);
00074         //bind to the service when the menu is created
00075         bindService(new Intent(this, ROSMonitorService.class), mConnection, 0);
00076     }
00077 
00078     @Override
00079     protected void onResume() {
00080         //open menu when resumed
00081         super.onResume();
00082         mResumed = true;
00083         openOptionsMenu();
00084     }
00085 
00086     @Override
00087     protected void onPause() {
00088         //pause the stuff
00089         super.onPause();
00090         mResumed = false;
00091     }
00092 
00093     @Override
00094     public void openOptionsMenu() {
00095         if (mResumed && mService != null) {
00096                 //if we have a service running than start up the menu
00097             super.openOptionsMenu();
00098         }
00099     }
00100 
00101     @Override
00102     public boolean onCreateOptionsMenu(Menu menu) {
00103         getMenuInflater().inflate(R.menu.main, menu);
00104         return true;
00105     }
00106 
00110     @Override
00111     public boolean onOptionsItemSelected(MenuItem item) {
00112         switch (item.getItemId()) {
00113             case R.id.stop:
00114                 stopService(new Intent(this, ROSMonitorService.class));
00115                 return true;
00116             case R.id.remove_card:
00117                 mService.remove_card();
00118                 return true;
00119             default:
00120                return super.onOptionsItemSelected(item);
00121         }
00122                
00123         }
00124     
00125                 
00126         
00127    
00128     
00129         @Override
00130     public void onOptionsMenuClosed(Menu menu) {
00131         super.onOptionsMenuClosed(menu);
00132 
00133         unbindService(mConnection);
00134 
00135         // We must call finish() from this method to ensure that the activity ends either when an
00136         // item is selected from the menu or when the menu is dismissed by swiping down.
00137         finish();
00138     }
00139 }


ros_glass_tools
Author(s):
autogenerated on Thu Aug 27 2015 14:47:21