| // Copyright 2014 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package org.chromium.chrome.browser.preferences; |
| |
| import android.content.Context; |
| import android.content.pm.PackageInfo; |
| import android.content.pm.PackageManager.NameNotFoundException; |
| import android.os.Bundle; |
| import android.preference.Preference; |
| import android.preference.PreferenceFragment; |
| import android.text.format.DateUtils; |
| |
| import org.chromium.chrome.R; |
| import org.chromium.chrome.browser.ChromeVersionInfo; |
| import org.chromium.chrome.browser.preferences.PrefServiceBridge.AboutVersionStrings; |
| import org.chromium.chrome.browser.preferences.developer.DeveloperPreferences; |
| import org.chromium.ui.widget.Toast; |
| |
| import java.util.Calendar; |
| |
| /** |
| * Settings fragment that displays information about Chrome. |
| */ |
| public class AboutChromePreferences |
| extends PreferenceFragment implements Preference.OnPreferenceClickListener { |
| private static final int TAPS_FOR_DEVELOPER_PREFERENCES = 7; |
| |
| private static final String PREF_APPLICATION_VERSION = "application_version"; |
| private static final String PREF_OS_VERSION = "os_version"; |
| private static final String PREF_LEGAL_INFORMATION = "legal_information"; |
| |
| private int mDeveloperHitCountdown = DeveloperPreferences.shouldShowDeveloperPreferences() |
| ? -1 |
| : TAPS_FOR_DEVELOPER_PREFERENCES; |
| private Toast mToast; |
| |
| @Override |
| public void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| getActivity().setTitle(R.string.prefs_about_chrome); |
| PreferenceUtils.addPreferencesFromResource(this, R.xml.about_chrome_preferences); |
| |
| PrefServiceBridge prefServiceBridge = PrefServiceBridge.getInstance(); |
| AboutVersionStrings versionStrings = prefServiceBridge.getAboutVersionStrings(); |
| Preference p = findPreference(PREF_APPLICATION_VERSION); |
| p.setSummary(getApplicationVersion(getActivity(), versionStrings.getApplicationVersion())); |
| p.setOnPreferenceClickListener(this); |
| p = findPreference(PREF_OS_VERSION); |
| p.setSummary(versionStrings.getOSVersion()); |
| p = findPreference(PREF_LEGAL_INFORMATION); |
| int currentYear = Calendar.getInstance().get(Calendar.YEAR); |
| p.setSummary(getString(R.string.legal_information_summary, currentYear)); |
| } |
| |
| /** |
| * Build the application version to be shown. In particular, this ensures the debug build |
| * versions are more useful. |
| */ |
| public static String getApplicationVersion(Context context, String version) { |
| if (ChromeVersionInfo.isOfficialBuild()) { |
| return version; |
| } |
| |
| // For developer builds, show how recently the app was installed/updated. |
| PackageInfo info; |
| try { |
| info = context.getPackageManager().getPackageInfo( |
| context.getPackageName(), 0); |
| } catch (NameNotFoundException e) { |
| return version; |
| } |
| CharSequence updateTimeString = DateUtils.getRelativeTimeSpanString( |
| info.lastUpdateTime, System.currentTimeMillis(), 0); |
| return context.getString(R.string.version_with_update_time, version, |
| updateTimeString); |
| } |
| |
| @Override |
| public boolean onPreferenceClick(Preference preference) { |
| if (mDeveloperHitCountdown > 0) { |
| mDeveloperHitCountdown--; |
| |
| if (mDeveloperHitCountdown == 0) { |
| DeveloperPreferences.setDeveloperPreferencesEnabled(); |
| |
| // Show a toast that the developer preferences were enabled. |
| if (mToast != null) { |
| mToast.cancel(); |
| } |
| mToast = Toast.makeText( |
| getActivity(), R.string.prefs_developer_enabled, Toast.LENGTH_LONG); |
| mToast.show(); |
| } else if (mDeveloperHitCountdown > 0 |
| && mDeveloperHitCountdown < (TAPS_FOR_DEVELOPER_PREFERENCES - 2)) { |
| // Show a countdown toast. |
| if (mToast != null) { |
| mToast.cancel(); |
| } |
| mToast = Toast.makeText(getActivity(), |
| getActivity().getResources().getQuantityString( |
| R.plurals.prefs_developer_enable_countdown, mDeveloperHitCountdown, |
| mDeveloperHitCountdown), |
| Toast.LENGTH_SHORT); |
| mToast.show(); |
| } |
| } else if (mDeveloperHitCountdown < 0) { |
| // Show a toast that the developer preferences are already enabled. |
| if (mToast != null) { |
| mToast.cancel(); |
| } |
| mToast = Toast.makeText( |
| getActivity(), R.string.prefs_developer_already_enabled, Toast.LENGTH_LONG); |
| mToast.show(); |
| } |
| return true; |
| } |
| } |