diff options
author | Raj Yengisetty <raj@cyngn.com> | 2015-01-21 20:15:39 +0800 |
---|---|---|
committer | Raj Yengisetty <raj@cyngn.com> | 2015-01-22 14:53:31 +0800 |
commit | 949ee701f5c54611e3c9b030f399ce81891ac66b (patch) | |
tree | 2875276b16afc25df5caf11df5ef77124788b37e | |
parent | 4656dc99b58b920c949c03324e212d3cdb5e0acb (diff) | |
download | android_packages_apps_DeskClock-949ee701f5c54611e3c9b030f399ce81891ac66b.tar.gz android_packages_apps_DeskClock-949ee701f5c54611e3c9b030f399ce81891ac66b.tar.bz2 android_packages_apps_DeskClock-949ee701f5c54611e3c9b030f399ce81891ac66b.zip |
DeskClock: Handle invalid default alarm URI
If the user sets an external file as their alarm and the file is
subsequently deleted, DeskClock will go into a crash loop trying
to reset to a default alarm uri (which was set to this external
file).
This patch will recover when this scenario is triggered, and the
default uri is invalid. DeskClock will fetch and reset default alarm
URI from the system property 'ro.config.alarm_alert'. If no such system
property exists or the file cannot be found, it will pick a random
alarm tone from MediaStore.
Change-Id: I481876e0a25fbbb7ab1be8e3d7c9f8a94d66a43c
-rw-r--r-- | src/com/android/deskclock/AlarmClockFragment.java | 9 | ||||
-rwxr-xr-x | src/com/android/deskclock/Utils.java | 35 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/com/android/deskclock/AlarmClockFragment.java b/src/com/android/deskclock/AlarmClockFragment.java index fb4009871..e4ac88c9c 100644 --- a/src/com/android/deskclock/AlarmClockFragment.java +++ b/src/com/android/deskclock/AlarmClockFragment.java @@ -1039,6 +1039,15 @@ public class AlarmClockFragment extends DeskClockFragment implements if (!Utils.isRingToneUriValid(mContext, alarm.alert)) { alarm.alert = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM); + + if (!Utils.isRingToneUriValid(mContext, alarm.alert)) { + Uri uri = Utils.getSystemDefaultAlarm(mContext); + alarm.alert = uri; + + RingtoneManager.setActualDefaultRingtoneUri( + getActivity(), RingtoneManager.TYPE_ALARM, uri); + } + asyncUpdateAlarm(alarm, false); } final ItemHolder itemHolder = (ItemHolder) tag; diff --git a/src/com/android/deskclock/Utils.java b/src/com/android/deskclock/Utils.java index 842475dbc..d7a759232 100755 --- a/src/com/android/deskclock/Utils.java +++ b/src/com/android/deskclock/Utils.java @@ -21,6 +21,7 @@ import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.TimeInterpolator; import android.app.AlarmManager; +import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -37,6 +38,7 @@ import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.SystemClock; +import android.os.SystemProperties; import android.preference.PreferenceManager; import android.provider.DocumentsContract.Document; import android.provider.MediaStore; @@ -79,6 +81,7 @@ import java.util.TimeZone; public class Utils { private final static String PARAM_LANGUAGE_CODE = "hl"; + private final static String PROP_ALARM = "ro.config.alarm_alert"; /** * Help URL query parameter key for the app version. @@ -705,4 +708,36 @@ public class Utils { return false; } + + public static Uri getSystemDefaultAlarm(Context c) { + String defaultAlarm = SystemProperties.get(PROP_ALARM, null); + if (defaultAlarm == null) { + defaultAlarm = ""; //If system prop isn't set return any alarm + } + + // The system property is a file name so we query the Data field to find the alarm + Cursor cursor = null; + try { + cursor = c.getContentResolver().query( + MediaStore.Audio.Media.INTERNAL_CONTENT_URI, + new String[] { MediaStore.Audio.Media._ID}, + MediaStore.Audio.Media.DATA + " LIKE ? AND " + + MediaStore.Audio.Media.ALBUM + " = 'alarms'", + new String[] {"%" + defaultAlarm}, + null); + + if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { + long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); + return ContentUris.withAppendedId(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, id); + } + } catch (Exception e) { + LogUtils.e("Cannot find default alarm ringtone: " + e.toString()); + } finally { + if (cursor != null) { + cursor.close(); + } + } + + return Alarm.NO_RINGTONE_URI; + } } |