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.systemui.volume;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.provider.Settings;
28 import android.provider.SettingsSlicesContract;
29 import android.text.TextUtils;
30 import android.util.Log;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.Window;
34 import android.view.WindowManager;
35 import android.widget.Button;
36 
37 import androidx.annotation.NonNull;
38 import androidx.lifecycle.Lifecycle;
39 import androidx.lifecycle.LifecycleOwner;
40 import androidx.lifecycle.LifecycleRegistry;
41 import androidx.lifecycle.LiveData;
42 import androidx.recyclerview.widget.LinearLayoutManager;
43 import androidx.recyclerview.widget.RecyclerView;
44 import androidx.slice.Slice;
45 import androidx.slice.SliceMetadata;
46 import androidx.slice.widget.EventInfo;
47 import androidx.slice.widget.SliceLiveData;
48 
49 import com.android.settingslib.bluetooth.A2dpProfile;
50 import com.android.settingslib.bluetooth.BluetoothUtils;
51 import com.android.settingslib.bluetooth.LocalBluetoothManager;
52 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
53 import com.android.settingslib.media.MediaOutputConstants;
54 import com.android.systemui.plugins.ActivityStarter;
55 import com.android.systemui.res.R;
56 import com.android.systemui.statusbar.phone.SystemUIDialog;
57 
58 import java.util.ArrayList;
59 import java.util.HashSet;
60 import java.util.LinkedHashMap;
61 import java.util.List;
62 import java.util.Map;
63 
64 /**
65  * Visual presentation of the volume panel dialog.
66  */
67 public class VolumePanelDialog extends SystemUIDialog implements LifecycleOwner {
68     private static final String TAG = "VolumePanelDialog";
69 
70     private static final int DURATION_SLICE_BINDING_TIMEOUT_MS = 200;
71     private static final int DEFAULT_SLICE_SIZE = 4;
72 
73     private final ActivityStarter mActivityStarter;
74     private RecyclerView mVolumePanelSlices;
75     private VolumePanelSlicesAdapter mVolumePanelSlicesAdapter;
76     private final LifecycleRegistry mLifecycleRegistry;
77     private final Handler mHandler = new Handler(Looper.getMainLooper());
78     private final Map<Uri, LiveData<Slice>> mSliceLiveData = new LinkedHashMap<>();
79     private final HashSet<Uri> mLoadedSlices = new HashSet<>();
80     private boolean mSlicesReadyToLoad;
81     private LocalBluetoothProfileManager mProfileManager;
82 
VolumePanelDialog(Context context, ActivityStarter activityStarter, boolean aboveStatusBar)83     public VolumePanelDialog(Context context,
84             ActivityStarter activityStarter, boolean aboveStatusBar) {
85         super(context);
86         mActivityStarter = activityStarter;
87         mLifecycleRegistry = new LifecycleRegistry(this);
88         if (!aboveStatusBar) {
89             getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
90         }
91     }
92 
93     @Override
onCreate(Bundle savedInstanceState)94     protected void onCreate(Bundle savedInstanceState) {
95         super.onCreate(savedInstanceState);
96         Log.d(TAG, "onCreate");
97 
98         View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.volume_panel_dialog,
99                 null);
100         final Window window = getWindow();
101         window.setContentView(dialogView);
102 
103         Button doneButton = dialogView.findViewById(R.id.done_button);
104         doneButton.setOnClickListener(v -> dismiss());
105         Button settingsButton = dialogView.findViewById(R.id.settings_button);
106         settingsButton.setOnClickListener(v -> {
107             dismiss();
108 
109             Intent intent = new Intent(Settings.ACTION_SOUND_SETTINGS);
110             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111             mActivityStarter.startActivity(intent, /* dismissShade= */ true);
112         });
113 
114         LocalBluetoothManager localBluetoothManager = LocalBluetoothManager.getInstance(
115                 getContext(), null);
116         if (localBluetoothManager != null) {
117             mProfileManager = localBluetoothManager.getProfileManager();
118         }
119 
120         mVolumePanelSlices = dialogView.findViewById(R.id.volume_panel_parent_layout);
121         mVolumePanelSlices.setLayoutManager(new LinearLayoutManager(getContext()));
122 
123         loadAllSlices();
124 
125         mLifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
126     }
127 
loadAllSlices()128     private void loadAllSlices() {
129         mSliceLiveData.clear();
130         mLoadedSlices.clear();
131         final List<Uri> sliceUris = getSlices();
132 
133         for (Uri uri : sliceUris) {
134             final LiveData<Slice> sliceLiveData = SliceLiveData.fromUri(getContext(), uri,
135                     (int type, Throwable source) -> {
136                         if (!removeSliceLiveData(uri)) {
137                             mLoadedSlices.add(uri);
138                         }
139                     });
140 
141             // Add slice first to make it in order.  Will remove it later if there's an error.
142             mSliceLiveData.put(uri, sliceLiveData);
143 
144             sliceLiveData.observe(this, slice -> {
145                 if (mLoadedSlices.contains(uri)) {
146                     return;
147                 }
148                 Log.d(TAG, "received slice: " + (slice == null ? null : slice.getUri()));
149                 final SliceMetadata metadata = SliceMetadata.from(getContext(), slice);
150                 if (slice == null || metadata.isErrorSlice()) {
151                     if (!removeSliceLiveData(uri)) {
152                         mLoadedSlices.add(uri);
153                     }
154                 } else if (metadata.getLoadingState() == SliceMetadata.LOADED_ALL) {
155                     mLoadedSlices.add(uri);
156                 } else {
157                     mHandler.postDelayed(() -> {
158                         mLoadedSlices.add(uri);
159                         setupAdapterWhenReady();
160                     }, DURATION_SLICE_BINDING_TIMEOUT_MS);
161                 }
162 
163                 setupAdapterWhenReady();
164             });
165         }
166     }
167 
setupAdapterWhenReady()168     private void setupAdapterWhenReady() {
169         if (mLoadedSlices.size() == mSliceLiveData.size() && !mSlicesReadyToLoad) {
170             mSlicesReadyToLoad = true;
171             mVolumePanelSlicesAdapter = new VolumePanelSlicesAdapter(this, mSliceLiveData);
172             mVolumePanelSlicesAdapter.setOnSliceActionListener((eventInfo, sliceItem) -> {
173                 if (eventInfo.actionType == EventInfo.ACTION_TYPE_SLIDER) {
174                     return;
175                 }
176                 this.dismiss();
177             });
178             if (mSliceLiveData.size() < DEFAULT_SLICE_SIZE) {
179                 mVolumePanelSlices.setMinimumHeight(0);
180             }
181             mVolumePanelSlices.setAdapter(mVolumePanelSlicesAdapter);
182         }
183     }
184 
removeSliceLiveData(Uri uri)185     private boolean removeSliceLiveData(Uri uri) {
186         boolean removed = false;
187         // Keeps observe media output slice
188         if (!uri.equals(MEDIA_OUTPUT_INDICATOR_SLICE_URI)) {
189             Log.d(TAG, "remove uri: " + uri);
190             removed = mSliceLiveData.remove(uri) != null;
191             if (mVolumePanelSlicesAdapter != null) {
192                 mVolumePanelSlicesAdapter.updateDataSet(new ArrayList<>(mSliceLiveData.values()));
193             }
194         }
195         return removed;
196     }
197 
198     @Override
start()199     protected void start() {
200         Log.d(TAG, "onStart");
201         mLifecycleRegistry.setCurrentState(Lifecycle.State.STARTED);
202         mLifecycleRegistry.setCurrentState(Lifecycle.State.RESUMED);
203     }
204 
205     @Override
stop()206     protected void stop() {
207         Log.d(TAG, "onStop");
208         mLifecycleRegistry.setCurrentState(Lifecycle.State.DESTROYED);
209     }
210 
getSlices()211     private List<Uri> getSlices() {
212         final List<Uri> uris = new ArrayList<>();
213         uris.add(REMOTE_MEDIA_SLICE_URI);
214         uris.add(VOLUME_MEDIA_URI);
215         Uri controlUri = getExtraControlUri();
216         if (controlUri != null) {
217             Log.d(TAG, "add extra control slice");
218             uris.add(controlUri);
219         }
220         uris.add(MEDIA_OUTPUT_INDICATOR_SLICE_URI);
221         uris.add(VOLUME_CALL_URI);
222         uris.add(VOLUME_RINGER_URI);
223         uris.add(VOLUME_ALARM_URI);
224         return uris;
225     }
226 
227     private static final String SETTINGS_SLICE_AUTHORITY = "com.android.settings.slices";
228     private static final Uri REMOTE_MEDIA_SLICE_URI = new Uri.Builder()
229             .scheme(ContentResolver.SCHEME_CONTENT)
230             .authority(SETTINGS_SLICE_AUTHORITY)
231             .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
232             .appendPath(MediaOutputConstants.KEY_REMOTE_MEDIA)
233             .build();
234     private static final Uri VOLUME_MEDIA_URI = new Uri.Builder()
235             .scheme(ContentResolver.SCHEME_CONTENT)
236             .authority(SETTINGS_SLICE_AUTHORITY)
237             .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
238             .appendPath("media_volume")
239             .build();
240     private static final Uri MEDIA_OUTPUT_INDICATOR_SLICE_URI = new Uri.Builder()
241             .scheme(ContentResolver.SCHEME_CONTENT)
242             .authority(SETTINGS_SLICE_AUTHORITY)
243             .appendPath(SettingsSlicesContract.PATH_SETTING_INTENT)
244             .appendPath("media_output_indicator")
245             .build();
246     private static final Uri VOLUME_CALL_URI = new Uri.Builder()
247             .scheme(ContentResolver.SCHEME_CONTENT)
248             .authority(SETTINGS_SLICE_AUTHORITY)
249             .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
250             .appendPath("call_volume")
251             .build();
252     private static final Uri VOLUME_RINGER_URI = new Uri.Builder()
253             .scheme(ContentResolver.SCHEME_CONTENT)
254             .authority(SETTINGS_SLICE_AUTHORITY)
255             .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
256             .appendPath("ring_volume")
257             .build();
258     private static final Uri VOLUME_ALARM_URI = new Uri.Builder()
259             .scheme(ContentResolver.SCHEME_CONTENT)
260             .authority(SETTINGS_SLICE_AUTHORITY)
261             .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
262             .appendPath("alarm_volume")
263             .build();
264 
getExtraControlUri()265     private Uri getExtraControlUri() {
266         Uri controlUri = null;
267         final BluetoothDevice bluetoothDevice = findActiveDevice();
268         if (bluetoothDevice != null) {
269             // The control slice width = dialog width - horizontal padding of two sides
270             final int dialogWidth =
271                     getWindow().getWindowManager().getCurrentWindowMetrics().getBounds().width();
272             final int controlSliceWidth = dialogWidth
273                     - getContext().getResources().getDimensionPixelSize(
274                     R.dimen.volume_panel_slice_horizontal_padding) * 2;
275             final String uri = BluetoothUtils.getControlUriMetaData(bluetoothDevice);
276             if (!TextUtils.isEmpty(uri)) {
277                 try {
278                     controlUri = Uri.parse(uri + controlSliceWidth);
279                 } catch (NullPointerException exception) {
280                     Log.d(TAG, "unable to parse extra control uri");
281                     controlUri = null;
282                 }
283             }
284         }
285         return controlUri;
286     }
287 
findActiveDevice()288     private BluetoothDevice findActiveDevice() {
289         if (mProfileManager != null) {
290             final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
291             if (a2dpProfile != null) {
292                 return a2dpProfile.getActiveDevice();
293             }
294         }
295         return null;
296     }
297 
298     @NonNull
299     @Override
getLifecycle()300     public Lifecycle getLifecycle() {
301         return mLifecycleRegistry;
302     }
303 }
304