• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.bluetooth.btservice.storage;
18 
19 import android.bluetooth.BluetoothA2dp;
20 import android.bluetooth.BluetoothDevice;
21 import android.bluetooth.BluetoothProfile;
22 
23 import androidx.annotation.NonNull;
24 import androidx.room.Embedded;
25 import androidx.room.Entity;
26 import androidx.room.PrimaryKey;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 @Entity(tableName = "metadata")
32 class Metadata {
33     @PrimaryKey
34     @NonNull
35     private String address;
36 
37     public boolean migrated;
38 
39     @Embedded
40     public ProfilePrioritiesEntity profilePriorities;
41 
42     @Embedded
43     @NonNull
44     public CustomizedMetadataEntity publicMetadata;
45 
46     public int a2dpSupportsOptionalCodecs;
47     public int a2dpOptionalCodecsEnabled;
48 
Metadata(String address)49     Metadata(String address) {
50         this.address = address;
51         migrated = false;
52         profilePriorities = new ProfilePrioritiesEntity();
53         publicMetadata = new CustomizedMetadataEntity();
54         a2dpSupportsOptionalCodecs = BluetoothA2dp.OPTIONAL_CODECS_SUPPORT_UNKNOWN;
55         a2dpOptionalCodecsEnabled = BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN;
56     }
57 
getAddress()58     String getAddress() {
59         return address;
60     }
61 
setProfilePriority(int profile, int priority)62     void setProfilePriority(int profile, int priority) {
63         switch (profile) {
64             case BluetoothProfile.A2DP:
65                 profilePriorities.a2dp_priority = priority;
66                 break;
67             case BluetoothProfile.A2DP_SINK:
68                 profilePriorities.a2dp_sink_priority = priority;
69                 break;
70             case BluetoothProfile.HEADSET:
71                 profilePriorities.hfp_priority = priority;
72                 break;
73             case BluetoothProfile.HEADSET_CLIENT:
74                 profilePriorities.hfp_client_priority = priority;
75                 break;
76             case BluetoothProfile.HID_HOST:
77                 profilePriorities.hid_host_priority = priority;
78                 break;
79             case BluetoothProfile.PAN:
80                 profilePriorities.pan_priority = priority;
81                 break;
82             case BluetoothProfile.PBAP:
83                 profilePriorities.pbap_priority = priority;
84                 break;
85             case BluetoothProfile.PBAP_CLIENT:
86                 profilePriorities.pbap_client_priority = priority;
87                 break;
88             case BluetoothProfile.MAP:
89                 profilePriorities.map_priority = priority;
90                 break;
91             case BluetoothProfile.MAP_CLIENT:
92                 profilePriorities.map_client_priority = priority;
93                 break;
94             case BluetoothProfile.SAP:
95                 profilePriorities.sap_priority = priority;
96                 break;
97             case BluetoothProfile.HEARING_AID:
98                 profilePriorities.hearing_aid_priority = priority;
99                 break;
100             default:
101                 throw new IllegalArgumentException("invalid profile " + profile);
102         }
103     }
104 
getProfilePriority(int profile)105     int getProfilePriority(int profile) {
106         switch (profile) {
107             case BluetoothProfile.A2DP:
108                 return profilePriorities.a2dp_priority;
109             case BluetoothProfile.A2DP_SINK:
110                 return profilePriorities.a2dp_sink_priority;
111             case BluetoothProfile.HEADSET:
112                 return profilePriorities.hfp_priority;
113             case BluetoothProfile.HEADSET_CLIENT:
114                 return profilePriorities.hfp_client_priority;
115             case BluetoothProfile.HID_HOST:
116                 return profilePriorities.hid_host_priority;
117             case BluetoothProfile.PAN:
118                 return profilePriorities.pan_priority;
119             case BluetoothProfile.PBAP:
120                 return profilePriorities.pbap_priority;
121             case BluetoothProfile.PBAP_CLIENT:
122                 return profilePriorities.pbap_client_priority;
123             case BluetoothProfile.MAP:
124                 return profilePriorities.map_priority;
125             case BluetoothProfile.MAP_CLIENT:
126                 return profilePriorities.map_client_priority;
127             case BluetoothProfile.SAP:
128                 return profilePriorities.sap_priority;
129             case BluetoothProfile.HEARING_AID:
130                 return profilePriorities.hearing_aid_priority;
131         }
132         return BluetoothProfile.PRIORITY_UNDEFINED;
133     }
134 
setCustomizedMeta(int key, byte[] value)135     void setCustomizedMeta(int key, byte[] value) {
136         switch (key) {
137             case BluetoothDevice.METADATA_MANUFACTURER_NAME:
138                 publicMetadata.manufacturer_name = value;
139                 break;
140             case BluetoothDevice.METADATA_MODEL_NAME:
141                 publicMetadata.model_name = value;
142                 break;
143             case BluetoothDevice.METADATA_SOFTWARE_VERSION:
144                 publicMetadata.software_version = value;
145                 break;
146             case BluetoothDevice.METADATA_HARDWARE_VERSION:
147                 publicMetadata.hardware_version = value;
148                 break;
149             case BluetoothDevice.METADATA_COMPANION_APP:
150                 publicMetadata.companion_app = value;
151                 break;
152             case BluetoothDevice.METADATA_MAIN_ICON:
153                 publicMetadata.main_icon = value;
154                 break;
155             case BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET:
156                 publicMetadata.is_untethered_headset = value;
157                 break;
158             case BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON:
159                 publicMetadata.untethered_left_icon = value;
160                 break;
161             case BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON:
162                 publicMetadata.untethered_right_icon = value;
163                 break;
164             case BluetoothDevice.METADATA_UNTETHERED_CASE_ICON:
165                 publicMetadata.untethered_case_icon = value;
166                 break;
167             case BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY:
168                 publicMetadata.untethered_left_battery = value;
169                 break;
170             case BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY:
171                 publicMetadata.untethered_right_battery = value;
172                 break;
173             case BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY:
174                 publicMetadata.untethered_case_battery = value;
175                 break;
176             case BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING:
177                 publicMetadata.untethered_left_charging = value;
178                 break;
179             case BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING:
180                 publicMetadata.untethered_right_charging = value;
181                 break;
182             case BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING:
183                 publicMetadata.untethered_case_charging = value;
184                 break;
185             case BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI:
186                 publicMetadata.enhanced_settings_ui_uri = value;
187                 break;
188         }
189     }
190 
getCustomizedMeta(int key)191     byte[] getCustomizedMeta(int key) {
192         byte[] value = null;
193         switch (key) {
194             case BluetoothDevice.METADATA_MANUFACTURER_NAME:
195                 value = publicMetadata.manufacturer_name;
196                 break;
197             case BluetoothDevice.METADATA_MODEL_NAME:
198                 value = publicMetadata.model_name;
199                 break;
200             case BluetoothDevice.METADATA_SOFTWARE_VERSION:
201                 value = publicMetadata.software_version;
202                 break;
203             case BluetoothDevice.METADATA_HARDWARE_VERSION:
204                 value = publicMetadata.hardware_version;
205                 break;
206             case BluetoothDevice.METADATA_COMPANION_APP:
207                 value = publicMetadata.companion_app;
208                 break;
209             case BluetoothDevice.METADATA_MAIN_ICON:
210                 value = publicMetadata.main_icon;
211                 break;
212             case BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET:
213                 value = publicMetadata.is_untethered_headset;
214                 break;
215             case BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON:
216                 value = publicMetadata.untethered_left_icon;
217                 break;
218             case BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON:
219                 value = publicMetadata.untethered_right_icon;
220                 break;
221             case BluetoothDevice.METADATA_UNTETHERED_CASE_ICON:
222                 value = publicMetadata.untethered_case_icon;
223                 break;
224             case BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY:
225                 value = publicMetadata.untethered_left_battery;
226                 break;
227             case BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY:
228                 value = publicMetadata.untethered_right_battery;
229                 break;
230             case BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY:
231                 value = publicMetadata.untethered_case_battery;
232                 break;
233             case BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING:
234                 value = publicMetadata.untethered_left_charging;
235                 break;
236             case BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING:
237                 value = publicMetadata.untethered_right_charging;
238                 break;
239             case BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING:
240                 value = publicMetadata.untethered_case_charging;
241                 break;
242             case BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI:
243                 value = publicMetadata.enhanced_settings_ui_uri;
244                 break;
245         }
246         return value;
247     }
248 
getChangedCustomizedMeta()249     List<Integer> getChangedCustomizedMeta() {
250         List<Integer> list = new ArrayList<>();
251         if (publicMetadata.manufacturer_name != null) {
252             list.add(BluetoothDevice.METADATA_MANUFACTURER_NAME);
253         }
254         if (publicMetadata.model_name != null) {
255             list.add(BluetoothDevice.METADATA_MODEL_NAME);
256         }
257         if (publicMetadata.software_version != null) {
258             list.add(BluetoothDevice.METADATA_SOFTWARE_VERSION);
259         }
260         if (publicMetadata.hardware_version != null) {
261             list.add(BluetoothDevice.METADATA_HARDWARE_VERSION);
262         }
263         if (publicMetadata.companion_app != null) {
264             list.add(BluetoothDevice.METADATA_COMPANION_APP);
265         }
266         if (publicMetadata.main_icon != null) {
267             list.add(BluetoothDevice.METADATA_MAIN_ICON);
268         }
269         if (publicMetadata.is_untethered_headset != null) {
270             list.add(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
271         }
272         if (publicMetadata.untethered_left_icon != null) {
273             list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON);
274         }
275         if (publicMetadata.untethered_right_icon != null) {
276             list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON);
277         }
278         if (publicMetadata.untethered_case_icon != null) {
279             list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_ICON);
280         }
281         if (publicMetadata.untethered_left_battery != null) {
282             list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY);
283         }
284         if (publicMetadata.untethered_right_battery != null) {
285             list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY);
286         }
287         if (publicMetadata.untethered_case_battery != null) {
288             list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY);
289         }
290         if (publicMetadata.untethered_left_charging != null) {
291             list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING);
292         }
293         if (publicMetadata.untethered_right_charging != null) {
294             list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING);
295         }
296         if (publicMetadata.untethered_case_charging != null) {
297             list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING);
298         }
299         if (publicMetadata.enhanced_settings_ui_uri != null) {
300             list.add(BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI);
301         }
302         return list;
303     }
304 }
305