1 package com.google.android.libraries.backup; 2 3 import android.content.Context; 4 import android.content.SharedPreferences; 5 import android.database.Cursor; 6 import android.media.Ringtone; 7 import android.media.RingtoneManager; 8 import android.net.Uri; 9 import android.support.annotation.Nullable; 10 import android.support.annotation.VisibleForTesting; 11 12 /** 13 * Utility class of static methods to help process shared preferences for backup and restore. 14 */ 15 public class PreferenceBackupUtil { 16 17 @VisibleForTesting 18 @Nullable getRingtoneTitleFromUri(Context context, @Nullable String uri)19 static String getRingtoneTitleFromUri(Context context, @Nullable String uri) { 20 if (uri == null) { 21 return null; 22 } 23 24 Ringtone sound = RingtoneManager.getRingtone(context, Uri.parse(uri)); 25 if (sound == null) { 26 return null; 27 } 28 return sound.getTitle(context); 29 } 30 31 /** 32 * Get ringtone uri from a preference key in a shared preferences file, retrieve the associated 33 * ringtone's title and, if possible, save the title to the target preference key. 34 * 35 * @param srcRingtoneUriPrefKey preference key of the ringtone uri. 36 * @param dstRingtoneTitlePrefKey preference key where the ringtone title should be put. 37 * @return whether the ringtoneTitleKey was set. 38 */ encodeRingtonePreference(Context context, String prefsName, String srcRingtoneUriPrefKey, String dstRingtoneTitlePrefKey)39 public static boolean encodeRingtonePreference(Context context, String prefsName, 40 String srcRingtoneUriPrefKey, String dstRingtoneTitlePrefKey) { 41 SharedPreferences preferences = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE); 42 43 String uri = preferences.getString(srcRingtoneUriPrefKey, null); 44 String title = getRingtoneTitleFromUri(context, uri); 45 if (title == null) { 46 return false; 47 } 48 49 preferences.edit().putString(dstRingtoneTitlePrefKey, title).apply(); 50 return true; 51 } 52 53 @VisibleForTesting 54 @Nullable getRingtoneUriFromTitle(Context context, @Nullable String title, int ringtoneType)55 static String getRingtoneUriFromTitle(Context context, @Nullable String title, int ringtoneType) { 56 // Check whether the ringtoneType is a valid combination of the 3 ringtone types. 57 if ((ringtoneType == 0) 58 || ((RingtoneManager.TYPE_ALL & ringtoneType) != ringtoneType)) { 59 throw new IllegalStateException(); 60 } 61 if (title == null) { 62 return null; 63 } 64 65 RingtoneManager manager = new RingtoneManager(context); 66 manager.setType(ringtoneType); 67 Cursor cur = manager.getCursor(); 68 for (int i = 0; i < cur.getCount(); i++) { 69 Ringtone ringtone = manager.getRingtone(i); 70 if (ringtone.getTitle(context).equals(title)) { 71 return manager.getRingtoneUri(i).toString(); 72 } 73 } 74 75 return null; 76 } 77 78 /** 79 * Get ringtone title from a preference key of a shared preferences file, find a ringtone with the 80 * same title and, if possible, save its uri to the target preference key. 81 * 82 * @param dstRingtoneUriPrefKey preference key where the ringtone uri should be put. 83 * @param srcRingtoneTitlePrefKey preference key of the ringtone title. 84 * @return whether the ringtoneUriKey was set. 85 */ decodeRingtonePreference(Context context, String prefsName, String dstRingtoneUriPrefKey, String srcRingtoneTitlePrefKey, int ringtoneType)86 public static boolean decodeRingtonePreference(Context context, String prefsName, 87 String dstRingtoneUriPrefKey, String srcRingtoneTitlePrefKey, int ringtoneType) { 88 SharedPreferences preferences = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE); 89 90 String title = preferences.getString(srcRingtoneTitlePrefKey, null); 91 String uri = getRingtoneUriFromTitle(context, title, ringtoneType); 92 if (uri == null) { 93 return false; 94 } 95 96 preferences.edit().putString(dstRingtoneUriPrefKey, uri).apply(); 97 return true; 98 } 99 }