1 /*
2  * Copyright (C) 2014 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.phone.common.util;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteException;
23 import android.media.Ringtone;
24 import android.media.RingtoneManager;
25 import android.net.Uri;
26 import android.os.Handler;
27 import android.os.Vibrator;
28 import android.preference.Preference;
29 import android.preference.PreferenceManager;
30 import android.provider.MediaStore;
31 import android.provider.Settings;
32 import android.text.TextUtils;
33 
34 import com.android.phone.common.R;
35 
36 import java.lang.CharSequence;
37 import java.lang.String;
38 
39 public class SettingsUtil {
40     private static final String DEFAULT_NOTIFICATION_URI_STRING =
41             Settings.System.DEFAULT_NOTIFICATION_URI.toString();
42 
43     /**
44      * Queries for a ringtone name, and sets the name using a handler.
45      * This is a method was originally copied from com.android.settings.SoundSettings.
46      *
47      * @param context The application context.
48      * @param handler The handler, which takes the name of the ringtone as a String as a parameter.
49      * @param type The type of sound.
50      * @param key The key to the shared preferences entry being updated.
51      * @param msg An integer identifying the message sent to the handler.
52      */
updateRingtoneName( Context context, Handler handler, int type, String key, int msg)53     public static void updateRingtoneName(
54             Context context, Handler handler, int type, String key, int msg) {
55         final Uri ringtoneUri;
56         boolean defaultRingtone = false;
57         if (type == RingtoneManager.TYPE_RINGTONE) {
58             // For ringtones, we can just lookup the system default because changing the settings
59             // in Call Settings changes the system default.
60             ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
61         } else {
62             final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
63             // For voicemail notifications, we use the value saved in Phone's shared preferences.
64             String uriString = prefs.getString(key, DEFAULT_NOTIFICATION_URI_STRING);
65             if (TextUtils.isEmpty(uriString)) {
66                 // silent ringtone
67                 ringtoneUri = null;
68             } else {
69                 if (uriString.equals(DEFAULT_NOTIFICATION_URI_STRING)) {
70                     // If it turns out that the voicemail notification is set to the system
71                     // default notification, we retrieve the actual URI to prevent it from showing
72                     // up as "Unknown Ringtone".
73                     defaultRingtone = true;
74                     ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
75                 } else {
76                     ringtoneUri = Uri.parse(uriString);
77                 }
78             }
79         }
80         CharSequence summary = context.getString(R.string.ringtone_unknown);
81         // Is it a silent ringtone?
82         if (ringtoneUri == null) {
83             summary = context.getString(R.string.ringtone_silent);
84         } else {
85             // Fetch the ringtone title from the media provider
86             final Ringtone ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
87             if (ringtone != null) {
88                 try {
89                     final String title = ringtone.getTitle(context);
90                     if (!TextUtils.isEmpty(title)) {
91                         summary = title;
92                     }
93                 } catch (SQLiteException sqle) {
94                     // Unknown title for the ringtone
95                 }
96             }
97         }
98         if (defaultRingtone) {
99             summary = context.getString(R.string.default_notification_description, summary);
100         }
101         handler.sendMessage(handler.obtainMessage(msg, summary));
102     }
103 }
104