Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.database.Cursor; |
| 21 | import android.database.sqlite.SQLiteDatabase; |
| 22 | import android.database.sqlite.SQLiteOpenHelper; |
| 23 | import android.provider.BaseColumns; |
| 24 | import android.util.Log; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 25 | import android.webkit.WebSettings.AutoFillProfile; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 26 | |
| 27 | public class AutoFillProfileDatabase { |
| 28 | |
| 29 | static final String LOGTAG = "AutoFillProfileDatabase"; |
| 30 | |
| 31 | static final String DATABASE_NAME = "autofill.db"; |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame^] | 32 | static final int DATABASE_VERSION = 2; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 33 | 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 Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame^] | 42 | 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 50 | } |
| 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 Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame^] | 62 | + 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 71 | + " );"); |
| 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 Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 98 | public void addOrUpdateProfile(final int id, AutoFillProfile profile) { |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame^] | 99 | final String sql = "INSERT OR REPLACE INTO " + PROFILES_TABLE_NAME + " (" |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 100 | + Profiles._ID + "," |
| 101 | + Profiles.FULL_NAME + "," |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame^] | 102 | + 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | public Cursor getProfile(int id) { |
Ben Murdoch | 36a23dd | 2010-10-13 13:20:06 +0100 | [diff] [blame^] | 127 | 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 Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 142 | null, null, null, "1"); |
| 143 | } |
| 144 | |
| 145 | public void close() { |
| 146 | mOpenHelper.close(); |
| 147 | } |
| 148 | } |