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.ActivityThread; 20 import android.app.NotificationManager; 21 import android.content.BroadcastReceiver; 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.provider.DeviceConfig; 30 import android.service.notification.NotificationListenerService; 31 32 import androidx.lifecycle.OnLifecycleEvent; 33 34 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; 35 import com.android.settings.R; 36 import com.android.settingslib.core.lifecycle.Lifecycle; 37 38 import java.util.Set; 39 40 /** 41 * This slider is used to represent ring volume when ring is separated from notification 42 */ 43 public class SeparateRingVolumePreferenceController extends 44 RingerModeAffectedVolumePreferenceController { 45 46 private static final String KEY_SEPARATE_RING_VOLUME = "separate_ring_volume"; 47 private static final String TAG = "SeparateRingVolumePreferenceController"; 48 49 private final RingReceiver mReceiver = new RingReceiver(); 50 private final H mHandler = new H(); 51 SeparateRingVolumePreferenceController(Context context)52 public SeparateRingVolumePreferenceController(Context context) { 53 this(context, KEY_SEPARATE_RING_VOLUME); 54 } 55 SeparateRingVolumePreferenceController(Context context, String key)56 public SeparateRingVolumePreferenceController(Context context, String key) { 57 super(context, key, TAG); 58 59 mNormalIconId = R.drawable.ic_ring_volume; 60 mVibrateIconId = R.drawable.ic_volume_ringer_vibrate; 61 mSilentIconId = R.drawable.ic_ring_volume_off; 62 63 mSeparateNotification = isSeparateNotificationConfigEnabled(); 64 updateRingerMode(); 65 } 66 67 /** 68 * Show/hide settings 69 */ onDeviceConfigChange(DeviceConfig.Properties properties)70 private void onDeviceConfigChange(DeviceConfig.Properties properties) { 71 Set<String> changeSet = properties.getKeyset(); 72 if (changeSet.contains(SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION)) { 73 boolean valueUpdated = readSeparateNotificationVolumeConfig(); 74 if (valueUpdated) { 75 updateEffectsSuppressor(); 76 selectPreferenceIconState(); 77 } 78 } 79 } 80 81 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) 82 @Override onResume()83 public void onResume() { 84 super.onResume(); 85 mReceiver.register(true); 86 readSeparateNotificationVolumeConfig(); 87 DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_SYSTEMUI, 88 ActivityThread.currentApplication().getMainExecutor(), this::onDeviceConfigChange); 89 updateEffectsSuppressor(); 90 selectPreferenceIconState(); 91 92 if (mPreference != null) { 93 mPreference.setVisible(getAvailabilityStatus() == AVAILABLE); 94 } 95 } 96 97 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) 98 @Override onPause()99 public void onPause() { 100 super.onPause(); 101 mReceiver.register(false); 102 DeviceConfig.removeOnPropertiesChangedListener(this::onDeviceConfigChange); 103 } 104 105 @Override getPreferenceKey()106 public String getPreferenceKey() { 107 return KEY_SEPARATE_RING_VOLUME; 108 } 109 110 @Override getAvailabilityStatus()111 public int getAvailabilityStatus() { 112 boolean separateNotification = isSeparateNotificationConfigEnabled(); 113 return separateNotification && !mHelper.isSingleVolume() 114 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 115 } 116 117 @Override getAudioStream()118 public int getAudioStream() { 119 return AudioManager.STREAM_RING; 120 } 121 122 @Override hintsMatch(int hints)123 protected boolean hintsMatch(int hints) { 124 return (hints & NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS) != 0 125 || (hints & NotificationListenerService.HINT_HOST_DISABLE_EFFECTS) != 0; 126 } 127 128 129 130 private final class H extends Handler { 131 private static final int UPDATE_EFFECTS_SUPPRESSOR = 1; 132 private static final int UPDATE_RINGER_MODE = 2; 133 H()134 private H() { 135 super(Looper.getMainLooper()); 136 } 137 138 @Override handleMessage(Message msg)139 public void handleMessage(Message msg) { 140 switch (msg.what) { 141 case UPDATE_EFFECTS_SUPPRESSOR: 142 updateEffectsSuppressor(); 143 break; 144 case UPDATE_RINGER_MODE: 145 updateRingerMode(); 146 break; 147 } 148 } 149 } 150 151 private class RingReceiver extends BroadcastReceiver { 152 private boolean mRegistered; 153 register(boolean register)154 public void register(boolean register) { 155 if (mRegistered == register) return; 156 if (register) { 157 final IntentFilter filter = new IntentFilter(); 158 filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); 159 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); 160 mContext.registerReceiver(this, filter); 161 } else { 162 mContext.unregisterReceiver(this); 163 } 164 mRegistered = register; 165 } 166 167 @Override onReceive(Context context, Intent intent)168 public void onReceive(Context context, Intent intent) { 169 final String action = intent.getAction(); 170 if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) { 171 mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR); 172 } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) { 173 mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE); 174 } 175 } 176 } 177 178 } 179