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

Commit 949ee701 authored by Raj Yengisetty's avatar Raj Yengisetty
Browse files

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
parent 4656dc99
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -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;
+35 −0
Original line number Diff line number Diff line
@@ -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;
    }
}