Skip to content

Commit

Permalink
Handle screen-off gesture, v1.0.2 bump
Browse files Browse the repository at this point in the history
- Restart the app when the screen comes back on if the palm-over-screen
  gesture is used
  • Loading branch information
Yoni Samlan committed Jul 6, 2014
1 parent 1be3af5 commit 57773ae
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 27 deletions.
4 changes: 2 additions & 2 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Wear Baby Time</string>
<string name="app_name">Baby Time for Android Wear</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Any other types of touches will keep the watch locked and just show a message:

<img alt="✖ BABY-LIKE TAPPING DETECTED" src="https://raw.githubusercontent.com/ysamlan/wearbabytime/master/artwork/screenshots/framed/babytime_denied_framed.png" title="Nice try, tiny human." width="218" height="218" />

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]
Expand Down
4 changes: 2 additions & 2 deletions wear/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
85 changes: 63 additions & 22 deletions wear/src/main/java/com/yonisamlan/wearbabytime/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -137,13 +169,22 @@ 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);
}

return true;
}

private void unlock() {
Toast.makeText(this, R.string.toast_adult_verified, Toast.LENGTH_LONG).show();

if (mScreenOnReceiver != null) {
unregisterReceiver(mScreenOnReceiver);
}

finish();
}
}
15 changes: 15 additions & 0 deletions wear/src/main/java/com/yonisamlan/wearbabytime/ScreenReceiver.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 57773ae

Please sign in to comment.