OSDN Git Service

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