private void initCacheDatabase(Context context) { assert !JniUtil.useChromiumHttpStack(); try { mCacheDatabase = context.openOrCreateDatabase(CACHE_DATABASE_FILE, 0, null); } catch (SQLiteException e) { // try again by deleting the old db and create a new one if (context.deleteDatabase(CACHE_DATABASE_FILE)) { mCacheDatabase = context.openOrCreateDatabase(CACHE_DATABASE_FILE, 0, null); } } mCacheDatabase.enableWriteAheadLogging(); // mCacheDatabase should not be null, // the only case is RequestAPI test has problem to create db if (mCacheDatabase == null) { mInitialized = true; notify(); return; } if (mCacheDatabase.getVersion() != CACHE_DATABASE_VERSION) { mCacheDatabase.beginTransactionNonExclusive(); try { upgradeCacheDatabase(); bootstrapCacheDatabase(); mCacheDatabase.setTransactionSuccessful(); } finally { mCacheDatabase.endTransaction(); } // Erase the files from the file system in the // case that the database was updated and the // there were existing cache content CacheManager.removeAllCacheFiles(); } // use read_uncommitted to speed up READ mCacheDatabase.execSQL("PRAGMA read_uncommitted = true;"); // as only READ can be called in the // non-WebViewWorkerThread, and read_uncommitted is used, // we can turn off database lock to use transaction. mCacheDatabase.setLockingEnabled(false); // use InsertHelper for faster insertion mCacheInserter = new DatabaseUtils.InsertHelper(mCacheDatabase, "cache"); mCacheUrlColIndex = mCacheInserter.getColumnIndex(CACHE_URL_COL); mCacheFilePathColIndex = mCacheInserter.getColumnIndex(CACHE_FILE_PATH_COL); mCacheLastModifyColIndex = mCacheInserter.getColumnIndex(CACHE_LAST_MODIFY_COL); mCacheETagColIndex = mCacheInserter.getColumnIndex(CACHE_ETAG_COL); mCacheExpiresColIndex = mCacheInserter.getColumnIndex(CACHE_EXPIRES_COL); mCacheExpiresStringColIndex = mCacheInserter.getColumnIndex(CACHE_EXPIRES_STRING_COL); mCacheMimeTypeColIndex = mCacheInserter.getColumnIndex(CACHE_MIMETYPE_COL); mCacheEncodingColIndex = mCacheInserter.getColumnIndex(CACHE_ENCODING_COL); mCacheHttpStatusColIndex = mCacheInserter.getColumnIndex(CACHE_HTTP_STATUS_COL); mCacheLocationColIndex = mCacheInserter.getColumnIndex(CACHE_LOCATION_COL); mCacheContentLengthColIndex = mCacheInserter.getColumnIndex(CACHE_CONTENTLENGTH_COL); mCacheContentDispositionColIndex = mCacheInserter.getColumnIndex(CACHE_CONTENTDISPOSITION_COL); mCacheCrossDomainColIndex = mCacheInserter.getColumnIndex(CACHE_CROSSDOMAIN_COL); }
private boolean addDefaultBookmarks(SQLiteDatabase db, long parentId, long firstBookmarkId) { long bookmarkId = firstBookmarkId; Resources res = getContext().getResources(); final CharSequence[] bookmarks = res.getTextArray(R.array.bookmarks); int size = bookmarks.length; TypedArray preloads = res.obtainTypedArray(R.array.bookmark_preloads); DatabaseUtils.InsertHelper insertHelper = null; try { insertHelper = new DatabaseUtils.InsertHelper(db, TABLE_BOOKMARKS); final int idColumn = insertHelper.getColumnIndex(PartnerBookmarksContract.Bookmarks.ID); final int titleColumn = insertHelper.getColumnIndex(PartnerBookmarksContract.Bookmarks.TITLE); final int urlColumn = insertHelper.getColumnIndex(PartnerBookmarksContract.Bookmarks.URL); final int typeColumn = insertHelper.getColumnIndex(PartnerBookmarksContract.Bookmarks.TYPE); final int parentColumn = insertHelper.getColumnIndex(PartnerBookmarksContract.Bookmarks.PARENT); final int faviconColumn = insertHelper.getColumnIndex(PartnerBookmarksContract.Bookmarks.FAVICON); final int touchiconColumn = insertHelper.getColumnIndex(PartnerBookmarksContract.Bookmarks.TOUCHICON); for (int i = 0; i + 1 < size; i = i + 2) { CharSequence bookmarkDestination = bookmarks[i + 1]; String bookmarkTitle = bookmarks[i].toString(); String bookmarkUrl = bookmarkDestination.toString(); byte[] favicon = null; if (i < preloads.length()) { int faviconId = preloads.getResourceId(i, 0); try { favicon = readRaw(res, faviconId); } catch (IOException e) { Log.i(TAG, "Failed to read favicon for " + bookmarkTitle, e); } } byte[] touchicon = null; if (i + 1 < preloads.length()) { int touchiconId = preloads.getResourceId(i + 1, 0); try { touchicon = readRaw(res, touchiconId); } catch (IOException e) { Log.i(TAG, "Failed to read touchicon for " + bookmarkTitle, e); } } insertHelper.prepareForInsert(); insertHelper.bind(idColumn, bookmarkId); insertHelper.bind(titleColumn, bookmarkTitle); insertHelper.bind(urlColumn, bookmarkUrl); insertHelper.bind(typeColumn, PartnerBookmarksContract.Bookmarks.BOOKMARK_TYPE_BOOKMARK); insertHelper.bind(parentColumn, parentId); if (favicon != null) { insertHelper.bind(faviconColumn, favicon); } if (touchicon != null) { insertHelper.bind(touchiconColumn, touchicon); } bookmarkId++; if (insertHelper.execute() == -1) { Log.i(TAG, "Failed to insert bookmark " + bookmarkTitle); return false; } } } finally { preloads.recycle(); insertHelper.close(); } return true; }