Skip to content

Commit

Permalink
Added a setting to do nothing/snooze/dismiss an alarm by flipping the…
Browse files Browse the repository at this point in the history
… device.

Change-Id: Ic42274cd8085032f5c3cc25b44c1843787316ae4
  • Loading branch information
maurodec committed Apr 17, 2012
1 parent 34a14cd commit e01d81c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 3 deletions.
24 changes: 24 additions & 0 deletions res/values/strings.xml
Expand Up @@ -184,6 +184,30 @@
<item>30</item>
</string-array>

<!-- Setting title for the flip action setting. -->
<string name="flip_action_title">Flip action</string>

<!-- Dialog title of the flip action setting. -->
<string name="flip_action_dialog_title">Flip action</string>

<!-- Setting summary for the flip action setting. -->
<string name="flip_action_summary">Flipping the phone down will
<xliff:g id="action">%s</xliff:g></string>

<!-- Entries listed in the setting for the flip action setting. -->
<string-array name="flip_action_entries">
<item>Do nothing</item>
<item>Snooze the alarm</item>
<item>Dismiss the alarm</item>
</string-array>

<!-- Values for the flip action setting. -->
<string-array name="flip_action_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>

<!-- Auto silence preference title -->
<string name="auto_silence_title">Auto-silence</string>

Expand Down
8 changes: 8 additions & 0 deletions res/xml/settings.xml
Expand Up @@ -38,6 +38,14 @@
android:defaultValue="10"
android:dialogTitle="@string/snooze_duration_title" />

<ListPreference
android:key="flip_action"
android:title="@string/flip_action_title"
android:dialogTitle="@string/flip_action_dialog_title"
android:entries="@array/flip_action_entries"
android:entryValues="@array/flip_action_values"
android:defaultValue="0" />

<ListPreference
android:key="auto_silence"
android:title="@string/auto_silence_title"
Expand Down
121 changes: 118 additions & 3 deletions src/com/android/deskclock/AlarmAlertFullScreen.java
Expand Up @@ -24,6 +24,10 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
Expand All @@ -32,7 +36,6 @@
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;
Expand All @@ -47,11 +50,14 @@ public class AlarmAlertFullScreen extends Activity {
// These defaults must match the values in res/xml/settings.xml
private static final String DEFAULT_SNOOZE = "10";
private static final String DEFAULT_VOLUME_BEHAVIOR = "2";
private static final String DEFAULT_FLIP_ACTION = "0";
protected static final String SCREEN_OFF = "screen_off";

protected Alarm mAlarm;
private int mVolumeBehavior;
boolean mFullscreenStyle;
private int mFlipAction;
SensorEventListener mOrientationListener;

// Receives the ALARM_KILLED action from the AlarmKlaxon,
// and also ALARM_SNOOZE_ACTION / ALARM_DISMISS_ACTION from other applications
Expand Down Expand Up @@ -85,6 +91,13 @@ protected void onCreate(Bundle icicle) {
DEFAULT_VOLUME_BEHAVIOR);
mVolumeBehavior = Integer.parseInt(vol);

final String flipAction =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(SettingsActivity.KEY_FLIP_ACTION,
DEFAULT_FLIP_ACTION);
Log.v("flipaction = " + flipAction);
mFlipAction = Integer.parseInt(flipAction);

final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// Turn on the screen unless we are being launched from the AlarmAlert
Expand All @@ -106,14 +119,14 @@ protected void onCreate(Bundle icicle) {

private void setTitle() {
final String titleText = mAlarm.getLabelOrDefault(this);

setTitle(titleText);
}

protected int getLayoutResId() {
return R.layout.alarm_alert_fullscreen;
}

private void updateLayout() {
LayoutInflater inflater = LayoutInflater.from(this);

Expand Down Expand Up @@ -198,6 +211,10 @@ private NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

private SensorManager getSensorManager() {
return (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}

// Dismiss the alarm.
private void dismiss(boolean killed) {
Log.i(killed ? "Alarm killed" : "Alarm dismissed by user");
Expand All @@ -212,6 +229,94 @@ private void dismiss(boolean killed) {
finish();
}

private void attachOrientationListener() {
if (mFlipAction != 0) {
mOrientationListener = new SensorEventListener() {
private static final int FACE_UP_LOWER_LIMIT = -45;
private static final int FACE_UP_UPPER_LIMIT = 45;
private static final int FACE_DOWN_UPPER_LIMIT = 135;
private static final int FACE_DOWN_LOWER_LIMIT = -135;
private static final int TILT_UPPER_LIMIT = 45;
private static final int TILT_LOWER_LIMIT = -45;
private static final int SENSOR_SAMPLES = 3;

private boolean mWasFaceUp;
private boolean[] mSamples = new boolean[SENSOR_SAMPLES];
private int mSampleIndex;

@Override
public void onAccuracyChanged(Sensor sensor, int acc) {
}

@Override
public void onSensorChanged(SensorEvent event) {
// Add a sample overwriting the oldest one. Several samples are used
// to avoid the erroneous values the sensor sometimes returns.
float y = event.values[1];
float z = event.values[2];

if (!mWasFaceUp) {
// Check if its face up enough.
mSamples[mSampleIndex] =
y > FACE_UP_LOWER_LIMIT && y < FACE_UP_UPPER_LIMIT
&& z > TILT_LOWER_LIMIT && z < TILT_UPPER_LIMIT;

// The device first needs to be face up.
boolean faceUp = true;
for (boolean sample : mSamples) {
faceUp = faceUp && sample;
}
if (faceUp) {
mWasFaceUp = true;
for (int i = 0; i < SENSOR_SAMPLES; i++)
mSamples[i] = false;
}
} else {
// Check if its face down enough. Note that wanted
// values go from FACE_DOWN_UPPER_LIMIT to 180
// and from -180 to FACE_DOWN_LOWER_LIMIT
mSamples[mSampleIndex] =
(y > FACE_DOWN_UPPER_LIMIT || y < FACE_DOWN_LOWER_LIMIT)
&& z > TILT_LOWER_LIMIT && z < TILT_UPPER_LIMIT;

boolean faceDown = true;
for (boolean sample : mSamples) {
faceDown = faceDown && sample;
}
if (faceDown) {
switch (mFlipAction) {
case 1:
snooze();
break;

case 2:
dismiss(false);
break;

default:
break;
}
}
}

mSampleIndex = ((mSampleIndex + 1) % SENSOR_SAMPLES);
}
};

// Register the sensor listener and start to get values
getSensorManager().registerListener(mOrientationListener,
getSensorManager().getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
}
}

private void detachAccelerationListener() {
if (mOrientationListener != null) {
getSensorManager().unregisterListener(mOrientationListener);
mOrientationListener = null;
}
}

/**
* this is called when a second alarm is triggered while a
* previous alert window is still active.
Expand All @@ -225,6 +330,8 @@ protected void onNewIntent(Intent intent) {
mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);

setTitle();

detachAccelerationListener();
}

@Override
Expand All @@ -235,6 +342,8 @@ protected void onResume() {
Button snooze = (Button) findViewById(R.id.snooze);
snooze.setEnabled(false);
}

attachOrientationListener();
}

@Override
Expand All @@ -245,6 +354,12 @@ public void onDestroy() {
unregisterReceiver(mReceiver);
}

@Override
public void onPause() {
super.onPause();
detachAccelerationListener();
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// Do this on key down to handle a few of the system keys.
Expand Down
18 changes: 18 additions & 0 deletions src/com/android/deskclock/SettingsActivity.java
Expand Up @@ -48,6 +48,8 @@ public class SettingsActivity extends PreferenceActivity
"default_ringtone";
static final String KEY_AUTO_SILENCE =
"auto_silence";
static final String KEY_FLIP_ACTION =
"flip_action";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -104,6 +106,10 @@ public boolean onPreferenceChange(Preference pref, Object newValue) {
final ListPreference listPref = (ListPreference) pref;
String delay = (String) newValue;
updateAutoSnoozeSummary(listPref, delay);
} else if (KEY_FLIP_ACTION.equals(pref.getKey())) {
final ListPreference listPref = (ListPreference) pref;
String action = (String) newValue;
updateFlipActionSummary(listPref, action);
}
return true;
}
Expand All @@ -118,6 +124,13 @@ private void updateAutoSnoozeSummary(ListPreference listPref,
}
}

private void updateFlipActionSummary(ListPreference listPref,
String action) {
int i = Integer.parseInt(action);
listPref.setSummary(
getString(R.string.flip_action_summary,
getResources().getStringArray(R.array.flip_action_entries)[i].toLowerCase()));
}

private void refresh() {
final CheckBoxPreference alarmInSilentModePref =
Expand All @@ -137,5 +150,10 @@ private void refresh() {
String delay = listPref.getValue();
updateAutoSnoozeSummary(listPref, delay);
listPref.setOnPreferenceChangeListener(this);

listPref = (ListPreference) findPreference(KEY_FLIP_ACTION);
String action = listPref.getValue();
updateFlipActionSummary(listPref, action);
listPref.setOnPreferenceChangeListener(this);
}
}

0 comments on commit e01d81c

Please sign in to comment.