1 /*
2  * Copyright (C) 2017 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.settings.notification;
18 
19 import static com.android.settings.notification.SettingPref.TYPE_GLOBAL;
20 
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.provider.Settings.Global;
24 import android.telephony.TelephonyManager;
25 
26 import com.android.settings.R;
27 import com.android.settings.SettingsPreferenceFragment;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 public class EmergencyTonePreferenceController extends SettingPrefController {
31 
32     private static final String KEY_EMERGENCY_TONE = "emergency_tone";
33     private static final int EMERGENCY_TONE_SILENT = 0;
34     private static final int EMERGENCY_TONE_ALERT = 1;
35     private static final int EMERGENCY_TONE_VIBRATE = 2;
36     private static final int DEFAULT_EMERGENCY_TONE = EMERGENCY_TONE_SILENT;
37 
EmergencyTonePreferenceController(Context context, SettingsPreferenceFragment parent, Lifecycle lifecycle)38     public EmergencyTonePreferenceController(Context context, SettingsPreferenceFragment parent,
39             Lifecycle lifecycle) {
40         super(context, parent, lifecycle);
41         mPreference = new SettingPref(
42             TYPE_GLOBAL, KEY_EMERGENCY_TONE, Global.EMERGENCY_TONE, DEFAULT_EMERGENCY_TONE,
43             EMERGENCY_TONE_ALERT, EMERGENCY_TONE_VIBRATE, EMERGENCY_TONE_SILENT) {
44 
45             @Override
46             public boolean isApplicable(Context context) {
47                 final TelephonyManager telephony =
48                     (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
49                 return telephony != null
50                     && telephony.getCurrentPhoneType() == TelephonyManager.PHONE_TYPE_CDMA;
51             }
52 
53             @Override
54             protected String getCaption(Resources res, int value) {
55                 switch(value) {
56                     case EMERGENCY_TONE_SILENT:
57                         return res.getString(R.string.emergency_tone_silent);
58                     case EMERGENCY_TONE_ALERT:
59                         return res.getString(R.string.emergency_tone_alert);
60                     case EMERGENCY_TONE_VIBRATE:
61                         return res.getString(R.string.emergency_tone_vibrate);
62                     default:
63                         throw new IllegalArgumentException();
64                 }
65             }
66         };
67     }
68 
69 }
70