blob: 99ee6a0b4be92b9b0d089b89102feb86ee639b3c [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;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000029import android.webkit.WebSettingsClassic.AutoFillProfile;
John Reck35e9dd62011-04-25 09:01:54 -070030
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
Ben Murdoch7458d4c2012-02-23 11:18:00 +0000161 if (c == null) {
162 return null;
163 }
164
Ben Murdoch3d6c7192011-10-27 18:04:58 +0100165 try {
166 // Just use the first returned value if we get more than one.
167 if (c.moveToFirst()) {
168 result = c.getString(0);
169 }
170 } finally {
171 c.close();
172 }
173 return result;
174 }
John Reck35e9dd62011-04-25 09:01:54 -0700175 }
176
177 public void setAutoFillProfile(AutoFillProfile profile, Message msg) {
John Reck72d77522011-05-06 10:33:16 -0700178 int profileId = NO_AUTOFILL_PROFILE_SET;
John Reck35e9dd62011-04-25 09:01:54 -0700179 if (profile != null) {
John Reck72d77522011-05-06 10:33:16 -0700180 profileId = profile.getUniqueId();
John Reck35e9dd62011-04-25 09:01:54 -0700181 // Update the AutoFill DB with the new profile.
182 new SaveProfileToDbTask(msg).execute(profile);
183 } else {
184 // Delete the current profile.
185 if (mAutoFillProfile != null) {
186 new DeleteProfileFromDbTask(msg).execute(mAutoFillProfile.getUniqueId());
John Reck35e9dd62011-04-25 09:01:54 -0700187 }
188 }
John Reck72d77522011-05-06 10:33:16 -0700189 // Make sure we set mAutoFillProfile before calling setActiveAutoFillProfileId
190 // Calling setActiveAutoFillProfileId will trigger an update of WebViews
191 // which will expect a new profile to be set
John Reck35e9dd62011-04-25 09:01:54 -0700192 mAutoFillProfile = profile;
John Reck72d77522011-05-06 10:33:16 -0700193 setActiveAutoFillProfileId(profileId);
John Reck35e9dd62011-04-25 09:01:54 -0700194 }
195
196 public AutoFillProfile getAutoFillProfile() {
197 return mAutoFillProfile;
198 }
199
200 private void setActiveAutoFillProfileId(int activeProfileId) {
201 mAutoFillActiveProfileId = activeProfileId;
202 Editor ed = PreferenceManager.
203 getDefaultSharedPreferences(mContext).edit();
204 ed.putInt(PreferenceKeys.PREF_AUTOFILL_ACTIVE_PROFILE_ID, activeProfileId);
205 ed.apply();
206 }
207
208 private abstract class AutoFillProfileDbTask<T> extends AsyncTask<T, Void, Void> {
209 AutoFillProfileDatabase mAutoFillProfileDb;
210 Message mCompleteMessage;
211
212 public AutoFillProfileDbTask(Message msg) {
213 mCompleteMessage = msg;
214 }
215
216 @Override
217 protected void onPostExecute(Void result) {
218 if (mCompleteMessage != null) {
219 mCompleteMessage.sendToTarget();
220 }
221 mAutoFillProfileDb.close();
222 }
223
224 @Override
225 abstract protected Void doInBackground(T... values);
226 }
227
228
229 private class SaveProfileToDbTask extends AutoFillProfileDbTask<AutoFillProfile> {
230 public SaveProfileToDbTask(Message msg) {
231 super(msg);
232 }
233
234 @Override
235 protected Void doInBackground(AutoFillProfile... values) {
236 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
237 assert mAutoFillActiveProfileId != NO_AUTOFILL_PROFILE_SET;
238 AutoFillProfile newProfile = values[0];
239 mAutoFillProfileDb.addOrUpdateProfile(mAutoFillActiveProfileId, newProfile);
240 return null;
241 }
242 }
243
244 private class DeleteProfileFromDbTask extends AutoFillProfileDbTask<Integer> {
245 public DeleteProfileFromDbTask(Message msg) {
246 super(msg);
247 }
248
249 @Override
250 protected Void doInBackground(Integer... values) {
251 mAutoFillProfileDb = AutoFillProfileDatabase.getInstance(mContext);
252 int id = values[0];
253 assert id > 0;
254 mAutoFillProfileDb.dropProfile(id);
255 return null;
256 }
257 }
258}