• 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 static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_INDICATOR_SLICE_URI;
20 
21 import android.annotation.ColorInt;
22 import android.app.PendingIntent;
23 import android.bluetooth.BluetoothDevice;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.telephony.TelephonyManager;
28 import android.util.Log;
29 
30 import androidx.core.graphics.drawable.IconCompat;
31 import androidx.slice.Slice;
32 import androidx.slice.builders.ListBuilder;
33 import androidx.slice.builders.SliceAction;
34 
35 import com.android.internal.util.CollectionUtils;
36 import com.android.settings.R;
37 import com.android.settings.Utils;
38 import com.android.settings.slices.CustomSliceable;
39 import com.android.settingslib.bluetooth.A2dpProfile;
40 import com.android.settingslib.bluetooth.HearingAidProfile;
41 import com.android.settingslib.bluetooth.LocalBluetoothManager;
42 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
43 import com.android.settingslib.media.MediaOutputSliceConstants;
44 
45 import java.util.ArrayList;
46 import java.util.List;
47 
48 public class MediaOutputIndicatorSlice implements CustomSliceable {
49 
50     private static final String TAG = "MediaOutputIndicatorSlice";
51 
52     private Context mContext;
53     private LocalBluetoothManager mLocalBluetoothManager;
54     private LocalBluetoothProfileManager mProfileManager;
55 
MediaOutputIndicatorSlice(Context context)56     public MediaOutputIndicatorSlice(Context context) {
57         mContext = context;
58         mLocalBluetoothManager = com.android.settings.bluetooth.Utils.getLocalBtManager(context);
59         if (mLocalBluetoothManager == null) {
60             Log.e(TAG, "Bluetooth is not supported on this device");
61             return;
62         }
63         mProfileManager = mLocalBluetoothManager.getProfileManager();
64     }
65 
66     @Override
getSlice()67     public Slice getSlice() {
68         if (!isVisible()) {
69             return null;
70         }
71         final IconCompat icon = IconCompat.createWithResource(mContext,
72                 com.android.internal.R.drawable.ic_settings_bluetooth);
73         final CharSequence title = mContext.getText(R.string.media_output_title);
74         final PendingIntent primaryActionIntent = PendingIntent.getActivity(mContext,
75                 0 /* requestCode */, getMediaOutputSliceIntent(), 0 /* flags */);
76         final SliceAction primarySliceAction = SliceAction.createDeeplink(
77                 primaryActionIntent, icon, ListBuilder.ICON_IMAGE, title);
78         @ColorInt final int color = Utils.getColorAccentDefaultColor(mContext);
79 
80         final ListBuilder listBuilder = new ListBuilder(mContext,
81                 MEDIA_OUTPUT_INDICATOR_SLICE_URI,
82                 ListBuilder.INFINITY)
83                 .setAccentColor(color)
84                 .addRow(new ListBuilder.RowBuilder()
85                         .setTitle(title)
86                         .setSubtitle(findActiveDeviceName())
87                         .setPrimaryAction(primarySliceAction));
88         return listBuilder.build();
89     }
90 
getMediaOutputSliceIntent()91     private Intent getMediaOutputSliceIntent() {
92         final Intent intent = new Intent()
93                 .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
94                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
95         return intent;
96     }
97 
98     @Override
getUri()99     public Uri getUri() {
100         return MEDIA_OUTPUT_INDICATOR_SLICE_URI;
101     }
102 
103     @Override
getIntent()104     public Intent getIntent() {
105         // This Slice reflects active media device information and launch MediaOutputSlice. It does
106         // not contain its owned Slice data
107         return null;
108     }
109 
110     @Override
getBackgroundWorkerClass()111     public Class getBackgroundWorkerClass() {
112         return MediaOutputIndicatorWorker.class;
113     }
114 
isVisible()115     private boolean isVisible() {
116         // To decide Slice's visibility.
117         // Return true if
118         // 1. phone is not in ongoing call mode
119         // 2. Bluetooth device is connected
120         final TelephonyManager telephonyManager =
121                 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
122         return telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE
123                 && (!CollectionUtils.isEmpty(getConnectedA2dpDevices())
124                 || !CollectionUtils.isEmpty(getConnectedHearingAidDevices()));
125     }
126 
getConnectedA2dpDevices()127     private List<BluetoothDevice> getConnectedA2dpDevices() {
128         // Get A2dp devices on states
129         // (STATE_CONNECTING, STATE_CONNECTED,  STATE_DISCONNECTING)
130         final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
131         if (a2dpProfile == null) {
132             return new ArrayList<>();
133         }
134         return a2dpProfile.getConnectedDevices();
135     }
136 
getConnectedHearingAidDevices()137     private List<BluetoothDevice> getConnectedHearingAidDevices() {
138         // Get hearing aid profile devices on states
139         // (STATE_CONNECTING, STATE_CONNECTED,  STATE_DISCONNECTING)
140         final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
141         if (hapProfile == null) {
142             return new ArrayList<>();
143         }
144 
145         return hapProfile.getConnectedDevices();
146     }
147 
findActiveDeviceName()148     private CharSequence findActiveDeviceName() {
149         // Return Hearing Aid device name if it is active
150         BluetoothDevice activeDevice = findActiveHearingAidDevice();
151         if (activeDevice != null) {
152             return activeDevice.getAliasName();
153         }
154         // Return A2DP device name if it is active
155         final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
156         if (a2dpProfile != null) {
157             activeDevice = a2dpProfile.getActiveDevice();
158             if (activeDevice != null) {
159                 return activeDevice.getAliasName();
160             }
161         }
162         // No active device, return default summary
163         return mContext.getText(R.string.media_output_default_summary);
164     }
165 
findActiveHearingAidDevice()166     private BluetoothDevice findActiveHearingAidDevice() {
167         final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
168         if (hearingAidProfile == null) {
169             return null;
170         }
171 
172         final List<BluetoothDevice> activeDevices = hearingAidProfile.getActiveDevices();
173         for (BluetoothDevice btDevice : activeDevices) {
174             if (btDevice != null) {
175                 return btDevice;
176             }
177         }
178         return null;
179     }
180 }
181