michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.preferences; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: import android.app.ProgressDialog; michael@0: import android.content.Context; michael@0: import android.util.AttributeSet; michael@0: import android.util.Log; michael@0: michael@0: class AndroidImportPreference extends MultiChoicePreference { michael@0: static final private String LOGTAG = "AndroidImport"; michael@0: private static final String PREF_KEY_PREFIX = "import_android.data."; michael@0: private Context mContext; michael@0: michael@0: public AndroidImportPreference(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: mContext = context; michael@0: } michael@0: michael@0: @Override michael@0: protected void onDialogClosed(boolean positiveResult) { michael@0: super.onDialogClosed(positiveResult); michael@0: michael@0: if (!positiveResult) michael@0: return; michael@0: michael@0: boolean bookmarksChecked = false; michael@0: boolean historyChecked = false; michael@0: michael@0: CharSequence keys[] = getEntryKeys(); michael@0: boolean values[] = getValues(); michael@0: michael@0: for (int i = 0; i < keys.length; i++) { michael@0: // Privacy pref checkbox values are stored in Android prefs to michael@0: // remember their check states. The key names are import_android.data.X michael@0: String key = keys[i].toString().substring(PREF_KEY_PREFIX.length()); michael@0: boolean value = values[i]; michael@0: michael@0: if (key.equals("bookmarks") && value) { michael@0: bookmarksChecked = true; michael@0: } michael@0: if (key.equals("history") && value) { michael@0: historyChecked = true; michael@0: } michael@0: } michael@0: michael@0: runImport(bookmarksChecked, historyChecked); michael@0: } michael@0: michael@0: protected void runImport(final boolean doBookmarks, final boolean doHistory) { michael@0: Log.i(LOGTAG, "Importing Android history/bookmarks"); michael@0: if (!doBookmarks && !doHistory) { michael@0: return; michael@0: } michael@0: michael@0: final String dialogTitle; michael@0: if (doBookmarks && doHistory) { michael@0: dialogTitle = mContext.getString(R.string.bookmarkhistory_import_both); michael@0: } else if (doBookmarks) { michael@0: dialogTitle = mContext.getString(R.string.bookmarkhistory_import_bookmarks); michael@0: } else { michael@0: dialogTitle = mContext.getString(R.string.bookmarkhistory_import_history); michael@0: } michael@0: michael@0: final ProgressDialog dialog = michael@0: ProgressDialog.show(mContext, michael@0: dialogTitle, michael@0: mContext.getString(R.string.bookmarkhistory_import_wait), michael@0: true); michael@0: michael@0: final Runnable stopCallback = new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: ThreadUtils.postToUiThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: dialog.dismiss(); michael@0: } michael@0: }); michael@0: } michael@0: }; michael@0: michael@0: ThreadUtils.postToBackgroundThread( michael@0: // Constructing AndroidImport may need finding the profile, michael@0: // which hits disk, so it needs to go into a Runnable too. michael@0: new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: new AndroidImport(mContext, stopCallback, doBookmarks, doHistory).run(); michael@0: } michael@0: } michael@0: ); michael@0: } michael@0: }