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                         /* k-okada
00122                         ribbonMenuView.setItemIcon(R.drawable.noncheck, R.drawable.check);
00123                         */
00124                         for (int i = 0; i < 13; i++) {
00125                                 if (availableSensor[i] != -1) {
00126                                         /* k-okada
00127                                         selectedSensor[i] = ribbonMenuView.addItem(
00128                                                         androidSensorMessageService.getSensorName(i),
00129                                                         publishedSensor[i]);
00130                                         */
00131                                 }
00132                         }
00133 
00134                         menuButton.setOnClickListener(new View.OnClickListener() {
00135                                 @Override
00136                                 public void onClick(View v) {
00137                                         ribbonMenuView.toggleMenu();
00138                                 }
00139                         });
00140 
00141                         setButton.setOnClickListener(new View.OnClickListener() {
00142                                 @Override
00143                                 public void onClick(View v) {
00144                                         androidSensorMessageService.changeSensor(currentSensor);
00145                                         ribbonMenuView.setMenuItems(R.menu.ribbon_menu);
00146                                         /* k-okada
00147                                         ribbonMenuView.clearButtonId();
00148                                         */
00149                                         publishedSensor[currentSensor] *= -1;
00150                                         if (publishedSensor[currentSensor] == -1) {
00151                                                 setButton.setText("ON");
00152                                         } else {
00153                                                 setButton.setText("OFF");
00154                                         }
00155                                         for (int i = 0; i < 13; i++) {
00156                                                 if (availableSensor[i] != -1) {
00157                                                   /* k-okada
00158                                                         ribbonMenuView.addItem(androidSensorMessageService
00159                                                                         .getSensorName(i), publishedSensor[i]);
00160                                                   */
00161                                                 }
00162                                         }
00163                                 }
00164                         });
00165 
00166                         xButton.setOnClickListener(new View.OnClickListener() {
00167                                 @Override
00168                                 public void onClick(View v) {
00169                                         androidSensorMessageView.viewChange(1);
00170                                 }
00171                         });
00172 
00173                         yButton.setOnClickListener(new View.OnClickListener() {
00174                                 @Override
00175                                 public void onClick(View v) {
00176                                         androidSensorMessageView.viewChange(2);
00177                                 }
00178                         });
00179 
00180                         zButton.setOnClickListener(new View.OnClickListener() {
00181                                 @Override
00182                                 public void onClick(View v) {
00183                                         androidSensorMessageView.viewChange(3);
00184                                 }
00185                         });
00186 
00187                         baseButton.setOnClickListener(new View.OnClickListener() {
00188                                 @Override
00189                                 public void onClick(View v) {
00190                                         androidSensorMessageView.viewChange(0);
00191                                 }
00192                         });
00193 
00194                         textViewSensorName.setText(androidSensorMessageService
00195                                         .getSensorName(0));
00196 
00197                 }
00198 
00199                 @Override
00200                 public void onServiceDisconnected(ComponentName name) {
00201                         androidSensorMessageService = null;
00202                 }
00203 
00204         };
00205 
00206         public AndroidSensorMessage() {
00207                 super("ROS Sensor Message", "ROS Sensor Message");
00208 
00209         }
00210 
00211         @Override
00212         public void onCreate(Bundle savedInstanceState) {
00213                 super.onCreate(savedInstanceState);
00214                 linearLayout = new LinearLayout(this);
00215                 setContentView(R.layout.main);
00216 
00217                 ribbonMenuView = (RibbonMenuView) findViewById(R.id.ribbonMenuView1);
00218                 ribbonMenuView.setMenuClickCallback(this);
00219                 ribbonMenuView.setMenuItems(R.menu.ribbon_menu);
00220 
00221                 surfaceView = (SurfaceView) findViewById(R.id.SV);
00222                 androidSensorMessageView = new AndroidSensorMessageView(this,
00223                                 surfaceView);
00224                 surfaceView.setOnTouchListener(this);
00225         }
00226 
00227         private LinearLayout.LayoutParams createParam(int width, int height) {
00228                 return new LinearLayout.LayoutParams(width, height);
00229         }
00230 
00231         @Override
00232         protected void onResume() {
00233                 super.onResume();
00234                 if (masterUri != null) {
00235 
00236                         intent = new Intent(getBaseContext(),
00237                                         AndroidSensorMessageService.class);
00238                         intent.putExtra("masterUri", masterUri.toString());
00239                         bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
00240                 }
00241 
00242         }
00243 
00244         @Override
00245         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
00246                 if (requestCode == 0 && resultCode == RESULT_OK) {
00247                         try {
00248                                 masterUri = new URI(data.getStringExtra("ROS_MASTER_URI"));
00249                         } catch (URISyntaxException e) {
00250                                 throw new RuntimeException(e);
00251                         }
00252                 }
00253         }
00254 
00255         @Override
00256         public boolean onCreateOptionsMenu(Menu menu) {
00257                 menu.add(0, 0, 0, R.string.app_about);
00258                 menu.add(0, 1, 1, R.string.str_exit);
00259                 return super.onCreateOptionsMenu(menu);
00260         }
00261 
00262         @Override
00263         public boolean onOptionsItemSelected(MenuItem item) {
00264                 super.onOptionsItemSelected(item);
00265                 switch (item.getItemId()) {
00266                 case 0:
00267                         openOptionsDialog();
00268                         break;
00269                 case 1:
00270                         exitOptionsDialog();
00271                         break;
00272                 }
00273                 return true;
00274         }
00275 
00276         private void openOptionsDialog() {
00277                 new AlertDialog.Builder(this)
00278                                 .setTitle(R.string.app_about)
00279                                 .setMessage(R.string.app_about_message)
00280                                 .setPositiveButton(R.string.str_ok,
00281                                                 new DialogInterface.OnClickListener() {
00282                                                         public void onClick(
00283                                                                         DialogInterface dialoginterface, int i) {
00284                                                         }
00285                                                 }).show();
00286         }
00287 
00288         private void exitOptionsDialog() {
00289                 stopService(new Intent(this, AndroidSensorMessageService.class));
00290                 finish();
00291         }
00292 
00293         @Override
00294         protected void onPause() {
00295                 super.onPause();
00296         }
00297 
00298         @Override
00299         protected void init(NodeMainExecutor nodeMainExecutor) {
00300 
00301         }
00302 
00303         @Override
00304         public void RibbonMenuItemClick(int itemId) {
00305                 for (int i = 0; i < 13; i++) {
00306                         if (itemId == selectedSensor[i]) {
00307                                 currentSensor = i;
00308                                 textViewSensorName.setText(androidSensorMessageService
00309                                                 .getSensorName(i));
00310                                 textViewX.setText("x:");
00311                                 textViewY.setText("y:");
00312                                 textViewZ.setText("z:");
00313                                 if (publishedSensor[i] == -1) {
00314                                         setButton.setText("ON");
00315                                 } else {
00316                                         setButton.setText("OFF");
00317                                 }
00318                                 androidSensorMessageService.changeView(currentSensor);
00319                                 break;
00320                         }
00321                 }
00322         }
00323 
00324         public boolean onTouch(View v, MotionEvent event) {
00325                 if (event.getPointerCount() == 1) {
00326                         if (multiTouchAction > 0) {
00327                                 if (event.getAction() == MotionEvent.ACTION_UP) {
00328                                         multiTouchAction--;
00329                                 }
00330                         } else {
00331                                 if (event.getAction() == MotionEvent.ACTION_MOVE) {
00332                                         androidSensorMessageView.updateOriginalY(event.getY()
00333                                                         - previousY);
00334                                 }
00335                                 previousY = event.getY();
00336                         }
00337                 } else {
00338                         int pointerId1 = event.getPointerId(0);
00339                         int pointerId2 = event.getPointerId(1);
00340 
00341                         int pointerIndex1 = event.findPointerIndex(pointerId1);
00342                         int pointerIndex2 = event.findPointerIndex(pointerId2);
00343 
00344                         float distanceY = event.getY(pointerIndex2) - event.getY(pointerIndex1);
00345                         distanceY = (float)Math.sqrt(distanceY * distanceY);
00346                         if (event.getAction() == MotionEvent.ACTION_MOVE) {
00347                                 androidSensorMessageView.updateScale(distanceY - previousDistanceY);
00348                         }
00349                         Log.v("distance","pre:"+previousDistanceY+" now:"+distanceY);
00350                         previousDistanceY = distanceY;
00351                         
00352                         multiTouchAction = 1;
00353 
00354                 }
00355 
00356                 return true;
00357         }
00358 
00359 }


jsk_android_apps
Author(s): Kazuto Murase
autogenerated on Thu Jun 6 2019 18:03:46