Home | History | Annotate | Download | only in browser
      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;
     25 import android.webkit.WebSettingsClassic.AutoFillProfile;
     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 = 2;
     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         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";
     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,"
     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"
     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 
     98     public void addOrUpdateProfile(final int id, AutoFillProfile profile) {
     99         final String sql = "INSERT OR REPLACE INTO " + PROFILES_TABLE_NAME + " ("
    100                 + Profiles._ID + ","
    101                 + Profiles.FULL_NAME + ","
    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);
    124     }
    125 
    126     public Cursor getProfile(int id) {
    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,
    142                 null, null, null, "1");
    143     }
    144 
    145     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 
    151     public void close() {
    152         mOpenHelper.close();
    153     }
    154 }
    155