public void importContacts() throws Exception { String path = mContext.getDatabasePath(DATABASE_NAME).getPath(); Log.w(TAG, "Importing contacts from " + path); if (!new File(path).exists()) { Log.i(TAG, "Legacy contacts database does not exist"); return; } for (int i = 0; i < MAX_ATTEMPTS; i++) { try { mSourceDb = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); importContactsFromLegacyDb(); Log.i(TAG, "Imported legacy contacts: " + mContactCount); mContactsProvider.notifyChange(); return; } catch (SQLiteException e) { Log.e(TAG, "Database import exception. Will retry in " + DELAY_BETWEEN_ATTEMPTS + "ms", e); // We could get a "database locked" exception here, in which // case we should retry Thread.sleep(DELAY_BETWEEN_ATTEMPTS); } finally { if (mSourceDb != null) { mSourceDb.close(); } } } }
public boolean importContacts() throws Exception { String path = mContext.getDatabasePath(DATABASE_NAME).getPath(); File file = new File(path); if (!file.exists()) { Log.i(TAG, "Legacy contacts database does not exist at " + path); return true; } Log.w(TAG, "Importing contacts from " + path); for (int i = 0; i < MAX_ATTEMPTS; i++) { try { mSourceDb = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); importContactsFromLegacyDb(); Log.i(TAG, "Imported legacy contacts: " + mContactCount); mContactsProvider.notifyChange(); return true; } catch (SQLiteException e) { Log.e(TAG, "Database import exception. Will retry in " + DELAY_BETWEEN_ATTEMPTS + "ms", e); // We could get a "database locked" exception here, in which // case we should retry Thread.sleep(DELAY_BETWEEN_ATTEMPTS); } finally { if (mSourceDb != null) { mSourceDb.close(); } } } long oldDatabaseSize = file.length(); mEstimatedStorageRequirement = oldDatabaseSize * DATABASE_SIZE_MULTIPLIER / 1024 / 1024; if (mEstimatedStorageRequirement < DATABASE_MIN_SIZE) { mEstimatedStorageRequirement = DATABASE_MIN_SIZE; } return false; }