• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ComponentName;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.os.SystemClock;
27 import android.os.SystemProperties;
28 import android.preference.Preference;
29 import android.preference.PreferenceGroup;
30 import android.preference.PreferenceScreen;
31 import android.provider.Settings;
32 import android.util.Log;
33 
34 import java.io.BufferedReader;
35 import java.io.FileReader;
36 import java.io.IOException;
37 import java.util.List;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40 
41 public class DeviceInfoSettings extends SettingsPreferenceFragment {
42 
43     private static final String LOG_TAG = "DeviceInfoSettings";
44 
45     private static final String FILENAME_PROC_VERSION = "/proc/version";
46     private static final String FILENAME_MSV = "/sys/board_properties/soc/msv";
47 
48     private static final String KEY_CONTAINER = "container";
49     private static final String KEY_TEAM = "team";
50     private static final String KEY_CONTRIBUTORS = "contributors";
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 KEY_KERNEL_VERSION = "kernel_version";
57     private static final String KEY_BUILD_NUMBER = "build_number";
58     private static final String KEY_DEVICE_MODEL = "device_model";
59     private static final String KEY_BASEBAND_VERSION = "baseband_version";
60     private static final String KEY_FIRMWARE_VERSION = "firmware_version";
61 
62     long[] mHits = new long[3];
63 
64     @Override
onCreate(Bundle icicle)65     public void onCreate(Bundle icicle) {
66         super.onCreate(icicle);
67 
68         addPreferencesFromResource(R.xml.device_info_settings);
69 
70         // If we don't have an IME tutorial, remove that option
71         String currentIme = Settings.Secure.getString(getContentResolver(),
72                 Settings.Secure.DEFAULT_INPUT_METHOD);
73         ComponentName component = ComponentName.unflattenFromString(currentIme);
74         Intent imeIntent = new Intent(component.getPackageName() + ".tutorial");
75         PackageManager pm = getPackageManager();
76         List<ResolveInfo> tutorials = pm.queryIntentActivities(imeIntent, 0);
77         if(tutorials == null || tutorials.isEmpty()) {
78             getPreferenceScreen().removePreference(findPreference("system_tutorial"));
79         }
80 
81         setStringSummary(KEY_FIRMWARE_VERSION, Build.VERSION.RELEASE);
82         findPreference(KEY_FIRMWARE_VERSION).setEnabled(true);
83         setValueSummary(KEY_BASEBAND_VERSION, "gsm.version.baseband");
84         setStringSummary(KEY_DEVICE_MODEL, Build.MODEL + getMsvSuffix());
85         setStringSummary(KEY_BUILD_NUMBER, Build.DISPLAY);
86         findPreference(KEY_KERNEL_VERSION).setSummary(getFormattedKernelVersion());
87 
88         // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
89         removePreferenceIfPropertyMissing(getPreferenceScreen(), "safetylegal",
90                 PROPERTY_URL_SAFETYLEGAL);
91 
92         // Remove Baseband version if wifi-only device
93         if (Utils.isWifiOnly(getActivity())) {
94             getPreferenceScreen().removePreference(findPreference(KEY_BASEBAND_VERSION));
95         }
96 
97         /*
98          * Settings is a generic app and should not contain any device-specific
99          * info.
100          */
101         final Activity act = getActivity();
102         // These are contained in the "container" preference group
103         PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
104         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
105                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
106         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
107                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
108         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
109                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
110         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TEAM,
111                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
112 
113         // These are contained by the root preference screen
114         parentPreference = getPreferenceScreen();
115         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference,
116                 KEY_SYSTEM_UPDATE_SETTINGS,
117                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
118         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_CONTRIBUTORS,
119                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
120     }
121 
122     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)123     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
124         if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
125             System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
126             mHits[mHits.length-1] = SystemClock.uptimeMillis();
127             if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
128                 Intent intent = new Intent(Intent.ACTION_MAIN);
129                 intent.setClassName("android",
130                         com.android.internal.app.PlatLogoActivity.class.getName());
131                 try {
132                     startActivity(intent);
133                 } catch (Exception e) {
134                     Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
135                 }
136             }
137         }
138         return super.onPreferenceTreeClick(preferenceScreen, preference);
139     }
140 
removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup, String preference, String property )141     private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
142             String preference, String property ) {
143         if (SystemProperties.get(property).equals(""))
144         {
145             // Property is missing so remove preference from group
146             try {
147                 preferenceGroup.removePreference(findPreference(preference));
148             } catch (RuntimeException e) {
149                 Log.d(LOG_TAG, "Property '" + property + "' missing and no '"
150                         + preference + "' preference");
151             }
152         }
153     }
154 
setStringSummary(String preference, String value)155     private void setStringSummary(String preference, String value) {
156         try {
157             findPreference(preference).setSummary(value);
158         } catch (RuntimeException e) {
159             findPreference(preference).setSummary(
160                 getResources().getString(R.string.device_info_default));
161         }
162     }
163 
setValueSummary(String preference, String property)164     private void setValueSummary(String preference, String property) {
165         try {
166             findPreference(preference).setSummary(
167                     SystemProperties.get(property,
168                             getResources().getString(R.string.device_info_default)));
169         } catch (RuntimeException e) {
170             // No recovery
171         }
172     }
173 
174     /**
175      * Reads a line from the specified file.
176      * @param filename the file to read from
177      * @return the first line, if any.
178      * @throws IOException if the file couldn't be read
179      */
readLine(String filename)180     private String readLine(String filename) throws IOException {
181         BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
182         try {
183             return reader.readLine();
184         } finally {
185             reader.close();
186         }
187     }
188 
getFormattedKernelVersion()189     private String getFormattedKernelVersion() {
190         String procVersionStr;
191 
192         try {
193             procVersionStr = readLine(FILENAME_PROC_VERSION);
194 
195             final String PROC_VERSION_REGEX =
196                 "\\w+\\s+" + /* ignore: Linux */
197                 "\\w+\\s+" + /* ignore: version */
198                 "([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
199                 "\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+" + /* group 2: (xxxxxx@xxxxx.constant) */
200                 "\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\s+" + /* ignore: (gcc ..) */
201                 "([^\\s]+)\\s+" + /* group 3: #26 */
202                 "(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
203                 "(.+)"; /* group 4: date */
204 
205             Pattern p = Pattern.compile(PROC_VERSION_REGEX);
206             Matcher m = p.matcher(procVersionStr);
207 
208             if (!m.matches()) {
209                 Log.e(LOG_TAG, "Regex did not match on /proc/version: " + procVersionStr);
210                 return "Unavailable";
211             } else if (m.groupCount() < 4) {
212                 Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()
213                         + " groups");
214                 return "Unavailable";
215             } else {
216                 return (new StringBuilder(m.group(1)).append("\n").append(
217                         m.group(2)).append(" ").append(m.group(3)).append("\n")
218                         .append(m.group(4))).toString();
219             }
220         } catch (IOException e) {
221             Log.e(LOG_TAG,
222                 "IO Exception when getting kernel version for Device Info screen",
223                 e);
224 
225             return "Unavailable";
226         }
227     }
228 
229     /**
230      * Returns " (ENGINEERING)" if the msv file has a zero value, else returns "".
231      * @return a string to append to the model number description.
232      */
getMsvSuffix()233     private String getMsvSuffix() {
234         // Production devices should have a non-zero value. If we can't read it, assume it's a
235         // production device so that we don't accidentally show that it's an ENGINEERING device.
236         try {
237             String msv = readLine(FILENAME_MSV);
238             // Parse as a hex number. If it evaluates to a zero, then it's an engineering build.
239             if (Long.parseLong(msv, 16) == 0) {
240                 return " (ENGINEERING)";
241             }
242         } catch (IOException ioe) {
243             // Fail quietly, as the file may not exist on some devices.
244         } catch (NumberFormatException nfe) {
245             // Fail quietly, returning empty string should be sufficient
246         }
247         return "";
248     }
249 }
250