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