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