auto import from //depot/cupcake/@135843
This commit is contained in:
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2007 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.os;
|
||||
|
||||
// Need the following import to get access to the app resources, since this
|
||||
// class is in a sub-package.
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.Vibrator;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import com.example.android.apis.R;
|
||||
|
||||
/**
|
||||
* <h3>App that vibrates the vibrator with the Morse Code for a string.</h3>
|
||||
|
||||
<p>This demonstrates the {@link android.os.Vibrator android.os.Vibrator} class.
|
||||
|
||||
<h4>Demo</h4>
|
||||
OS / Morse Code Vibrator
|
||||
|
||||
<h4>Source files</h4>
|
||||
* <table class="LinkTable">
|
||||
* <tr>
|
||||
* <td >src/com.example.android.apis/os/MorseCode.java</td>
|
||||
* <td >The Morse Code Vibrator</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td >res/any/layout/morse_code.xml</td>
|
||||
* <td >Defines contents of the screen</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*/
|
||||
public class MorseCode extends Activity
|
||||
{
|
||||
/** Tag string for our debug logs */
|
||||
private static final String TAG = "MorseCode";
|
||||
|
||||
/** Our text view */
|
||||
private TextView mTextView;
|
||||
|
||||
;
|
||||
|
||||
/**
|
||||
* Initialization of the Activity after it is first created. Must at least
|
||||
* call {@link android.app.Activity#setContentView setContentView()} to
|
||||
* describe what is to be displayed in the screen.
|
||||
*/
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
// Be sure to call the super class.
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// See assets/res/any/layout/hello_world.xml for this
|
||||
// view layout definition, which is being set here as
|
||||
// the content of our screen.
|
||||
setContentView(R.layout.morse_code);
|
||||
|
||||
// Set the OnClickListener for the button so we see when it's pressed.
|
||||
findViewById(R.id.button).setOnClickListener(mClickListener);
|
||||
|
||||
// Save the text view so we don't have to look it up each time
|
||||
mTextView = (TextView)findViewById(R.id.text);
|
||||
}
|
||||
|
||||
/** Called when the button is pushed */
|
||||
View.OnClickListener mClickListener = new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
// Get the text out of the view
|
||||
String text = mTextView.getText().toString();
|
||||
|
||||
// convert it using the function defined above. See the docs for
|
||||
// android.os.Vibrator for more info about the format of this array
|
||||
long[] pattern = MorseCodeConverter.pattern(text);
|
||||
|
||||
// Start the vibration
|
||||
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
|
||||
vibrator.vibrate(pattern, -1);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.os;
|
||||
|
||||
/** Class that implements the text to morse code coversion */
|
||||
class MorseCodeConverter {
|
||||
private static final long SPEED_BASE = 100;
|
||||
static final long DOT = SPEED_BASE;
|
||||
static final long DASH = SPEED_BASE * 3;
|
||||
static final long GAP = SPEED_BASE;
|
||||
static final long LETTER_GAP = SPEED_BASE * 3;
|
||||
static final long WORD_GAP = SPEED_BASE * 7;
|
||||
|
||||
/** The characters from 'A' to 'Z' */
|
||||
private static final long[][] LETTERS = new long[][] {
|
||||
/* A */ new long[] { DOT, GAP, DASH },
|
||||
/* B */ new long[] { DASH, GAP, DOT, GAP, DOT, GAP, DOT },
|
||||
/* C */ new long[] { DASH, GAP, DOT, GAP, DASH, GAP, DOT },
|
||||
/* D */ new long[] { DASH, GAP, DOT, GAP, DOT },
|
||||
/* E */ new long[] { DOT },
|
||||
/* F */ new long[] { DOT, GAP, DOT, GAP, DASH, GAP, DOT },
|
||||
/* G */ new long[] { DASH, GAP, DASH, GAP, DOT },
|
||||
/* H */ new long[] { DOT, GAP, DOT, GAP, DOT, GAP, DOT },
|
||||
/* I */ new long[] { DOT, GAP, DOT },
|
||||
/* J */ new long[] { DOT, GAP, DASH, GAP, DASH, GAP, DASH },
|
||||
/* K */ new long[] { DASH, GAP, DOT, GAP, DASH },
|
||||
/* L */ new long[] { DOT, GAP, DASH, GAP, DOT, GAP, DOT },
|
||||
/* M */ new long[] { DASH, GAP, DASH },
|
||||
/* N */ new long[] { DASH, GAP, DOT },
|
||||
/* O */ new long[] { DASH, GAP, DASH, GAP, DASH },
|
||||
/* P */ new long[] { DOT, GAP, DASH, GAP, DASH, GAP, DOT },
|
||||
/* Q */ new long[] { DASH, GAP, DASH, GAP, DOT, GAP, DASH },
|
||||
/* R */ new long[] { DOT, GAP, DASH, GAP, DOT },
|
||||
/* S */ new long[] { DOT, GAP, DOT, GAP, DOT },
|
||||
/* T */ new long[] { DASH },
|
||||
/* U */ new long[] { DOT, GAP, DOT, GAP, DASH },
|
||||
/* V */ new long[] { DOT, GAP, DOT, GAP, DASH },
|
||||
/* W */ new long[] { DOT, GAP, DASH, GAP, DASH },
|
||||
/* X */ new long[] { DASH, GAP, DOT, GAP, DOT, GAP, DASH },
|
||||
/* Y */ new long[] { DASH, GAP, DOT, GAP, DASH, GAP, DASH },
|
||||
/* Z */ new long[] { DASH, GAP, DASH, GAP, DOT, GAP, DOT },
|
||||
};
|
||||
|
||||
/** The characters from '0' to '9' */
|
||||
private static final long[][] NUMBERS = new long[][] {
|
||||
/* 0 */ new long[] { DASH, GAP, DASH, GAP, DASH, GAP, DASH, GAP, DASH },
|
||||
/* 1 */ new long[] { DOT, GAP, DASH, GAP, DASH, GAP, DASH, GAP, DASH },
|
||||
/* 2 */ new long[] { DOT, GAP, DOT, GAP, DASH, GAP, DASH, GAP, DASH },
|
||||
/* 3 */ new long[] { DOT, GAP, DOT, GAP, DOT, GAP, DASH, GAP, DASH },
|
||||
/* 4 */ new long[] { DOT, GAP, DOT, GAP, DOT, GAP, DOT, GAP, DASH },
|
||||
/* 5 */ new long[] { DOT, GAP, DOT, GAP, DOT, GAP, DOT, GAP, DOT },
|
||||
/* 6 */ new long[] { DASH, GAP, DOT, GAP, DOT, GAP, DOT, GAP, DOT },
|
||||
/* 7 */ new long[] { DASH, GAP, DASH, GAP, DOT, GAP, DOT, GAP, DOT },
|
||||
/* 8 */ new long[] { DASH, GAP, DASH, GAP, DASH, GAP, DOT, GAP, DOT },
|
||||
/* 9 */ new long[] { DASH, GAP, DASH, GAP, DASH, GAP, DASH, GAP, DOT },
|
||||
};
|
||||
|
||||
private static final long[] ERROR_GAP = new long[] { GAP };
|
||||
|
||||
/** Return the pattern data for a given character */
|
||||
static long[] pattern(char c) {
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
return LETTERS[c - 'A'];
|
||||
}
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
return LETTERS[c - 'a'];
|
||||
}
|
||||
else if (c >= '0' && c <= '9') {
|
||||
return NUMBERS[c - '0'];
|
||||
}
|
||||
else {
|
||||
return ERROR_GAP;
|
||||
}
|
||||
}
|
||||
|
||||
static long[] pattern(String str) {
|
||||
boolean lastWasWhitespace;
|
||||
int strlen = str.length();
|
||||
|
||||
// Calculate how long our array needs to be.
|
||||
int len = 1;
|
||||
lastWasWhitespace = true;
|
||||
for (int i=0; i<strlen; i++) {
|
||||
char c = str.charAt(i);
|
||||
if (Character.isWhitespace(c)) {
|
||||
if (!lastWasWhitespace) {
|
||||
len++;
|
||||
lastWasWhitespace = true;
|
||||
}
|
||||
} else {
|
||||
if (!lastWasWhitespace) {
|
||||
len++;
|
||||
}
|
||||
lastWasWhitespace = false;
|
||||
len += pattern(c).length;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the pattern array. Note that we put an extra element of 0
|
||||
// in at the beginning, because the pattern always starts with the pause,
|
||||
// not with the vibration.
|
||||
long[] result = new long[len+1];
|
||||
result[0] = 0;
|
||||
int pos = 1;
|
||||
lastWasWhitespace = true;
|
||||
for (int i=0; i<strlen; i++) {
|
||||
char c = str.charAt(i);
|
||||
if (Character.isWhitespace(c)) {
|
||||
if (!lastWasWhitespace) {
|
||||
result[pos] = WORD_GAP;
|
||||
pos++;
|
||||
lastWasWhitespace = true;
|
||||
}
|
||||
} else {
|
||||
if (!lastWasWhitespace) {
|
||||
result[pos] = LETTER_GAP;
|
||||
pos++;
|
||||
}
|
||||
lastWasWhitespace = false;
|
||||
long[] letter = pattern(c);
|
||||
System.arraycopy(letter, 0, result, pos, letter.length);
|
||||
pos += letter.length;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,241 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2007 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.os;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.hardware.SensorManager;
|
||||
import android.hardware.SensorListener;
|
||||
import android.util.Log;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
import android.graphics.RectF;
|
||||
|
||||
/**
|
||||
* <h3>Application that displays the values of the acceleration sensor graphically.</h3>
|
||||
|
||||
<p>This demonstrates the {@link android.hardware.SensorManager android.hardware.SensorManager} class.
|
||||
|
||||
<h4>Demo</h4>
|
||||
OS / Sensors
|
||||
|
||||
<h4>Source files</h4>
|
||||
* <table class="LinkTable">
|
||||
* <tr>
|
||||
* <td >src/com.example.android.apis/os/Sensors.java</td>
|
||||
* <td >Sensors</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*/
|
||||
public class Sensors extends Activity {
|
||||
/** Tag string for our debug logs */
|
||||
private static final String TAG = "Sensors";
|
||||
|
||||
private SensorManager mSensorManager;
|
||||
private GraphView mGraphView;
|
||||
|
||||
private class GraphView extends View implements SensorListener
|
||||
{
|
||||
private Bitmap mBitmap;
|
||||
private Paint mPaint = new Paint();
|
||||
private Canvas mCanvas = new Canvas();
|
||||
private Path mPath = new Path();
|
||||
private RectF mRect = new RectF();
|
||||
private float mLastValues[] = new float[3*2];
|
||||
private float mOrientationValues[] = new float[3];
|
||||
private int mColors[] = new int[3*2];
|
||||
private float mLastX;
|
||||
private float mScale[] = new float[2];
|
||||
private float mYOffset;
|
||||
private float mMaxX;
|
||||
private float mSpeed = 1.0f;
|
||||
private float mWidth;
|
||||
private float mHeight;
|
||||
|
||||
public GraphView(Context context) {
|
||||
super(context);
|
||||
mColors[0] = Color.argb(192, 255, 64, 64);
|
||||
mColors[1] = Color.argb(192, 64, 128, 64);
|
||||
mColors[2] = Color.argb(192, 64, 64, 255);
|
||||
mColors[3] = Color.argb(192, 64, 255, 255);
|
||||
mColors[4] = Color.argb(192, 128, 64, 128);
|
||||
mColors[5] = Color.argb(192, 255, 255, 64);
|
||||
|
||||
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
|
||||
mRect.set(-0.5f, -0.5f, 0.5f, 0.5f);
|
||||
mPath.arcTo(mRect, 0, 180);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
|
||||
mCanvas.setBitmap(mBitmap);
|
||||
mCanvas.drawColor(0xFFFFFFFF);
|
||||
mYOffset = h * 0.5f;
|
||||
mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
|
||||
mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
|
||||
mWidth = w;
|
||||
mHeight = h;
|
||||
if (mWidth < mHeight) {
|
||||
mMaxX = w;
|
||||
} else {
|
||||
mMaxX = w-50;
|
||||
}
|
||||
mLastX = mMaxX;
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
synchronized (this) {
|
||||
if (mBitmap != null) {
|
||||
final Paint paint = mPaint;
|
||||
final Path path = mPath;
|
||||
final int outer = 0xFFC0C0C0;
|
||||
final int inner = 0xFFff7010;
|
||||
|
||||
if (mLastX >= mMaxX) {
|
||||
mLastX = 0;
|
||||
final Canvas cavas = mCanvas;
|
||||
final float yoffset = mYOffset;
|
||||
final float maxx = mMaxX;
|
||||
final float oneG = SensorManager.STANDARD_GRAVITY * mScale[0];
|
||||
paint.setColor(0xFFAAAAAA);
|
||||
cavas.drawColor(0xFFFFFFFF);
|
||||
cavas.drawLine(0, yoffset, maxx, yoffset, paint);
|
||||
cavas.drawLine(0, yoffset+oneG, maxx, yoffset+oneG, paint);
|
||||
cavas.drawLine(0, yoffset-oneG, maxx, yoffset-oneG, paint);
|
||||
}
|
||||
canvas.drawBitmap(mBitmap, 0, 0, null);
|
||||
|
||||
float[] values = mOrientationValues;
|
||||
if (mWidth < mHeight) {
|
||||
float w0 = mWidth * 0.333333f;
|
||||
float w = w0 - 32;
|
||||
float x = w0*0.5f;
|
||||
for (int i=0 ; i<3 ; i++) {
|
||||
canvas.save(Canvas.MATRIX_SAVE_FLAG);
|
||||
canvas.translate(x, w*0.5f + 4.0f);
|
||||
canvas.save(Canvas.MATRIX_SAVE_FLAG);
|
||||
paint.setColor(outer);
|
||||
canvas.scale(w, w);
|
||||
canvas.drawOval(mRect, paint);
|
||||
canvas.restore();
|
||||
canvas.scale(w-5, w-5);
|
||||
paint.setColor(inner);
|
||||
canvas.rotate(-values[i]);
|
||||
canvas.drawPath(path, paint);
|
||||
canvas.restore();
|
||||
x += w0;
|
||||
}
|
||||
} else {
|
||||
float h0 = mHeight * 0.333333f;
|
||||
float h = h0 - 32;
|
||||
float y = h0*0.5f;
|
||||
for (int i=0 ; i<3 ; i++) {
|
||||
canvas.save(Canvas.MATRIX_SAVE_FLAG);
|
||||
canvas.translate(mWidth - (h*0.5f + 4.0f), y);
|
||||
canvas.save(Canvas.MATRIX_SAVE_FLAG);
|
||||
paint.setColor(outer);
|
||||
canvas.scale(h, h);
|
||||
canvas.drawOval(mRect, paint);
|
||||
canvas.restore();
|
||||
canvas.scale(h-5, h-5);
|
||||
paint.setColor(inner);
|
||||
canvas.rotate(-values[i]);
|
||||
canvas.drawPath(path, paint);
|
||||
canvas.restore();
|
||||
y += h0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onSensorChanged(int sensor, float[] values) {
|
||||
//Log.d(TAG, "sensor: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
|
||||
synchronized (this) {
|
||||
if (mBitmap != null) {
|
||||
final Canvas canvas = mCanvas;
|
||||
final Paint paint = mPaint;
|
||||
if (sensor == SensorManager.SENSOR_ORIENTATION) {
|
||||
for (int i=0 ; i<3 ; i++) {
|
||||
mOrientationValues[i] = values[i];
|
||||
}
|
||||
} else {
|
||||
float deltaX = mSpeed;
|
||||
float newX = mLastX + deltaX;
|
||||
|
||||
int j = (sensor == SensorManager.SENSOR_MAGNETIC_FIELD) ? 1 : 0;
|
||||
for (int i=0 ; i<3 ; i++) {
|
||||
int k = i+j*3;
|
||||
final float v = mYOffset + values[i] * mScale[j];
|
||||
paint.setColor(mColors[k]);
|
||||
canvas.drawLine(mLastX, mLastValues[k], newX, v, paint);
|
||||
mLastValues[k] = v;
|
||||
}
|
||||
if (sensor == SensorManager.SENSOR_MAGNETIC_FIELD)
|
||||
mLastX += mSpeed;
|
||||
}
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onAccuracyChanged(int sensor, int accuracy) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialization of the Activity after it is first created. Must at least
|
||||
* call {@link android.app.Activity#setContentView setContentView()} to
|
||||
* describe what is to be displayed in the screen.
|
||||
*/
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
// Be sure to call the super class.
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||||
mGraphView = new GraphView(this);
|
||||
setContentView(mGraphView);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mSensorManager.registerListener(mGraphView,
|
||||
SensorManager.SENSOR_ACCELEROMETER |
|
||||
SensorManager.SENSOR_MAGNETIC_FIELD |
|
||||
SensorManager.SENSOR_ORIENTATION,
|
||||
SensorManager.SENSOR_DELAY_FASTEST);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
mSensorManager.unregisterListener(mGraphView);
|
||||
super.onStop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user