| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 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; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 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; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 30 | |
| 31 | import com.android.internal.annotations.VisibleForTesting; |
| Fan Zhang | 8878a95 | 2018-03-26 17:37:47 -0700 | [diff] [blame^] | 32 | import com.android.settings.R; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 33 | import com.android.settings.Utils; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 34 | import com.android.settings.notification.VolumeSeekBarPreference.Callback; |
| Juan Lang | 777ed25 | 2017-05-09 15:42:36 -0700 | [diff] [blame] | 35 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 36 | |
| 37 | import java.util.Objects; |
| 38 | |
| 39 | public class RingVolumePreferenceController extends VolumeSeekBarPreferenceController { |
| 40 | |
| 41 | private static final String TAG = "RingVolumeController"; |
| 42 | private static final String KEY_RING_VOLUME = "ring_volume"; |
| 43 | |
| 44 | private AudioManager mAudioManager; |
| 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 | private AudioHelper mHelper; |
| 51 | |
| 52 | public RingVolumePreferenceController(Context context, Callback callback, Lifecycle lifecycle) { |
| 53 | this(context, callback, lifecycle, new AudioHelper(context)); |
| 54 | } |
| 55 | |
| 56 | @VisibleForTesting |
| 57 | RingVolumePreferenceController(Context context, Callback callback, Lifecycle lifecycle, |
| 58 | AudioHelper helper) { |
| 59 | super(context, callback, lifecycle); |
| 60 | mHelper = helper; |
| 61 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 62 | mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); |
| 63 | if (mVibrator != null && !mVibrator.hasVibrator()) { |
| 64 | mVibrator = null; |
| 65 | } |
| 66 | updateRingerMode(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onResume() { |
| 71 | super.onResume(); |
| 72 | mReceiver.register(true); |
| 73 | updateEffectsSuppressor(); |
| 74 | updatePreferenceIcon(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onPause() { |
| 79 | super.onPause(); |
| 80 | mReceiver.register(false); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String getPreferenceKey() { |
| 85 | return KEY_RING_VOLUME; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean isAvailable() { |
| 90 | return Utils.isVoiceCapable(mContext) && !mHelper.isSingleVolume(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public int getAudioStream() { |
| 95 | return AudioManager.STREAM_RING; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public int getMuteIcon() { |
| Fan Zhang | 8878a95 | 2018-03-26 17:37:47 -0700 | [diff] [blame^] | 100 | return R.drawable.ic_volume_ringer_vibrate; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private void updateRingerMode() { |
| 104 | final int ringerMode = mAudioManager.getRingerModeInternal(); |
| 105 | if (mRingerMode == ringerMode) return; |
| 106 | mRingerMode = ringerMode; |
| 107 | updatePreferenceIcon(); |
| 108 | } |
| 109 | |
| 110 | private boolean wasRingerModeVibrate() { |
| 111 | return mVibrator != null && mRingerMode == AudioManager.RINGER_MODE_SILENT |
| 112 | && mAudioManager.getLastAudibleStreamVolume(AudioManager.STREAM_RING) == 0; |
| 113 | } |
| 114 | |
| 115 | private void updateEffectsSuppressor() { |
| 116 | final ComponentName suppressor = NotificationManager.from(mContext).getEffectsSuppressor(); |
| 117 | if (Objects.equals(suppressor, mSuppressor)) return; |
| 118 | mSuppressor = suppressor; |
| 119 | if (mPreference != null) { |
| jackqdyulei | d2c0eef | 2016-10-03 17:44:59 -0700 | [diff] [blame] | 120 | final String text = SuppressorHelper.getSuppressionText(mContext, suppressor); |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 121 | mPreference.setSuppressionText(text); |
| 122 | } |
| 123 | updatePreferenceIcon(); |
| 124 | } |
| 125 | |
| 126 | private void updatePreferenceIcon() { |
| 127 | if (mPreference != null) { |
| 128 | mPreference.showIcon(mSuppressor != null |
| 129 | ? com.android.internal.R.drawable.ic_audio_ring_notif_mute |
| 130 | : mRingerMode == AudioManager.RINGER_MODE_VIBRATE || wasRingerModeVibrate() |
| 131 | ? com.android.internal.R.drawable.ic_audio_ring_notif_vibrate |
| 132 | : com.android.internal.R.drawable.ic_audio_ring_notif); |
| 133 | } |
| 134 | } |
| 135 | |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 136 | private final class H extends Handler { |
| 137 | private static final int UPDATE_EFFECTS_SUPPRESSOR = 1; |
| 138 | private static final int UPDATE_RINGER_MODE = 2; |
| 139 | |
| 140 | private H() { |
| 141 | super(Looper.getMainLooper()); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void handleMessage(Message msg) { |
| 146 | switch (msg.what) { |
| 147 | case UPDATE_EFFECTS_SUPPRESSOR: |
| 148 | updateEffectsSuppressor(); |
| 149 | break; |
| 150 | case UPDATE_RINGER_MODE: |
| 151 | updateRingerMode(); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private class RingReceiver extends BroadcastReceiver { |
| 158 | private boolean mRegistered; |
| 159 | |
| 160 | public void register(boolean register) { |
| 161 | if (mRegistered == register) return; |
| 162 | if (register) { |
| 163 | final IntentFilter filter = new IntentFilter(); |
| 164 | filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
| 165 | filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); |
| 166 | mContext.registerReceiver(this, filter); |
| 167 | } else { |
| 168 | mContext.unregisterReceiver(this); |
| 169 | } |
| 170 | mRegistered = register; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onReceive(Context context, Intent intent) { |
| 175 | final String action = intent.getAction(); |
| 176 | if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) { |
| 177 | mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR); |
| 178 | } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) { |
| 179 | mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } |