From 57773ae02b909cce47d4eb824a7125c7906f4b99 Mon Sep 17 00:00:00 2001 From: Yoni Samlan Date: Sun, 6 Jul 2014 15:13:26 -0400 Subject: [PATCH] Handle screen-off gesture, v1.0.2 bump - Restart the app when the screen comes back on if the palm-over-screen gesture is used --- mobile/build.gradle | 4 +- mobile/src/main/res/values/strings.xml | 2 +- readme.md | 3 + wear/build.gradle | 4 +- .../yonisamlan/wearbabytime/MainActivity.java | 85 ++++++++++++++----- .../wearbabytime/ScreenReceiver.java | 15 ++++ 6 files changed, 86 insertions(+), 27 deletions(-) create mode 100644 wear/src/main/java/com/yonisamlan/wearbabytime/ScreenReceiver.java diff --git a/mobile/build.gradle b/mobile/build.gradle index 3a47753..c53f4c0 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.yonisamlan.wearbabytime" minSdkVersion 18 targetSdkVersion 20 - versionCode 2 - versionName "1.0.1" + versionCode 3 + versionName "1.0.2" } buildTypes { release { diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 9977649..5790a8f 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -1,7 +1,7 @@ - Wear Baby Time + Baby Time for Android Wear Hello world! Settings diff --git a/readme.md b/readme.md index d982e1b..8b622e7 100644 --- a/readme.md +++ b/readme.md @@ -15,6 +15,9 @@ Any other types of touches will keep the watch locked and just show a message: ✖ BABY-LIKE TAPPING DETECTED +The screen can still be turned off with the palm-over-screen gesture, but Baby Time will restart as +soon as the screen is turned on again. + # Getting the app To run the app, install the release application from [Google Play] diff --git a/wear/build.gradle b/wear/build.gradle index 2af4508..4c6b3a3 100644 --- a/wear/build.gradle +++ b/wear/build.gradle @@ -9,8 +9,8 @@ android { applicationId "com.yonisamlan.wearbabytime" minSdkVersion 20 targetSdkVersion 20 - versionCode 2 - versionName "1.0.1" + versionCode 3 + versionName "1.0.2" } buildTypes { release { diff --git a/wear/src/main/java/com/yonisamlan/wearbabytime/MainActivity.java b/wear/src/main/java/com/yonisamlan/wearbabytime/MainActivity.java index 75b4afc..060de65 100644 --- a/wear/src/main/java/com/yonisamlan/wearbabytime/MainActivity.java +++ b/wear/src/main/java/com/yonisamlan/wearbabytime/MainActivity.java @@ -1,6 +1,9 @@ package com.yonisamlan.wearbabytime; import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -15,11 +18,9 @@ import android.widget.TextView; import android.widget.Toast; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.concurrent.TimeUnit; -public class MainActivity extends Activity implements GestureDetector.OnGestureListener { +public final class MainActivity extends Activity implements GestureDetector.OnGestureListener { private static final String TAG = "babytime"; private static final long TOAST_RATE_LIMIT_MILLIS = 3500; // AOSP's Toast.LENGTH_LONG private static final long MAX_UNLOCK_GESTURE_TIME_MILLIS = TimeUnit.SECONDS.toMillis(5); @@ -31,15 +32,52 @@ public class MainActivity extends Activity implements GestureDetector.OnGestureL private long mLastNotificationTimeMillis; private long mUnlockGestureStartedMillis; private int mUnlockStepCount; - + BroadcastReceiver mScreenOnReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wear_baby_time); + dimScreen(); + interceptTouchEvents(); + setClockUpdates(); + setUpScreenOnReceiver(); + } + + /** + * Set the clock to update the displayed time every minute. + */ + private void setClockUpdates() { + final TextView timeView = (TextView) findViewById(R.id.time); + + mClockHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + if (!isFinishing() && timeView != null) { + TIME.setToNow(); + long millisUntilNextMinute = (60 - TIME.second) * 1000; + mClockHandler.sendEmptyMessageDelayed(0, millisUntilNextMinute); + ((TextView) timeView).setText(TIME.format(TIME_FORMAT)); + } + } + }; + + mClockHandler.sendEmptyMessage(0); + } + + /** + * Set the screen brightness to its minimum. + */ + private void dimScreen() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 0.0f; getWindow().setAttributes(lp); + } + + /** + * Set up our swipe listener, and don't let the system do its usual swipe-right-to-exit. + */ + private void interceptTouchEvents() { mGestureDetector = new GestureDetector(this, this); final ViewGroup box = (ViewGroup) findViewById(R.id.box); box.setClickable(true); @@ -54,23 +92,17 @@ public boolean onTouch(View v, MotionEvent event) { return true; } }); + } - // Update the clock every minute - final TextView timeView = (TextView) findViewById(R.id.time); - - mClockHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - if (!isFinishing() && timeView != null) { - TIME.setToNow(); - long millisUntilNextMinute = (60 - TIME.second) * 1000; - mClockHandler.sendEmptyMessageDelayed(0, millisUntilNextMinute); - ((TextView) timeView).setText(TIME.format(TIME_FORMAT)); - } - } - }; - - mClockHandler.sendEmptyMessage(0); + /** + * Set up a receiver that can restart the app if the palm-over-screen gesture is activated. + * There's no good way I've found to intercept that gesture and prevent it; all we can do is + * kick back into the app after the screen is turned on again. + */ + private void setUpScreenOnReceiver() { + IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); + mScreenOnReceiver = new ScreenReceiver(); + registerReceiver(mScreenOnReceiver, filter); } @Override @@ -137,8 +169,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve } else if (vertical && !swipeUp && mUnlockStepCount >= 2) { // downswipe after both ups mUnlockStepCount++; if (mUnlockStepCount == 4) { - Toast.makeText(this, R.string.toast_adult_verified, Toast.LENGTH_LONG).show(); - finish(); + unlock(); } } else { // out of order or horizontal swipes lock(R.string.toast_babylike_swiping); @@ -146,4 +177,14 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve return true; } + + private void unlock() { + Toast.makeText(this, R.string.toast_adult_verified, Toast.LENGTH_LONG).show(); + + if (mScreenOnReceiver != null) { + unregisterReceiver(mScreenOnReceiver); + } + + finish(); + } } diff --git a/wear/src/main/java/com/yonisamlan/wearbabytime/ScreenReceiver.java b/wear/src/main/java/com/yonisamlan/wearbabytime/ScreenReceiver.java new file mode 100644 index 0000000..a3e5112 --- /dev/null +++ b/wear/src/main/java/com/yonisamlan/wearbabytime/ScreenReceiver.java @@ -0,0 +1,15 @@ +package com.yonisamlan.wearbabytime; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public final class ScreenReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) { + context.startActivity(new Intent(context, MainActivity.class).addFlags( + Intent.FLAG_ACTIVITY_NO_ANIMATION|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)); + } + } +}