1 /* 2 * Copyright (C) 2009 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.providers.contacts; 18 19 import android.accounts.Account; 20 import android.content.Context; 21 import android.database.sqlite.SQLiteDatabase; 22 23 import java.util.Locale; 24 25 /** 26 * A version of {@link ContactsProvider2} class that performs aggregation 27 * synchronously and wipes all data at construction time. 28 */ 29 public class SynchronousContactsProvider2 extends ContactsProvider2 { 30 public static final String READ_ONLY_ACCOUNT_TYPE = "ro"; 31 32 private static Boolean sDataWiped = false; 33 private static ContactsDatabaseHelper mDbHelper; 34 private boolean mDataWipeEnabled = true; 35 private Account mAccount; 36 private boolean mNetworkNotified; 37 38 @Override getDatabaseHelper(final Context context)39 protected ContactsDatabaseHelper getDatabaseHelper(final Context context) { 40 if (mDbHelper == null) { 41 mDbHelper = new ContactsDatabaseHelper(context); 42 } 43 return mDbHelper; 44 } 45 resetOpenHelper()46 public static void resetOpenHelper() { 47 mDbHelper = null; 48 } 49 setDataWipeEnabled(boolean flag)50 public void setDataWipeEnabled(boolean flag) { 51 mDataWipeEnabled = flag; 52 } 53 54 @Override onBeginTransaction()55 protected void onBeginTransaction() { 56 super.onBeginTransaction(); 57 mNetworkNotified = false; 58 } 59 60 @Override notifyChange(boolean syncToNetwork)61 protected void notifyChange(boolean syncToNetwork) { 62 mNetworkNotified |= syncToNetwork; 63 } 64 isNetworkNotified()65 public boolean isNetworkNotified() { 66 return mNetworkNotified; 67 } 68 69 @Override onCreate()70 public boolean onCreate() { 71 boolean created = super.onCreate(); 72 if (mDataWipeEnabled) { 73 synchronized (sDataWiped) { 74 if (!sDataWiped) { 75 sDataWiped = true; 76 wipeData(); 77 } 78 } 79 } 80 return created; 81 } 82 83 @Override verifyAccounts()84 protected void verifyAccounts() { 85 } 86 87 @Override verifyLocale()88 protected void verifyLocale() { 89 } 90 91 @Override getDefaultAccount()92 protected Account getDefaultAccount() { 93 if (mAccount == null) { 94 mAccount = new Account("androidtest@gmail.com", "com.google"); 95 } 96 return mAccount; 97 } 98 99 /** 100 * Creates a mock PhotoPriorityResolver 101 */ 102 @Override createPhotoPriorityResolver(Context context)103 PhotoPriorityResolver createPhotoPriorityResolver(Context context) { 104 return new PhotoPriorityResolver(context) { 105 @Override 106 public synchronized int getPhotoPriority(String accountType) { 107 if ("cupcake".equals(accountType)) { 108 return 3; 109 } 110 if ("donut".equals(accountType)) { 111 return 2; 112 } 113 if ("froyo".equals(accountType)) { 114 return 1; 115 } 116 return 0; 117 } 118 }; 119 } 120 121 @Override 122 protected Locale getLocale() { 123 return Locale.US; 124 } 125 126 @Override 127 protected boolean isWritableAccount(String accountType) { 128 return !READ_ONLY_ACCOUNT_TYPE.equals(accountType); 129 } 130 131 public void prepareForFullAggregation(int maxContact) { 132 SQLiteDatabase db = getDatabaseHelper().getWritableDatabase(); 133 db.execSQL("UPDATE raw_contacts SET aggregation_mode=0,aggregation_needed=1;"); 134 long rowId = 135 db.compileStatement("SELECT _id FROM raw_contacts LIMIT 1 OFFSET " + maxContact) 136 .simpleQueryForLong(); 137 db.execSQL("DELETE FROM raw_contacts WHERE _id > " + rowId + ";"); 138 } 139 140 public long getRawContactCount() { 141 SQLiteDatabase db = getDatabaseHelper().getReadableDatabase(); 142 return db.compileStatement("SELECT COUNT(*) FROM raw_contacts").simpleQueryForLong(); 143 } 144 145 public long getContactCount() { 146 SQLiteDatabase db = getDatabaseHelper().getReadableDatabase(); 147 return db.compileStatement("SELECT COUNT(*) FROM contacts").simpleQueryForLong(); 148 } 149 150 @Override 151 public void wipeData() { 152 super.wipeData(); 153 SQLiteDatabase db = getDatabaseHelper().getWritableDatabase(); 154 db.execSQL("replace into SQLITE_SEQUENCE (name,seq) values('raw_contacts', 42)"); 155 db.execSQL("replace into SQLITE_SEQUENCE (name,seq) values('contacts', 2009)"); 156 db.execSQL("replace into SQLITE_SEQUENCE (name,seq) values('data', 777)"); 157 } 158 159 @Override 160 protected boolean isLegacyContactImportNeeded() { 161 162 // We have an explicit test for data conversion - no need to do it every time 163 return false; 164 } 165 } 166