summaryrefslogtreecommitdiffstats
path: root/src/com/google/android/libraries/backup/PreferenceBackupUtil.java
blob: 6cc40123e10fd1c9d342ec5b7da3deb1f7b926e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.google.android.libraries.backup;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

/**
 * Utility class of static methods to help process shared preferences for backup and restore.
 */
public class PreferenceBackupUtil {

  @VisibleForTesting
  @Nullable
  static String getRingtoneTitleFromUri(Context context, @Nullable String uri) {
    if (uri == null) {
      return null;
    }

    Ringtone sound = RingtoneManager.getRingtone(context, Uri.parse(uri));
    if (sound == null) {
      return null;
    }
    return sound.getTitle(context);
  }

  /**
   * Get ringtone uri from a preference key in a shared preferences file, retrieve the associated
   * ringtone's title and, if possible, save the title to the target preference key.
   *
   * @param srcRingtoneUriPrefKey preference key of the ringtone uri.
   * @param dstRingtoneTitlePrefKey preference key where the ringtone title should be put.
   * @return whether the ringtoneTitleKey was set.
   */
  public static boolean encodeRingtonePreference(Context context, String prefsName,
      String srcRingtoneUriPrefKey, String dstRingtoneTitlePrefKey) {
    SharedPreferences preferences = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);

    String uri = preferences.getString(srcRingtoneUriPrefKey, null);
    String title = getRingtoneTitleFromUri(context, uri);
    if (title == null) {
      return false;
    }

    preferences.edit().putString(dstRingtoneTitlePrefKey, title).apply();
    return true;
  }

  @VisibleForTesting
  @Nullable
  static String getRingtoneUriFromTitle(Context context, @Nullable String title, int ringtoneType) {
    // Check whether the ringtoneType is a valid combination of the 3 ringtone types.
    if ((ringtoneType == 0)
        || ((RingtoneManager.TYPE_ALL & ringtoneType) != ringtoneType)) {
      throw new IllegalStateException();
    }
    if (title == null) {
      return null;
    }

    RingtoneManager manager = new RingtoneManager(context);
    manager.setType(ringtoneType);
    Cursor cur = manager.getCursor();
    for (int i = 0; i < cur.getCount(); i++) {
      Ringtone ringtone = manager.getRingtone(i);
      if (ringtone.getTitle(context).equals(title)) {
        return manager.getRingtoneUri(i).toString();
      }
    }

    return null;
  }

  /**
   * Get ringtone title from a preference key of a shared preferences file, find a ringtone with the
   * same title and, if possible, save its uri to the target preference key.
   *
   * @param dstRingtoneUriPrefKey preference key where the ringtone uri should be put.
   * @param srcRingtoneTitlePrefKey preference key of the ringtone title.
   * @return whether the ringtoneUriKey was set.
   */
  public static boolean decodeRingtonePreference(Context context, String prefsName,
      String dstRingtoneUriPrefKey, String srcRingtoneTitlePrefKey, int ringtoneType) {
    SharedPreferences preferences = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);

    String title = preferences.getString(srcRingtoneTitlePrefKey, null);
    String uri = getRingtoneUriFromTitle(context, title, ringtoneType);
    if (uri == null) {
      return false;
    }

    preferences.edit().putString(dstRingtoneUriPrefKey, uri).apply();
    return true;
  }
}