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.settings; 18 19 import android.content.Context; 20 import android.media.RingtoneManager; 21 import android.os.Bundle; 22 import android.os.Handler; 23 import android.os.Message; 24 import android.os.Vibrator; 25 import android.preference.CheckBoxPreference; 26 import android.preference.Preference; 27 import android.preference.PreferenceCategory; 28 import android.preference.PreferenceFragment; 29 import android.preference.PreferenceScreen; 30 import android.provider.Settings; 31 32 import com.android.dialer.R; 33 import com.android.phone.common.util.SettingsUtil; 34 35 import java.lang.Boolean; 36 import java.lang.CharSequence; 37 import java.lang.Object; 38 import java.lang.Override; 39 import java.lang.Runnable; 40 import java.lang.String; 41 import java.lang.Thread; 42 43 public class GeneralSettingsFragment extends PreferenceFragment 44 implements Preference.OnPreferenceChangeListener { 45 private static final String CATEGORY_SOUNDS_KEY = "dialer_general_sounds_category_key"; 46 private static final String BUTTON_RINGTONE_KEY = "button_ringtone_key"; 47 private static final String BUTTON_VIBRATE_ON_RING = "button_vibrate_on_ring"; 48 private static final String BUTTON_PLAY_DTMF_TONE = "button_play_dtmf_tone"; 49 private static final String BUTTON_RESPOND_VIA_SMS_KEY = "button_respond_via_sms_key"; 50 51 private static final int MSG_UPDATE_RINGTONE_SUMMARY = 1; 52 53 private Context mContext; 54 55 private Preference mRingtonePreference; 56 private CheckBoxPreference mVibrateWhenRinging; 57 private CheckBoxPreference mPlayDtmfTone; 58 private Preference mRespondViaSms; 59 60 private Runnable mRingtoneLookupRunnable; 61 private final Handler mRingtoneLookupComplete = new Handler() { 62 @Override 63 public void handleMessage(Message msg) { 64 switch (msg.what) { 65 case MSG_UPDATE_RINGTONE_SUMMARY: 66 mRingtonePreference.setSummary((CharSequence) msg.obj); 67 break; 68 } 69 } 70 }; 71 72 @Override 73 public void onCreate(Bundle savedInstanceState) { 74 super.onCreate(savedInstanceState); 75 76 mContext = getActivity().getApplicationContext(); 77 78 addPreferencesFromResource(R.xml.general_settings); 79 80 mRingtonePreference = findPreference(BUTTON_RINGTONE_KEY); 81 mVibrateWhenRinging = (CheckBoxPreference) findPreference(BUTTON_VIBRATE_ON_RING); 82 mPlayDtmfTone = (CheckBoxPreference) findPreference(BUTTON_PLAY_DTMF_TONE); 83 mRespondViaSms = findPreference(BUTTON_RESPOND_VIA_SMS_KEY); 84 85 PreferenceCategory soundCategory = (PreferenceCategory) findPreference(CATEGORY_SOUNDS_KEY); 86 if (mVibrateWhenRinging != null) { 87 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); 88 if (vibrator != null && vibrator.hasVibrator()) { 89 mVibrateWhenRinging.setOnPreferenceChangeListener(this); 90 } else { 91 soundCategory.removePreference(mVibrateWhenRinging); 92 mVibrateWhenRinging = null; 93 } 94 } 95 96 if (mPlayDtmfTone != null) { 97 mPlayDtmfTone.setOnPreferenceChangeListener(this); 98 mPlayDtmfTone.setChecked(Settings.System.getInt(mContext.getContentResolver(), 99 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0); 100 } 101 102 mRingtoneLookupRunnable = new Runnable() { 103 @Override 104 public void run() { 105 if (mRingtonePreference != null) { 106 SettingsUtil.updateRingtoneName( 107 mContext, 108 mRingtoneLookupComplete, 109 RingtoneManager.TYPE_RINGTONE, 110 mRingtonePreference.getKey(), 111 MSG_UPDATE_RINGTONE_SUMMARY); 112 } 113 } 114 }; 115 } 116 117 /** 118 * Supports onPreferenceChangeListener to look for preference changes. 119 * 120 * @param preference The preference to be changed 121 * @param objValue The value of the selection, NOT its localized display value. 122 */ 123 @Override 124 public boolean onPreferenceChange(Preference preference, Object objValue) { 125 if (preference == mVibrateWhenRinging) { 126 boolean doVibrate = (Boolean) objValue; 127 Settings.System.putInt(mContext.getContentResolver(), 128 Settings.System.VIBRATE_WHEN_RINGING, doVibrate ? 1 : 0); 129 } 130 return true; 131 } 132 133 /** 134 * Click listener for toggle events. 135 */ 136 @Override 137 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 138 if (preference == mPlayDtmfTone) { 139 Settings.System.putInt(mContext.getContentResolver(), 140 Settings.System.DTMF_TONE_WHEN_DIALING, mPlayDtmfTone.isChecked() ? 1 : 0); 141 } else if (preference == mRespondViaSms) { 142 // Needs to return false for the intent to launch. 143 return false; 144 } 145 return true; 146 } 147 148 @Override 149 public void onResume() { 150 super.onResume(); 151 152 if (mVibrateWhenRinging != null) { 153 mVibrateWhenRinging.setChecked(SettingsUtil.getVibrateWhenRingingSetting(mContext)); 154 } 155 156 // Lookup the ringtone name asynchronously. 157 new Thread(mRingtoneLookupRunnable).start(); 158 } 159 } 160