Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
  • Loading branch information...
commit 2de3674bbf727b726c69fe4c4bb9e7483f8aa513 1 parent 7413d1c
@cvpcs cvpcs authored kecinzer committed
View
3  res/values/strings.xml
@@ -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>
View
3  res/xml/alarm_prefs.xml
@@ -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>
View
14 src/com/android/deskclock/Alarm.java
@@ -58,6 +58,7 @@ public void writeToParcel(Parcel p, int flags) {
p.writeString(label);
p.writeParcelable(alert, flags);
p.writeInt(silent ? 1 : 0);
+ p.writeInt(incvol ? 1 : 0);
}
//////////////////////////////
// end Parcelable apis
@@ -123,6 +124,12 @@ public void writeToParcel(Parcel p, int flags) {
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
*/
public static final String DEFAULT_SORT_ORDER =
@@ -133,7 +140,7 @@ public void writeToParcel(Parcel p, int flags) {
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 void writeToParcel(Parcel p, int flags) {
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 void writeToParcel(Parcel p, int flags) {
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 Alarm(Cursor c) {
RingtoneManager.TYPE_ALARM);
}
}
+ incvol = c.getInt(Columns.ALARM_INCVOL_INDEX) == 1;
}
public Alarm(Parcel p) {
@@ -205,6 +215,7 @@ public Alarm(Parcel p) {
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 Alarm() {
vibrate = true;
daysOfWeek = new DaysOfWeek(0);
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
+ incvol = false;
}
public String getLabelOrDefault(Context context) {
View
11 src/com/android/deskclock/AlarmDatabaseHelper.java
@@ -31,7 +31,7 @@
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 @@ public void onCreate(SQLiteDatabase db) {
"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
View
38 src/com/android/deskclock/AlarmKlaxon.java
@@ -45,6 +45,14 @@
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 @@
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 void handleMessage(Message msg) {
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 boolean onError(MediaPlayer mp, int what, int extra) {
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 boolean onError(MediaPlayer mp, int what, int extra) {
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 boolean onError(MediaPlayer mp, int what, int extra) {
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 void stop() {
if (mPlaying) {
mPlaying = false;
+ mHandler.removeMessages(INCVOL);
+
Intent alarmDone = new Intent(Alarms.ALARM_DONE_ACTION);
sendBroadcast(alarmDone);
View
2  src/com/android/deskclock/Alarms.java
@@ -166,6 +166,8 @@ private static ContentValues createContentValues(Alarm alarm) {
values.put(Alarm.Columns.ALERT, alarm.alert == null ? ALARM_ALERT_SILENT
: alarm.alert.toString());
+ values.put(Alarm.Columns.INCVOL, alarm.incvol);
+
return values;
}
View
1  src/com/android/deskclock/HandleSetAlarm.java
@@ -89,6 +89,7 @@ protected void onCreate(Bundle icicle) {
values.put(Alarm.Columns.VIBRATE, 1);
values.put(Alarm.Columns.DAYS_OF_WEEK, 0);
values.put(Alarm.Columns.ALARM_TIME, timeInMillis);
+ values.put(Alarm.Columns.INCVOL, 0);
ContentResolver cr = getContentResolver();
Uri result = cr.insert(Alarm.Columns.CONTENT_URI, values);
View
5 src/com/android/deskclock/SetAlarm.java
@@ -57,6 +57,7 @@
private Preference mTimePref;
private AlarmPreference mAlarmPref;
private CheckBoxPreference mVibratePref;
+ private CheckBoxPreference mIncVolPref;
private RepeatPreference mRepeatPref;
private int mId;
@@ -93,6 +94,8 @@ protected void onCreate(Bundle icicle) {
if (!v.hasVibrator()) {
getPreferenceScreen().removePreference(mVibratePref);
}
+ mIncVolPref = (CheckBoxPreference) findPreference("incvol");
+ mIncVolPref.setOnPreferenceChangeListener(this);
mRepeatPref = (RepeatPreference) findPreference("setRepeat");
mRepeatPref.setOnPreferenceChangeListener(this);
@@ -248,6 +251,7 @@ private void updatePrefs(Alarm alarm) {
mMinute = alarm.minutes;
mRepeatPref.setDaysOfWeek(alarm.daysOfWeek);
mVibratePref.setChecked(alarm.vibrate);
+ mIncVolPref.setChecked(alarm.incvol);
// Give the alert uri to the preference.
mAlarmPref.setAlert(alarm.alert);
updateTime();
@@ -333,6 +337,7 @@ private Alarm buildAlarmFromUi() {
alarm.vibrate = mVibratePref.isChecked();
alarm.label = mLabel.getText().toString();
alarm.alert = mAlarmPref.getAlert();
+ alarm.incvol = mIncVolPref.isChecked();
return alarm;
}
Please sign in to comment.
Something went wrong with that request. Please try again.