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"; |
| 32 | static final int DATABASE_VERSION = 1; |
| 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"; |
| 42 | } |
| 43 | |
| 44 | private static class AutoFillProfileDatabaseHelper extends SQLiteOpenHelper { |
| 45 | AutoFillProfileDatabaseHelper(Context context) { |
| 46 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void onCreate(SQLiteDatabase db) { |
| 51 | db.execSQL("CREATE TABLE " + PROFILES_TABLE_NAME + " (" |
| 52 | + Profiles._ID + " INTEGER PRIMARY KEY," |
| 53 | + Profiles.FULL_NAME + " TEXT," |
| 54 | + Profiles.EMAIL_ADDRESS + " TEXT" |
| 55 | + " );"); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 60 | Log.w(LOGTAG, "Upgrading database from version " + oldVersion + " to " |
| 61 | + newVersion + ", which will destroy all old data"); |
| 62 | db.execSQL("DROP TABLE IF EXISTS " + PROFILES_TABLE_NAME); |
| 63 | onCreate(db); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private AutoFillProfileDatabase(Context context) { |
| 68 | mOpenHelper = new AutoFillProfileDatabaseHelper(context); |
| 69 | } |
| 70 | |
| 71 | public static AutoFillProfileDatabase getInstance(Context context) { |
| 72 | if (sInstance == null) { |
| 73 | sInstance = new AutoFillProfileDatabase(context); |
| 74 | } |
| 75 | return sInstance; |
| 76 | } |
| 77 | |
| 78 | private SQLiteDatabase getDatabase(boolean writable) { |
| 79 | return writable ? mOpenHelper.getWritableDatabase() : mOpenHelper.getReadableDatabase(); |
| 80 | } |
| 81 | |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 82 | public void addOrUpdateProfile(final int id, AutoFillProfile profile) { |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 83 | final String SQL = "INSERT OR REPLACE INTO " + PROFILES_TABLE_NAME + " (" |
| 84 | + Profiles._ID + "," |
| 85 | + Profiles.FULL_NAME + "," |
| 86 | + Profiles.EMAIL_ADDRESS |
| 87 | + ") VALUES (?,?,?);"; |
Ben Murdoch | 0cb8189 | 2010-10-08 12:41:33 +0100 | [diff] [blame] | 88 | final Object[] PARAMS = { id, profile.getFullName(), profile.getEmailAddress() }; |
Ben Murdoch | af55452 | 2010-09-10 22:09:30 +0100 | [diff] [blame] | 89 | getDatabase(true).execSQL(SQL, PARAMS); |
| 90 | } |
| 91 | |
| 92 | public Cursor getProfile(int id) { |
| 93 | final String[] COLS = {Profiles.FULL_NAME, Profiles.EMAIL_ADDRESS }; |
| 94 | final String[] SEL_ARGS = { Integer.toString(id) }; |
| 95 | return getDatabase(false).query(PROFILES_TABLE_NAME, COLS, Profiles._ID + "=?", SEL_ARGS, |
| 96 | null, null, null, "1"); |
| 97 | } |
| 98 | |
| 99 | public void close() { |
| 100 | mOpenHelper.close(); |
| 101 | } |
| 102 | } |