| 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; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 24 | import android.media.AudioManager; |
| 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
| 27 | import android.os.Message; |
| Behnam Heydarshahi | 3ac6aaf | 2022-09-06 22:12:37 +0000 | [diff] [blame] | 28 | import android.service.notification.NotificationListenerService; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 29 | |
| Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 30 | import androidx.lifecycle.OnLifecycleEvent; |
| 31 | |
| Fan Zhang | 8878a95 | 2018-03-26 17:37:47 -0700 | [diff] [blame] | 32 | import com.android.settings.R; |
| Julia Reynolds | 0ddba4c | 2018-04-17 09:11:43 -0400 | [diff] [blame] | 33 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 34 | |
| Behnam Heydarshahi | 3ac6aaf | 2022-09-06 22:12:37 +0000 | [diff] [blame] | 35 | /** |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 36 | * This slider represents both ring and notification |
| Behnam Heydarshahi | 3ac6aaf | 2022-09-06 22:12:37 +0000 | [diff] [blame] | 37 | */ |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 38 | public class RingVolumePreferenceController extends |
| 39 | RingerModeAffectedVolumePreferenceController { |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 40 | |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 41 | private static final String KEY_RING_VOLUME = "ring_volume"; |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 42 | private static final String TAG = "RingVolumePreferenceController"; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 43 | |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 44 | private final RingReceiver mReceiver = new RingReceiver(); |
| 45 | private final H mHandler = new H(); |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 46 | |
| Julia Reynolds | 5606379 | 2018-04-03 13:48:55 -0400 | [diff] [blame] | 47 | public RingVolumePreferenceController(Context context) { |
| 48 | this(context, KEY_RING_VOLUME); |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| Julia Reynolds | 5606379 | 2018-04-03 13:48:55 -0400 | [diff] [blame] | 51 | public RingVolumePreferenceController(Context context, String key) { |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 52 | super(context, key, TAG); |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 53 | |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 54 | mNormalIconId = R.drawable.ic_notifications; |
| Behnam Heydarshahi | 2ac6fc5 | 2022-10-12 15:08:22 +0000 | [diff] [blame] | 55 | mVibrateIconId = R.drawable.ic_volume_ringer_vibrate; |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 56 | mSilentIconId = R.drawable.ic_notifications_off_24dp; |
| 57 | |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 58 | updateRingerMode(); |
| Behnam Heydarshahi | 2ac6fc5 | 2022-10-12 15:08:22 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| Julia Reynolds | 0ddba4c | 2018-04-17 09:11:43 -0400 | [diff] [blame] | 61 | @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 62 | @Override |
| 63 | public void onResume() { |
| 64 | super.onResume(); |
| 65 | mReceiver.register(true); |
| 66 | updateEffectsSuppressor(); |
| Behnam Heydarshahi | 2ac6fc5 | 2022-10-12 15:08:22 +0000 | [diff] [blame] | 67 | selectPreferenceIconState(); |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 68 | |
| 69 | if (mPreference != null) { |
| 70 | mPreference.setVisible(getAvailabilityStatus() == AVAILABLE); |
| 71 | } |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| Julia Reynolds | 0ddba4c | 2018-04-17 09:11:43 -0400 | [diff] [blame] | 74 | @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 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 |
| Julia Reynolds | 5606379 | 2018-04-03 13:48:55 -0400 | [diff] [blame] | 87 | public int getAvailabilityStatus() { |
| Behnam Heydarshahi | f07e023 | 2023-05-15 15:15:33 +0000 | [diff] [blame] | 88 | return UNSUPPORTED_ON_DEVICE; |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public int getAudioStream() { |
| 93 | return AudioManager.STREAM_RING; |
| 94 | } |
| 95 | |
| 96 | @Override |
| Behnam Heydarshahi | 4f87dd4 | 2022-12-15 21:51:13 +0000 | [diff] [blame] | 97 | protected boolean hintsMatch(int hints) { |
| Behnam Heydarshahi | 3ac6aaf | 2022-09-06 22:12:37 +0000 | [diff] [blame] | 98 | return (hints & NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS) != 0 |
| Behnam Heydarshahi | f07e023 | 2023-05-15 15:15:33 +0000 | [diff] [blame] | 99 | || (hints & NotificationListenerService.HINT_HOST_DISABLE_EFFECTS) != 0; |
| Behnam Heydarshahi | 3ac6aaf | 2022-09-06 22:12:37 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| Doris Ling | 1b3ec04 | 2016-11-30 17:33:50 -0800 | [diff] [blame] | 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 | |
| 106 | private H() { |
| 107 | super(Looper.getMainLooper()); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 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 | |
| 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 |
| 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 | } |