DeviceInfoSettings.java revision a6b9dcbb161e9d7ede0573397ee22d6c3f0bbcae
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
17package com.android.settings;
18
19import android.content.Intent;
20import android.content.pm.ApplicationInfo;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.os.Build;
24import android.os.Bundle;
25import android.os.SystemProperties;
26import android.preference.Preference;
27import android.preference.PreferenceActivity;
28import android.preference.PreferenceGroup;
29import android.util.Config;
30import android.util.Log;
31
32import java.io.BufferedReader;
33import java.io.FileReader;
34import java.io.IOException;
35import java.util.List;
36import java.util.regex.Matcher;
37import java.util.regex.Pattern;
38
39public class DeviceInfoSettings extends PreferenceActivity {
40
41    private static final String TAG = "DeviceInfoSettings";
42    private static final boolean LOGD = false || Config.LOGD;
43
44    private static final String KEY_CONTAINER = "container";
45    private static final String KEY_TEAM = "team";
46    private static final String KEY_CONTRIBUTORS = "contributors";
47    private static final String KEY_TERMS = "terms";
48    private static final String KEY_LICENSE = "license";
49    private static final String KEY_COPYRIGHT = "copyright";
50    private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
51    private static final String PROPERTY_URL_SAFETYLEGAL = "ro.url.safetylegal";
52
53    @Override
54    protected void onCreate(Bundle icicle) {
55        super.onCreate(icicle);
56
57        addPreferencesFromResource(R.xml.device_info_settings);
58
59        setStringSummary("firmware_version", Build.VERSION.RELEASE);
60        setValueSummary("baseband_version", "gsm.version.baseband");
61        setStringSummary("device_model", Build.MODEL);
62        setStringSummary("build_number", Build.DISPLAY);
63        findPreference("kernel_version").setSummary(getFormattedKernelVersion());
64
65        // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
66        removePreferenceIfPropertyMissing(getPreferenceScreen(), "safetylegal",
67                PROPERTY_URL_SAFETYLEGAL);
68
69        /*
70         * Settings is a generic app and should not contain any device-specific
71         * info.
72         */
73
74        // These are contained in the "container" preference group
75        PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
76        Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_TERMS,
77                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
78        Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_LICENSE,
79                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
80        Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_COPYRIGHT,
81                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
82        Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_TEAM,
83                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
84
85        // These are contained by the root preference screen
86        parentPreference = getPreferenceScreen();
87        Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference,
88                KEY_SYSTEM_UPDATE_SETTINGS,
89                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
90        Utils.updatePreferenceToSpecificActivityOrRemove(this, parentPreference, KEY_CONTRIBUTORS,
91                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
92    }
93
94    private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
95            String preference, String property ) {
96        if (SystemProperties.get(property).equals(""))
97        {
98            // Property is missing so remove preference from group
99            try {
100                preferenceGroup.removePreference(findPreference(preference));
101            } catch (RuntimeException e) {
102                Log.d(TAG, "Property '" + property + "' missing and no '"
103                        + preference + "' preference");
104            }
105        }
106    }
107
108    private void setStringSummary(String preference, String value) {
109        try {
110            findPreference(preference).setSummary(value);
111        } catch (RuntimeException e) {
112            findPreference(preference).setSummary(
113                getResources().getString(R.string.device_info_default));
114        }
115    }
116
117    private void setValueSummary(String preference, String property) {
118        try {
119            findPreference(preference).setSummary(
120                    SystemProperties.get(property,
121                            getResources().getString(R.string.device_info_default)));
122        } catch (RuntimeException e) {
123
124        }
125    }
126
127    private String getFormattedKernelVersion() {
128        String procVersionStr;
129
130        try {
131            BufferedReader reader = new BufferedReader(new FileReader("/proc/version"), 256);
132            try {
133                procVersionStr = reader.readLine();
134            } finally {
135                reader.close();
136            }
137
138            final String PROC_VERSION_REGEX =
139                "\\w+\\s+" + /* ignore: Linux */
140                "\\w+\\s+" + /* ignore: version */
141                "([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
142                "\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+" + /* group 2: (xxxxxx@xxxxx.constant) */
143                "\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\s+" + /* ignore: (gcc ..) */
144                "([^\\s]+)\\s+" + /* group 3: #26 */
145                "(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
146                "(.+)"; /* group 4: date */
147
148            Pattern p = Pattern.compile(PROC_VERSION_REGEX);
149            Matcher m = p.matcher(procVersionStr);
150
151            if (!m.matches()) {
152                Log.e(TAG, "Regex did not match on /proc/version: " + procVersionStr);
153                return "Unavailable";
154            } else if (m.groupCount() < 4) {
155                Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount()
156                        + " groups");
157                return "Unavailable";
158            } else {
159                return (new StringBuilder(m.group(1)).append("\n").append(
160                        m.group(2)).append(" ").append(m.group(3)).append("\n")
161                        .append(m.group(4))).toString();
162            }
163        } catch (IOException e) {
164            Log.e(TAG,
165                "IO Exception when getting kernel version for Device Info screen",
166                e);
167
168            return "Unavailable";
169        }
170    }
171
172}
173