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

Commit ee5220a5 authored by Austen Dicken's avatar Austen Dicken
Browse files

add increasing volume option for alarm clocks

this commit adds a checkbox preference for "increasing volume" to
alarm clocks. when checked, the alarm will start off at 10% of the
system-wide alarm volume and slowly increase to 100% over the
course of a minute.

Change-Id: Ifa5a5152c9c8a2e709e8a4c58abd086940cb3406
parent 1659f283
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -61,6 +61,9 @@
    <!-- Setting labels on Set alarm screen: Vibration on or off -->
    <string name="alarm_vibrate">Vibrate</string>

    <!-- Setting labels on Set alarm screen: Increasing volume on or off -->
    <string name="alarm_incvol">Increasing Volume</string>

    <!-- Setting labels on Set alarm screen: Repeat -->
    <string name="alarm_repeat">Repeat</string>

+3 −0
Original line number Diff line number Diff line
@@ -36,4 +36,7 @@
    <CheckBoxPreference android:key="vibrate"
        android:persistent="false"
        android:title="@string/alarm_vibrate"/>
    <CheckBoxPreference android:key="incvol"
        android:persistent="false"
        android:title="@string/alarm_incvol" />
</PreferenceScreen>
+13 −1
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ public final class Alarm implements Parcelable {
        p.writeString(label);
        p.writeParcelable(alert, flags);
        p.writeInt(silent ? 1 : 0);
        p.writeInt(incvol ? 1 : 0);
    }
    //////////////////////////////
    // end Parcelable apis
@@ -122,6 +123,12 @@ public final class Alarm implements Parcelable {
         */
        public static final String ALERT = "alert";

        /**
         * True if alarm should start off quiet and slowly increase volume
         * <P>Type: BOOLEAN</P>
         */
        public static final String INCVOL = "incvol";

        /**
         * The default sort order for this table
         */
@@ -133,7 +140,7 @@ public final class Alarm implements Parcelable {

        static final String[] ALARM_QUERY_COLUMNS = {
            _ID, HOUR, MINUTES, DAYS_OF_WEEK, ALARM_TIME,
            ENABLED, VIBRATE, MESSAGE, ALERT };
            ENABLED, VIBRATE, MESSAGE, ALERT, INCVOL };

        /**
         * These save calls to cursor.getColumnIndexOrThrow()
@@ -148,6 +155,7 @@ public final class Alarm implements Parcelable {
        public static final int ALARM_VIBRATE_INDEX = 6;
        public static final int ALARM_MESSAGE_INDEX = 7;
        public static final int ALARM_ALERT_INDEX = 8;
        public static final int ALARM_INCVOL_INDEX = 9;
    }
    //////////////////////////////
    // End column definitions
@@ -164,6 +172,7 @@ public final class Alarm implements Parcelable {
    public String     label;
    public Uri        alert;
    public boolean    silent;
    public boolean    incvol;

    public Alarm(Cursor c) {
        id = c.getInt(Columns.ALARM_ID_INDEX);
@@ -192,6 +201,7 @@ public final class Alarm implements Parcelable {
                        RingtoneManager.TYPE_ALARM);
            }
        }
        incvol = c.getInt(Columns.ALARM_INCVOL_INDEX) == 1;
    }

    public Alarm(Parcel p) {
@@ -205,6 +215,7 @@ public final class Alarm implements Parcelable {
        label = p.readString();
        alert = (Uri) p.readParcelable(null);
        silent = p.readInt() == 1;
        incvol = p.readInt() == 1;
    }

    // Creates a default alarm at the current time.
@@ -217,6 +228,7 @@ public final class Alarm implements Parcelable {
        vibrate = true;
        daysOfWeek = new DaysOfWeek(0);
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        incvol = false;
    }

    public String getLabelOrDefault(Context context) {
+6 −5
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import android.net.Uri;
class AlarmDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "alarms.db";
    private static final int DATABASE_VERSION = 5;
    private static final int DATABASE_VERSION = 6;

    public AlarmDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
@@ -48,14 +48,15 @@ class AlarmDatabaseHelper extends SQLiteOpenHelper {
                   "enabled INTEGER, " +
                   "vibrate INTEGER, " +
                   "message TEXT, " +
                   "alert TEXT);");
                   "alert TEXT, " +
                   "incvol INTEGER);");

        // insert default alarms
        String insertMe = "INSERT INTO alarms " +
                "(hour, minutes, daysofweek, alarmtime, enabled, vibrate, " +
                " message, alert) VALUES ";
        db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
        db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '');");
                " message, alert, incvol) VALUES ";
        db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '', 0);");
        db.execSQL(insertMe + "(9, 00, 96, 0, 0, 1, '', '', 0);");
    }

    @Override
+38 −0
Original line number Diff line number Diff line
@@ -45,6 +45,14 @@ public class AlarmKlaxon extends Service {

    private static final long[] sVibratePattern = new long[] { 500, 500 };

    /* 675ms delays between volume increases from 0.1f to 1.0f at
     * 0.01f intervals equates to approximately 1 minute before the
     * alarm reaches full volume
     */
    private static final long INCVOL_DELAY = 675;
    private static final float INCVOL_START = 0.1f;
    private static final float INCVOL_DELTA = 0.01f;

    private boolean mPlaying = false;
    private Vibrator mVibrator;
    private MediaPlayer mMediaPlayer;
@@ -52,9 +60,11 @@ public class AlarmKlaxon extends Service {
    private long mStartTime;
    private TelephonyManager mTelephonyManager;
    private int mInitialCallState;
    private float mCurrentIncVol = 1.0f;

    // Internal messages
    private static final int KILLER = 1000;
    private static final int INCVOL = 1001;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
@@ -65,6 +75,17 @@ public class AlarmKlaxon extends Service {
                    sendKillBroadcast((Alarm) msg.obj);
                    stopSelf();
                    break;
                case INCVOL:
                    if (mPlaying && mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                        mCurrentIncVol += INCVOL_DELTA;
                        if (mCurrentIncVol < 1.0f) {
                            mHandler.sendEmptyMessageDelayed(INCVOL, INCVOL_DELAY);
                        } else {
                            mCurrentIncVol = 1.0f;
                        }
                        mMediaPlayer.setVolume(mCurrentIncVol, mCurrentIncVol);
                    }
                    break;
            }
        }
    };
@@ -194,6 +215,11 @@ public class AlarmKlaxon extends Service {
                            R.raw.in_call_alarm);
                } else {
                    mMediaPlayer.setDataSource(this, alert);

                    if (alarm.incvol) {
                        mCurrentIncVol = INCVOL_START;
                        mMediaPlayer.setVolume(mCurrentIncVol, mCurrentIncVol);
                    }
                }
                startAlarm(mMediaPlayer);
            } catch (Exception ex) {
@@ -205,6 +231,12 @@ public class AlarmKlaxon extends Service {
                    mMediaPlayer.reset();
                    setDataSourceFromResource(getResources(), mMediaPlayer,
                            R.raw.fallbackring);

                    if (alarm.incvol) {
                        mCurrentIncVol = INCVOL_START;
                        mMediaPlayer.setVolume(mCurrentIncVol, mCurrentIncVol);
                    }

                    startAlarm(mMediaPlayer);
                } catch (Exception ex2) {
                    // At this point we just don't play anything.
@@ -223,6 +255,10 @@ public class AlarmKlaxon extends Service {
        enableKiller(alarm);
        mPlaying = true;
        mStartTime = System.currentTimeMillis();

        if (alarm.incvol) {
            mHandler.sendEmptyMessageDelayed(INCVOL, INCVOL_DELAY);
        }
    }

    // Do the common stuff when starting the alarm.
@@ -259,6 +295,8 @@ public class AlarmKlaxon extends Service {
        if (mPlaying) {
            mPlaying = false;

            mHandler.removeMessages(INCVOL);

            Intent alarmDone = new Intent(Alarms.ALARM_DONE_ACTION);
            sendBroadcast(alarmDone);

Loading