1 package android.support.v13.dreams; 2 3 import android.app.Activity; 4 import android.content.BroadcastReceiver; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.content.IntentFilter; 8 import android.graphics.Canvas; 9 import android.os.BatteryManager; 10 import android.util.AttributeSet; 11 import android.util.Log; 12 import android.view.MotionEvent; 13 import android.view.View; 14 import android.view.WindowManager; 15 16 public class BasicDream extends Activity { 17 /** A simple Dream implementation that can be subclassed to write your own Dreams. Any Activity 18 * may be used as a Dream, so this class isn't strictly necessary. However, it does take care of 19 * a number of housekeeping tasks that most screensavers will want: 20 * <ul> 21 * <li>Keep the screen on as long as the device is plugged in 22 * <li>Exit (using <code>finish()</code>) as soon as any user activity is detected 23 * <li>Hide the system UI (courtesy its inner {@link BasicDreamView} class) 24 * </ul> 25 * Finally, it exposes an {@link BasicDream#onDraw(Canvas)} method that you can override to do 26 * your own drawing. As with a <code>View,</code> call {@link BasicDream#invalidate()} any time 27 * to request that a new frame be drawn. 28 */ 29 private final static String TAG = "BasicDream"; 30 private final static boolean DEBUG = true; 31 32 private View mView; 33 private boolean mPlugged = false; 34 35 public class BasicDreamView extends View { 36 /** A simple view that just calls back to {@link BasicDream#onDraw(Canvas) onDraw} on its 37 * parent BasicDream Activity. It also hides the system UI if this feature is available on 38 * the current device. 39 */ 40 public BasicDreamView(Context c) { 41 super(c); 42 } 43 44 public BasicDreamView(Context c, AttributeSet at) { 45 super(c, at); 46 } 47 48 @Override 49 public void onAttachedToWindow() { 50 setSystemUiVisibility(View.STATUS_BAR_HIDDEN); 51 } 52 53 @Override 54 public void onDraw(Canvas c) { 55 BasicDream.this.onDraw(c); 56 } 57 } 58 59 private final BroadcastReceiver mPowerIntentReceiver = new BroadcastReceiver() { 60 @Override 61 public void onReceive(Context context, Intent intent) { 62 final String action = intent.getAction(); 63 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 64 // Only keep the screen on if we're plugged in. 65 boolean plugged = (1 == intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)); 66 67 if (plugged != mPlugged) { 68 if (DEBUG) Log.d(TAG, "now " + (plugged ? "plugged in" : "unplugged")); 69 70 mPlugged = plugged; 71 if (mPlugged) { 72 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 73 } else { 74 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 75 } 76 } 77 } 78 } 79 }; 80 81 @Override 82 public void onStart() { 83 super.onStart(); 84 setContentView(new BasicDreamView(this)); 85 getWindow().addFlags( 86 WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON 87 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 88 ); 89 IntentFilter filter = new IntentFilter(); 90 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 91 registerReceiver(mPowerIntentReceiver, filter); 92 } 93 94 @Override 95 public void onPause() { 96 super.onPause(); 97 // Anything that would pause this activity should probably end the screensaver. 98 if (DEBUG) Log.d(TAG, "exiting onPause"); 99 finish(); 100 } 101 102 @Override 103 public void onStop() { 104 super.onStop(); 105 unregisterReceiver(mPowerIntentReceiver); 106 } 107 108 protected View getContentView() { 109 return mView; 110 } 111 112 @Override 113 public void setContentView(View v) { 114 super.setContentView(v); 115 mView = v; 116 } 117 118 protected void invalidate() { 119 getContentView().invalidate(); 120 } 121 122 public void onDraw(Canvas c) { 123 } 124 125 public void onUserInteraction() { 126 if (DEBUG) Log.d(TAG, "exiting onUserInteraction"); 127 finish(); 128 } 129 } 130 131