AndroidSensorMessage.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * 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, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android.androidSensorMessage;
00018 
00019 import android.os.Bundle;
00020 import android.view.Menu;
00021 import android.view.MenuItem;
00022 import android.view.SurfaceView;
00023 //add
00024 import android.view.MotionEvent;
00025 import android.view.View;
00026 import android.view.View.OnTouchListener;
00027 
00028 import org.ros.android.MasterChooser;
00029 import org.ros.android.RosActivity;
00030 import org.ros.android.view.RosTextView;
00031 import org.ros.node.NodeMainExecutor;
00032 import android.content.ServiceConnection;
00033 
00034 import java.net.URI;
00035 import java.net.URISyntaxException;
00036 import java.util.ArrayList;
00037 
00038 import android.app.AlertDialog;
00039 import android.content.ClipData.Item;
00040 import android.content.DialogInterface;
00041 import android.content.Intent;
00042 import android.widget.LinearLayout;
00043 import android.widget.Button;
00044 import android.util.Log;
00045 import android.content.ComponentName;
00046 import android.os.IBinder;
00047 import android.content.Context;
00048 import android.view.View;
00049 import android.widget.TextView;
00050 import android.widget.CheckBox;
00051 
00052 //add ribbon_menu
00053 import com.darvds.ribbonmenu.RibbonMenuView;
00054 import com.darvds.ribbonmenu.iRibbonMenuCallback;
00055 
00056 public class AndroidSensorMessage extends RosActivity implements
00057                 iRibbonMenuCallback, OnTouchListener {
00058 
00059         private URI masterUri;
00060         private Intent intent;
00061         private AndroidSensorMessageService androidSensorMessageService;
00062         private int currentSensor = 0;
00063         private int multiTouchAction = 0;
00064         private float previousY;
00065         private float previousDistanceY;
00066         private int[] availableSensor;
00067         private int[] publishedSensor;
00068         private int[] selectedSensor;
00069 
00070         private Button menuButton;
00071         private Button setButton;
00072 
00073         private RibbonMenuView ribbonMenuView;
00074         private AndroidSensorMessageView androidSensorMessageView;
00075         private SurfaceView surfaceView;
00076 
00077         private LinearLayout linearLayout;
00078 
00079         // Button
00080         Button baseButton;
00081         Button xButton;
00082         Button yButton;
00083         Button zButton;
00084 
00085         // Label
00086         TextView textViewX;
00087         TextView textViewY;
00088         TextView textViewZ;
00089         TextView textViewSensorName;
00090 
00091         ServiceConnection serviceConnection = new ServiceConnection() {
00092 
00093                 public void onServiceConnected(ComponentName name, IBinder service) {
00094                         androidSensorMessageService = ((AndroidSensorMessageService.MyBinder) service)
00095                                         .getService();
00096                         startService(intent);
00097                         availableSensor = new int[13];
00098                         publishedSensor = new int[13];
00099                         selectedSensor = new int[13];
00100 
00101                         menuButton = (Button) findViewById(R.id.menuButton);
00102                         setButton = (Button) findViewById(R.id.setButton);
00103                         xButton = (Button) findViewById(R.id.viewXButton);
00104                         yButton = (Button) findViewById(R.id.viewYButton);
00105                         zButton = (Button) findViewById(R.id.viewZButton);
00106                         baseButton = (Button) findViewById(R.id.viewBaseButton);
00107 
00108                         textViewX = (TextView) findViewById(R.id.sensorX);
00109                         textViewY = (TextView) findViewById(R.id.sensorY);
00110                         textViewZ = (TextView) findViewById(R.id.sensorZ);
00111                         textViewSensorName = (TextView) findViewById(R.id.sensorName);
00112 
00113                         for (int i = 0; i < 13; i++) {
00114                                 availableSensor[i] = androidSensorMessageService.getSensor(i);
00115                                 publishedSensor[i] = -1;
00116                                 selectedSensor[i] = -1;
00117                         }
00118 
00119                         androidSensorMessageService.setNode(publishedSensor,
00120                                         androidSensorMessageView, textViewX, textViewY, textViewZ);
00121                         ribbonMenuView.setItemIcon(R.drawable.noncheck, R.drawable.check);
00122                         for (int i = 0; i < 13; i++) {
00123                                 if (availableSensor[i] != -1) {
00124                                         selectedSensor[i] = ribbonMenuView.addItem(
00125                                                         androidSensorMessageService.getSensorName(i),
00126                                                         publishedSensor[i]);
00127                                 }
00128                         }
00129 
00130                         menuButton.setOnClickListener(new View.OnClickListener() {
00131                                 @Override
00132                                 public void onClick(View v) {
00133                                         ribbonMenuView.toggleMenu();
00134                                 }
00135                         });
00136 
00137                         setButton.setOnClickListener(new View.OnClickListener() {
00138                                 @Override
00139                                 public void onClick(View v) {
00140                                         androidSensorMessageService.changeSensor(currentSensor);
00141                                         ribbonMenuView.setMenuItems(R.menu.ribbon_menu);
00142                                         ribbonMenuView.clearButtonId();
00143                                         publishedSensor[currentSensor] *= -1;
00144                                         if (publishedSensor[currentSensor] == -1) {
00145                                                 setButton.setText("ON");
00146                                         } else {
00147                                                 setButton.setText("OFF");
00148                                         }
00149                                         for (int i = 0; i < 13; i++) {
00150                                                 if (availableSensor[i] != -1) {
00151                                                         ribbonMenuView.addItem(androidSensorMessageService
00152                                                                         .getSensorName(i), publishedSensor[i]);
00153                                                 }
00154                                         }
00155                                 }
00156                         });
00157 
00158                         xButton.setOnClickListener(new View.OnClickListener() {
00159                                 @Override
00160                                 public void onClick(View v) {
00161                                         androidSensorMessageView.viewChange(1);
00162                                 }
00163                         });
00164 
00165                         yButton.setOnClickListener(new View.OnClickListener() {
00166                                 @Override
00167                                 public void onClick(View v) {
00168                                         androidSensorMessageView.viewChange(2);
00169                                 }
00170                         });
00171 
00172                         zButton.setOnClickListener(new View.OnClickListener() {
00173                                 @Override
00174                                 public void onClick(View v) {
00175                                         androidSensorMessageView.viewChange(3);
00176                                 }
00177                         });
00178 
00179                         baseButton.setOnClickListener(new View.OnClickListener() {
00180                                 @Override
00181                                 public void onClick(View v) {
00182                                         androidSensorMessageView.viewChange(0);
00183                                 }
00184                         });
00185 
00186                         textViewSensorName.setText(androidSensorMessageService
00187                                         .getSensorName(0));
00188 
00189                 }
00190 
00191                 @Override
00192                 public void onServiceDisconnected(ComponentName name) {
00193                         androidSensorMessageService = null;
00194                 }
00195 
00196         };
00197 
00198         public AndroidSensorMessage() {
00199                 super("AndroidSensorMessage", "AndroidSensorMessage");
00200 
00201         }
00202 
00203         @Override
00204         public void onCreate(Bundle savedInstanceState) {
00205                 super.onCreate(savedInstanceState);
00206                 linearLayout = new LinearLayout(this);
00207                 setContentView(R.layout.main);
00208 
00209                 ribbonMenuView = (RibbonMenuView) findViewById(R.id.ribbonMenuView1);
00210                 ribbonMenuView.setMenuClickCallback(this);
00211                 ribbonMenuView.setMenuItems(R.menu.ribbon_menu);
00212 
00213                 surfaceView = (SurfaceView) findViewById(R.id.SV);
00214                 androidSensorMessageView = new AndroidSensorMessageView(this,
00215                                 surfaceView);
00216                 surfaceView.setOnTouchListener(this);
00217         }
00218 
00219         private LinearLayout.LayoutParams createParam(int width, int height) {
00220                 return new LinearLayout.LayoutParams(width, height);
00221         }
00222 
00223         @Override
00224         protected void onResume() {
00225                 super.onResume();
00226                 if (masterUri != null) {
00227 
00228                         intent = new Intent(getBaseContext(),
00229                                         AndroidSensorMessageService.class);
00230                         intent.putExtra("masterUri", masterUri.toString());
00231                         bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
00232                 }
00233 
00234         }
00235 
00236         @Override
00237         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
00238                 if (requestCode == 0 && resultCode == RESULT_OK) {
00239                         try {
00240                                 masterUri = new URI(data.getStringExtra("ROS_MASTER_URI"));
00241                         } catch (URISyntaxException e) {
00242                                 throw new RuntimeException(e);
00243                         }
00244                 }
00245         }
00246 
00247         @Override
00248         public boolean onCreateOptionsMenu(Menu menu) {
00249                 menu.add(0, 0, 0, R.string.app_about);
00250                 menu.add(0, 1, 1, R.string.str_exit);
00251                 return super.onCreateOptionsMenu(menu);
00252         }
00253 
00254         @Override
00255         public boolean onOptionsItemSelected(MenuItem item) {
00256                 super.onOptionsItemSelected(item);
00257                 switch (item.getItemId()) {
00258                 case 0:
00259                         openOptionsDialog();
00260                         break;
00261                 case 1:
00262                         exitOptionsDialog();
00263                         break;
00264                 }
00265                 return true;
00266         }
00267 
00268         private void openOptionsDialog() {
00269                 new AlertDialog.Builder(this)
00270                                 .setTitle(R.string.app_about)
00271                                 .setMessage(R.string.app_about_message)
00272                                 .setPositiveButton(R.string.str_ok,
00273                                                 new DialogInterface.OnClickListener() {
00274                                                         public void onClick(
00275                                                                         DialogInterface dialoginterface, int i) {
00276                                                         }
00277                                                 }).show();
00278         }
00279 
00280         private void exitOptionsDialog() {
00281                 stopService(new Intent(this, AndroidSensorMessageService.class));
00282                 finish();
00283         }
00284 
00285         @Override
00286         protected void onPause() {
00287                 super.onPause();
00288         }
00289 
00290         @Override
00291         protected void init(NodeMainExecutor nodeMainExecutor) {
00292 
00293         }
00294 
00295         @Override
00296         public void RibbonMenuItemClick(int itemId) {
00297                 for (int i = 0; i < 13; i++) {
00298                         if (itemId == selectedSensor[i]) {
00299                                 currentSensor = i;
00300                                 textViewSensorName.setText(androidSensorMessageService
00301                                                 .getSensorName(i));
00302                                 textViewX.setText("x:");
00303                                 textViewY.setText("y:");
00304                                 textViewZ.setText("z:");
00305                                 if (publishedSensor[i] == -1) {
00306                                         setButton.setText("ON");
00307                                 } else {
00308                                         setButton.setText("OFF");
00309                                 }
00310                                 androidSensorMessageService.changeView(currentSensor);
00311                                 break;
00312                         }
00313                 }
00314         }
00315 
00316         public boolean onTouch(View v, MotionEvent event) {
00317                 if (event.getPointerCount() == 1) {
00318                         if (multiTouchAction > 0) {
00319                                 if (event.getAction() == MotionEvent.ACTION_UP) {
00320                                         multiTouchAction--;
00321                                 }
00322                         } else {
00323                                 if (event.getAction() == MotionEvent.ACTION_MOVE) {
00324                                         androidSensorMessageView.updateOriginalY(event.getY()
00325                                                         - previousY);
00326                                 }
00327                                 previousY = event.getY();
00328                         }
00329                 } else {
00330                         int pointerId1 = event.getPointerId(0);
00331                         int pointerId2 = event.getPointerId(1);
00332 
00333                         int pointerIndex1 = event.findPointerIndex(pointerId1);
00334                         int pointerIndex2 = event.findPointerIndex(pointerId2);
00335 
00336                         float distanceY = event.getY(pointerIndex2) - event.getY(pointerIndex1);
00337                         distanceY = (float)Math.sqrt(distanceY * distanceY);
00338                         if (event.getAction() == MotionEvent.ACTION_MOVE) {
00339                                 androidSensorMessageView.updateScale(distanceY - previousDistanceY);
00340                         }
00341                         Log.v("distance","pre:"+previousDistanceY+" now:"+distanceY);
00342                         previousDistanceY = distanceY;
00343                         
00344                         multiTouchAction = 1;
00345 
00346                 }
00347 
00348                 return true;
00349         }
00350 
00351 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


android_sensor_message
Author(s): Damon Kohler
autogenerated on Sun Mar 24 2013 21:01:42