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.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Build;
23import android.os.Bundle;
24import android.os.SELinux;
25import android.os.SystemClock;
26import android.os.SystemProperties;
27import android.os.UserHandle;
28import android.preference.Preference;
29import android.preference.PreferenceGroup;
30import android.preference.PreferenceScreen;
31import android.util.Log;
32import android.widget.Toast;
33
34import java.io.BufferedReader;
35import java.io.FileReader;
36import java.io.IOException;
37import java.util.regex.Matcher;
38import java.util.regex.Pattern;
39
40public class DeviceInfoSettings extends SettingsPreferenceFragment {
41
42    private static final String LOG_TAG = "DeviceInfoSettings";
43
44    private static final String FILENAME_PROC_VERSION = "/proc/version";
45    private static final String FILENAME_MSV = "/sys/board_properties/soc/msv";
46
47    private static final String KEY_CONTAINER = "container";
48    private static final String KEY_TEAM = "team";
49    private static final String KEY_CONTRIBUTORS = "contributors";
50    private static final String KEY_REGULATORY_INFO = "regulatory_info";
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 PROPERTY_SELINUX_STATUS = "ro.build.selinux";
57    private static final String KEY_KERNEL_VERSION = "kernel_version";
58    private static final String KEY_BUILD_NUMBER = "build_number";
59    private static final String KEY_DEVICE_MODEL = "device_model";
60    private static final String KEY_SELINUX_STATUS = "selinux_status";
61    private static final String KEY_BASEBAND_VERSION = "baseband_version";
62    private static final String KEY_FIRMWARE_VERSION = "firmware_version";
63    private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
64    private static final String KEY_EQUIPMENT_ID = "fcc_equipment_id";
65    private static final String PROPERTY_EQUIPMENT_ID = "ro.ril.fccid";
66
67    static final int TAPS_TO_BE_A_DEVELOPER = 7;
68
69    long[] mHits = new long[3];
70    int mDevHitCountdown;
71    Toast mDevHitToast;
72
73    @Override
74    public void onCreate(Bundle icicle) {
75        super.onCreate(icicle);
76
77        addPreferencesFromResource(R.xml.device_info_settings);
78
79        setStringSummary(KEY_FIRMWARE_VERSION, Build.VERSION.RELEASE);
80        findPreference(KEY_FIRMWARE_VERSION).setEnabled(true);
81        setValueSummary(KEY_BASEBAND_VERSION, "gsm.version.baseband");
82        setStringSummary(KEY_DEVICE_MODEL, Build.MODEL + getMsvSuffix());
83        setValueSummary(KEY_EQUIPMENT_ID, PROPERTY_EQUIPMENT_ID);
84        setStringSummary(KEY_DEVICE_MODEL, Build.MODEL);
85        setStringSummary(KEY_BUILD_NUMBER, Build.DISPLAY);
86        findPreference(KEY_BUILD_NUMBER).setEnabled(true);
87        findPreference(KEY_KERNEL_VERSION).setSummary(getFormattedKernelVersion());
88
89        if (!SELinux.isSELinuxEnabled()) {
90            String status = getResources().getString(R.string.selinux_status_disabled);
91            setStringSummary(KEY_SELINUX_STATUS, status);
92        } else if (!SELinux.isSELinuxEnforced()) {
93            String status = getResources().getString(R.string.selinux_status_permissive);
94            setStringSummary(KEY_SELINUX_STATUS, status);
95        }
96
97        // Remove selinux information if property is not present
98        removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_SELINUX_STATUS,
99                PROPERTY_SELINUX_STATUS);
100
101        // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
102        removePreferenceIfPropertyMissing(getPreferenceScreen(), "safetylegal",
103                PROPERTY_URL_SAFETYLEGAL);
104
105        // Remove Equipment id preference if FCC ID is not set by RIL
106        removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_EQUIPMENT_ID,
107                PROPERTY_EQUIPMENT_ID);
108
109        // Remove Baseband version if wifi-only device
110        if (Utils.isWifiOnly(getActivity())) {
111            getPreferenceScreen().removePreference(findPreference(KEY_BASEBAND_VERSION));
112        }
113
114        /*
115         * Settings is a generic app and should not contain any device-specific
116         * info.
117         */
118        final Activity act = getActivity();
119        // These are contained in the "container" preference group
120        PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
121        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
122                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
123        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
124                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
125        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
126                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
127        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TEAM,
128                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
129
130        // These are contained by the root preference screen
131        parentPreference = getPreferenceScreen();
132        if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
133            Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference,
134                    KEY_SYSTEM_UPDATE_SETTINGS,
135                    Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
136        } else {
137            // Remove for secondary users
138            removePreference(KEY_SYSTEM_UPDATE_SETTINGS);
139        }
140        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_CONTRIBUTORS,
141                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
142
143        // Read platform settings for additional system update setting
144        removePreferenceIfBoolFalse(KEY_UPDATE_SETTING,
145                R.bool.config_additional_system_update_setting_enable);
146
147        // Remove regulatory information if not enabled.
148        removePreferenceIfBoolFalse(KEY_REGULATORY_INFO,
149                R.bool.config_show_regulatory_info);
150    }
151
152    @Override
153    public void onResume() {
154        super.onResume();
155        mDevHitCountdown = getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
156                Context.MODE_PRIVATE).getBoolean(DevelopmentSettings.PREF_SHOW,
157                        android.os.Build.TYPE.equals("eng")) ? -1 : TAPS_TO_BE_A_DEVELOPER;
158        mDevHitToast = null;
159    }
160
161    @Override
162    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
163        if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
164            System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
165            mHits[mHits.length-1] = SystemClock.uptimeMillis();
166            if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
167                Intent intent = new Intent(Intent.ACTION_MAIN);
168                intent.setClassName("android",
169                        com.android.internal.app.PlatLogoActivity.class.getName());
170                try {
171                    startActivity(intent);
172                } catch (Exception e) {
173                    Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
174                }
175            }
176        } else if (preference.getKey().equals(KEY_BUILD_NUMBER)) {
177            // Don't enable developer options for secondary users.
178            if (UserHandle.myUserId() != UserHandle.USER_OWNER) return true;
179
180            if (mDevHitCountdown > 0) {
181                mDevHitCountdown--;
182                if (mDevHitCountdown == 0) {
183                    getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
184                            Context.MODE_PRIVATE).edit().putBoolean(
185                                    DevelopmentSettings.PREF_SHOW, true).apply();
186                    if (mDevHitToast != null) {
187                        mDevHitToast.cancel();
188                    }
189                    mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_on,
190                            Toast.LENGTH_LONG);
191                    mDevHitToast.show();
192                } else if (mDevHitCountdown > 0
193                        && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER-2)) {
194                    if (mDevHitToast != null) {
195                        mDevHitToast.cancel();
196                    }
197                    mDevHitToast = Toast.makeText(getActivity(), getResources().getQuantityString(
198                            R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown),
199                            Toast.LENGTH_SHORT);
200                    mDevHitToast.show();
201                }
202            } else if (mDevHitCountdown < 0) {
203                if (mDevHitToast != null) {
204                    mDevHitToast.cancel();
205                }
206                mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_already,
207                        Toast.LENGTH_LONG);
208                mDevHitToast.show();
209            }
210        }
211        return super.onPreferenceTreeClick(preferenceScreen, preference);
212    }
213
214    private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
215            String preference, String property ) {
216        if (SystemProperties.get(property).equals("")) {
217            // Property is missing so remove preference from group
218            try {
219                preferenceGroup.removePreference(findPreference(preference));
220            } catch (RuntimeException e) {
221                Log.d(LOG_TAG, "Property '" + property + "' missing and no '"
222                        + preference + "' preference");
223            }
224        }
225    }
226
227    private void removePreferenceIfBoolFalse(String preference, int resId) {
228        if (!getResources().getBoolean(resId)) {
229            Preference pref = findPreference(preference);
230            if (pref != null) {
231                getPreferenceScreen().removePreference(pref);
232            }
233        }
234    }
235
236    private void setStringSummary(String preference, String value) {
237        try {
238            findPreference(preference).setSummary(value);
239        } catch (RuntimeException e) {
240            findPreference(preference).setSummary(
241                getResources().getString(R.string.device_info_default));
242        }
243    }
244
245    private void setValueSummary(String preference, String property) {
246        try {
247            findPreference(preference).setSummary(
248                    SystemProperties.get(property,
249                            getResources().getString(R.string.device_info_default)));
250        } catch (RuntimeException e) {
251            // No recovery
252        }
253    }
254
255    /**
256     * Reads a line from the specified file.
257     * @param filename the file to read from
258     * @return the first line, if any.
259     * @throws IOException if the file couldn't be read
260     */
261    private static String readLine(String filename) throws IOException {
262        BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
263        try {
264            return reader.readLine();
265        } finally {
266            reader.close();
267        }
268    }
269
270    public static String getFormattedKernelVersion() {
271        try {
272            return formatKernelVersion(readLine(FILENAME_PROC_VERSION));
273
274        } catch (IOException e) {
275            Log.e(LOG_TAG,
276                "IO Exception when getting kernel version for Device Info screen",
277                e);
278
279            return "Unavailable";
280        }
281    }
282
283    public static String formatKernelVersion(String rawKernelVersion) {
284        // Example (see tests for more):
285        // Linux version 3.0.31-g6fb96c9 (android-build@xxx.xxx.xxx.xxx.com) \
286        //     (gcc version 4.6.x-xxx 20120106 (prerelease) (GCC) ) #1 SMP PREEMPT \
287        //     Thu Jun 28 11:02:39 PDT 2012
288
289        final String PROC_VERSION_REGEX =
290            "Linux version (\\S+) " + /* group 1: "3.0.31-g6fb96c9" */
291            "\\((\\S+?)\\) " +        /* group 2: "x@y.com" (kernel builder) */
292            "(?:\\(gcc.+? \\)) " +    /* ignore: GCC version information */
293            "(#\\d+) " +              /* group 3: "#1" */
294            "(?:.*?)?" +              /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
295            "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 4: "Thu Jun 28 11:02:39 PDT 2012" */
296
297        Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
298        if (!m.matches()) {
299            Log.e(LOG_TAG, "Regex did not match on /proc/version: " + rawKernelVersion);
300            return "Unavailable";
301        } else if (m.groupCount() < 4) {
302            Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()
303                    + " groups");
304            return "Unavailable";
305        }
306        return m.group(1) + "\n" +                 // 3.0.31-g6fb96c9
307            m.group(2) + " " + m.group(3) + "\n" + // x@y.com #1
308            m.group(4);                            // Thu Jun 28 11:02:39 PDT 2012
309    }
310
311    /**
312     * Returns " (ENGINEERING)" if the msv file has a zero value, else returns "".
313     * @return a string to append to the model number description.
314     */
315    private String getMsvSuffix() {
316        // Production devices should have a non-zero value. If we can't read it, assume it's a
317        // production device so that we don't accidentally show that it's an ENGINEERING device.
318        try {
319            String msv = readLine(FILENAME_MSV);
320            // Parse as a hex number. If it evaluates to a zero, then it's an engineering build.
321            if (Long.parseLong(msv, 16) == 0) {
322                return " (ENGINEERING)";
323            }
324        } catch (IOException ioe) {
325            // Fail quietly, as the file may not exist on some devices.
326        } catch (NumberFormatException nfe) {
327            // Fail quietly, returning empty string should be sufficient
328        }
329        return "";
330    }
331}
332