1 /*
2  * Copyright (C) 2017 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.settings.deviceinfo;
17 
18 import android.content.Context;
19 import android.os.Build;
20 import android.util.Log;
21 
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.BasePreferenceController;
26 import com.android.settingslib.DeviceInfoUtils;
27 
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.FutureTask;
30 
31 public class HardwareInfoPreferenceController extends BasePreferenceController {
32 
33     private static final String TAG = "DeviceModelPrefCtrl";
34 
HardwareInfoPreferenceController(Context context, String key)35     public HardwareInfoPreferenceController(Context context, String key) {
36         super(context, key);
37     }
38 
39     @Override
displayPreference(PreferenceScreen screen)40     public void displayPreference(PreferenceScreen screen) {
41         super.displayPreference(screen);
42     }
43 
44     @Override
getAvailabilityStatus()45     public int getAvailabilityStatus() {
46         return mContext.getResources().getBoolean(R.bool.config_show_device_model)
47                 ? AVAILABLE_UNSEARCHABLE : UNSUPPORTED_ON_DEVICE;
48     }
49 
50     @Override
getSummary()51     public CharSequence getSummary() {
52         return mContext.getResources().getString(R.string.model_summary, getDeviceModel());
53     }
54 
getDeviceModel()55     public static String getDeviceModel() {
56         FutureTask<String> msvSuffixTask = new FutureTask<>(() -> DeviceInfoUtils.getMsvSuffix());
57 
58         msvSuffixTask.run();
59         try {
60             // Wait for msv suffix value.
61             final String msvSuffix = msvSuffixTask.get();
62             return Build.MODEL + msvSuffix;
63         } catch (ExecutionException e) {
64             Log.e(TAG, "Execution error, so we only show model name");
65         } catch (InterruptedException e) {
66             Log.e(TAG, "Interruption error, so we only show model name");
67         }
68         // If we can't get an msv suffix value successfully,
69         // it's better to return model name.
70         return Build.MODEL;
71     }
72 }
73