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

Commit de1c10e2 authored by Artem Chep's avatar Artem Chep Committed by Jorge Ruesga
Browse files

DeskClock: Add back flip and shake actions

Cherry-picked and ported from:
 * http://review.cyanogenmod.org/#/c/29894/



Change-Id: I222b6cd53a95db6f8662e7aef52aa6922d8a5d6a
Signed-off-by: default avatarJorge Ruesga <jorge@ruesga.com>
parent 2a4d4537
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2012-2013 The CyanogenMod Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources>

    <!-- Summary texts for shake and flip actions. Must be ordered by numerical
         value (do nothing = 0, snooze = 1, dismiss = 2) -->
    <string-array name="action_summary_entries" translatable="false">
      <item>@string/action_summary_do_nothing</item>
      <item>@string/action_summary_snooze</item>
      <item>@string/action_summary_dismiss</item>
    </string-array>

    <!-- Values for for shake and flip actions -->
    <string-array name="action_summary_values" translatable="false">
      <item>0</item>
      <item>1</item>
      <item>2</item>
    </string-array>

</resources>
+27 −0
Original line number Diff line number Diff line
@@ -47,4 +47,31 @@
    <string name="cities_delete_city_msg">Do you want to delete <xliff:g id="city">%s</xliff:g>?</string>
    <string name="cities_delete_city_failed">Can\'t delete the city</string>

    <!-- 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" product="tablet">Flip the tablet to\u2026</string>
    <string name="flip_action_dialog_title" product="default">Flip the phone to\u2026</string>

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

    <!-- Setting title for the shake action setting. -->
    <string name="shake_action_title">Shake action</string>

    <!-- Dialog title for the shake action setting. -->
    <string name="shake_action_dialog_title" product="tablet">Shake the tablet to\u2026</string>
    <string name="shake_action_dialog_title" product="default">Shake the phone to\u2026</string>

    <!-- Setting summary for the shake action setting. -->
    <string name="shake_action_summary" product="tablet">Shaking the tablet will <xliff:g id="action">%s</xliff:g></string>
    <string name="shake_action_summary" product="default">Shaking the phone will <xliff:g id="action">%s</xliff:g></string>

    <!-- Summary texts for shake and flip actions -->
    <string name="action_summary_do_nothing">do nothing</string>
    <string name="action_summary_snooze">snooze alarm</string>
    <string name="action_summary_dismiss">dismiss alarm</string>

</resources>
+17 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@

    <PreferenceCategory
        android:title="@string/alarm_settings">

        <ListPreference
            android:key="auto_silence"
            android:title="@string/auto_silence_title"
@@ -71,5 +72,21 @@
            android:title="@string/show_status_bar_icon_title"
            android:summary="@string/show_status_bar_icon_summary"
            android:defaultValue="true"  />

       <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"/>
    </PreferenceCategory>
</PreferenceScreen>
+23 −0
Original line number Diff line number Diff line
@@ -56,6 +56,10 @@ public class SettingsActivity extends PreferenceActivity
            "snooze_duration";
    public static final String KEY_VOLUME_BEHAVIOR =
            "volume_button_setting";
    public static final String KEY_FLIP_ACTION =
            "flip_action";
    public static final String KEY_SHAKE_ACTION =
            "shake_action";
    public static final String KEY_AUTO_SILENCE =
            "auto_silence";
    public static final String KEY_CLOCK_STYLE =
@@ -176,6 +180,12 @@ public class SettingsActivity extends PreferenceActivity
            final ListPreference listPref = (ListPreference) pref;
            final int idx = listPref.findIndexOfValue((String) newValue);
            listPref.setSummary(listPref.getEntries()[idx]);
        } else if (KEY_FLIP_ACTION.equals(pref.getKey())) {
            final ListPreference listPref = (ListPreference) pref;
            updateActionSummary(listPref, (String) newValue, R.string.flip_action_summary);
        } else if (KEY_SHAKE_ACTION.equals(pref.getKey())) {
            final ListPreference listPref = (ListPreference) pref;
            updateActionSummary(listPref, (String) newValue, R.string.shake_action_summary);
        } else if (KEY_SHOW_STATUS_BAR_ICON.equals(pref.getKey())) {
            // Check if any alarms are active. If yes and
            // we allow showing the alarm icon, the icon will be shown.
@@ -205,6 +215,11 @@ public class SettingsActivity extends PreferenceActivity
        sendBroadcast(i);
    }

    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]));
    }

    private void refresh() {
        ListPreference listPref = (ListPreference) findPreference(KEY_AUTO_SILENCE);
@@ -228,6 +243,14 @@ public class SettingsActivity extends PreferenceActivity
        listPref.setSummary(listPref.getEntry());
        listPref.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);

        CheckBoxPreference hideStatusbarIcon = (CheckBoxPreference) findPreference(KEY_SHOW_STATUS_BAR_ICON);
        hideStatusbarIcon.setOnPreferenceChangeListener(this);

+165 −0
Original line number Diff line number Diff line
@@ -21,8 +21,13 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
@@ -54,6 +59,14 @@ public class AlarmActivity extends Activity {
    // can dismiss the alarm (after ALARM_ALERT_ACTION and before ALARM_DONE_ACTION).
    public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";

    // 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;

    // Controller for GlowPadView.
    private class GlowPadController extends Handler implements GlowPadView.OnTriggerListener {
        private static final int PING_MESSAGE_WHAT = 101;
@@ -111,7 +124,109 @@ public class AlarmActivity extends Activity {
        }
    }

    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 AlarmInstance mInstance;
    private SensorManager mSensorManager;
    private int mFlipAction;
    private int mShakeAction;
    private int mVolumeBehavior;
    private GlowPadView mGlowPadView;
    private GlowPadController glowPadController = new GlowPadController();
@@ -155,6 +270,8 @@ public class AlarmActivity extends Activity {
            return;
        }

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        // Get the volume/camera button behavior setting
        final String vol =
                PreferenceManager.getDefaultSharedPreferences(this)
@@ -185,6 +302,13 @@ public class AlarmActivity extends Activity {
        filter.addAction(ALARM_SNOOZE_ACTION);
        filter.addAction(ALARM_DISMISS_ACTION);
        registerReceiver(mReceiver, filter);

        final 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));

    }


@@ -224,12 +348,14 @@ public class AlarmActivity extends Activity {
    protected void onResume() {
        super.onResume();
        glowPadController.startPinger();
        attachListeners();
    }

    @Override
    protected void onPause() {
        super.onPause();
        glowPadController.stopPinger();
        detachListeners();
    }

    @Override
@@ -275,4 +401,43 @@ public class AlarmActivity extends Activity {
        }
        return super.dispatchKeyEvent(event);
    }

    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:
                snooze();
                break;
            case ALARM_DISMISS:
                dismiss();
                break;
            case ALARM_NO_ACTION:
            default:
                break;
        }
    }
}