Home | History | Annotate | Download | only in util
      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.RingtoneManager;
     24 import android.net.Uri;
     25 import android.os.Handler;
     26 import android.os.Vibrator;
     27 import android.preference.Preference;
     28 import android.preference.PreferenceManager;
     29 import android.provider.MediaStore;
     30 import android.provider.Settings;
     31 import android.text.TextUtils;
     32 
     33 import com.android.phone.common.R;
     34 
     35 import java.lang.CharSequence;
     36 import java.lang.String;
     37 
     38 public class SettingsUtil {
     39     private static final String DEFAULT_NOTIFICATION_URI_STRING =
     40             Settings.System.DEFAULT_NOTIFICATION_URI.toString();
     41 
     42     /**
     43      * Obtain the setting for "vibrate when ringing" setting.
     44      *
     45      * Watch out: if the setting is missing in the device, this will try obtaining the old
     46      * "vibrate on ring" setting from AudioManager, and save the previous setting to the new one.
     47      */
     48     public static boolean getVibrateWhenRingingSetting(Context context) {
     49         Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
     50         if (vibrator == null || !vibrator.hasVibrator()) {
     51             return false;
     52         }
     53         return Settings.System.getInt(context.getContentResolver(),
     54                 Settings.System.VIBRATE_WHEN_RINGING, 0) != 0;
     55     }
     56 
     57     /**
     58      * Queries for a ringtone name, and sets the name using a handler.
     59      * This is a method was originally copied from com.android.settings.SoundSettings.
     60      *
     61      * @param context The application context.
     62      * @param handler The handler, which takes the name of the ringtone as a String as a parameter.
     63      * @param type The type of sound.
     64      * @param key The key to the shared preferences entry being updated.
     65      * @param msg An integer identifying the message sent to the handler.
     66      */
     67     public static void updateRingtoneName(
     68             Context context, Handler handler, int type, String key, int msg) {
     69         final Uri ringtoneUri;
     70         boolean defaultRingtone = false;
     71         if (type == RingtoneManager.TYPE_RINGTONE) {
     72             // For ringtones, we can just lookup the system default because changing the settings
     73             // in Call Settings changes the system default.
     74             ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
     75         } else {
     76             final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
     77             // For voicemail notifications, we use the value saved in Phone's shared preferences.
     78             String uriString = prefs.getString(key, DEFAULT_NOTIFICATION_URI_STRING);
     79             if (TextUtils.isEmpty(uriString)) {
     80                 // silent ringtone
     81                 ringtoneUri = null;
     82             } else {
     83                 if (uriString.equals(DEFAULT_NOTIFICATION_URI_STRING)) {
     84                     // If it turns out that the voicemail notification is set to the system
     85                     // default notification, we retrieve the actual URI to prevent it from showing
     86                     // up as "Unknown Ringtone".
     87                     defaultRingtone = true;
     88                     ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
     89                 } else {
     90                     ringtoneUri = Uri.parse(uriString);
     91                 }
     92             }
     93         }
     94         CharSequence summary = context.getString(com.android.internal.R.string.ringtone_unknown);
     95         // Is it a silent ringtone?
     96         if (ringtoneUri == null) {
     97             summary = context.getString(com.android.internal.R.string.ringtone_silent);
     98         } else {
     99             // Fetch the ringtone title from the media provider
    100             try {
    101                 Cursor cursor = context.getContentResolver().query(ringtoneUri,
    102                         new String[] { MediaStore.Audio.Media.TITLE }, null, null, null);
    103                 if (cursor != null) {
    104                     if (cursor.moveToFirst()) {
    105                         summary = cursor.getString(0);
    106                     }
    107                     cursor.close();
    108                 }
    109             } catch (SQLiteException sqle) {
    110                 // Unknown title for the ringtone
    111             }
    112         }
    113         if (defaultRingtone) {
    114             summary = context.getString(R.string.default_notification_description, summary);
    115         }
    116         handler.sendMessage(handler.obtainMessage(msg, summary));
    117     }
    118 }
    119