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.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 represents both ring and notification 37 */ 38 public class RingVolumePreferenceController extends 39 RingerModeAffectedVolumePreferenceController { 40 41 private static final String KEY_RING_VOLUME = "ring_volume"; 42 private static final String TAG = "RingVolumePreferenceController"; 43 44 private final RingReceiver mReceiver = new RingReceiver(); 45 private final H mHandler = new H(); 46 RingVolumePreferenceController(Context context)47 public RingVolumePreferenceController(Context context) { 48 this(context, KEY_RING_VOLUME); 49 } 50 RingVolumePreferenceController(Context context, String key)51 public RingVolumePreferenceController(Context context, String key) { 52 super(context, key, TAG); 53 54 mNormalIconId = R.drawable.ic_notifications; 55 mVibrateIconId = R.drawable.ic_volume_ringer_vibrate; 56 mSilentIconId = R.drawable.ic_notifications_off_24dp; 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 69 if (mPreference != null) { 70 mPreference.setVisible(getAvailabilityStatus() == AVAILABLE); 71 } 72 } 73 74 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) 75 @Override onPause()76 public void onPause() { 77 super.onPause(); 78 mReceiver.register(false); 79 } 80 81 @Override getPreferenceKey()82 public String getPreferenceKey() { 83 return KEY_RING_VOLUME; 84 } 85 86 @Override getAvailabilityStatus()87 public int getAvailabilityStatus() { 88 return UNSUPPORTED_ON_DEVICE; 89 } 90 91 @Override getAudioStream()92 public int getAudioStream() { 93 return AudioManager.STREAM_RING; 94 } 95 96 @Override hintsMatch(int hints)97 protected boolean hintsMatch(int hints) { 98 return (hints & NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS) != 0 99 || (hints & NotificationListenerService.HINT_HOST_DISABLE_EFFECTS) != 0; 100 } 101 102 private final class H extends Handler { 103 private static final int UPDATE_EFFECTS_SUPPRESSOR = 1; 104 private static final int UPDATE_RINGER_MODE = 2; 105 H()106 private H() { 107 super(Looper.getMainLooper()); 108 } 109 110 @Override handleMessage(Message msg)111 public void handleMessage(Message msg) { 112 switch (msg.what) { 113 case UPDATE_EFFECTS_SUPPRESSOR: 114 updateEffectsSuppressor(); 115 break; 116 case UPDATE_RINGER_MODE: 117 updateRingerMode(); 118 break; 119 } 120 } 121 } 122 123 private class RingReceiver extends BroadcastReceiver { 124 private boolean mRegistered; 125 register(boolean register)126 public void register(boolean register) { 127 if (mRegistered == register) return; 128 if (register) { 129 final IntentFilter filter = new IntentFilter(); 130 filter.addAction(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); 131 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); 132 mContext.registerReceiver(this, filter); 133 } else { 134 mContext.unregisterReceiver(this); 135 } 136 mRegistered = register; 137 } 138 139 @Override onReceive(Context context, Intent intent)140 public void onReceive(Context context, Intent intent) { 141 final String action = intent.getAction(); 142 if (NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED.equals(action)) { 143 mHandler.sendEmptyMessage(H.UPDATE_EFFECTS_SUPPRESSOR); 144 } else if (AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION.equals(action)) { 145 mHandler.sendEmptyMessage(H.UPDATE_RINGER_MODE); 146 } 147 } 148 } 149 150 } 151