Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.os;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.os.Bundle;
     22 import android.view.View;
     23 import android.hardware.Sensor;
     24 import android.hardware.SensorEvent;
     25 import android.hardware.SensorEventListener;
     26 import android.hardware.SensorManager;
     27 import android.graphics.Bitmap;
     28 import android.graphics.Canvas;
     29 import android.graphics.Color;
     30 import android.graphics.Paint;
     31 import android.graphics.Path;
     32 import android.graphics.RectF;
     33 
     34 /**
     35  * <h3>Application that displays the values of the acceleration sensor graphically.</h3>
     36 
     37 <p>This demonstrates the {@link android.hardware.SensorManager android.hardware.SensorManager} class.
     38 
     39 <h4>Demo</h4>
     40 OS / Sensors
     41 
     42 <h4>Source files</h4>
     43  * <table class="LinkTable">
     44  *         <tr>
     45  *             <td >src/com.example.android.apis/os/Sensors.java</td>
     46  *             <td >Sensors</td>
     47  *         </tr>
     48  * </table>
     49  */
     50 public class Sensors extends Activity {
     51     private SensorManager mSensorManager;
     52     private GraphView mGraphView;
     53 
     54     private class GraphView extends View implements SensorEventListener
     55     {
     56         private Bitmap  mBitmap;
     57         private Paint   mPaint = new Paint();
     58         private Canvas  mCanvas = new Canvas();
     59         private Path    mPath = new Path();
     60         private RectF   mRect = new RectF();
     61         private float   mLastValues[] = new float[3*2];
     62         private float   mOrientationValues[] = new float[3];
     63         private int     mColors[] = new int[3*2];
     64         private float   mLastX;
     65         private float   mScale[] = new float[2];
     66         private float   mYOffset;
     67         private float   mMaxX;
     68         private float   mSpeed = 1.0f;
     69         private float   mWidth;
     70         private float   mHeight;
     71 
     72         public GraphView(Context context) {
     73             super(context);
     74             mColors[0] = Color.argb(192, 255, 64, 64);
     75             mColors[1] = Color.argb(192, 64, 128, 64);
     76             mColors[2] = Color.argb(192, 64, 64, 255);
     77             mColors[3] = Color.argb(192, 64, 255, 255);
     78             mColors[4] = Color.argb(192, 128, 64, 128);
     79             mColors[5] = Color.argb(192, 255, 255, 64);
     80 
     81             mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
     82             mRect.set(-0.5f, -0.5f, 0.5f, 0.5f);
     83             mPath.arcTo(mRect, 0, 180);
     84         }
     85 
     86         @Override
     87         protected void onSizeChanged(int w, int h, int oldw, int oldh) {
     88             mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
     89             mCanvas.setBitmap(mBitmap);
     90             mCanvas.drawColor(0xFFFFFFFF);
     91             mYOffset = h * 0.5f;
     92             mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
     93             mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
     94             mWidth = w;
     95             mHeight = h;
     96             if (mWidth < mHeight) {
     97                 mMaxX = w;
     98             } else {
     99                 mMaxX = w-50;
    100             }
    101             mLastX = mMaxX;
    102             super.onSizeChanged(w, h, oldw, oldh);
    103         }
    104 
    105         @Override
    106         protected void onDraw(Canvas canvas) {
    107             synchronized (this) {
    108                 if (mBitmap != null) {
    109                     final Paint paint = mPaint;
    110                     final Path path = mPath;
    111                     final int outer = 0xFFC0C0C0;
    112                     final int inner = 0xFFff7010;
    113 
    114                     if (mLastX >= mMaxX) {
    115                         mLastX = 0;
    116                         final Canvas cavas = mCanvas;
    117                         final float yoffset = mYOffset;
    118                         final float maxx = mMaxX;
    119                         final float oneG = SensorManager.STANDARD_GRAVITY * mScale[0];
    120                         paint.setColor(0xFFAAAAAA);
    121                         cavas.drawColor(0xFFFFFFFF);
    122                         cavas.drawLine(0, yoffset,      maxx, yoffset,      paint);
    123                         cavas.drawLine(0, yoffset+oneG, maxx, yoffset+oneG, paint);
    124                         cavas.drawLine(0, yoffset-oneG, maxx, yoffset-oneG, paint);
    125                     }
    126                     canvas.drawBitmap(mBitmap, 0, 0, null);
    127 
    128                     float[] values = mOrientationValues;
    129                     if (mWidth < mHeight) {
    130                         float w0 = mWidth * 0.333333f;
    131                         float w  = w0 - 32;
    132                         float x = w0*0.5f;
    133                         for (int i=0 ; i<3 ; i++) {
    134                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
    135                             canvas.translate(x, w*0.5f + 4.0f);
    136                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
    137                             paint.setColor(outer);
    138                             canvas.scale(w, w);
    139                             canvas.drawOval(mRect, paint);
    140                             canvas.restore();
    141                             canvas.scale(w-5, w-5);
    142                             paint.setColor(inner);
    143                             canvas.rotate(-values[i]);
    144                             canvas.drawPath(path, paint);
    145                             canvas.restore();
    146                             x += w0;
    147                         }
    148                     } else {
    149                         float h0 = mHeight * 0.333333f;
    150                         float h  = h0 - 32;
    151                         float y = h0*0.5f;
    152                         for (int i=0 ; i<3 ; i++) {
    153                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
    154                             canvas.translate(mWidth - (h*0.5f + 4.0f), y);
    155                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
    156                             paint.setColor(outer);
    157                             canvas.scale(h, h);
    158                             canvas.drawOval(mRect, paint);
    159                             canvas.restore();
    160                             canvas.scale(h-5, h-5);
    161                             paint.setColor(inner);
    162                             canvas.rotate(-values[i]);
    163                             canvas.drawPath(path, paint);
    164                             canvas.restore();
    165                             y += h0;
    166                         }
    167                     }
    168 
    169                 }
    170             }
    171         }
    172 
    173         public void onSensorChanged(SensorEvent event) {
    174             //Log.d(TAG, "sensor: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
    175             synchronized (this) {
    176                 if (mBitmap != null) {
    177                     final Canvas canvas = mCanvas;
    178                     final Paint paint = mPaint;
    179                     if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
    180                         for (int i=0 ; i<3 ; i++) {
    181                             mOrientationValues[i] = event.values[i];
    182                         }
    183                     } else {
    184                         float deltaX = mSpeed;
    185                         float newX = mLastX + deltaX;
    186 
    187                         int j = (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) ? 1 : 0;
    188                         for (int i=0 ; i<3 ; i++) {
    189                             int k = i+j*3;
    190                             final float v = mYOffset + event.values[i] * mScale[j];
    191                             paint.setColor(mColors[k]);
    192                             canvas.drawLine(mLastX, mLastValues[k], newX, v, paint);
    193                             mLastValues[k] = v;
    194                         }
    195                         if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    196                             mLastX += mSpeed;
    197                     }
    198                     invalidate();
    199                 }
    200             }
    201         }
    202 
    203         public void onAccuracyChanged(Sensor sensor, int accuracy) {
    204         }
    205     }
    206 
    207     /**
    208      * Initialization of the Activity after it is first created.  Must at least
    209      * call {@link android.app.Activity#setContentView setContentView()} to
    210      * describe what is to be displayed in the screen.
    211      */
    212     @Override
    213     protected void onCreate(Bundle savedInstanceState) {
    214         // Be sure to call the super class.
    215         super.onCreate(savedInstanceState);
    216 
    217         mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    218         mGraphView = new GraphView(this);
    219         setContentView(mGraphView);
    220     }
    221 
    222     @Override
    223     protected void onResume() {
    224         super.onResume();
    225         mSensorManager.registerListener(mGraphView,
    226                 mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    227                 SensorManager.SENSOR_DELAY_FASTEST);
    228         mSensorManager.registerListener(mGraphView,
    229                 mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
    230                 SensorManager.SENSOR_DELAY_FASTEST);
    231         mSensorManager.registerListener(mGraphView,
    232                 mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
    233                 SensorManager.SENSOR_DELAY_FASTEST);
    234     }
    235 
    236     @Override
    237     protected void onStop() {
    238         mSensorManager.unregisterListener(mGraphView);
    239         super.onStop();
    240     }
    241 }
    242