|
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.R; |
|
9 import org.mozilla.gecko.util.ThreadUtils; |
|
10 |
|
11 import android.app.ProgressDialog; |
|
12 import android.content.Context; |
|
13 import android.util.AttributeSet; |
|
14 import android.util.Log; |
|
15 |
|
16 class AndroidImportPreference extends MultiChoicePreference { |
|
17 static final private String LOGTAG = "AndroidImport"; |
|
18 private static final String PREF_KEY_PREFIX = "import_android.data."; |
|
19 private Context mContext; |
|
20 |
|
21 public AndroidImportPreference(Context context, AttributeSet attrs) { |
|
22 super(context, attrs); |
|
23 mContext = context; |
|
24 } |
|
25 |
|
26 @Override |
|
27 protected void onDialogClosed(boolean positiveResult) { |
|
28 super.onDialogClosed(positiveResult); |
|
29 |
|
30 if (!positiveResult) |
|
31 return; |
|
32 |
|
33 boolean bookmarksChecked = false; |
|
34 boolean historyChecked = false; |
|
35 |
|
36 CharSequence keys[] = getEntryKeys(); |
|
37 boolean values[] = getValues(); |
|
38 |
|
39 for (int i = 0; i < keys.length; i++) { |
|
40 // Privacy pref checkbox values are stored in Android prefs to |
|
41 // remember their check states. The key names are import_android.data.X |
|
42 String key = keys[i].toString().substring(PREF_KEY_PREFIX.length()); |
|
43 boolean value = values[i]; |
|
44 |
|
45 if (key.equals("bookmarks") && value) { |
|
46 bookmarksChecked = true; |
|
47 } |
|
48 if (key.equals("history") && value) { |
|
49 historyChecked = true; |
|
50 } |
|
51 } |
|
52 |
|
53 runImport(bookmarksChecked, historyChecked); |
|
54 } |
|
55 |
|
56 protected void runImport(final boolean doBookmarks, final boolean doHistory) { |
|
57 Log.i(LOGTAG, "Importing Android history/bookmarks"); |
|
58 if (!doBookmarks && !doHistory) { |
|
59 return; |
|
60 } |
|
61 |
|
62 final String dialogTitle; |
|
63 if (doBookmarks && doHistory) { |
|
64 dialogTitle = mContext.getString(R.string.bookmarkhistory_import_both); |
|
65 } else if (doBookmarks) { |
|
66 dialogTitle = mContext.getString(R.string.bookmarkhistory_import_bookmarks); |
|
67 } else { |
|
68 dialogTitle = mContext.getString(R.string.bookmarkhistory_import_history); |
|
69 } |
|
70 |
|
71 final ProgressDialog dialog = |
|
72 ProgressDialog.show(mContext, |
|
73 dialogTitle, |
|
74 mContext.getString(R.string.bookmarkhistory_import_wait), |
|
75 true); |
|
76 |
|
77 final Runnable stopCallback = new Runnable() { |
|
78 @Override |
|
79 public void run() { |
|
80 ThreadUtils.postToUiThread(new Runnable() { |
|
81 @Override |
|
82 public void run() { |
|
83 dialog.dismiss(); |
|
84 } |
|
85 }); |
|
86 } |
|
87 }; |
|
88 |
|
89 ThreadUtils.postToBackgroundThread( |
|
90 // Constructing AndroidImport may need finding the profile, |
|
91 // which hits disk, so it needs to go into a Runnable too. |
|
92 new Runnable() { |
|
93 @Override |
|
94 public void run() { |
|
95 new AndroidImport(mContext, stopCallback, doBookmarks, doHistory).run(); |
|
96 } |
|
97 } |
|
98 ); |
|
99 } |
|
100 } |