Jason Sams | 177f844 | 2010-10-29 10:19:21 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 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.android.balls; |
| 18 | |
| 19 | import android.renderscript.RSSurfaceView; |
| 20 | import android.renderscript.RenderScript; |
| 21 | |
| 22 | import android.app.Activity; |
| 23 | import android.content.res.Configuration; |
| 24 | import android.os.Bundle; |
| 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
| 27 | import android.os.Message; |
| 28 | import android.provider.Settings.System; |
| 29 | import android.util.Config; |
| 30 | import android.util.Log; |
| 31 | import android.view.Menu; |
| 32 | import android.view.MenuItem; |
| 33 | import android.view.View; |
| 34 | import android.view.Window; |
| 35 | import android.widget.Button; |
| 36 | import android.widget.ListView; |
| 37 | |
| 38 | import java.lang.Runtime; |
| 39 | |
| 40 | import android.app.Activity; |
| 41 | import android.content.Context; |
| 42 | import android.os.Bundle; |
| 43 | import android.view.View; |
| 44 | import android.hardware.Sensor; |
| 45 | import android.hardware.SensorEvent; |
| 46 | import android.hardware.SensorEventListener; |
| 47 | import android.hardware.SensorManager; |
| 48 | |
| 49 | public class Balls extends Activity implements SensorEventListener { |
| 50 | //EventListener mListener = new EventListener(); |
| 51 | |
| 52 | private static final String LOG_TAG = "libRS_jni"; |
| 53 | private static final boolean DEBUG = false; |
| 54 | private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; |
| 55 | |
| 56 | private BallsView mView; |
| 57 | private SensorManager mSensorManager; |
| 58 | |
| 59 | // get the current looper (from your Activity UI thread for instance |
| 60 | |
| 61 | |
| 62 | public void onSensorChanged(SensorEvent event) { |
| 63 | //android.util.Log.d("rs", "sensor: " + event.sensor + ", x: " + event.values[0] + ", y: " + event.values[1] + ", z: " + event.values[2]); |
| 64 | synchronized (this) { |
| 65 | if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { |
| 66 | if(mView != null) { |
| 67 | mView.setAccel(event.values[0], event.values[1], event.values[2]); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onCreate(Bundle icicle) { |
| 78 | super.onCreate(icicle); |
| 79 | |
| 80 | mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); |
| 81 | |
| 82 | // Create our Preview view and set it as the content of our |
| 83 | // Activity |
| 84 | mView = new BallsView(this); |
| 85 | setContentView(mView); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected void onResume() { |
| 90 | mSensorManager.registerListener(this, |
| 91 | mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), |
| 92 | SensorManager.SENSOR_DELAY_FASTEST); |
| 93 | |
| 94 | // Ideally a game should implement onResume() and onPause() |
| 95 | // to take appropriate action when the activity looses focus |
| 96 | super.onResume(); |
| 97 | mView.onResume(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void onPause() { |
| 102 | Log.e("rs", "onPause"); |
| 103 | |
| 104 | // Ideally a game should implement onResume() and onPause() |
| 105 | // to take appropriate action when the activity looses focus |
| 106 | super.onPause(); |
| 107 | mView.onPause(); |
| 108 | |
| 109 | |
| 110 | |
| 111 | //Runtime.getRuntime().exit(0); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void onStop() { |
| 116 | mSensorManager.unregisterListener(this); |
| 117 | super.onStop(); |
| 118 | } |
| 119 | |
| 120 | static void log(String message) { |
| 121 | if (LOG_ENABLED) { |
| 122 | Log.v(LOG_TAG, message); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | } |
| 128 | |