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