Back to project page android-memento.
The source code is released under:
Apache License
If you think the Android project android-memento listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * android-memento-lib https://github.com/twofortyfouram/android-memento * Copyright 2014 two forty four a.m. LLC */*from w w w . j av a 2 s .co m*/ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package com.twofortyfouram.memento.provider.sqlite; import com.twofortyfouram.assertion.Assertions; import com.twofortyfouram.log.Lumberjack; import com.twofortyfouram.spackle.util.AndroidSdkVersion; import net.jcip.annotations.ThreadSafe; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.database.DatabaseErrorHandler; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; /** * This class offers backwards compatibility with older Android SDKs by automatically calling the * {@link #onConfigure(SQLiteDatabase)} method. In addition, this class can handle enabling foreign * key constraints. */ @ThreadSafe public abstract class SqliteOpenHelperCompat extends SQLiteOpenHelper { public SqliteOpenHelperCompat(@NonNull final Context context, @Nullable final String name, @Nullable final CursorFactory factory, final int version) { super(context, name, factory, version); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public SqliteOpenHelperCompat(@NonNull final Context context, @Nullable final String name, @Nullable final CursorFactory factory, final int version, @Nullable final DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } @Override @SuppressLint("NewApi") public void onOpen(final SQLiteDatabase db) { super.onOpen(db); Lumberjack .v("Database opened; SQLite library version is: %s", DatabaseUtils .stringForQuery(db, "select sqlite_version()", null)); //$NON-NLS-1$ //$NON-NLS-2$ /* * Since onConfigure() won't be called on older SDKs, explicitly call it here. */ if (!AndroidSdkVersion.isAtLeastSdk(Build.VERSION_CODES.JELLY_BEAN)) { onConfigure(db); } } @Override @SuppressLint("NewApi") public void onConfigure(final SQLiteDatabase db) { if (AndroidSdkVersion.isAtLeastSdk(Build.VERSION_CODES.JELLY_BEAN)) { super.onConfigure(db); } if (isForeignKeyConstraintsEnabled()) { setForeignKeyConstraintsCompat(db); } } @SuppressLint("NewApi") private static void setForeignKeyConstraintsCompat(@NonNull final SQLiteDatabase db) { Assertions.assertNotNull(db, "db"); //$NON-NLS-1$ if (AndroidSdkVersion.isAtLeastSdk(Build.VERSION_CODES.JELLY_BEAN)) { db.setForeignKeyConstraintsEnabled(true); } else { db.execSQL("PRAGMA foreign_keys = ON;"); //$NON-NLS-1$ } } /** * @return True if foreign key constraints should be enabled. */ protected abstract boolean isForeignKeyConstraintsEnabled(); }