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