1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 package org.mozilla.gecko.preferences; 7 8 import org.mozilla.gecko.AppConstants.Versions; 9 import org.mozilla.gecko.R; 10 import org.mozilla.gecko.restrictions.Restrictable; 11 import org.mozilla.gecko.restrictions.Restrictions; 12 import org.mozilla.gecko.util.ThreadUtils; 13 14 import java.util.Set; 15 16 import android.app.ProgressDialog; 17 import android.content.Context; 18 import android.preference.Preference; 19 import android.util.AttributeSet; 20 import android.util.Log; 21 22 class AndroidImportPreference extends MultiPrefMultiChoicePreference { 23 private static final String LOGTAG = "AndroidImport"; 24 public static final String PREF_KEY = "android.not_a_preference.import_android"; 25 private static final String PREF_KEY_PREFIX = "import_android.data."; 26 private final Context mContext; 27 28 public static class Handler implements GeckoPreferences.PrefHandler { setupPref(Context context, Preference pref)29 public boolean setupPref(Context context, Preference pref) { 30 // Feature disabled on devices running Android M+ (Bug 1183559) 31 return Versions.preMarshmallow && Restrictions.isAllowed(context, Restrictable.IMPORT_SETTINGS); 32 } 33 onChange(Context context, Preference pref, Object newValue)34 public void onChange(Context context, Preference pref, Object newValue) { } 35 } 36 AndroidImportPreference(Context context, AttributeSet attrs)37 public AndroidImportPreference(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 mContext = context; 40 } 41 42 @Override onDialogClosed(boolean positiveResult)43 protected void onDialogClosed(boolean positiveResult) { 44 super.onDialogClosed(positiveResult); 45 46 if (!positiveResult) 47 return; 48 49 boolean bookmarksChecked = false; 50 boolean historyChecked = false; 51 52 Set<String> values = getValues(); 53 54 for (String value : values) { 55 // Import checkbox values are stored in Android prefs to 56 // remember their check states. The key names are import_android.data.X 57 String key = value.substring(PREF_KEY_PREFIX.length()); 58 if ("bookmarks".equals(key)) { 59 bookmarksChecked = true; 60 } else if ("history".equals(key)) { 61 historyChecked = true; 62 } 63 } 64 65 runImport(bookmarksChecked, historyChecked); 66 } 67 runImport(final boolean doBookmarks, final boolean doHistory)68 protected void runImport(final boolean doBookmarks, final boolean doHistory) { 69 Log.i(LOGTAG, "Importing Android history/bookmarks"); 70 if (!doBookmarks && !doHistory) { 71 return; 72 } 73 74 final String dialogTitle; 75 if (doBookmarks && doHistory) { 76 dialogTitle = mContext.getString(R.string.bookmarkhistory_import_both); 77 } else if (doBookmarks) { 78 dialogTitle = mContext.getString(R.string.bookmarkhistory_import_bookmarks); 79 } else { 80 dialogTitle = mContext.getString(R.string.bookmarkhistory_import_history); 81 } 82 83 final ProgressDialog dialog = 84 ProgressDialog.show(mContext, 85 dialogTitle, 86 mContext.getString(R.string.bookmarkhistory_import_wait), 87 true); 88 89 final Runnable stopCallback = new Runnable() { 90 @Override 91 public void run() { 92 ThreadUtils.postToUiThread(new Runnable() { 93 @Override 94 public void run() { 95 dialog.dismiss(); 96 } 97 }); 98 } 99 }; 100 101 ThreadUtils.postToBackgroundThread( 102 // Constructing AndroidImport may need finding the profile, 103 // which hits disk, so it needs to go into a Runnable too. 104 new Runnable() { 105 @Override 106 public void run() { 107 new AndroidImport(mContext, stopCallback, doBookmarks, doHistory).run(); 108 } 109 } 110 ); 111 } 112 } 113