1 /* 2 * Copyright (C) 2022 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.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.media.AudioManager; 25 import android.os.Handler; 26 import android.os.Looper; 27 import android.os.Message; 28 import android.service.notification.NotificationListenerService; 29 30 import androidx.lifecycle.OnLifecycleEvent; 31 32 import com.android.settings.R; 33 import com.android.settingslib.core.lifecycle.Lifecycle; 34 35 /** 36 * This slider is used to represent ring volume when ring is separated from notification 37 */ 38 public class SeparateRingVolumePreferenceController extends 39 RingerModeAffectedVolumePreferenceController { 40 41 private static final String KEY_SEPARATE_RING_VOLUME = "separate_ring_volume"; 42 private static final String TAG = "SeparateRingVolumePreferenceController"; 43 44 private final RingReceiver mReceiver = new RingReceiver(); 45 private final H mHandler = new H(); 46 SeparateRingVolumePreferenceController(Context context)47 public SeparateRingVolumePreferenceController(Context context) { 48 this(context, KEY_SEPARATE_RING_VOLUME); 49 } 50 SeparateRingVolumePreferenceController(Context context, String key)51 public SeparateRingVolumePreferenceController(Context context, String key) { 52 super(context, key, TAG); 53 54 mNormalIconId = R.drawable.ic_ring_volume; 55 mVibrateIconId = R.drawable.ic_volume_ringer_vibrate; 56 mSilentIconId = R.drawable.ic_ring_volume_off; 57 58 updateRingerMode(); 59 } 60 61 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) 62 @Override onResume()63 public void onResume() { 64 super.onResume(); 65 mReceiver.register(true); 66 updateEffectsSuppressor(); 67 selectPreferenceIconState(); 68 updateContentDescription(); 69 70 if (mPreference != null) { 71 mPreference.setVisible(getAvailabilityStatus() == AVAILABLE); 72 } 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_SEPARATE_RING_VOLUME; 85 } 86 87 @Override getAvailabilityStatus()88 public int getAvailabilityStatus() { 89 return !mHelper.isSingleVolume() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 90 } 91 92 @Override getAudioStream()93 public int getAudioStream() { 94 return AudioManager.STREAM_RING; 95 } 96 97 @Override hintsMatch(int hints)98 protected boolean hintsMatch(int hints) { 99 return (hints & NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS) != 0 100 || (hints & NotificationListenerService.HINT_HOST_DISABLE_EFFECTS) != 0; 101 } 102 103 private final class H extends Handler { 104 private static final int UPDATE_EFFECTS_SUPPRESSOR = 1; 105 private static final int UPDATE_RINGER_MODE = 2; 106 H()107 private H() { 108 super(Looper.getMainLooper()); 109 } 110 111 @Override handleMessage(Message msg)112 public void handleMessage(Message msg) { 113 switch (msg.what) { 114 case UPDATE_EFFECTS_SUPPRESSOR: 115 updateEffectsSuppressor(); 116 break; 117 case UPDATE_RINGER_MODE: 118 updateRingerMode(); 119 break; 120 } 121 } 122 } 123 124 private class RingReceiver extends BroadcastReceiver { 125 private boolean mRegistered; 126 register(boolean register)127 public void register(boolean register) { 128 if (mRegistered == register) return; 129 if (register) { 130 final IntentFilter filter = new IntentFilter(); 131 filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); 132 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); 133 mContext.registerReceiver(this, filter); 134 } else { 135 mContext.unregisterReceiver(this); 136 } 137 mRegistered = register; 138 } 139 140 @Override onReceive(Context context, Intent intent)141 public void onReceive(Context context, Intent intent) { 142 final String action = intent.getAction(); 143 if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) { 144 mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR); 145 } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) { 146 mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE); 147 } 148 } 149 } 150 151 } 152