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