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.content.pm.ApplicationInfo;
     23 import android.content.pm.PackageManager;
     24 import android.content.pm.ResolveInfo;
     25 import android.os.Binder;
     26 import android.os.Build;
     27 import android.os.Bundle;
     28 import android.os.Parcel;
     29 import android.os.RemoteException;
     30 import android.os.SELinux;
     31 import android.os.SystemClock;
     32 import android.os.SystemProperties;
     33 import android.os.UserHandle;
     34 import android.os.UserManager;
     35 import android.preference.Preference;
     36 import android.preference.PreferenceGroup;
     37 import android.preference.PreferenceScreen;
     38 import android.provider.SearchIndexableResource;
     39 import android.provider.Settings;
     40 import android.text.TextUtils;
     41 import android.util.Log;
     42 import android.widget.Toast;
     43 import com.android.settings.search.BaseSearchIndexProvider;
     44 import com.android.settings.search.Index;
     45 import com.android.settings.search.Indexable;
     46 
     47 import java.io.BufferedReader;
     48 import java.io.FileReader;
     49 import java.io.IOException;
     50 import java.util.ArrayList;
     51 import java.util.Arrays;
     52 import java.util.List;
     53 import java.util.regex.Matcher;
     54 import java.util.regex.Pattern;
     55 
     56 public class DeviceInfoSettings extends SettingsPreferenceFragment implements Indexable {
     57 
     58     private static final String LOG_TAG = "DeviceInfoSettings";
     59     private static final String FILENAME_PROC_VERSION = "/proc/version";
     60     private static final String FILENAME_MSV = "/sys/board_properties/soc/msv";
     61 
     62     private static final String KEY_CONTAINER = "container";
     63     private static final String KEY_REGULATORY_INFO = "regulatory_info";
     64     private static final String KEY_TERMS = "terms";
     65     private static final String KEY_LICENSE = "license";
     66     private static final String KEY_COPYRIGHT = "copyright";
     67     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
     68     private static final String PROPERTY_URL_SAFETYLEGAL = "ro.url.safetylegal";
     69     private static final String PROPERTY_SELINUX_STATUS = "ro.build.selinux";
     70     private static final String KEY_KERNEL_VERSION = "kernel_version";
     71     private static final String KEY_BUILD_NUMBER = "build_number";
     72     private static final String KEY_DEVICE_MODEL = "device_model";
     73     private static final String KEY_SELINUX_STATUS = "selinux_status";
     74     private static final String KEY_BASEBAND_VERSION = "baseband_version";
     75     private static final String KEY_FIRMWARE_VERSION = "firmware_version";
     76     private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
     77     private static final String KEY_EQUIPMENT_ID = "fcc_equipment_id";
     78     private static final String PROPERTY_EQUIPMENT_ID = "ro.ril.fccid";
     79     private static final String KEY_DEVICE_FEEDBACK = "device_feedback";
     80     private static final String KEY_SAFETY_LEGAL = "safetylegal";
     81 
     82     static final int TAPS_TO_BE_A_DEVELOPER = 7;
     83 
     84     long[] mHits = new long[3];
     85     int mDevHitCountdown;
     86     Toast mDevHitToast;
     87 
     88     @Override
     89     public void onCreate(Bundle icicle) {
     90         super.onCreate(icicle);
     91 
     92         addPreferencesFromResource(R.xml.device_info_settings);
     93 
     94         setStringSummary(KEY_FIRMWARE_VERSION, Build.VERSION.RELEASE);
     95         findPreference(KEY_FIRMWARE_VERSION).setEnabled(true);
     96         setValueSummary(KEY_BASEBAND_VERSION, "gsm.version.baseband");
     97         setStringSummary(KEY_DEVICE_MODEL, Build.MODEL + getMsvSuffix());
     98         setValueSummary(KEY_EQUIPMENT_ID, PROPERTY_EQUIPMENT_ID);
     99         setStringSummary(KEY_DEVICE_MODEL, Build.MODEL);
    100         setStringSummary(KEY_BUILD_NUMBER, Build.DISPLAY);
    101         findPreference(KEY_BUILD_NUMBER).setEnabled(true);
    102         findPreference(KEY_KERNEL_VERSION).setSummary(getFormattedKernelVersion());
    103 
    104         if (!SELinux.isSELinuxEnabled()) {
    105             String status = getResources().getString(R.string.selinux_status_disabled);
    106             setStringSummary(KEY_SELINUX_STATUS, status);
    107         } else if (!SELinux.isSELinuxEnforced()) {
    108             String status = getResources().getString(R.string.selinux_status_permissive);
    109             setStringSummary(KEY_SELINUX_STATUS, status);
    110         }
    111 
    112         // Remove selinux information if property is not present
    113         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_SELINUX_STATUS,
    114                 PROPERTY_SELINUX_STATUS);
    115 
    116         // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
    117         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_SAFETY_LEGAL,
    118                 PROPERTY_URL_SAFETYLEGAL);
    119 
    120         // Remove Equipment id preference if FCC ID is not set by RIL
    121         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_EQUIPMENT_ID,
    122                 PROPERTY_EQUIPMENT_ID);
    123 
    124         // Remove Baseband version if wifi-only device
    125         if (Utils.isWifiOnly(getActivity())) {
    126             getPreferenceScreen().removePreference(findPreference(KEY_BASEBAND_VERSION));
    127         }
    128 
    129         // Dont show feedback option if there is no reporter.
    130         if (TextUtils.isEmpty(getFeedbackReporterPackage(getActivity()))) {
    131             getPreferenceScreen().removePreference(findPreference(KEY_DEVICE_FEEDBACK));
    132         }
    133 
    134         /*
    135          * Settings is a generic app and should not contain any device-specific
    136          * info.
    137          */
    138         final Activity act = getActivity();
    139         // These are contained in the "container" preference group
    140         PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
    141         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
    142                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    143         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
    144                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    145         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
    146                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    147 
    148         // These are contained by the root preference screen
    149         parentPreference = getPreferenceScreen();
    150         if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
    151             Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference,
    152                     KEY_SYSTEM_UPDATE_SETTINGS,
    153                     Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    154         } else {
    155             // Remove for secondary users
    156             removePreference(KEY_SYSTEM_UPDATE_SETTINGS);
    157         }
    158 
    159         // Read platform settings for additional system update setting
    160         removePreferenceIfBoolFalse(KEY_UPDATE_SETTING,
    161                 R.bool.config_additional_system_update_setting_enable);
    162 
    163         // Remove regulatory information if none present.
    164         final Intent intent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
    165         if (getPackageManager().queryIntentActivities(intent, 0).isEmpty()) {
    166             Preference pref = findPreference(KEY_REGULATORY_INFO);
    167             if (pref != null) {
    168                 getPreferenceScreen().removePreference(pref);
    169             }
    170         }
    171     }
    172 
    173     @Override
    174     public void onResume() {
    175         super.onResume();
    176         mDevHitCountdown = getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
    177                 Context.MODE_PRIVATE).getBoolean(DevelopmentSettings.PREF_SHOW,
    178                         android.os.Build.TYPE.equals("eng")) ? -1 : TAPS_TO_BE_A_DEVELOPER;
    179         mDevHitToast = null;
    180     }
    181 
    182     @Override
    183     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    184         if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
    185             System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
    186             mHits[mHits.length-1] = SystemClock.uptimeMillis();
    187             if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
    188                 Intent intent = new Intent(Intent.ACTION_MAIN);
    189                 intent.setClassName("android",
    190                         com.android.internal.app.PlatLogoActivity.class.getName());
    191                 try {
    192                     startActivity(intent);
    193                 } catch (Exception e) {
    194                     Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
    195                 }
    196             }
    197         } else if (preference.getKey().equals(KEY_BUILD_NUMBER)) {
    198             // Don't enable developer options for secondary users.
    199             if (UserHandle.myUserId() != UserHandle.USER_OWNER) return true;
    200 
    201             final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
    202             if (um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)) return true;
    203 
    204             if (mDevHitCountdown > 0) {
    205                 mDevHitCountdown--;
    206                 if (mDevHitCountdown == 0) {
    207                     getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
    208                             Context.MODE_PRIVATE).edit().putBoolean(
    209                                     DevelopmentSettings.PREF_SHOW, true).apply();
    210                     if (mDevHitToast != null) {
    211                         mDevHitToast.cancel();
    212                     }
    213                     mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_on,
    214                             Toast.LENGTH_LONG);
    215                     mDevHitToast.show();
    216                     // This is good time to index the Developer Options
    217                     Index.getInstance(
    218                             getActivity().getApplicationContext()).updateFromClassNameResource(
    219                                     DevelopmentSettings.class.getName(), true, true);
    220 
    221                 } else if (mDevHitCountdown > 0
    222                         && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER-2)) {
    223                     if (mDevHitToast != null) {
    224                         mDevHitToast.cancel();
    225                     }
    226                     mDevHitToast = Toast.makeText(getActivity(), getResources().getQuantityString(
    227                             R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown),
    228                             Toast.LENGTH_SHORT);
    229                     mDevHitToast.show();
    230                 }
    231             } else if (mDevHitCountdown < 0) {
    232                 if (mDevHitToast != null) {
    233                     mDevHitToast.cancel();
    234                 }
    235                 mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_already,
    236                         Toast.LENGTH_LONG);
    237                 mDevHitToast.show();
    238             }
    239         } else if (preference.getKey().equals(KEY_DEVICE_FEEDBACK)) {
    240             sendFeedback();
    241         }
    242         return super.onPreferenceTreeClick(preferenceScreen, preference);
    243     }
    244 
    245     private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
    246             String preference, String property ) {
    247         if (SystemProperties.get(property).equals("")) {
    248             // Property is missing so remove preference from group
    249             try {
    250                 preferenceGroup.removePreference(findPreference(preference));
    251             } catch (RuntimeException e) {
    252                 Log.d(LOG_TAG, "Property '" + property + "' missing and no '"
    253                         + preference + "' preference");
    254             }
    255         }
    256     }
    257 
    258     private void removePreferenceIfBoolFalse(String preference, int resId) {
    259         if (!getResources().getBoolean(resId)) {
    260             Preference pref = findPreference(preference);
    261             if (pref != null) {
    262                 getPreferenceScreen().removePreference(pref);
    263             }
    264         }
    265     }
    266 
    267     private void setStringSummary(String preference, String value) {
    268         try {
    269             findPreference(preference).setSummary(value);
    270         } catch (RuntimeException e) {
    271             findPreference(preference).setSummary(
    272                 getResources().getString(R.string.device_info_default));
    273         }
    274     }
    275 
    276     private void setValueSummary(String preference, String property) {
    277         try {
    278             findPreference(preference).setSummary(
    279                     SystemProperties.get(property,
    280                             getResources().getString(R.string.device_info_default)));
    281         } catch (RuntimeException e) {
    282             // No recovery
    283         }
    284     }
    285 
    286     private void sendFeedback() {
    287         String reporterPackage = getFeedbackReporterPackage(getActivity());
    288         if (TextUtils.isEmpty(reporterPackage)) {
    289             return;
    290         }
    291         Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
    292         intent.setPackage(reporterPackage);
    293         startActivityForResult(intent, 0);
    294     }
    295 
    296     /**
    297      * Reads a line from the specified file.
    298      * @param filename the file to read from
    299      * @return the first line, if any.
    300      * @throws IOException if the file couldn't be read
    301      */
    302     private static String readLine(String filename) throws IOException {
    303         BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
    304         try {
    305             return reader.readLine();
    306         } finally {
    307             reader.close();
    308         }
    309     }
    310 
    311     public static String getFormattedKernelVersion() {
    312         try {
    313             return formatKernelVersion(readLine(FILENAME_PROC_VERSION));
    314 
    315         } catch (IOException e) {
    316             Log.e(LOG_TAG,
    317                 "IO Exception when getting kernel version for Device Info screen",
    318                 e);
    319 
    320             return "Unavailable";
    321         }
    322     }
    323 
    324     public static String formatKernelVersion(String rawKernelVersion) {
    325         // Example (see tests for more):
    326         // Linux version 3.0.31-g6fb96c9 (android-build (at) xxx.xxx.xxx.xxx.com) \
    327         //     (gcc version 4.6.x-xxx 20120106 (prerelease) (GCC) ) #1 SMP PREEMPT \
    328         //     Thu Jun 28 11:02:39 PDT 2012
    329 
    330         final String PROC_VERSION_REGEX =
    331             "Linux version (\\S+) " + /* group 1: "3.0.31-g6fb96c9" */
    332             "\\((\\S+?)\\) " +        /* group 2: "x (at) y.com" (kernel builder) */
    333             "(?:\\(gcc.+? \\)) " +    /* ignore: GCC version information */
    334             "(#\\d+) " +              /* group 3: "#1" */
    335             "(?:.*?)?" +              /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
    336             "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 4: "Thu Jun 28 11:02:39 PDT 2012" */
    337 
    338         Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
    339         if (!m.matches()) {
    340             Log.e(LOG_TAG, "Regex did not match on /proc/version: " + rawKernelVersion);
    341             return "Unavailable";
    342         } else if (m.groupCount() < 4) {
    343             Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()
    344                     + " groups");
    345             return "Unavailable";
    346         }
    347         return m.group(1) + "\n" +                 // 3.0.31-g6fb96c9
    348             m.group(2) + " " + m.group(3) + "\n" + // x (at) y.com #1
    349             m.group(4);                            // Thu Jun 28 11:02:39 PDT 2012
    350     }
    351 
    352     /**
    353      * Returns " (ENGINEERING)" if the msv file has a zero value, else returns "".
    354      * @return a string to append to the model number description.
    355      */
    356     private String getMsvSuffix() {
    357         // Production devices should have a non-zero value. If we can't read it, assume it's a
    358         // production device so that we don't accidentally show that it's an ENGINEERING device.
    359         try {
    360             String msv = readLine(FILENAME_MSV);
    361             // Parse as a hex number. If it evaluates to a zero, then it's an engineering build.
    362             if (Long.parseLong(msv, 16) == 0) {
    363                 return " (ENGINEERING)";
    364             }
    365         } catch (IOException ioe) {
    366             // Fail quietly, as the file may not exist on some devices.
    367         } catch (NumberFormatException nfe) {
    368             // Fail quietly, returning empty string should be sufficient
    369         }
    370         return "";
    371     }
    372 
    373     private static String getFeedbackReporterPackage(Context context) {
    374         final String feedbackReporter =
    375                 context.getResources().getString(R.string.oem_preferred_feedback_reporter);
    376         if (TextUtils.isEmpty(feedbackReporter)) {
    377             // Reporter not configured. Return.
    378             return feedbackReporter;
    379         }
    380         // Additional checks to ensure the reporter is on system image, and reporter is
    381         // configured to listen to the intent. Otherwise, dont show the "send feedback" option.
    382         final Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
    383 
    384         PackageManager pm = context.getPackageManager();
    385         List<ResolveInfo> resolvedPackages =
    386                 pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
    387         for (ResolveInfo info : resolvedPackages) {
    388             if (info.activityInfo != null) {
    389                 if (!TextUtils.isEmpty(info.activityInfo.packageName)) {
    390                     try {
    391                         ApplicationInfo ai = pm.getApplicationInfo(info.activityInfo.packageName, 0);
    392                         if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
    393                             // Package is on the system image
    394                             if (TextUtils.equals(
    395                                         info.activityInfo.packageName, feedbackReporter)) {
    396                                 return feedbackReporter;
    397                             }
    398                         }
    399                     } catch (PackageManager.NameNotFoundException e) {
    400                          // No need to do anything here.
    401                     }
    402                 }
    403             }
    404         }
    405         return null;
    406     }
    407 
    408     /**
    409      * For Search.
    410      */
    411     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
    412         new BaseSearchIndexProvider() {
    413 
    414             @Override
    415             public List<SearchIndexableResource> getXmlResourcesToIndex(
    416                     Context context, boolean enabled) {
    417                 final SearchIndexableResource sir = new SearchIndexableResource(context);
    418                 sir.xmlResId = R.xml.device_info_settings;
    419                 return Arrays.asList(sir);
    420             }
    421 
    422             @Override
    423             public List<String> getNonIndexableKeys(Context context) {
    424                 final List<String> keys = new ArrayList<String>();
    425                 if (isPropertyMissing(PROPERTY_SELINUX_STATUS)) {
    426                     keys.add(KEY_SELINUX_STATUS);
    427                 }
    428                 if (isPropertyMissing(PROPERTY_URL_SAFETYLEGAL)) {
    429                     keys.add(KEY_SAFETY_LEGAL);
    430                 }
    431                 if (isPropertyMissing(PROPERTY_EQUIPMENT_ID)) {
    432                     keys.add(KEY_EQUIPMENT_ID);
    433                 }
    434                 // Remove Baseband version if wifi-only device
    435                 if (Utils.isWifiOnly(context)) {
    436                     keys.add((KEY_BASEBAND_VERSION));
    437                 }
    438                 // Dont show feedback option if there is no reporter.
    439                 if (TextUtils.isEmpty(getFeedbackReporterPackage(context))) {
    440                     keys.add(KEY_DEVICE_FEEDBACK);
    441                 }
    442                 if (!checkIntentAction(context, "android.settings.TERMS")) {
    443                     keys.add(KEY_TERMS);
    444                 }
    445                 if (!checkIntentAction(context, "android.settings.LICENSE")) {
    446                     keys.add(KEY_LICENSE);
    447                 }
    448                 if (!checkIntentAction(context, "android.settings.COPYRIGHT")) {
    449                     keys.add(KEY_COPYRIGHT);
    450                 }
    451                 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
    452                     keys.add(KEY_SYSTEM_UPDATE_SETTINGS);
    453                 }
    454                 if (!context.getResources().getBoolean(
    455                         R.bool.config_additional_system_update_setting_enable)) {
    456                     keys.add(KEY_UPDATE_SETTING);
    457                 }
    458                 return keys;
    459             }
    460 
    461             private boolean isPropertyMissing(String property) {
    462                 return SystemProperties.get(property).equals("");
    463             }
    464 
    465             private boolean checkIntentAction(Context context, String action) {
    466                 final Intent intent = new Intent(action);
    467 
    468                 // Find the activity that is in the system image
    469                 final PackageManager pm = context.getPackageManager();
    470                 final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
    471                 final int listSize = list.size();
    472 
    473                 for (int i = 0; i < listSize; i++) {
    474                     ResolveInfo resolveInfo = list.get(i);
    475                     if ((resolveInfo.activityInfo.applicationInfo.flags &
    476                             ApplicationInfo.FLAG_SYSTEM) != 0) {
    477                         return true;
    478                     }
    479                 }
    480 
    481                 return false;
    482             }
    483         };
    484 
    485 }
    486 
    487