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 
17 package com.android.settingslib.deviceinfo;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.support.annotation.VisibleForTesting;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceScreen;
24 import android.text.TextUtils;
25 
26 import com.android.settingslib.core.AbstractPreferenceController;
27 
28 /**
29  * Preference controller for displaying device serial number. Wraps {@link Build#getSerial()}.
30  */
31 public class AbstractSerialNumberPreferenceController extends AbstractPreferenceController {
32 
33     @VisibleForTesting
34     static final String KEY_SERIAL_NUMBER = "serial_number";
35 
36     private final String mSerialNumber;
37 
AbstractSerialNumberPreferenceController(Context context)38     public AbstractSerialNumberPreferenceController(Context context) {
39         this(context, Build.getSerial());
40     }
41 
42     @VisibleForTesting
AbstractSerialNumberPreferenceController(Context context, String serialNumber)43     AbstractSerialNumberPreferenceController(Context context, String serialNumber) {
44         super(context);
45         mSerialNumber = serialNumber;
46     }
47 
48     @Override
isAvailable()49     public boolean isAvailable() {
50         return !TextUtils.isEmpty(mSerialNumber);
51     }
52 
53     @Override
displayPreference(PreferenceScreen screen)54     public void displayPreference(PreferenceScreen screen) {
55         super.displayPreference(screen);
56         final Preference pref = screen.findPreference(KEY_SERIAL_NUMBER);
57         if (pref != null) {
58             pref.setSummary(mSerialNumber);
59         }
60     }
61 
62     @Override
getPreferenceKey()63     public String getPreferenceKey() {
64         return KEY_SERIAL_NUMBER;
65     }
66 }
67