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

Commit 09ca6d30 authored by Clark Scheff's avatar Clark Scheff Committed by Gerrit Code Review
Browse files

Alarms: Fallback to defualt when uri is invalid

This fixes an issue where the media is no longer in the media store
but since the URI uses the "content" scheme it is deemed a valid
URI even though the media is no longer available. We check if the
content is actually available and if not we will fallback to the
default ringtone.

REF: BACON-1463

Change-Id: I7b77265dfbf410681a245b3b020d838f3d1c7eba
parent 489a7342
Loading
Loading
Loading
Loading
+10 −17
Original line number Diff line number Diff line
@@ -1328,6 +1328,11 @@ public class AlarmClockFragment extends DeskClockFragment implements
                // The view was converted but somehow lost its tag.
                setNewHolder(view);
            }
            if (!Utils.isRingToneUriValid(mContext, alarm.alert)) {
                alarm.alert = RingtoneManager.getActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_ALARM);
                asyncUpdateAlarm(alarm, false);
            }
            final ItemHolder itemHolder = (ItemHolder) tag;
            itemHolder.alarm = alarm;

@@ -1578,7 +1583,7 @@ public class AlarmClockFragment extends DeskClockFragment implements
            if (Alarm.NO_RINGTONE_URI.equals(alarm.alert)) {
                ringtone = mContext.getResources().getString(R.string.silent_alarm_summary);
            } else {
                ringtitle = getRingToneTitle(alarm.alert);
                ringtitle = getRingToneTitle(alarm);
                if (ringtitle != null) {
                    ringtone = ringtitle;
                } else {
@@ -1687,17 +1692,18 @@ public class AlarmClockFragment extends DeskClockFragment implements
        /**
         * Does a read-through cache for ringtone titles.
         *
         * @param uri The uri of the ringtone.
         * @param Alarm The alarm to get the ringtone title from.
         * @return The ringtone title. {@literal null} if no matching ringtone found.
         */
        private String getRingToneTitle(Uri uri) {
        private String getRingToneTitle(Alarm alarm) {
            Uri uri = alarm.alert;
            // Try the cache first
            String title = mRingtoneTitleCache.getString(uri.toString());
            if (title == null) {
                if (uri.equals(AlarmMediaPlayer.RANDOM_URI)) {
                    title = mContext.getResources().getString(R.string.alarm_type_random);
                } else {
                    if (isRingToneUriValid(uri)) {
                    if (Utils.isRingToneUriValid(mContext, uri)) {
                        if (uri.getAuthority().equals(DOC_AUTHORITY)
                                || uri.getAuthority().equals(DOC_DOWNLOAD)) {
                            title = getDisplayNameFromDatabase(mContext,uri);
@@ -1716,19 +1722,6 @@ public class AlarmClockFragment extends DeskClockFragment implements
            return title;
        }

        private boolean isRingToneUriValid(Uri uri) {
            if (uri.getScheme().contentEquals("file")) {
                File f = new File(uri.getPath());
                if (f.exists()) {
                    return true;
                }
            } else if (uri.getScheme().contentEquals("content")) {
                return true;
            }

            return false;
        }

        private String getDisplayNameFromDatabase(Context context,Uri uri) {
            String selection = null;
            String[] selectionArgs = null;
+32 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
@@ -37,6 +38,7 @@ import android.os.Build;
import android.os.Handler;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.provider.Settings;
import android.text.Spannable;
import android.text.SpannableString;
@@ -54,12 +56,14 @@ import android.view.animation.DecelerateInterpolator;
import android.widget.TextClock;
import android.widget.TextView;

import com.android.deskclock.provider.Alarm;
import com.android.deskclock.stopwatch.Stopwatches;
import com.android.deskclock.timer.Timers;
import com.android.deskclock.worldclock.CityObj;
import com.android.deskclock.worldclock.db.DbCities;
import com.android.deskclock.worldclock.db.DbCity;

import java.io.File;
import java.text.Collator;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -608,4 +612,32 @@ public class Utils {
    public static String getCityName(CityObj city, CityObj dbCity) {
        return (city.mCityId == null || dbCity == null) ? city.mCityName : dbCity.mCityName;
    }

    public static boolean isRingToneUriValid(Context context, Uri uri) {
        if (uri.equals(AlarmMediaPlayer.RANDOM_URI) || uri.equals(Alarm.NO_RINGTONE_URI)) {
            return true;
        } else if (uri.getScheme().contentEquals("file")) {
            File f = new File(uri.getPath());
            if (f.exists()) {
                return true;
            }
        } else if (uri.getScheme().contentEquals("content")) {
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri,
                        new String[] {MediaStore.Audio.Media.TITLE}, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    return true;
                }
            } catch (Exception e) {
                Log.e("Get ringtone uri Exception: e.toString=" + e.toString());
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }

        return false;
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.os.Vibrator;
import com.android.deskclock.Log;
import com.android.deskclock.AlarmMediaPlayer;
import com.android.deskclock.R;
import com.android.deskclock.Utils;
import com.android.deskclock.provider.AlarmInstance;

import java.io.IOException;
@@ -114,11 +115,20 @@ public class AlarmKlaxon {
            Uri alarmNoise = instance.mRingtone;
            // Fall back on the default alarm if the database does not have an
            // alarm stored.
            if (alarmNoise == null) {
                // Try to get the actual default first, this will be the one set by the user
                alarmNoise = RingtoneManager.getActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_ALARM);
                // if the actual default is null, fallback to the system default.
                if (alarmNoise == null) {
                    alarmNoise = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
                }
                if (Log.LOGV) {
                    Log.v("Using default alarm: " + alarmNoise.toString());
                }
            } else if (!Utils.isRingToneUriValid(context, alarmNoise)) {
                alarmNoise = RingtoneManager.getActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_ALARM);
            }

            final Context appContext = context.getApplicationContext();