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 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 = 2; 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 static final String COMPANY_NAME = "companyname"; 42 static final String ADDRESS_LINE_1 = "addressline1"; 43 static final String ADDRESS_LINE_2 = "addressline2"; 44 static final String CITY = "city"; 45 static final String STATE = "state"; 46 static final String ZIP_CODE = "zipcode"; 47 static final String COUNTRY = "country"; 48 static final String PHONE_NUMBER = "phone"; 49 } 50 51 private static class AutoFillProfileDatabaseHelper extends SQLiteOpenHelper { 52 AutoFillProfileDatabaseHelper(Context context) { 53 super(context, DATABASE_NAME, null, DATABASE_VERSION); 54 } 55 56 @Override 57 public void onCreate(SQLiteDatabase db) { 58 db.execSQL("CREATE TABLE " + PROFILES_TABLE_NAME + " (" 59 + Profiles._ID + " INTEGER PRIMARY KEY," 60 + Profiles.FULL_NAME + " TEXT," 61 + Profiles.EMAIL_ADDRESS + " TEXT," 62 + Profiles.COMPANY_NAME + " TEXT," 63 + Profiles.ADDRESS_LINE_1 + " TEXT," 64 + Profiles.ADDRESS_LINE_2 + " TEXT," 65 + Profiles.CITY + " TEXT," 66 + Profiles.STATE + " TEXT," 67 + Profiles.ZIP_CODE + " TEXT," 68 + Profiles.COUNTRY + " TEXT," 69 + Profiles.PHONE_NUMBER + " TEXT" 70 + " );"); 71 } 72 73 @Override 74 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 75 Log.w(LOGTAG, "Upgrading database from version " + oldVersion + " to " 76 + newVersion + ", which will destroy all old data"); 77 db.execSQL("DROP TABLE IF EXISTS " + PROFILES_TABLE_NAME); 78 onCreate(db); 79 } 80 } 81 82 private AutoFillProfileDatabase(Context context) { 83 mOpenHelper = new AutoFillProfileDatabaseHelper(context); 84 } 85 86 public static AutoFillProfileDatabase getInstance(Context context) { 87 if (sInstance == null) { 88 sInstance = new AutoFillProfileDatabase(context); 89 } 90 return sInstance; 91 } 92 93 private SQLiteDatabase getDatabase(boolean writable) { 94 return writable ? mOpenHelper.getWritableDatabase() : mOpenHelper.getReadableDatabase(); 95 } 96 97 public Cursor getProfile(int id) { 98 final String[] cols = { 99 Profiles.FULL_NAME, 100 Profiles.EMAIL_ADDRESS, 101 Profiles.COMPANY_NAME, 102 Profiles.ADDRESS_LINE_1, 103 Profiles.ADDRESS_LINE_2, 104 Profiles.CITY, 105 Profiles.STATE, 106 Profiles.ZIP_CODE, 107 Profiles.COUNTRY, 108 Profiles.PHONE_NUMBER 109 }; 110 111 final String[] selectArgs = { Integer.toString(id) }; 112 return getDatabase(false).query(PROFILES_TABLE_NAME, cols, Profiles._ID + "=?", selectArgs, 113 null, null, null, "1"); 114 } 115 116 public void dropProfile(int id) { 117 final String sql = "DELETE FROM " + PROFILES_TABLE_NAME +" WHERE " + Profiles._ID + " = ?;"; 118 final Object[] params = { id }; 119 getDatabase(true).execSQL(sql, params); 120 } 121 122 public void close() { 123 mOpenHelper.close(); 124 } 125} 126