• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.media;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 
22 import androidx.annotation.VisibleForTesting;
23 
24 import com.android.settings.slices.SliceBackgroundWorker;
25 import com.android.settingslib.media.LocalMediaManager;
26 import com.android.settingslib.media.MediaDevice;
27 import com.android.settingslib.utils.ThreadUtils;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * SliceBackgroundWorker for get MediaDevice list and handle MediaDevice state change event.
34  */
35 public class MediaDeviceUpdateWorker extends SliceBackgroundWorker
36         implements LocalMediaManager.DeviceCallback {
37 
38     private final Context mContext;
39     private final List<MediaDevice> mMediaDevices = new ArrayList<>();
40 
41     private String mPackageName;
42 
43     @VisibleForTesting
44     LocalMediaManager mLocalMediaManager;
45 
MediaDeviceUpdateWorker(Context context, Uri uri)46     public MediaDeviceUpdateWorker(Context context, Uri uri) {
47         super(context, uri);
48         mContext = context;
49     }
50 
setPackageName(String packageName)51     public void setPackageName(String packageName) {
52         mPackageName = packageName;
53     }
54 
55     @Override
onSlicePinned()56     protected void onSlicePinned() {
57         mMediaDevices.clear();
58         if (mLocalMediaManager == null) {
59             mLocalMediaManager = new LocalMediaManager(mContext, mPackageName, null);
60         }
61 
62         mLocalMediaManager.registerCallback(this);
63         mLocalMediaManager.startScan();
64     }
65 
66     @Override
onSliceUnpinned()67     protected void onSliceUnpinned() {
68         mLocalMediaManager.unregisterCallback(this);
69         mLocalMediaManager.stopScan();
70     }
71 
72     @Override
close()73     public void close() {
74         mLocalMediaManager = null;
75     }
76 
77     @Override
onDeviceListUpdate(List<MediaDevice> devices)78     public void onDeviceListUpdate(List<MediaDevice> devices) {
79         buildMediaDevices(devices);
80         notifySliceChange();
81     }
82 
buildMediaDevices(List<MediaDevice> devices)83     private void buildMediaDevices(List<MediaDevice> devices) {
84         mMediaDevices.clear();
85         mMediaDevices.addAll(devices);
86     }
87 
88     @Override
onSelectedDeviceStateChanged(MediaDevice device, int state)89     public void onSelectedDeviceStateChanged(MediaDevice device, int state) {
90         notifySliceChange();
91     }
92 
getMediaDevices()93     public List<MediaDevice> getMediaDevices() {
94         return new ArrayList<>(mMediaDevices);
95     }
96 
connectDevice(MediaDevice device)97     public void connectDevice(MediaDevice device) {
98         ThreadUtils.postOnBackgroundThread(() -> {
99             mLocalMediaManager.connectDevice(device);
100         });
101     }
102 
getMediaDeviceById(String id)103     public MediaDevice getMediaDeviceById(String id) {
104         return mLocalMediaManager.getMediaDeviceById(mMediaDevices, id);
105     }
106 
getCurrentConnectedMediaDevice()107     public MediaDevice getCurrentConnectedMediaDevice() {
108         return mLocalMediaManager.getCurrentConnectedDevice();
109     }
110 }
111