1 package com.google.android.DemoKit; 2 3 import android.os.Bundle; 4 import android.view.Menu; 5 import android.view.MenuItem; 6 7 public class BaseActivity extends DemoKitActivity { 8 9 private InputController mInputController; 10 11 public BaseActivity() { 12 super(); 13 } 14 15 @Override 16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 if (mAccessory != null) { 19 showControls(); 20 } else { 21 hideControls(); 22 } 23 } 24 25 @Override 26 public boolean onCreateOptionsMenu(Menu menu) { 27 menu.add("Simulate"); 28 menu.add("Quit"); 29 return true; 30 } 31 32 @Override 33 public boolean onOptionsItemSelected(MenuItem item) { 34 if (item.getTitle() == "Simulate") { 35 showControls(); 36 } else if (item.getTitle() == "Quit") { 37 finish(); 38 System.exit(0); 39 } 40 return true; 41 } 42 43 protected void enableControls(boolean enable) { 44 if (enable) { 45 showControls(); 46 } else { 47 hideControls(); 48 } 49 } 50 51 protected void hideControls() { 52 setContentView(R.layout.no_device); 53 mInputController = null; 54 } 55 56 protected void showControls() { 57 setContentView(R.layout.main); 58 59 mInputController = new InputController(this); 60 mInputController.accessoryAttached(); 61 } 62 63 protected void handleJoyMessage(JoyMsg j) { 64 if (mInputController != null) { 65 mInputController.joystickMoved(j.getX(), j.getY()); 66 } 67 } 68 69 protected void handleLightMessage(LightMsg l) { 70 if (mInputController != null) { 71 mInputController.setLightValue(l.getLight()); 72 } 73 } 74 75 protected void handleTemperatureMessage(TemperatureMsg t) { 76 if (mInputController != null) { 77 mInputController.setTemperature(t.getTemperature()); 78 } 79 } 80 81 protected void handleSwitchMessage(SwitchMsg o) { 82 if (mInputController != null) { 83 byte sw = o.getSw(); 84 if (sw >= 0 && sw < 4) { 85 mInputController.switchStateChanged(sw, o.getState() != 0); 86 } else if (sw == 4) { 87 mInputController 88 .joystickButtonSwitchStateChanged(o.getState() != 0); 89 } 90 } 91 } 92 93 }