• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.accessibility;
18 
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.net.Uri;
22 import android.os.Handler;
23 import android.os.Looper;
24 import android.os.Vibrator;
25 import android.provider.Settings;
26 import android.support.v7.preference.Preference;
27 import android.support.v7.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnStart;
33 import com.android.settingslib.core.lifecycle.events.OnStop;
34 
35 public abstract class VibrationIntensityPreferenceController extends BasePreferenceController
36         implements LifecycleObserver, OnStart, OnStop {
37 
38     protected final Vibrator mVibrator;
39     private final SettingObserver mSettingsContentObserver;
40     private final String mSettingKey;
41 
42     private Preference mPreference;
43 
VibrationIntensityPreferenceController(Context context, String prefkey, String settingKey)44     public VibrationIntensityPreferenceController(Context context, String prefkey,
45             String settingKey) {
46         super(context, prefkey);
47         mVibrator = mContext.getSystemService(Vibrator.class);
48         mSettingKey = settingKey;
49         mSettingsContentObserver = new SettingObserver(settingKey) {
50             @Override
51             public void onChange(boolean selfChange, Uri uri) {
52                 updateState(mPreference);
53             }
54         };
55     }
56 
57     @Override
onStart()58     public void onStart() {
59         mContext.getContentResolver().registerContentObserver(
60                 mSettingsContentObserver.uri,
61                 false /* notifyForDescendants */,
62                 mSettingsContentObserver);
63     }
64 
65     @Override
onStop()66     public void onStop() {
67         mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
68     }
69 
70     @Override
displayPreference(PreferenceScreen screen)71     public void displayPreference(PreferenceScreen screen) {
72         super.displayPreference(screen);
73         mPreference = screen.findPreference(getPreferenceKey());
74     }
75 
76     @Override
getSummary()77     public CharSequence getSummary() {
78         final int intensity = Settings.System.getInt(mContext.getContentResolver(),
79                 mSettingKey, getDefaultIntensity());
80         return getIntensityString(mContext, intensity);
81    }
82 
getIntensityString(Context context, int intensity)83     public static CharSequence getIntensityString(Context context, int intensity) {
84         final boolean supportsMultipleIntensities = context.getResources().getBoolean(
85                 R.bool.config_vibration_supports_multiple_intensities);
86         if (supportsMultipleIntensities) {
87             switch (intensity) {
88                 case Vibrator.VIBRATION_INTENSITY_OFF:
89                     return context.getString(R.string.accessibility_vibration_intensity_off);
90                 case Vibrator.VIBRATION_INTENSITY_LOW:
91                     return context.getString(R.string.accessibility_vibration_intensity_low);
92                 case Vibrator.VIBRATION_INTENSITY_MEDIUM:
93                     return context.getString(R.string.accessibility_vibration_intensity_medium);
94                 case Vibrator.VIBRATION_INTENSITY_HIGH:
95                     return context.getString(R.string.accessibility_vibration_intensity_high);
96                 default:
97                     return "";
98             }
99         } else {
100             if (intensity == Vibrator.VIBRATION_INTENSITY_OFF) {
101                 return context.getString(R.string.switch_off_text);
102             } else {
103                 return context.getString(R.string.switch_on_text);
104             }
105         }
106     }
107 
getDefaultIntensity()108     protected abstract int getDefaultIntensity();
109 
110     private static class SettingObserver extends ContentObserver {
111 
112         public final Uri uri;
113 
SettingObserver(String settingKey)114         public SettingObserver(String settingKey) {
115             super(new Handler(Looper.getMainLooper()));
116             uri = Settings.System.getUriFor(settingKey);
117         }
118     }
119 }
120