blob: 3345e925826c5dd83734862e06a5b443858e30b9 [file] [log] [blame]
Ben Murdochaf554522010-09-10 22:09:30 +01001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.database.sqlite.SQLiteDatabase;
22import android.database.sqlite.SQLiteOpenHelper;
23import android.provider.BaseColumns;
24import android.util.Log;
Ben Murdoch0cb81892010-10-08 12:41:33 +010025import android.webkit.WebSettings.AutoFillProfile;
Ben Murdochaf554522010-09-10 22:09:30 +010026
27public class AutoFillProfileDatabase {
28
29 static final String LOGTAG = "AutoFillProfileDatabase";
30
31 static final String DATABASE_NAME = "autofill.db";
Ben Murdoch36a23dd2010-10-13 13:20:06 +010032 static final int DATABASE_VERSION = 2;
Ben Murdochaf554522010-09-10 22:09:30 +010033 static final String PROFILES_TABLE_NAME = "profiles";
34 private AutoFillProfileDatabaseHelper mOpenHelper;
35 private static AutoFillProfileDatabase sInstance;
36
37 public static final class Profiles implements BaseColumns {
38 private Profiles() { }
39
40 static final String FULL_NAME = "fullname";
41 static final String EMAIL_ADDRESS = "email";
Ben Murdoch36a23dd2010-10-13 13:20:06 +010042 static final String COMPANY_NAME = "companyname";
43 static final String ADDRESS_LINE_1 = "addressline1";
44 static final String ADDRESS_LINE_2 = "addressline2";
45 static final String CITY = "city";
46 static final String STATE = "state";
47 static final String ZIP_CODE = "zipcode";
48 static final String COUNTRY = "country";
49 static final String PHONE_NUMBER = "phone";
Ben Murdochaf554522010-09-10 22:09:30 +010050 }
51
52 private static class AutoFillProfileDatabaseHelper extends SQLiteOpenHelper {
53 AutoFillProfileDatabaseHelper(Context context) {
54 super(context, DATABASE_NAME, null, DATABASE_VERSION);
55 }
56
57 @Override
58 public void onCreate(SQLiteDatabase db) {
59 db.execSQL("CREATE TABLE " + PROFILES_TABLE_NAME + " ("
60 + Profiles._ID + " INTEGER PRIMARY KEY,"
61 + Profiles.FULL_NAME + " TEXT,"
Ben Murdoch36a23dd2010-10-13 13:20:06 +010062 + Profiles.EMAIL_ADDRESS + " TEXT,"
63 + Profiles.COMPANY_NAME + " TEXT,"
64 + Profiles.ADDRESS_LINE_1 + " TEXT,"
65 + Profiles.ADDRESS_LINE_2 + " TEXT,"
66 + Profiles.CITY + " TEXT,"
67 + Profiles.STATE + " TEXT,"
68 + Profiles.ZIP_CODE + " TEXT,"
69 + Profiles.COUNTRY + " TEXT,"
70 + Profiles.PHONE_NUMBER + " TEXT"
Ben Murdochaf554522010-09-10 22:09:30 +010071 + " );");
72 }
73
74 @Override
75 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
76 Log.w(LOGTAG, "Upgrading database from version " + oldVersion + " to "
77 + newVersion + ", which will destroy all old data");
78 db.execSQL("DROP TABLE IF EXISTS " + PROFILES_TABLE_NAME);
79 onCreate(db);
80 }
81 }
82
83 private AutoFillProfileDatabase(Context context) {
84 mOpenHelper = new AutoFillProfileDatabaseHelper(context);
85 }
86
87 public static AutoFillProfileDatabase getInstance(Context context) {
88 if (sInstance == null) {
89 sInstance = new AutoFillProfileDatabase(context);
90 }
91 return sInstance;
92 }
93
94 private SQLiteDatabase getDatabase(boolean writable) {
95 return writable ? mOpenHelper.getWritableDatabase() : mOpenHelper.getReadableDatabase();
96 }
97
Ben Murdoch0cb81892010-10-08 12:41:33 +010098 public void addOrUpdateProfile(final int id, AutoFillProfile profile) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +010099 final String sql = "INSERT OR REPLACE INTO " + PROFILES_TABLE_NAME + " ("
Ben Murdochaf554522010-09-10 22:09:30 +0100100 + Profiles._ID + ","
101 + Profiles.FULL_NAME + ","
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100102 + Profiles.EMAIL_ADDRESS + ","
103 + Profiles.COMPANY_NAME + ","
104 + Profiles.ADDRESS_LINE_1 + ","
105 + Profiles.ADDRESS_LINE_2 + ","
106 + Profiles.CITY + ","
107 + Profiles.STATE + ","
108 + Profiles.ZIP_CODE + ","
109 + Profiles.COUNTRY + ","
110 + Profiles.PHONE_NUMBER
111 + ") VALUES (?,?,?,?,?,?,?,?,?,?,?);";
112 final Object[] params = { id,
113 profile.getFullName(),
114 profile.getEmailAddress(),
115 profile.getCompanyName(),
116 profile.getAddressLine1(),
117 profile.getAddressLine2(),
118 profile.getCity(),
119 profile.getState(),
120 profile.getZipCode(),
121 profile.getCountry(),
122 profile.getPhoneNumber() };
123 getDatabase(true).execSQL(sql, params);
Ben Murdochaf554522010-09-10 22:09:30 +0100124 }
125
126 public Cursor getProfile(int id) {
Ben Murdoch36a23dd2010-10-13 13:20:06 +0100127 final String[] cols = {
128 Profiles.FULL_NAME,
129 Profiles.EMAIL_ADDRESS,
130 Profiles.COMPANY_NAME,
131 Profiles.ADDRESS_LINE_1,
132 Profiles.ADDRESS_LINE_2,
133 Profiles.CITY,
134 Profiles.STATE,
135 Profiles.ZIP_CODE,
136 Profiles.COUNTRY,
137 Profiles.PHONE_NUMBER
138 };
139
140 final String[] selectArgs = { Integer.toString(id) };
141 return getDatabase(false).query(PROFILES_TABLE_NAME, cols, Profiles._ID + "=?", selectArgs,
Ben Murdochaf554522010-09-10 22:09:30 +0100142 null, null, null, "1");
143 }
144
Ben Murdoch23da30e2010-10-26 15:18:44 +0100145 public void dropProfile(int id) {
146 final String sql = "DELETE FROM " + PROFILES_TABLE_NAME +" WHERE " + Profiles._ID + " = ?;";
147 final Object[] params = { id };
148 getDatabase(true).execSQL(sql, params);
149 }
150
Ben Murdochaf554522010-09-10 22:09:30 +0100151 public void close() {
152 mOpenHelper.close();
153 }
154}