1 /*
2  * Copyright (C) 2023 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.compatibility.common.deviceinfo;
17 
18 import android.content.ComponentName;
19 
20 import com.android.compatibility.common.util.DeviceInfoStore;
21 
22 /** DefaultAppDeviceInfo collector. */
23 public final class DefaultAppDeviceInfo extends DeviceInfo {
24 
25     private static final String CONFIG_QR_CODE_COMPONENT = "config_defaultQrCodeComponent";
26     private static final String DEFAULT_QR_CODE_COMPONENT_CLASS_NAME =
27             "default_qr_code_component_class_name";
28 
29     @Override
collectDeviceInfo(DeviceInfoStore store)30     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
31         final ComponentName defaultQrCodeComponent = getDefaultQrCodeComponent();
32         String defaultQrCodeComponentClassName = "";
33         if (defaultQrCodeComponent != null) {
34             defaultQrCodeComponentClassName = defaultQrCodeComponent.getClassName();
35         }
36         store.addResult(DEFAULT_QR_CODE_COMPONENT_CLASS_NAME, defaultQrCodeComponentClassName);
37     }
38 
getDefaultQrCodeComponent()39     private ComponentName getDefaultQrCodeComponent() {
40         try {
41             final String defaultQrCodeComponent = getRawDeviceConfig(CONFIG_QR_CODE_COMPONENT);
42             return ComponentName.unflattenFromString(defaultQrCodeComponent);
43         } catch (RuntimeException e) {
44             return null;
45         }
46     }
47 
48     /** Returns the value of a device configuration setting available in android.internal.R.* */
getRawDeviceConfig(String name)49     private String getRawDeviceConfig(String name) {
50         return getContext().getResources().getString(getDeviceResourcesIdentifier(name, "string"));
51     }
52 
getDeviceResourcesIdentifier(String name, String type)53     private int getDeviceResourcesIdentifier(String name, String type) {
54         return getContext().getResources().getIdentifier(name, type, "android");
55     }
56 }
57