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