mobile/android/base/preferences/AndroidImportPreference.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/preferences/AndroidImportPreference.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.preferences;
    1.10 +
    1.11 +import org.mozilla.gecko.R;
    1.12 +import org.mozilla.gecko.util.ThreadUtils;
    1.13 +
    1.14 +import android.app.ProgressDialog;
    1.15 +import android.content.Context;
    1.16 +import android.util.AttributeSet;
    1.17 +import android.util.Log;
    1.18 +
    1.19 +class AndroidImportPreference extends MultiChoicePreference {
    1.20 +    static final private String LOGTAG = "AndroidImport";
    1.21 +    private static final String PREF_KEY_PREFIX = "import_android.data.";
    1.22 +    private Context mContext;
    1.23 +
    1.24 +    public AndroidImportPreference(Context context, AttributeSet attrs) {
    1.25 +        super(context, attrs);
    1.26 +        mContext = context;
    1.27 +    }
    1.28 +
    1.29 +    @Override
    1.30 +    protected void onDialogClosed(boolean positiveResult) {
    1.31 +        super.onDialogClosed(positiveResult);
    1.32 +
    1.33 +        if (!positiveResult)
    1.34 +            return;
    1.35 +
    1.36 +        boolean bookmarksChecked = false;
    1.37 +        boolean historyChecked = false;
    1.38 +
    1.39 +        CharSequence keys[] = getEntryKeys();
    1.40 +        boolean values[] = getValues();
    1.41 +
    1.42 +        for (int i = 0; i < keys.length; i++) {
    1.43 +            // Privacy pref checkbox values are stored in Android prefs to
    1.44 +            // remember their check states. The key names are import_android.data.X
    1.45 +            String key = keys[i].toString().substring(PREF_KEY_PREFIX.length());
    1.46 +            boolean value = values[i];
    1.47 +
    1.48 +            if (key.equals("bookmarks") && value) {
    1.49 +                bookmarksChecked = true;
    1.50 +            }
    1.51 +            if (key.equals("history") && value) {
    1.52 +                historyChecked = true;
    1.53 +            }
    1.54 +        }
    1.55 +
    1.56 +        runImport(bookmarksChecked, historyChecked);
    1.57 +    }
    1.58 +
    1.59 +    protected void runImport(final boolean doBookmarks, final boolean doHistory) {
    1.60 +        Log.i(LOGTAG, "Importing Android history/bookmarks");
    1.61 +        if (!doBookmarks && !doHistory) {
    1.62 +            return;
    1.63 +        }
    1.64 +
    1.65 +        final String dialogTitle;
    1.66 +        if (doBookmarks && doHistory) {
    1.67 +            dialogTitle = mContext.getString(R.string.bookmarkhistory_import_both);
    1.68 +        } else if (doBookmarks) {
    1.69 +            dialogTitle = mContext.getString(R.string.bookmarkhistory_import_bookmarks);
    1.70 +        } else {
    1.71 +            dialogTitle = mContext.getString(R.string.bookmarkhistory_import_history);
    1.72 +        }
    1.73 +
    1.74 +        final ProgressDialog dialog =
    1.75 +            ProgressDialog.show(mContext,
    1.76 +                                dialogTitle,
    1.77 +                                mContext.getString(R.string.bookmarkhistory_import_wait),
    1.78 +                                true);
    1.79 +
    1.80 +        final Runnable stopCallback = new Runnable() {
    1.81 +            @Override
    1.82 +            public void run() {
    1.83 +                ThreadUtils.postToUiThread(new Runnable() {
    1.84 +                    @Override
    1.85 +                    public void run() {
    1.86 +                        dialog.dismiss();
    1.87 +                    }
    1.88 +                });
    1.89 +            }
    1.90 +        };
    1.91 +
    1.92 +        ThreadUtils.postToBackgroundThread(
    1.93 +            // Constructing AndroidImport may need finding the profile,
    1.94 +            // which hits disk, so it needs to go into a Runnable too.
    1.95 +            new Runnable() {
    1.96 +                @Override
    1.97 +                public void run() {
    1.98 +                    new AndroidImport(mContext, stopCallback, doBookmarks, doHistory).run();
    1.99 +                }
   1.100 +            }
   1.101 +        );
   1.102 +    }
   1.103 +}

mercurial