1 /* 2 * Copyright (C) 2020 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.slices; 18 19 import static com.android.settings.slices.CustomSliceRegistry.VOLUME_SLICES_URI; 20 21 import android.content.ContentProvider; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.media.AudioManager; 26 import android.net.Uri; 27 import android.util.ArrayMap; 28 import android.util.Log; 29 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.settingslib.SliceBroadcastRelay; 33 34 import java.util.Map; 35 36 /** 37 * This helper is to handle the broadcasts of volume slices 38 */ 39 public class VolumeSliceHelper { 40 41 private static final String TAG = "VolumeSliceHelper"; 42 43 @VisibleForTesting 44 static Map<Uri, Integer> sRegisteredUri = new ArrayMap<>(); 45 @VisibleForTesting 46 static IntentFilter sIntentFilter; 47 registerIntentToUri(Context context, IntentFilter intentFilter, Uri sliceUri, int audioStream)48 static void registerIntentToUri(Context context, IntentFilter intentFilter, Uri sliceUri, 49 int audioStream) { 50 Log.d(TAG, "Registering uri for broadcast relay: " + sliceUri); 51 synchronized (sRegisteredUri) { 52 if (sRegisteredUri.isEmpty()) { 53 SliceBroadcastRelay.registerReceiver(context, VOLUME_SLICES_URI, 54 VolumeSliceRelayReceiver.class, intentFilter); 55 sIntentFilter = intentFilter; 56 } 57 sRegisteredUri.put(sliceUri, audioStream); 58 } 59 } 60 unregisterUri(Context context, Uri sliceUri)61 static boolean unregisterUri(Context context, Uri sliceUri) { 62 if (!sRegisteredUri.containsKey(sliceUri)) { 63 return false; 64 } 65 66 Log.d(TAG, "Unregistering uri broadcast relay: " + sliceUri); 67 synchronized (sRegisteredUri) { 68 sRegisteredUri.remove(sliceUri); 69 if (sRegisteredUri.isEmpty()) { 70 sIntentFilter = null; 71 SliceBroadcastRelay.unregisterReceivers(context, VOLUME_SLICES_URI); 72 } 73 } 74 return true; 75 } 76 onReceive(Context context, Intent intent)77 static void onReceive(Context context, Intent intent) { 78 final String action = intent.getAction(); 79 if (sIntentFilter == null || action == null || !sIntentFilter.hasAction(action)) { 80 return; 81 } 82 83 final String uriString = intent.getStringExtra(SliceBroadcastRelay.EXTRA_URI); 84 if (uriString == null) { 85 return; 86 } 87 88 final Uri uri = Uri.parse(uriString); 89 if (!VOLUME_SLICES_URI.equals(ContentProvider.getUriWithoutUserId(uri))) { 90 Log.w(TAG, "Invalid uri: " + uriString); 91 return; 92 } 93 94 if (AudioManager.VOLUME_CHANGED_ACTION.equals(action)) { 95 handleVolumeChanged(context, intent); 96 } else if (AudioManager.STREAM_MUTE_CHANGED_ACTION.equals(action) 97 || AudioManager.STREAM_DEVICES_CHANGED_ACTION.equals(action)) { 98 handleStreamChanged(context, intent); 99 } else { 100 notifyAllStreamsChanged(context); 101 } 102 } 103 handleVolumeChanged(Context context, Intent intent)104 private static void handleVolumeChanged(Context context, Intent intent) { 105 final int vol = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1); 106 final int prevVol = intent.getIntExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, -1); 107 if (vol != prevVol) { 108 handleStreamChanged(context, intent); 109 } 110 } 111 handleStreamChanged(Context context, Intent intent)112 private static void handleStreamChanged(Context context, Intent intent) { 113 final int inputType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); 114 synchronized (sRegisteredUri) { 115 for (Map.Entry<Uri, Integer> entry : sRegisteredUri.entrySet()) { 116 if (entry.getValue() == inputType) { 117 context.getContentResolver().notifyChange(entry.getKey(), null /* observer */); 118 break; 119 } 120 } 121 } 122 } 123 notifyAllStreamsChanged(Context context)124 private static void notifyAllStreamsChanged(Context context) { 125 synchronized (sRegisteredUri) { 126 sRegisteredUri.forEach((uri, audioStream) -> { 127 context.getContentResolver().notifyChange(uri, null /* observer */); 128 }); 129 } 130 } 131 } 132