blob: dc23d004568efc158d8304f5f917a15b06a8e46e [file] [log] [blame]
John Reck35e9dd62011-04-25 09:01:54 -07001
2/*
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.browser;
19
20import android.content.Context;
21import android.content.SharedPreferences;
22import android.content.SharedPreferences.Editor;
23import android.database.Cursor;
Ben Murdoch3d6c7192011-10-27 18:04:58 +010024import android.net.Uri;
John Reck35e9dd62011-04-25 09:01:54 -070025import android.os.AsyncTask;
26import android.os.Message;
27import android.preference.PreferenceManager;
Ben Murdoch3d6c7192011-10-27 18:04:58 +010028import android.provider.ContactsContract;
John Reck35e9dd62011-04-25 09:01:54 -070029import android.webkit.WebSettings.AutoFillProfile;
30
John Reck7b06c902011-04-26 13:48:09 -070031import java.util.concurrent.CountDownLatch;
32
John Reck35e9dd62011-04-25 09:01:54 -070033public class AutofillHandler {
34
35 private AutoFillProfile mAutoFillProfile;
36 // Default to zero. In the case no profile is set up, the initial
37 // value will come from the AutoFillSettingsFragment when the user
38 // creates a profile. Otherwise, we'll read the ID of the last used
39 // profile from the prefs db.
40 private int mAutoFillActiveProfileId;
41 private static final int NO_AUTOFILL_PROFILE_SET = 0;
42
John Reck7b06c902011-04-26 13:48:09 -070043 private CountDownLatch mLoaded = new CountDownLatch(1);
John Reck35e9dd62011-04-25 09:01:54 -070044 private Context mContext;
45
46 public AutofillHandler(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +010047 mContext = context.getApplicationContext();
John Reck35e9dd62011-04-25 09:01:54 -070048 }
49
50 /**
51 * Load settings from the browser app's database. It is performed in
52 * an AsyncTask as it involves plenty of slow disk IO.
53 * NOTE: Strings used for the preferences must match those specified
54 * in the various preference XML files.
55 */
56 public void asyncLoadFromDb() {
John Reck35e9dd62011-04-25 09:01:54 -070057 // Run the initial settings load in an AsyncTask as it hits the
58 // disk multiple times through SharedPreferences and SQLite. We
59 // need to be certain though that this has completed before we start
60 // to load pages though, so in the worst case we will block waiting
61 // for it to finish in BrowserActivity.onCreate().
John Reck7b06c902011-04-26 13:48:09 -070062 new LoadFromDb().start();
John Reck35e9dd62011-04-25 09:01:54 -070063 }
64
65 public void waitForLoad() {
John Reck7b06c902011-04-26 13:48:09 -070066 try {
67 mLoaded.await();
68 } catch (InterruptedException e) {}
John Reck35e9dd62011-04-25 09:01:54 -070069 }
70
John Reck7b06c902011-04-26 13:48:09 -070071 private class LoadFromDb extends Thread {
John Reck35e9dd62011-04-25 09:01:54 -070072
73 @Override
John Reck7b06c902011-04-26 13:48:09 -070074 public void run() {
John Reck35e9dd62011-04-25 09:01:54 -070075 SharedPreferences p =
76 PreferenceManager.getDefaultSharedPreferences(mContext);
77
78 // Read the last active AutoFill profile id.
79 mAutoFillActiveProfileId = p.getInt(
80 PreferenceKeys.PREF_AUTOFILL_ACTIVE_PROFILE_ID,
81 mAutoFillActiveProfileId);
82
83 // Load the autofill profile data from the database. We use a database separate
84 // to the browser preference DB to make it easier to support multiple profiles
85 // and switching between them.
86 AutoFillProfileDatabase autoFillDb = AutoFillProfileDatabase.getInstance(mContext);
87 Cursor c = autoFillDb.getProfile(mAutoFillActiveProfileId);
88
89 if (c.getCount() > 0) {
90 c.moveToFirst();
91
92 String fullName = c.getString(c.getColumnIndex(
93 AutoFillProfileDatabase.Profiles.FULL_NAME));
94 String email = c.getString(c.getColumnIndex(
95 AutoFillProfileDatabase.Profiles.EMAIL_ADDRESS));
96 String company = c.getString(c.getColumnIndex(
97 AutoFillProfileDatabase.Profiles.COMPANY_NAME));
98 String addressLine1 = c.getString(c.getColumnIndex(
99 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_1));
100 String addressLine2 = c.getString(c.getColumnIndex(
101 AutoFillProfileDatabase.Profiles.ADDRESS_LINE_2));
102 String city = c.getString(c.getColumnIndex(
103 AutoFillProfileDatabase.Profiles.CITY));
104 String state = c.getString(c.getColumnIndex(
105 AutoFillProfileDatabase.Profiles.STATE));
106 String zip = c.getString(c.getColumnIndex(
107 AutoFillProfileDatabase.Profiles.ZIP_CODE));
108 String country = c.getString(c.getColumnIndex(
109 AutoFillProfileDatabase.Profiles.COUNTRY));
110 String phone = c.getString(c.getColumnIndex(
111 AutoFillProfileDatabase.Profiles.PHONE_NUMBER));
112 mAutoFillProfile = new AutoFillProfile(mAutoFillActiveProfileId,
113 fullName, email, company, addressLine1, addressLine2, city,
114 state, zip, country, phone);
115 }
116 c.close();
117 autoFillDb.close();
118
Ben Murdoch3d6c7192011-10-27 18:04:58 +0100119 if (mAutoFillProfile == null) {
120 // We did not load a profile from disk. Try to populate one with the user's
121 // "me" contact.
122 final Uri profileUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
123 ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
124
125 String name = getContactField(profileUri,
126 ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
127 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
128 // Only attempt to read other data and set a profile if we could successfully
129 // get a name.
130 if (name != null) {
131 String email = getContactField(profileUri,
132 ContactsContract.CommonDataKinds.Email.ADDRESS,
133 ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
134 String phone = getContactField(profileUri,
135 ContactsContract.CommonDataKinds.Phone.NUMBER,
136 ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
137 String company = getContactField(profileUri,
138 ContactsContract.CommonDataKinds.Organization.COMPANY,
139 ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
140
141 // Can't easily get structured postal address information (even using
142 // CommonDataKinds.StructuredPostal) so omit prepopulating that for now.
143 // When querying structured postal data, it often all comes back as a string
144 // inside the "street" field.
145
146 mAutoFillProfile = new AutoFillProfile(
147 1, name, email, company, null, null, null, null,
148 null, null, phone);
149 }
150 }
151
John Reck7b06c902011-04-26 13:48:09 -0700152 mLoaded.countDown();
John Reck35e9dd62011-04-25 09:01:54 -0700153 }
Ben Murdoch3d6c7192011-10-27 18:04:58 +0100154
155 private String getContactField(Uri uri, String field, String itemType) {
156 String result = null;
157
158 Cursor c = mContext.getContentResolver().query(uri, new String[] { field },
159 ContactsContract.Data.MIMETYPE + "=?", new String[] { itemType }, null);
160
161 try {
162 // Just use the first returned value if we get more than one.
163 if (c.moveToFirst()) {
164 result = c.getString(0);
165 }
166 } finally {
167 c.close();
168 }
169 return result;
170 }
John Reck35e9dd62011-04-25 09:01:54 -0700171 }
172
173 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
John Reck72d77522011-05-06 10:33:16 -0700174 int profileId = NO_AUTOFILL_PROFILE_SET;
John Reck35e9dd62011-04-25 09:01:54 -0700175 if (profile != null) {
John Reck72d77522011-05-06 10:33:16 -0700176 profileId = profile.getUniqueId();
John Reck35e9dd62011-04-25 09:01:54 -0700177 // Update the AutoFill DB with the new profile.
178 new SaveProfileToDbTask(msg).execute(profile);
179 } else {
180 // Delete the current profile.
181 if (mAutoFillProfile != null) {
182 new DeleteProfileFromDbTask(msg).execute(mAutoFillProfile.getUniqueId());
John Reck35e9dd62011-04-25 09:01:54 -0700183 }
184 }
John Reck72d77522011-05-06 10:33:16 -0700185 // Make sure we set mAutoFillProfile before calling setActiveAutoFillProfileId
186 // Calling setActiveAutoFillProfileId will trigger an update of WebViews
187 // which will expect a new profile to be set
John Reck35e9dd62011-04-25 09:01:54 -0700188 mAutoFillProfile = profile;
John Reck72d77522011-05-06 10:33:16 -0700189 setActiveAutoFillProfileId(profileId);
John Reck35e9dd62011-04-25 09:01:54 -0700190 }
191
192 public AutoFillProfile getAutoFillProfile() {
193 return mAutoFillProfile;
194 }
195
196 private void setActiveAutoFillProfileId(int activeProfileId) {
197 mAutoFillActiveProfileId = activeProfileId;
198 Editor ed = PreferenceManager.
199 getDefaultSharedPreferences(mContext).edit();
200 ed.putInt(PreferenceKeys.PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
201 ed.apply();
202 }
203
204 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
205 AutoFillProfileDatabase mAutoFillProfileDb;
206 Message mCompleteMessage;
207
208 public AutoFillProfileDbTask(Message msg) {
209 mCompleteMessage = msg;
210 }
211
212 @Override
213 protected void onPostExecute(Void result) {
214 if (mCompleteMessage != null) {
215 mCompleteMessage.sendToTarget();
216 }
217 mAutoFillProfileDb.close();
218 }
219
220 @Override
221 abstract protected Void doInBackground(T... values);
222 }
223
224
225 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
226 public SaveProfileToDbTask(Message msg) {
227 super(msg);
228 }
229
230 @Override
231 protected Void doInBackground(AutoFillProfile... values) {
232 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
233 assert mAutoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
234 AutoFillProfile newProfile = values[0];
235 mAutoFillProfileDb.addOrUpdateProfile(mAutoFillActiveProfileId, newProfile);
236 return null;
237 }
238 }
239
240 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
241 public DeleteProfileFromDbTask(Message msg) {
242 super(msg);
243 }
244
245 @Override
246 protected Void doInBackground(Integer... values) {
247 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
248 int id = values[0];
249 assert id > 0;
250 mAutoFillProfileDb.dropProfile(id);
251 return null;
252 }
253 }
254}