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.content.ComponentName;
     20 import android.content.Intent;
     21 import android.content.pm.PackageManager;
     22 import android.content.pm.ResolveInfo;
     23 import android.os.Build;
     24 import android.os.Bundle;
     25 import android.os.SystemClock;
     26 import android.os.SystemProperties;
     27 import android.preference.Preference;
     28 import android.preference.PreferenceActivity;
     29 import android.preference.PreferenceGroup;
     30 import android.preference.PreferenceScreen;
     31 import android.provider.Settings;
     32 import android.util.Log;
     33 import android.view.MotionEvent;
     34 
     35 import java.io.BufferedReader;
     36 import java.io.FileReader;
     37 import java.io.IOException;
     38 import java.util.List;
     39 import java.util.regex.Matcher;
     40 import java.util.regex.Pattern;
     41 
     42 public class DeviceInfoSettings extends PreferenceActivity {
     43     private static final String TAG = "DeviceInfoSettings";
     44 
     45     private static final String KEY_CONTAINER = "container";
     46     private static final String KEY_TEAM = "team";
     47     private static final String KEY_CONTRIBUTORS = "contributors";
     48     private static final String KEY_TERMS = "terms";
     49     private static final String KEY_LICENSE = "license";
     50     private static final String KEY_COPYRIGHT = "copyright";
     51     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
     52     private static final String PROPERTY_URL_SAFETYLEGAL = "ro.url.safetylegal";
     53     private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
     54 
     55     long[] mHits = new long[3];
     56 
     57     @Override
     58     protected void onCreate(Bundle icicle) {
     59         super.onCreate(icicle);
     60 
     61         addPreferencesFromResource(R.xml.device_info_settings);
     62 
     63         // If we don't have an IME tutorial, remove that option
     64         String currentIme = Settings.Secure.getString(getContentResolver(),
     65                 Settings.Secure.DEFAULT_INPUT_METHOD);
     66         ComponentName component = ComponentName.unflattenFromString(currentIme);
     67         Intent imeIntent = new Intent(component.getPackageName() + ".tutorial");
     68         PackageManager pm = getPackageManager();
     69         List<ResolveInfo> tutorials = pm.queryIntentActivities(imeIntent, 0);
     70         if(tutorials == null || tutorials.isEmpty()) {
     71             getPreferenceScreen().removePreference(findPreference("system_tutorial"));
     72         }
     73 
     74         setStringSummary("firmware_version", Build.VERSION.RELEASE);
     75         findPreference("firmware_version").setEnabled(true);
     76         setValueSummary("baseband_version", "gsm.version.baseband");
     77         setStringSummary("device_model", Build.MODEL);
     78         setStringSummary("build_number", Build.DISPLAY);
     79         findPreference("kernel_version").setSummary(getFormattedKernelVersion());
     80 
     81         // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
     82         removePreferenceIfPropertyMissing(getPreferenceScreen(), "safetylegal",
     83                 PROPERTY_URL_SAFETYLEGAL);
     84 
     85         /*
     86          * Settings is a generic app and should not contain any device-specific
     87          * info.
     88          */
     89 
     90         // These are contained in the "container" preference group
     91         PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
     92         Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_TERMS,
     93                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
     94         Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_LICENSE,
     95                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
     96         Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_COPYRIGHT,
     97                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
     98         Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_TEAM,
     99                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    100 
    101         // These are contained by the root preference screen
    102         parentPreference = getPreferenceScreen();
    103         Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference,
    104                 KEY_SYSTEM_UPDATE_SETTINGS,
    105                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    106         Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_CONTRIBUTORS,
    107                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
    108 
    109         // Read platform settings for additional system update setting
    110         boolean mUpdateSettingAvailable =
    111                 getResources().getBoolean(R.bool.config_additional_system_update_setting_enable);
    112 
    113         if(mUpdateSettingAvailable == false) {
    114             getPreferenceScreen().removePreference(findPreference(KEY_UPDATE_SETTING));
    115         }
    116     }
    117 
    118     @Override
    119     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    120         if (preference.getKey().equals("firmware_version")) {
    121             System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
    122             mHits[mHits.length-1] = SystemClock.uptimeMillis();
    123             if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
    124                 Intent intent = new Intent(Intent.ACTION_MAIN);
    125                 intent.setClassName("android",
    126                         com.android.internal.app.PlatLogoActivity.class.getName());
    127                 try {
    128                     startActivity(intent);
    129                 } catch (Exception e) {
    130                 }
    131             }
    132         }
    133         return super.onPreferenceTreeClick(preferenceScreen, preference);
    134     }
    135 
    136     private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
    137             String preference, String property ) {
    138         if (SystemProperties.get(property).equals(""))
    139         {
    140             // Property is missing so remove preference from group
    141             try {
    142                 preferenceGroup.removePreference(findPreference(preference));
    143             } catch (RuntimeException e) {
    144                 Log.d(TAG, "Property '" + property + "' missing and no '"
    145                         + preference + "' preference");
    146             }
    147         }
    148     }
    149 
    150     private void setStringSummary(String preference, String value) {
    151         try {
    152             findPreference(preference).setSummary(value);
    153         } catch (RuntimeException e) {
    154             findPreference(preference).setSummary(
    155                 getResources().getString(R.string.device_info_default));
    156         }
    157     }
    158 
    159     private void setValueSummary(String preference, String property) {
    160         try {
    161             findPreference(preference).setSummary(
    162                     SystemProperties.get(property,
    163                             getResources().getString(R.string.device_info_default)));
    164         } catch (RuntimeException e) {
    165 
    166         }
    167     }
    168 
    169     private String getFormattedKernelVersion() {
    170         String procVersionStr;
    171 
    172         try {
    173             BufferedReader reader = new BufferedReader(new FileReader("/proc/version"), 256);
    174             try {
    175                 procVersionStr = reader.readLine();
    176             } finally {
    177                 reader.close();
    178             }
    179 
    180             final String PROC_VERSION_REGEX =
    181                 "\\w+\\s+" + /* ignore: Linux */
    182                 "\\w+\\s+" + /* ignore: version */
    183                 "([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
    184                 "\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+" + /* group 2: (xxxxxx (at) xxxxx.constant) */
    185                 "\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\s+" + /* ignore: (gcc ..) */
    186                 "([^\\s]+)\\s+" + /* group 3: #26 */
    187                 "(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
    188                 "(.+)"; /* group 4: date */
    189 
    190             Pattern p = Pattern.compile(PROC_VERSION_REGEX);
    191             Matcher m = p.matcher(procVersionStr);
    192 
    193             if (!m.matches()) {
    194                 Log.e(TAG, "Regex did not match on /proc/version: " + procVersionStr);
    195                 return "Unavailable";
    196             } else if (m.groupCount() < 4) {
    197                 Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount()
    198                         + " groups");
    199                 return "Unavailable";
    200             } else {
    201                 return (new StringBuilder(m.group(1)).append("\n").append(
    202                         m.group(2)).append(" ").append(m.group(3)).append("\n")
    203                         .append(m.group(4))).toString();
    204             }
    205         } catch (IOException e) {
    206             Log.e(TAG,
    207                 "IO Exception when getting kernel version for Device Info screen",
    208                 e);
    209 
    210             return "Unavailable";
    211         }
    212     }
    213 
    214 }
    215