Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e01d81ca authored by Mauro de Carvalho's avatar Mauro de Carvalho
Browse files

Added a setting to do nothing/snooze/dismiss an alarm by flipping the device.

Change-Id: Ic42274cd8085032f5c3cc25b44c1843787316ae4
parent 34a14cdd
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -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>

+8 −0
Original line number Diff line number Diff line
@@ -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"
+118 −3
Original line number Diff line number Diff line
@@ -24,6 +24,10 @@ import android.content.BroadcastReceiver;
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;
@@ -32,7 +36,6 @@ import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;
@@ -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
@@ -85,6 +91,13 @@ public class AlarmAlertFullScreen extends Activity {
                        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
@@ -198,6 +211,10 @@ public class AlarmAlertFullScreen extends Activity {
        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");
@@ -212,6 +229,94 @@ public class AlarmAlertFullScreen extends Activity {
        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.
@@ -225,6 +330,8 @@ public class AlarmAlertFullScreen extends Activity {
        mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);

        setTitle();

        detachAccelerationListener();
    }

    @Override
@@ -235,6 +342,8 @@ public class AlarmAlertFullScreen extends Activity {
            Button snooze = (Button) findViewById(R.id.snooze);
            snooze.setEnabled(false);
        }

        attachOrientationListener();
    }

    @Override
@@ -245,6 +354,12 @@ public class AlarmAlertFullScreen extends Activity {
        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.
+18 −0
Original line number Diff line number Diff line
@@ -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) {
@@ -104,6 +106,10 @@ public class SettingsActivity extends PreferenceActivity
            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;
    }
@@ -118,6 +124,13 @@ public class SettingsActivity extends PreferenceActivity
        }
    }

    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 =
@@ -137,5 +150,10 @@ public class SettingsActivity extends PreferenceActivity
        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);
    }
}