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

Commit 6f955004 authored by wjiang's avatar wjiang Committed by Linux Build Service Account
Browse files

Ringtone: return default ringtone if actual one doesn't exist

Improve getActualRingtoneUriBySubId() and getActualDefaultRingtoneUri()
APIs to return default system ringtone when selected one doesn't exist
in media database any more.

This API enhancement is to ensure we can roll back to default ringtone
even if clip is removed, so 3rd party applications don't need to handle
such scenario.

CRs-Fixed: 671624
Change-Id: I516107e88a53eae8aea30a455f834cfd6baeea01
parent 0518b45a
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -2196,6 +2196,14 @@ public final class Settings {
         */
        public static final String APPEND_FOR_LAST_AUDIBLE = "_last_audible";

        /**
         * Persistent store for the static default system ringtone URI.
         * This setting is initialized within media scanner and read only afterwards.
         * It provides the persistent URI of default system ringtone.
         * @hide
         */
        public static final String DEFAULT_RINGTONE = "ringtone_default";

        /**
         * Persistent store for the system-wide default ringtone URI.
         * <p>
+12 −0
Original line number Diff line number Diff line
@@ -46,9 +46,11 @@ import android.sax.ElementListener;
import android.sax.RootElement;
import android.system.ErrnoException;
import android.system.Os;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
import com.android.internal.telephony.PhoneConstants;

import java.io.BufferedReader;
import java.io.File;
@@ -1003,7 +1005,17 @@ public class MediaScanner
                    setSettingIfNotSet(Settings.System.NOTIFICATION_SOUND, tableUri, rowId);
                    mDefaultNotificationSet = true;
                } else if (ringtones) {
                    // memorize default system ringtone persistently
                    setSettingIfNotSet(Settings.System.DEFAULT_RINGTONE, tableUri, rowId);

                    setSettingIfNotSet(Settings.System.RINGTONE, tableUri, rowId);
                    if (TelephonyManager.getDefault().isMultiSimEnabled()) {
                        int phoneCount = TelephonyManager.getDefault().getPhoneCount();
                        for (int i = PhoneConstants.SUB2; i < phoneCount; i++) {
                            // Set the default setting to the given URI for multi SIMs
                            setSettingIfNotSet((Settings.System.RINGTONE + "_" + (i+1)), tableUri, rowId);
                        }
                    }
                    mDefaultRingtoneSet = true;
                } else if (alarms) {
                    setSettingIfNotSet(Settings.System.ALARM_ALERT, tableUri, rowId);
+55 −2
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
@@ -614,9 +615,27 @@ public class RingtoneManager {
        String setting = getSettingForType(type);
        if (setting == null) return null;
        final String uriString = Settings.System.getString(context.getContentResolver(), setting);
        if ((uriString == null) || (type & TYPE_RINGTONE) == 0) {
            return uriString != null ? Uri.parse(uriString) : null;
        }

        Uri ringToneUri = getStaticDefaultRingtoneUri(context);
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(Uri.parse(uriString),
                    null, null, null, null);
            if ((cursor != null) && (cursor.getCount() > 0)) {
                ringToneUri = Uri.parse(uriString);
            }
        } catch (SQLiteException ex) {
            Log.e(TAG, "ex " + ex);
        } finally {
            if (cursor != null) cursor.close();
        }

        return ringToneUri;
    }
    
    /**
     * Sets the {@link Uri} of the default sound for a given sound type.
     * 
@@ -702,6 +721,21 @@ public class RingtoneManager {
        }
    }

    /**
     * Returns the {@link Uri} for the static default ringtone.
     * Rather than returning the actual ringtone's sound {@link Uri}, this will
     * return the default system ringtone. When actual ringtone is not valid
     * in media provider, default system ringtone is the one to rollback to.
     *
     * @return The {@link Uri} of the default system ringtone.
     * @hide
     */
    public static Uri getStaticDefaultRingtoneUri(Context context) {
        final String uriString = Settings.System.getString(
                context.getContentResolver(), Settings.System.DEFAULT_RINGTONE.toString());
        return uriString != null ? Uri.parse(uriString) : null;
    }

    /**
     * Returns the subscription ID of {@link Uri}.
     *
@@ -778,8 +812,27 @@ public class RingtoneManager {
        } else {
            setting = Settings.System.RINGTONE + "_" + (subId + 1);
        }

        final String uriString = Settings.System.getString(context.getContentResolver(), setting);
        return uriString != null ? Uri.parse(uriString) : null;
        if (uriString == null) {
            return null;
        }

        Uri ringToneUri = getStaticDefaultRingtoneUri(context);
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(Uri.parse(uriString),
                    null, null, null, null);
            if ((cursor != null) && (cursor.getCount() > 0)) {
                ringToneUri = Uri.parse(uriString);
            }
        } catch (SQLiteException ex) {
            Log.e(TAG, "ex " + ex);
        } finally {
            if (cursor != null) cursor.close();
        }

        return ringToneUri;
    }

    /**