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