diff options
author | Artem Chep <artemchep@gmail.com> | 2013-01-08 10:36:33 -0500 |
---|---|---|
committer | Linux Build Service Account <lnxbuild@localhost> | 2016-08-24 08:06:57 -0600 |
commit | 59d1062e8cd7ef514feffb3925f82b29c5101f6b (patch) | |
tree | 8768e6b76ec9249b69f63d032011da36df094a44 | |
parent | 1cc3728ba7a99c66dab2662469d2f85b2d71ead2 (diff) | |
download | android_packages_apps_DeskClock-59d1062e8cd7ef514feffb3925f82b29c5101f6b.tar.gz android_packages_apps_DeskClock-59d1062e8cd7ef514feffb3925f82b29c5101f6b.tar.bz2 android_packages_apps_DeskClock-59d1062e8cd7ef514feffb3925f82b29c5101f6b.zip |
DeskClock: Add back flip and shake actions
Cherry-picked and ported from:
* http://review.cyanogenmod.org/#/c/29894/
Signed-off-by: Jorge Ruesga <jorge@ruesga.com>
Conflicts:
res/xml/settings.xml
src/com/android/deskclock/SettingsActivity.java
src/com/android/deskclock/alarms/AlarmService.java
CRs-Fixed: 1014635
Change-Id: I9be82187559ccd483b8e4da068890f532e60b8f7
-rw-r--r-- | res/xml/settings.xml | 16 | ||||
-rw-r--r-- | src/com/android/deskclock/alarms/AlarmService.java | 188 | ||||
-rw-r--r-- | src/com/android/deskclock/settings/SettingsActivity.java | 28 |
3 files changed, 232 insertions, 0 deletions
diff --git a/res/xml/settings.xml b/res/xml/settings.xml index 4baddc8de..405ab4ae9 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -48,6 +48,7 @@ <PreferenceCategory android:title="@string/alarm_settings"> + <ListPreference android:key="auto_silence" android:title="@string/auto_silence_title" @@ -79,6 +80,21 @@ android:defaultValue="0" /> <ListPreference + android:key="flip_action" + android:title="@string/flip_action_title" + android:dialogTitle="@string/flip_action_dialog_title" + android:entries="@array/action_summary_entries" + android:entryValues="@array/action_summary_values" + android:defaultValue="0" /> + + <ListPreference + android:key="shake_action" + android:title="@string/shake_action_title" + android:dialogTitle="@string/shake_action_dialog_title" + android:entries="@array/action_summary_entries" + android:entryValues="@array/action_summary_values" + android:defaultValue="0"/> + <ListPreference android:key="week_start" android:title="@string/week_start_title" android:dialogTitle="@string/week_start_title" diff --git a/src/com/android/deskclock/alarms/AlarmService.java b/src/com/android/deskclock/alarms/AlarmService.java index e9eb2265f..3f31c7f8b 100644 --- a/src/com/android/deskclock/alarms/AlarmService.java +++ b/src/com/android/deskclock/alarms/AlarmService.java @@ -18,6 +18,7 @@ */ package com.android.deskclock.alarms; +import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.ContentResolver; @@ -26,11 +27,20 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.Binder; import android.os.IBinder; +import android.content.SharedPreferences; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.preference.PreferenceManager; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import com.android.deskclock.AlarmAlertWakeLock; import com.android.deskclock.LogUtils; + +import com.android.deskclock.SettingsActivity; +import com.android.deskclock.provider.AlarmInstance; import com.android.deskclock.R; import com.android.deskclock.events.Events; import com.android.deskclock.provider.AlarmInstance; @@ -65,9 +75,30 @@ public class AlarmService extends Service { /** Private action used to stop an alarm with this service. */ public static final String STOP_ALARM_ACTION = "STOP_ALARM"; + /** Binder given to AlarmActivity */ private final IBinder mBinder = new Binder(); + // default action for flip and shake + private static final String DEFAULT_ACTION = "0"; + + // constants for no action/snooze/dismiss + private static final int ALARM_NO_ACTION = 0; + private static final int ALARM_SNOOZE = 1; + private static final int ALARM_DISMISS = 2; + + /** + * Utility method to help start alarm properly. If alarm is already firing, it + * will mark it as missed and start the new one. + * + * @param context application context + * @param instance to trigger alarm + */ + public static void startAlarm(Context context, AlarmInstance instance) { + Intent intent = AlarmInstance.createIntent(context, AlarmService.class, instance.mId); + intent.setAction(START_ALARM_ACTION); + } + /** Whether the service is currently bound to AlarmActivity */ private boolean mIsBound = false; @@ -104,6 +135,9 @@ public class AlarmService extends Service { private TelephonyManager mTelephonyManager; private int mInitialCallState; private AlarmInstance mCurrentAlarm = null; + private SensorManager mSensorManager; + private int mFlipAction; + private int mShakeAction; private PhoneStateListener mPhoneStateListener = new PhoneStateListener() { @Override @@ -138,6 +172,7 @@ public class AlarmService extends Service { mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); AlarmKlaxon.start(this, mCurrentAlarm); sendBroadcast(new Intent(ALARM_ALERT_ACTION)); + attachListeners(); } private void stopCurrentAlarm() { @@ -164,6 +199,7 @@ public class AlarmService extends Service { } mCurrentAlarm = null; + detachListeners(); AlarmAlertWakeLock.releaseCpuLock(); } @@ -209,6 +245,13 @@ public class AlarmService extends Service { filter.addAction(ALARM_DISMISS_ACTION); registerReceiver(mActionsReceiver, filter); mIsRegistered = true; + + mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + mFlipAction = Integer.parseInt(prefs.getString( + SettingsActivity.KEY_FLIP_ACTION, DEFAULT_ACTION)); + mShakeAction = Integer.parseInt(prefs.getString( + SettingsActivity.KEY_SHAKE_ACTION, DEFAULT_ACTION)); } @Override @@ -267,4 +310,149 @@ public class AlarmService extends Service { mIsRegistered = false; } } + + private final SensorEventListener mFlipListener = 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) { + handleAction(mFlipAction); + } + } + + mSampleIndex = ((mSampleIndex + 1) % SENSOR_SAMPLES); + } + }; + + private final SensorEventListener mShakeListener = new SensorEventListener() { + private static final float SENSITIVITY = 16; + private static final int BUFFER = 5; + private float[] gravity = new float[3]; + private float average = 0; + private int fill = 0; + + @Override + public void onAccuracyChanged(Sensor sensor, int acc) { + } + + public void onSensorChanged(SensorEvent event) { + final float alpha = 0.8F; + + for (int i = 0; i < 3; i++) { + gravity[i] = alpha * gravity[i] + (1 - alpha) * event.values[i]; + } + + float x = event.values[0] - gravity[0]; + float y = event.values[1] - gravity[1]; + float z = event.values[2] - gravity[2]; + + if (fill <= BUFFER) { + average += Math.abs(x) + Math.abs(y) + Math.abs(z); + fill++; + } else { + if (average / BUFFER >= SENSITIVITY) { + handleAction(mShakeAction); + } + average = 0; + fill = 0; + } + } + }; + + private void attachListeners() { + if (mFlipAction != ALARM_NO_ACTION) { + mSensorManager.registerListener(mFlipListener, + mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), + SensorManager.SENSOR_DELAY_NORMAL, + 300 * 1000); //batch every 300 milliseconds + } + + if (mShakeAction != ALARM_NO_ACTION) { + mSensorManager.registerListener(mShakeListener, + mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), + SensorManager.SENSOR_DELAY_GAME, + 50 * 1000); //batch every 50 milliseconds + } + } + + private void detachListeners() { + if (mFlipAction != ALARM_NO_ACTION) { + mSensorManager.unregisterListener(mFlipListener); + } + if (mShakeAction != ALARM_NO_ACTION) { + mSensorManager.unregisterListener(mShakeListener); + } + } + + private void handleAction(int action) { + switch (action) { + case ALARM_SNOOZE: + // Setup Snooze Action + Intent snoozeIntent = AlarmStateManager.createStateChangeIntent(this, "SNOOZE_TAG", + mCurrentAlarm, AlarmInstance.SNOOZE_STATE); + sendBroadcast(snoozeIntent); + break; + case ALARM_DISMISS: + // Setup Dismiss Action + Intent dismissIntent = AlarmStateManager.createStateChangeIntent(this, "DISMISS_TAG", + mCurrentAlarm, AlarmInstance.DISMISSED_STATE); + sendBroadcast(dismissIntent); + break; + case ALARM_NO_ACTION: + default: + break; + } + } + } diff --git a/src/com/android/deskclock/settings/SettingsActivity.java b/src/com/android/deskclock/settings/SettingsActivity.java index c5d228484..82f4fa5dd 100644 --- a/src/com/android/deskclock/settings/SettingsActivity.java +++ b/src/com/android/deskclock/settings/SettingsActivity.java @@ -20,6 +20,8 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Resources; +import android.hardware.Sensor; +import android.hardware.SensorManager; import android.media.AudioManager; import android.os.Bundle; import android.preference.ListPreference; @@ -65,8 +67,12 @@ public final class SettingsActivity extends BaseActivity { public static final String KEY_VOLUME_BUTTONS = "volume_button_setting"; public static final String KEY_WEEK_START = "week_start"; + public static final String KEY_FLIP_ACTION = "flip_action"; + public static final String KEY_SHAKE_ACTION = "shake_action"; + public static final String TIMEZONE_LOCALE = "tz_locale"; + public static final String DEFAULT_VOLUME_BEHAVIOR = "0"; public static final String VOLUME_BEHAVIOR_SNOOZE = "1"; public static final String VOLUME_BEHAVIOR_DISMISS = "2"; @@ -169,12 +175,26 @@ public final class SettingsActivity extends BaseActivity { findPreference(KEY_TIMER_RINGTONE); timerRingtonePref.setSummary(DataModel.getDataModel().getTimerRingtoneTitle()); break; + case KEY_FLIP_ACTION: + final ListPreference listPref = (ListPreference) pref; + updateActionSummary(listPref, (String) newValue, R.string.flip_action_summary); + break; + case KEY_SHAKE_ACTION: + final ListPreference listPref = (ListPreference) pref; + updateActionSummary(listPref, (String) newValue, R.string.shake_action_summary); + break; } // Set result so DeskClock knows to refresh itself getActivity().setResult(RESULT_OK); return true; } + private void updateActionSummary(ListPreference listPref, String action, int summaryResId) { + int i = Integer.parseInt(action); + listPref.setSummary(getString(summaryResId, + getResources().getStringArray(R.array.action_summary_entries)[i])); + } + @Override public boolean onPreferenceClick(Preference pref) { final Activity activity = getActivity(); @@ -302,6 +322,14 @@ public final class SettingsActivity extends BaseActivity { (RingtonePreference) findPreference(KEY_TIMER_RINGTONE); timerRingtonePref.setSummary(DataModel.getDataModel().getTimerRingtoneTitle()); timerRingtonePref.setOnPreferenceChangeListener(this); + + listPref = (ListPreference) findPreference(KEY_FLIP_ACTION); + updateActionSummary(listPref, listPref.getValue(), R.string.flip_action_summary); + listPref.setOnPreferenceChangeListener(this); + + listPref = (ListPreference) findPreference(KEY_SHAKE_ACTION); + updateActionSummary(listPref, listPref.getValue(), R.string.shake_action_summary); + listPref.setOnPreferenceChangeListener(this); } private void updateAutoSnoozeSummary(ListPreference listPref, String delay) { |