Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2008 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.settings;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Build;
     23 import android.os.Bundle;
     24 import android.os.SELinux;
     25 import android.os.SystemClock;
     26 import android.os.SystemProperties;
     27 import android.os.UserHandle;
     28 import android.preference.Preference;
     29 import android.preference.PreferenceGroup;
     30 import android.preference.PreferenceScreen;
     31 import android.util.Log;
     32 import android.widget.Toast;
     33 
     34 import java.io.BufferedReader;
     35 import java.io.FileReader;
     36 import java.io.IOException;
     37 import java.util.regex.Matcher;
     38 import java.util.regex.Pattern;
     39 
     40 public class DeviceInfoSettings extends RestrictedSettingsFragment {
     41 
     42     private static final String LOG_TAG = "DeviceInfoSettings";
     43 
     44     private static final String FILENAME_PROC_VERSION = "/proc/version";
     45     private static final String FILENAME_MSV = "/sys/board_properties/soc/msv";
     46 
     47     private static final String KEY_CONTAINER = "container";
     48     private static final String KEY_TEAM = "team";
     49     private static final String KEY_CONTRIBUTORS = "contributors";
     50     private static final String KEY_REGULATORY_INFO = "regulatory_info";
     51     private static final String KEY_TERMS = "terms";
     52     private static final String KEY_LICENSE = "license";
     53     private static final String KEY_COPYRIGHT = "copyright";
     54     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
     55     private static final String PROPERTY_URL_SAFETYLEGAL = "ro.url.safetylegal";
     56     private static final String PROPERTY_SELINUX_STATUS = "ro.build.selinux";
     57     private static final String KEY_KERNEL_VERSION = "kernel_version";
     58     private static final String KEY_BUILD_NUMBER = "build_number";
     59     private static final String KEY_DEVICE_MODEL = "device_model";
     60     private static final String KEY_SELINUX_STATUS = "selinux_status";
     61     private static final String KEY_BASEBAND_VERSION = "baseband_version";
     62     private static final String KEY_FIRMWARE_VERSION = "firmware_version";
     63     private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
     64     private static final String KEY_EQUIPMENT_ID = "fcc_equipment_id";
     65     private static final String PROPERTY_EQUIPMENT_ID = "ro.ril.fccid";
     66 
     67     static final int TAPS_TO_BE_A_DEVELOPER = 7;
     68 
     69     long[] mHits = new long[3];
     70     int mDevHitCountdown;
     71     Toast mDevHitToast;
     72 
     73     public DeviceInfoSettings() {
     74         super(null /* Don't PIN protect the entire screen */);
     75     }
     76 
     77     @Override
     78     public void onCreate(Bundle icicle) {
     79         super.onCreate(icicle);
     80 
     81         addPreferencesFromResource(R.xml.device_info_settings);
     82 
     83         // We only call ensurePinRestrictedPreference() when mDevHitCountdown == 0.
     84         // This will keep us from entering developer mode without a PIN.
     85         protectByRestrictions(KEY_BUILD_NUMBER);
     86 
     87         setStringSummary(KEY_FIRMWARE_VERSION, Build.VERSION.RELEASE);
     88         findPreference(KEY_FIRMWARE_VERSION).setEnabled(true);
     89         setValueSummary(KEY_BASEBAND_VERSION, "gsm.version.baseband");
     90         setStringSummary(KEY_DEVICE_MODEL, Build.MODEL + getMsvSuffix());
     91         setValueSummary(KEY_EQUIPMENT_ID, PROPERTY_EQUIPMENT_ID);
     92         setStringSummary(KEY_DEVICE_MODEL, Build.MODEL);
     93         setStringSummary(KEY_BUILD_NUMBER, Build.DISPLAY);
     94         findPreference(KEY_BUILD_NUMBER).setEnabled(true);
     95         findPreference(KEY_KERNEL_VERSION).setSummary(getFormattedKernelVersion());
     96 
     97         if (!SELinux.isSELinuxEnabled()) {
     98             String status = getResources().getString(R.string.selinux_status_disabled);
     99             setStringSummary(KEY_SELINUX_STATUS, status);
    100         } else if (!SELinux.isSELinuxEnforced()) {
    101             String status = getResources().getString(R.string.selinux_status_permissive);
    102             setStringSummary(KEY_SELINUX_STATUS, status);
    103         }
    104 
    105         // Remove selinux information if property is not present
    106         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_SELINUX_STATUS,
    107                 PROPERTY_SELINUX_STATUS);
    108 
    109         // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
    110         removePreferenceIfPropertyMissing(getPreferenceScreen(), "safetylegal",
    111                 PROPERTY_URL_SAFETYLEGAL);
    112 
    113         // Remove Equipment id preference if FCC ID is not set by RIL
    114         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_EQUIPMENT_ID,
    115                 PROPERTY_EQUIPMENT_ID);
    116 
    117         // Remove Baseband version if wifi-only device
    118         if (Utils.isWifiOnly(getActivity())) {
    119             getPreferenceScreen().removePreference(findPreference(KEY_BASEBAND_VERSION));
    120         }
    121 
    122         /*
    123          * Settings is a generic app and should not contain any device-specific
    124          * info.
    125          */
    126         final Activity act = getActivity();
    127         // These are contained in the "container" preference group
    128         PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
    129         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
    130                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    131         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
    132                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    133         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
    134                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    135         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TEAM,
    136                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    137 
    138         // These are contained by the root preference screen
    139         parentPreference = getPreferenceScreen();
    140         if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
    141             Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference,
    142                     KEY_SYSTEM_UPDATE_SETTINGS,
    143                     Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    144         } else {
    145             // Remove for secondary users
    146             removePreference(KEY_SYSTEM_UPDATE_SETTINGS);
    147         }
    148         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_CONTRIBUTORS,
    149                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    150 
    151         // Read platform settings for additional system update setting
    152         removePreferenceIfBoolFalse(KEY_UPDATE_SETTING,
    153                 R.bool.config_additional_system_update_setting_enable);
    154 
    155         // Remove regulatory information if not enabled.
    156         removePreferenceIfBoolFalse(KEY_REGULATORY_INFO,
    157                 R.bool.config_show_regulatory_info);
    158     }
    159 
    160     @Override
    161     public void onResume() {
    162         super.onResume();
    163         mDevHitCountdown = getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
    164                 Context.MODE_PRIVATE).getBoolean(DevelopmentSettings.PREF_SHOW,
    165                         android.os.Build.TYPE.equals("eng")) ? -1 : TAPS_TO_BE_A_DEVELOPER;
    166         mDevHitToast = null;
    167     }
    168 
    169     @Override
    170     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    171         if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
    172             System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
    173             mHits[mHits.length-1] = SystemClock.uptimeMillis();
    174             if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
    175                 Intent intent = new Intent(Intent.ACTION_MAIN);
    176                 intent.setClassName("android",
    177                         com.android.internal.app.PlatLogoActivity.class.getName());
    178                 try {
    179                     startActivity(intent);
    180                 } catch (Exception e) {
    181                     Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
    182                 }
    183             }
    184         } else if (preference.getKey().equals(KEY_BUILD_NUMBER)) {
    185             // Don't enable developer options for secondary users.
    186             if (UserHandle.myUserId() != UserHandle.USER_OWNER) return true;
    187 
    188             if (mDevHitCountdown > 0) {
    189                 if (mDevHitCountdown == 1) {
    190                     if (super.ensurePinRestrictedPreference(preference)) {
    191                         return true;
    192                     }
    193                 }
    194                 mDevHitCountdown--;
    195                 if (mDevHitCountdown == 0) {
    196                     getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
    197                             Context.MODE_PRIVATE).edit().putBoolean(
    198                                     DevelopmentSettings.PREF_SHOW, true).apply();
    199                     if (mDevHitToast != null) {
    200                         mDevHitToast.cancel();
    201                     }
    202                     mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_on,
    203                             Toast.LENGTH_LONG);
    204                     mDevHitToast.show();
    205                 } else if (mDevHitCountdown > 0
    206                         && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER-2)) {
    207                     if (mDevHitToast != null) {
    208                         mDevHitToast.cancel();
    209                     }
    210                     mDevHitToast = Toast.makeText(getActivity(), getResources().getQuantityString(
    211                             R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown),
    212                             Toast.LENGTH_SHORT);
    213                     mDevHitToast.show();
    214                 }
    215             } else if (mDevHitCountdown < 0) {
    216                 if (mDevHitToast != null) {
    217                     mDevHitToast.cancel();
    218                 }
    219                 mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_already,
    220                         Toast.LENGTH_LONG);
    221                 mDevHitToast.show();
    222             }
    223         }
    224         return super.onPreferenceTreeClick(preferenceScreen, preference);
    225     }
    226 
    227     private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
    228             String preference, String property ) {
    229         if (SystemProperties.get(property).equals("")) {
    230             // Property is missing so remove preference from group
    231             try {
    232                 preferenceGroup.removePreference(findPreference(preference));
    233             } catch (RuntimeException e) {
    234                 Log.d(LOG_TAG, "Property '" + property + "' missing and no '"
    235                         + preference + "' preference");
    236             }
    237         }
    238     }
    239 
    240     private void removePreferenceIfBoolFalse(String preference, int resId) {
    241         if (!getResources().getBoolean(resId)) {
    242             Preference pref = findPreference(preference);
    243             if (pref != null) {
    244                 getPreferenceScreen().removePreference(pref);
    245             }
    246         }
    247     }
    248 
    249     private void setStringSummary(String preference, String value) {
    250         try {
    251             findPreference(preference).setSummary(value);
    252         } catch (RuntimeException e) {
    253             findPreference(preference).setSummary(
    254                 getResources().getString(R.string.device_info_default));
    255         }
    256     }
    257 
    258     private void setValueSummary(String preference, String property) {
    259         try {
    260             findPreference(preference).setSummary(
    261                     SystemProperties.get(property,
    262                             getResources().getString(R.string.device_info_default)));
    263         } catch (RuntimeException e) {
    264             // No recovery
    265         }
    266     }
    267 
    268     /**
    269      * Reads a line from the specified file.
    270      * @param filename the file to read from
    271      * @return the first line, if any.
    272      * @throws IOException if the file couldn't be read
    273      */
    274     private static String readLine(String filename) throws IOException {
    275         BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
    276         try {
    277             return reader.readLine();
    278         } finally {
    279             reader.close();
    280         }
    281     }
    282 
    283     public static String getFormattedKernelVersion() {
    284         try {
    285             return formatKernelVersion(readLine(FILENAME_PROC_VERSION));
    286 
    287         } catch (IOException e) {
    288             Log.e(LOG_TAG,
    289                 "IO Exception when getting kernel version for Device Info screen",
    290                 e);
    291 
    292             return "Unavailable";
    293         }
    294     }
    295 
    296     public static String formatKernelVersion(String rawKernelVersion) {
    297         // Example (see tests for more):
    298         // Linux version 3.0.31-g6fb96c9 (android-build (at) xxx.xxx.xxx.xxx.com) \
    299         //     (gcc version 4.6.x-xxx 20120106 (prerelease) (GCC) ) #1 SMP PREEMPT \
    300         //     Thu Jun 28 11:02:39 PDT 2012
    301 
    302         final String PROC_VERSION_REGEX =
    303             "Linux version (\\S+) " + /* group 1: "3.0.31-g6fb96c9" */
    304             "\\((\\S+?)\\) " +        /* group 2: "x (at) y.com" (kernel builder) */
    305             "(?:\\(gcc.+? \\)) " +    /* ignore: GCC version information */
    306             "(#\\d+) " +              /* group 3: "#1" */
    307             "(?:.*?)?" +              /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
    308             "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 4: "Thu Jun 28 11:02:39 PDT 2012" */
    309 
    310         Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
    311         if (!m.matches()) {
    312             Log.e(LOG_TAG, "Regex did not match on /proc/version: " + rawKernelVersion);
    313             return "Unavailable";
    314         } else if (m.groupCount() < 4) {
    315             Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()
    316                     + " groups");
    317             return "Unavailable";
    318         }
    319         return m.group(1) + "\n" +                 // 3.0.31-g6fb96c9
    320             m.group(2) + " " + m.group(3) + "\n" + // x (at) y.com #1
    321             m.group(4);                            // Thu Jun 28 11:02:39 PDT 2012
    322     }
    323 
    324     /**
    325      * Returns " (ENGINEERING)" if the msv file has a zero value, else returns "".
    326      * @return a string to append to the model number description.
    327      */
    328     private String getMsvSuffix() {
    329         // Production devices should have a non-zero value. If we can't read it, assume it's a
    330         // production device so that we don't accidentally show that it's an ENGINEERING device.
    331         try {
    332             String msv = readLine(FILENAME_MSV);
    333             // Parse as a hex number. If it evaluates to a zero, then it's an engineering build.
    334             if (Long.parseLong(msv, 16) == 0) {
    335                 return " (ENGINEERING)";
    336             }
    337         } catch (IOException ioe) {
    338             // Fail quietly, as the file may not exist on some devices.
    339         } catch (NumberFormatException nfe) {
    340             // Fail quietly, returning empty string should be sufficient
    341         }
    342         return "";
    343     }
    344 }
    345