Home | History | Annotate | Download | only in balls
      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.example.android.rs.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.Log;
     30 import android.view.Menu;
     31 import android.view.MenuItem;
     32 import android.view.View;
     33 import android.view.Window;
     34 import android.widget.Button;
     35 import android.widget.ListView;
     36 
     37 import android.app.Activity;
     38 import android.content.Context;
     39 import android.os.Bundle;
     40 import android.view.View;
     41 import android.hardware.Sensor;
     42 import android.hardware.SensorEvent;
     43 import android.hardware.SensorEventListener;
     44 import android.hardware.SensorManager;
     45 
     46 public class Balls extends Activity implements SensorEventListener {
     47     //EventListener mListener = new EventListener();
     48 
     49     private static final String LOG_TAG = "libRS_jni";
     50     private static final boolean DEBUG  = false;
     51     private static final boolean LOG_ENABLED = false;
     52 
     53     private BallsView mView;
     54     private SensorManager mSensorManager;
     55 
     56     // get the current looper (from your Activity UI thread for instance
     57 
     58 
     59     public void onSensorChanged(SensorEvent event) {
     60         //android.util.Log.d("rs", "sensor: " + event.sensor + ", x: " + event.values[0] + ", y: " + event.values[1] + ", z: " + event.values[2]);
     61         synchronized (this) {
     62             if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
     63                 if(mView != null) {
     64                     mView.setAccel(event.values[0], event.values[1], event.values[2]);
     65                 }
     66             }
     67         }
     68     }
     69 
     70     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     71     }
     72 
     73     @Override
     74     public void onCreate(Bundle icicle) {
     75         super.onCreate(icicle);
     76 
     77         mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
     78 
     79         // Create our Preview view and set it as the content of our
     80         // Activity
     81         mView = new BallsView(this);
     82         setContentView(mView);
     83     }
     84 
     85     @Override
     86     protected void onResume() {
     87         mSensorManager.registerListener(this,
     88                                         mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
     89                                         SensorManager.SENSOR_DELAY_FASTEST);
     90 
     91         // Ideally a game should implement onResume() and onPause()
     92         // to take appropriate action when the activity looses focus
     93         super.onResume();
     94         mView.resume();
     95     }
     96 
     97     @Override
     98     protected void onPause() {
     99         super.onPause();
    100         mView.pause();
    101         onStop();
    102     }
    103 
    104     @Override
    105     protected void onStop() {
    106         mSensorManager.unregisterListener(this);
    107         super.onStop();
    108     }
    109 
    110     static void log(String message) {
    111         if (LOG_ENABLED) {
    112             Log.v(LOG_TAG, message);
    113         }
    114     }
    115 
    116 
    117 }
    118 
    119