• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 android.app.NotificationManager;
20 import android.content.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.media.AudioManager;
26 import android.os.Handler;
27 import android.os.Looper;
28 import android.os.Message;
29 import android.os.Vibrator;
30 import android.text.TextUtils;
31 
32 import androidx.lifecycle.OnLifecycleEvent;
33 
34 import com.android.settings.R;
35 import com.android.settings.Utils;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 
38 import java.util.Objects;
39 
40 public class RingVolumePreferenceController extends VolumeSeekBarPreferenceController {
41 
42     private static final String TAG = "RingVolumeController";
43     private static final String KEY_RING_VOLUME = "ring_volume";
44 
45     private Vibrator mVibrator;
46     private int mRingerMode = -1;
47     private ComponentName mSuppressor;
48     private final RingReceiver mReceiver = new RingReceiver();
49     private final H mHandler = new H();
50 
51     private int mMuteIcon;
52 
RingVolumePreferenceController(Context context)53     public RingVolumePreferenceController(Context context) {
54         this(context, KEY_RING_VOLUME);
55     }
56 
RingVolumePreferenceController(Context context, String key)57     public RingVolumePreferenceController(Context context, String key) {
58         super(context, key);
59         mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
60         if (mVibrator != null && !mVibrator.hasVibrator()) {
61             mVibrator = null;
62         }
63         updateRingerMode();
64     }
65 
66     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
67     @Override
onResume()68     public void onResume() {
69         super.onResume();
70         mReceiver.register(true);
71         updateEffectsSuppressor();
72         updatePreferenceIcon();
73     }
74 
75     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
76     @Override
onPause()77     public void onPause() {
78         super.onPause();
79         mReceiver.register(false);
80     }
81 
82     @Override
getPreferenceKey()83     public String getPreferenceKey() {
84         return KEY_RING_VOLUME;
85     }
86 
87     @Override
getAvailabilityStatus()88     public int getAvailabilityStatus() {
89         return Utils.isVoiceCapable(mContext) && !mHelper.isSingleVolume()
90                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
91     }
92 
93     @Override
isSliceable()94     public boolean isSliceable() {
95         return TextUtils.equals(getPreferenceKey(), KEY_RING_VOLUME);
96     }
97 
98     @Override
useDynamicSliceSummary()99     public boolean useDynamicSliceSummary() {
100         return true;
101     }
102 
103     @Override
getAudioStream()104     public int getAudioStream() {
105         return AudioManager.STREAM_RING;
106     }
107 
108     @Override
getMuteIcon()109     public int getMuteIcon() {
110         return mMuteIcon;
111     }
112 
updateRingerMode()113     private void updateRingerMode() {
114         final int ringerMode = mHelper.getRingerModeInternal();
115         if (mRingerMode == ringerMode) return;
116         mRingerMode = ringerMode;
117         updatePreferenceIcon();
118     }
119 
updateEffectsSuppressor()120     private void updateEffectsSuppressor() {
121         final ComponentName suppressor = NotificationManager.from(mContext).getEffectsSuppressor();
122         if (Objects.equals(suppressor, mSuppressor)) return;
123         mSuppressor = suppressor;
124         if (mPreference != null) {
125             final String text = SuppressorHelper.getSuppressionText(mContext, suppressor);
126             mPreference.setSuppressionText(text);
127         }
128         updatePreferenceIcon();
129     }
130 
updatePreferenceIcon()131     private void updatePreferenceIcon() {
132         if (mPreference != null) {
133             if (mRingerMode == AudioManager.RINGER_MODE_VIBRATE) {
134                 mMuteIcon = R.drawable.ic_volume_ringer_vibrate;
135                 mPreference.showIcon(R.drawable.ic_volume_ringer_vibrate);
136             } else if (mRingerMode == AudioManager.RINGER_MODE_SILENT) {
137                 mMuteIcon = R.drawable.ic_notifications_off_24dp;
138                 mPreference.showIcon(R.drawable.ic_notifications_off_24dp);
139             } else {
140                 mPreference.showIcon(R.drawable.ic_notifications);
141             }
142         }
143     }
144 
145     private final class H extends Handler {
146         private static final int UPDATE_EFFECTS_SUPPRESSOR = 1;
147         private static final int UPDATE_RINGER_MODE = 2;
148 
H()149         private H() {
150             super(Looper.getMainLooper());
151         }
152 
153         @Override
handleMessage(Message msg)154         public void handleMessage(Message msg) {
155             switch (msg.what) {
156                 case UPDATE_EFFECTS_SUPPRESSOR:
157                     updateEffectsSuppressor();
158                     break;
159                 case UPDATE_RINGER_MODE:
160                     updateRingerMode();
161                     break;
162             }
163         }
164     }
165 
166     private class RingReceiver extends BroadcastReceiver {
167         private boolean mRegistered;
168 
register(boolean register)169         public void register(boolean register) {
170             if (mRegistered == register) return;
171             if (register) {
172                 final IntentFilter filter = new IntentFilter();
173                 filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED);
174                 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
175                 mContext.registerReceiver(this, filter);
176             } else {
177                 mContext.unregisterReceiver(this);
178             }
179             mRegistered = register;
180         }
181 
182         @Override
onReceive(Context context, Intent intent)183         public void onReceive(Context context, Intent intent) {
184             final String action = intent.getAction();
185             if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) {
186                 mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR);
187             } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) {
188                 mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE);
189             }
190         }
191     }
192 
193 }
194