• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 package com.android.settingslib.media;
17 
18 import android.content.Context;
19 import android.graphics.drawable.Drawable;
20 import android.util.Log;
21 
22 import com.android.settingslib.R;
23 import com.android.settingslib.bluetooth.A2dpProfile;
24 import com.android.settingslib.bluetooth.BluetoothUtils;
25 import com.android.settingslib.bluetooth.HearingAidProfile;
26 import com.android.settingslib.bluetooth.LocalBluetoothManager;
27 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
28 
29 /**
30  * PhoneMediaDevice extends MediaDevice to represents Phone device.
31  */
32 public class PhoneMediaDevice extends MediaDevice {
33 
34     private static final String TAG = "PhoneMediaDevice";
35 
36     public static final String ID = "phone_media_device_id_1";
37 
38     private LocalBluetoothProfileManager mProfileManager;
39     private LocalBluetoothManager mLocalBluetoothManager;
40     private String mSummary = "";
41 
PhoneMediaDevice(Context context, LocalBluetoothManager localBluetoothManager)42     PhoneMediaDevice(Context context, LocalBluetoothManager localBluetoothManager) {
43         super(context, MediaDeviceType.TYPE_PHONE_DEVICE);
44 
45         mLocalBluetoothManager = localBluetoothManager;
46         mProfileManager = mLocalBluetoothManager.getProfileManager();
47         initDeviceRecord();
48     }
49 
50     @Override
getName()51     public String getName() {
52         return mContext.getString(R.string.media_transfer_this_device_name);
53     }
54 
55     @Override
getSummary()56     public String getSummary() {
57         return mSummary;
58     }
59 
60     @Override
getIcon()61     public Drawable getIcon() {
62         return BluetoothUtils.buildBtRainbowDrawable(mContext,
63                 mContext.getDrawable(R.drawable.ic_smartphone), getId().hashCode());
64     }
65 
66     @Override
getId()67     public String getId() {
68         return ID;
69     }
70 
71     @Override
connect()72     public boolean connect() {
73         final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
74         final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
75 
76         // Some device may not have HearingAidProfile, consider all situation to set active device.
77         boolean isConnected = false;
78         if (hapProfile != null && a2dpProfile != null) {
79             isConnected = hapProfile.setActiveDevice(null) && a2dpProfile.setActiveDevice(null);
80         } else if (a2dpProfile != null) {
81             isConnected = a2dpProfile.setActiveDevice(null);
82         } else if (hapProfile != null) {
83             isConnected = hapProfile.setActiveDevice(null);
84         }
85         updateSummary(isConnected);
86         setConnectedRecord();
87 
88         Log.d(TAG, "connect() device : " + getName() + ", is selected : " + isConnected);
89         return isConnected;
90     }
91 
92     @Override
disconnect()93     public void disconnect() {
94         updateSummary(false);
95     }
96 
97     @Override
isConnected()98     public boolean isConnected() {
99         return true;
100     }
101 
102     /**
103      * According current active device is {@link PhoneMediaDevice} or not to update summary.
104      */
updateSummary(boolean isActive)105     public void updateSummary(boolean isActive) {
106         mSummary = isActive
107                 ? mContext.getString(R.string.bluetooth_active_no_battery_level)
108                 : "";
109     }
110 }
111