• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.deskclock.data;
18 
19 import android.content.SharedPreferences;
20 import android.net.Uri;
21 
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 
28 /**
29  * This class encapsulates the transfer of data between {@link CustomRingtone} domain objects and
30  * their permanent storage in {@link SharedPreferences}.
31  */
32 final class CustomRingtoneDAO {
33 
34     /** Key to a preference that stores the set of all custom ringtone ids. */
35     private static final String RINGTONE_IDS = "ringtone_ids";
36 
37     /** Key to a preference that stores the next unused ringtone id. */
38     private static final String NEXT_RINGTONE_ID = "next_ringtone_id";
39 
40     /** Prefix for a key to a preference that stores the URI associated with the ringtone id. */
41     private static final String RINGTONE_URI = "ringtone_uri_";
42 
43     /** Prefix for a key to a preference that stores the title associated with the ringtone id. */
44     private static final String RINGTONE_TITLE = "ringtone_title_";
45 
CustomRingtoneDAO()46     private CustomRingtoneDAO() {}
47 
48     /**
49      * @param uri points to an audio file located on the file system
50      * @param title the title of the audio content at the given {@code uri}
51      * @return the newly added custom ringtone
52      */
addCustomRingtone(SharedPreferences prefs, Uri uri, String title)53     static CustomRingtone addCustomRingtone(SharedPreferences prefs, Uri uri, String title) {
54         final long id = prefs.getLong(NEXT_RINGTONE_ID, 0);
55         final Set<String> ids = getRingtoneIds(prefs);
56         ids.add(String.valueOf(id));
57 
58         prefs.edit()
59                 .putString(RINGTONE_URI + id, uri.toString())
60                 .putString(RINGTONE_TITLE + id, title)
61                 .putLong(NEXT_RINGTONE_ID, id + 1)
62                 .putStringSet(RINGTONE_IDS, ids)
63                 .apply();
64 
65         return new CustomRingtone(id, uri, title, true);
66     }
67 
68     /**
69      * @param id identifies the ringtone to be removed
70      */
removeCustomRingtone(SharedPreferences prefs, long id)71     static void removeCustomRingtone(SharedPreferences prefs, long id) {
72         final Set<String> ids = getRingtoneIds(prefs);
73         ids.remove(String.valueOf(id));
74 
75         final SharedPreferences.Editor editor = prefs.edit();
76         editor.remove(RINGTONE_URI + id);
77         editor.remove(RINGTONE_TITLE + id);
78         if (ids.isEmpty()) {
79             editor.remove(RINGTONE_IDS);
80             editor.remove(NEXT_RINGTONE_ID);
81         } else {
82             editor.putStringSet(RINGTONE_IDS, ids);
83         }
84         editor.apply();
85     }
86 
87     /**
88      * @return a list of all known custom ringtones
89      */
getCustomRingtones(SharedPreferences prefs)90     static List<CustomRingtone> getCustomRingtones(SharedPreferences prefs) {
91         final Set<String> ids = prefs.getStringSet(RINGTONE_IDS, Collections.<String>emptySet());
92         final List<CustomRingtone> ringtones = new ArrayList<>(ids.size());
93 
94         for (String id : ids) {
95             final long idLong = Long.parseLong(id);
96             final Uri uri = Uri.parse(prefs.getString(RINGTONE_URI + id, null));
97             final String title = prefs.getString(RINGTONE_TITLE + id, null);
98             ringtones.add(new CustomRingtone(idLong, uri, title, true));
99         }
100 
101         return ringtones;
102     }
103 
getRingtoneIds(SharedPreferences prefs)104     private static Set<String> getRingtoneIds(SharedPreferences prefs) {
105         return new HashSet<>(prefs.getStringSet(RINGTONE_IDS, Collections.<String>emptySet()));
106     }
107 }