Skip to content

Instantly share code, notes, and snippets.

@blundell
Last active August 29, 2015 14:03
Show Gist options
  • Save blundell/bba18a128214d5fa1250 to your computer and use it in GitHub Desktop.
Save blundell/bba18a128214d5fa1250 to your computer and use it in GitHub Desktop.

Revisions

  1. blundell revised this gist Jul 14, 2014. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion WatchFaceLifecycle.java
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,14 @@ public void onDisplayRemoved(int displayId) {

    @Override
    public void onDisplayChanged(int displayId) {
    switch (displayManager.getDisplay(displayId).getState()) {
    Display display = displayManager.getDisplay(displayId);
    if(display == null) {
    // No display found for this ID, treating this as an onScreenOff() but you could remove this line
    // and swallow this exception quietly. What circumstance means 'there is no display for this id'?
    listener.onScreenOff();
    return;
    }
    switch (display.getState()) {
    case Display.STATE_DOZING:
    listener.onScreenDim();
    break;
  2. blundell revised this gist Jul 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WatchFaceLifecycle.java
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    * Prefer composition over inheritance
    * <p/>
    * This was created by Paul Blundell based on the Unofficial Base WatchFace Activity by Tavon Gatling
    *
    * https://gist.github.com/blundell/bba18a128214d5fa1250
    * https://gist.github.com/kentarosu/52fb21eb92181716b0ce
    * http://gist.github.com/PomepuyN/cdd821eca163a3279de2.
    */
  3. blundell revised this gist Jul 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WatchFaceLifecycle-ExampleActivity.java
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ protected void onCreate(Bundle savedInstanceState) {
    WatchFaceLifecycle.attach(this, savedInstanceState, this);
    }

    @Override
    @Override
    public void onScreenDim() {

    }
  4. blundell renamed this gist Jul 13, 2014. 1 changed file with 0 additions and 0 deletions.
  5. blundell renamed this gist Jul 13, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. blundell revised this gist Jul 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ExampleActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    public class ExampleAtivity extends Activity implements WatchFaceLifecycle.Listener {
    public class ExampleActivity extends Activity implements WatchFaceLifecycle.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
  7. blundell revised this gist Jul 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ExampleActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    public class Exampletivity extends Activity implements WatchFaceLifecycle.Listener {
    public class ExampleAtivity extends Activity implements WatchFaceLifecycle.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
  8. blundell revised this gist Jul 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ExampleActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    public class MyExampletivity extends Activity implements WatchFaceLifecycle.Listener {
    public class Exampletivity extends Activity implements WatchFaceLifecycle.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
  9. blundell revised this gist Jul 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ExampleActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    public class MyActivity extends Activity implements WatchFaceLifecycle.Listener {
    public class MyExampletivity extends Activity implements WatchFaceLifecycle.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
  10. blundell renamed this gist Jul 13, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. blundell renamed this gist Jul 13, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Example Activity → ExampleActivity
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    Example use:

    public class MyActivity extends Activity implements WatchFaceLifecycle.Listener {

    @Override
  12. blundell renamed this gist Jul 13, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. blundell created this gist Jul 13, 2014.
    31 changes: 31 additions & 0 deletions Example Activity
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    Example use:

    public class MyActivity extends Activity implements WatchFaceLifecycle.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_layout);
    WatchFaceLifecycle.attach(this, savedInstanceState, this);
    }

    @Override
    public void onScreenDim() {

    }

    @Override
    public void onScreenAwake() {

    }

    @Override
    public void onWatchFaceRemoved() {

    }

    @Override
    public void onScreenOff() {

    }
    }
    136 changes: 136 additions & 0 deletions WatchFaceLifecycle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    import android.app.Activity;
    import android.app.Application;
    import android.content.Context;
    import android.hardware.display.DisplayManager;
    import android.os.Bundle;
    import android.view.Display;

    /**
    * A listener for watch faces that have callbacks for the various screen states (dim, awake, and off)
    * as well as a callback for when the watch face is removed.
    * <p/>
    * Prefer composition over inheritance
    * <p/>
    * This was created by Paul Blundell based on the Unofficial Base WatchFace Activity by Tavon Gatling
    *
    * https://gist.github.com/kentarosu/52fb21eb92181716b0ce
    * http://gist.github.com/PomepuyN/cdd821eca163a3279de2.
    */
    public class WatchFaceLifecycle {

    interface Listener {
    /**
    * Used to detect when the watch is dimming.<br/>
    * Remember to make gray-scale versions of your watch face so they won't burn
    * and drain battery unnecessarily.
    */
    void onScreenDim();

    /**
    * Used to detect when the watch is not in a dimmed state.<br/>
    * This does not handle when returning "home" from a different activity/application.
    */
    void onScreenAwake();

    /**
    * Used to detect when a watch face is being removed (switched).<br/>
    * You can either do what you need here, or simply override {@code onDestroy()}.
    */
    void onWatchFaceRemoved();

    /**
    * When the screen is OFF due to "Always-On" being disabled.
    */
    void onScreenOff();

    }

    private WatchFaceLifecycle() {
    }

    public static void attach(Activity activity, Bundle savedInstanceState, Listener listener) {
    RealWatchFaceLifecycleCallbacks.newInstance(activity, savedInstanceState, listener);
    }

    private static class RealWatchFaceLifecycleCallbacks implements Application.ActivityLifecycleCallbacks, DisplayManager.DisplayListener {

    private final DisplayManager displayManager;
    private final Listener listener;

    public RealWatchFaceLifecycleCallbacks(DisplayManager displayManager, Listener listener) {
    this.displayManager = displayManager;
    this.listener = listener;
    }

    private static void newInstance(Activity activity, Bundle savedInstanceState, Listener listener) {
    DisplayManager displayManager = (DisplayManager) activity.getSystemService(Context.DISPLAY_SERVICE);
    RealWatchFaceLifecycleCallbacks watchFace = new RealWatchFaceLifecycleCallbacks(displayManager, listener);
    activity.getApplication().registerActivityLifecycleCallbacks(watchFace);
    watchFace.onActivityCreated(activity, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    // Set up the display manager and register a listener (this activity).
    displayManager.registerDisplayListener(this, null);
    }

    @Override
    public void onActivityStarted(Activity activity) {
    // not used
    }

    @Override
    public void onActivityResumed(Activity activity) {
    // not used
    }

    @Override
    public void onDisplayAdded(int displayId) {
    // In testing, this was never called, so the listener for this was removed.
    }

    @Override
    public void onDisplayRemoved(int displayId) {
    listener.onWatchFaceRemoved();
    }

    @Override
    public void onDisplayChanged(int displayId) {
    switch (displayManager.getDisplay(displayId).getState()) {
    case Display.STATE_DOZING:
    listener.onScreenDim();
    break;
    case Display.STATE_OFF:
    listener.onScreenOff();
    break;
    default:
    // Not really sure what to so about Display.STATE_UNKNOWN, so we'll treat it as if the screen is normal.
    listener.onScreenAwake();
    break;
    }
    }

    @Override
    public void onActivityPaused(Activity activity) {
    // not used
    }

    @Override
    public void onActivityStopped(Activity activity) {
    // not used
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    // not used
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    activity.getApplication().unregisterActivityLifecycleCallbacks(this);
    // Unregister the listener. If you don't, even after the watch face is gone, it will still accept your callbacks.
    displayManager.unregisterDisplayListener(this);
    }
    }
    }