OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.preferences; |
| 6 |
| 7 import android.annotation.TargetApi; |
| 8 import android.app.ActivityManager; |
| 9 import android.app.ActivityManager.AppTask; |
| 10 import android.content.Context; |
| 11 import android.content.DialogInterface; |
| 12 import android.content.pm.PackageManager; |
| 13 import android.os.Build; |
| 14 import android.os.Bundle; |
| 15 import android.preference.Preference; |
| 16 import android.preference.Preference.OnPreferenceChangeListener; |
| 17 import android.preference.PreferenceFragment; |
| 18 import android.preference.SwitchPreference; |
| 19 import android.support.v7.app.AlertDialog; |
| 20 |
| 21 import com.google.android.apps.chrome.R; |
| 22 |
| 23 import org.chromium.base.metrics.RecordUserAction; |
| 24 import org.chromium.chrome.browser.IntentHandler; |
| 25 import org.chromium.chrome.browser.document.DocumentActivity; |
| 26 import org.chromium.chrome.browser.document.DocumentMigrationHelper; |
| 27 import org.chromium.chrome.browser.document.DocumentUtils; |
| 28 |
| 29 import java.util.List; |
| 30 |
| 31 /** |
| 32 * A preference to control whether user's tabs are appearing in the tab switcher
. |
| 33 */ |
| 34 public class DocumentModePreference extends PreferenceFragment { |
| 35 |
| 36 private static final String PREF_DOCUMENT_MODE_SWITCH = "document_mode_switc
h"; |
| 37 private SwitchPreference mDocumentModeSwitch; |
| 38 private DocumentModeManager mDocumentModeManager; |
| 39 |
| 40 @Override |
| 41 public void onCreate(Bundle savedInstanceState) { |
| 42 super.onCreate(savedInstanceState); |
| 43 addPreferencesFromResource(R.xml.document_mode_preferences); |
| 44 getActivity().setTitle(R.string.tabs_and_apps_title); |
| 45 |
| 46 mDocumentModeManager = DocumentModeManager.getInstance(getActivity()); |
| 47 |
| 48 mDocumentModeSwitch = (SwitchPreference) findPreference(PREF_DOCUMENT_MO
DE_SWITCH); |
| 49 |
| 50 boolean isdocumentModeEnabled = !mDocumentModeManager.isOptedOutOfDocume
ntMode(); |
| 51 mDocumentModeSwitch.setChecked(isdocumentModeEnabled); |
| 52 |
| 53 mDocumentModeSwitch.setOnPreferenceChangeListener(new OnPreferenceChange
Listener() { |
| 54 @Override |
| 55 public boolean onPreferenceChange(Preference preference, Object newV
alue) { |
| 56 if ((boolean) newValue == !mDocumentModeManager.isOptedOutOfDocu
mentMode()) { |
| 57 return true; |
| 58 } |
| 59 createOptOutAlertDialog((boolean) newValue).show(); |
| 60 return true; |
| 61 } |
| 62 }); |
| 63 } |
| 64 |
| 65 private AlertDialog createOptOutAlertDialog(final boolean optOut) { |
| 66 final boolean isSwitchEnabled = !mDocumentModeManager.isOptedOutOfDocume
ntMode(); |
| 67 |
| 68 AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.Aler
tDialogTheme) |
| 69 .setTitle(optOut ? R.string.tabs_and_apps_turn_on_title |
| 70 : R.string.tabs_and_apps_turn_off_title) |
| 71 .setMessage(optOut ? R.string.tabs_and_apps_opt_in_confirmation |
| 72 : R.string.tabs_and_apps_opt_out_confirmation) |
| 73 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickL
istener() { |
| 74 @Override |
| 75 public void onClick(DialogInterface dialog, int id) { |
| 76 mDocumentModeSwitch.setChecked(isSwitchEnabled); |
| 77 dialog.dismiss(); |
| 78 } |
| 79 }) |
| 80 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListe
ner() { |
| 81 @Override |
| 82 public void onClick(DialogInterface dialog, int which) { |
| 83 dialog.dismiss(); |
| 84 if (optOut) { |
| 85 RecordUserAction.record("DocumentActivity_UserOptOut
"); |
| 86 } else { |
| 87 RecordUserAction.record("DocumentActivity_UserOptIn"
); |
| 88 } |
| 89 mDocumentModeManager.setOptedOutState(optOut |
| 90 ? DocumentModeManager.OPT_OUT_PROMO_DISMISSED |
| 91 : DocumentModeManager.OPTED_OUT_OF_DOCUMENT_MODE
); |
| 92 mDocumentModeManager.setOptOutCleanUpPending(true); |
| 93 DocumentMigrationHelper.migrateTabs( |
| 94 optOut, getActivity(), isRestartNeeded(optOut)); |
| 95 } |
| 96 }) |
| 97 .setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 98 @Override |
| 99 public void onCancel(DialogInterface dialog) { |
| 100 mDocumentModeSwitch.setChecked(isSwitchEnabled); |
| 101 } |
| 102 }) |
| 103 .create(); |
| 104 |
| 105 return dialog; |
| 106 } |
| 107 |
| 108 /** |
| 109 * Figure out whether we need to restart the application after the tab migra
tion is complete. |
| 110 * We don't need to restart if this is being accessed from FRE and no docume
nt activities have |
| 111 * been created yet. |
| 112 * @param optOut This is true when we are starting out in opted-out mode. |
| 113 * @return Whether to restart the application. |
| 114 */ |
| 115 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 116 private boolean isRestartNeeded(boolean optOut) { |
| 117 if (optOut) return true; |
| 118 boolean isFromFre = getActivity().getIntent() != null |
| 119 && getActivity().getIntent().getBooleanExtra( |
| 120 IntentHandler.EXTRA_INVOKED_FROM_FRE, false); |
| 121 if (!isFromFre) return true; |
| 122 |
| 123 ActivityManager am = (ActivityManager) getActivity().getSystemService( |
| 124 Context.ACTIVITY_SERVICE); |
| 125 PackageManager pm = getActivity().getPackageManager(); |
| 126 List<AppTask> taskList = am.getAppTasks(); |
| 127 |
| 128 for (int i = 0; i < taskList.size(); i++) { |
| 129 String className = DocumentUtils.getTaskClassName(taskList.get(i), p
m); |
| 130 if (className == null) continue; |
| 131 if (DocumentActivity.isDocumentActivity(className)) return true; |
| 132 } |
| 133 return false; |
| 134 } |
| 135 |
| 136 } |
OLD | NEW |