Java Code Examples for android.content.Context#openOrCreateDatabase()
The following examples show how to use
android.content.Context#openOrCreateDatabase() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UniversalSearchContractTest.java From android-tv-leanback with Apache License 2.0 | 6 votes |
private SQLiteDatabase createTables(final boolean delete, final UniversalSearchContract.Table... tables) { SQLiteDatabase db = null; final String filenamePrefix = "test."; RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( new MockContext2(), // The context that most methods are //delegated to getContext(), // The context that file methods are delegated to filenamePrefix); final Context context = new IsolatedContext(super.getContext().getContentResolver(), targetContextWrapper); try { db = context.openOrCreateDatabase(DATABASE, 1, null); for (final UniversalSearchContract.Table table : tables) { table.onCreate(db); log("Table " + table + " onCreate successful"); } return db; } finally { if (null != db && delete) { db.close(); context.deleteDatabase(DATABASE); } } }
Example 2
Source File: TasksNeedExecuteReceiver.java From FreezeYou with Apache License 2.0 | 6 votes |
@Override public void onReceive(Context context, Intent intent) { int id = intent.getIntExtra("id", -5); int hour = intent.getIntExtra("hour", -1); int minute = intent.getIntExtra("minute", -1); String task = intent.getStringExtra("task"); String repeat = intent.getStringExtra("repeat"); if (id != -6) {//-6为延时任务 if ("0".equals(repeat) && id != -5) { SQLiteDatabase db = context.openOrCreateDatabase("scheduledTasks", Context.MODE_PRIVATE, null); db.execSQL("UPDATE tasks SET enabled = 0 WHERE _id = " + id + ";"); db.close(); } else { TasksUtils.publishTask(context, id, hour, minute, repeat, task); } } if (task != null && !"".equals(task)) { TasksUtils.runTask(task, context.getApplicationContext(), null); } }
Example 3
Source File: TasksUtils.java From FreezeYou with Apache License 2.0 | 6 votes |
public static void checkTimeTasks(Context context) { SQLiteDatabase db = context.openOrCreateDatabase("scheduledTasks", Context.MODE_PRIVATE, null); db.execSQL( "create table if not exists tasks(_id integer primary key autoincrement,hour integer(2),minutes integer(2),repeat varchar,enabled integer(1),label varchar,task varchar,column1 varchar,column2 varchar)" ); final Cursor cursor = db.query("tasks", null, null, null, null, null, null); if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { int id = cursor.getInt(cursor.getColumnIndex("_id")); String repeat = cursor.getString(cursor.getColumnIndex("repeat")); int hour = cursor.getInt(cursor.getColumnIndex("hour")); int minutes = cursor.getInt(cursor.getColumnIndex("minutes")); int enabled = cursor.getInt(cursor.getColumnIndex("enabled")); String task = cursor.getString(cursor.getColumnIndex("task")); TasksUtils.cancelTheTask(context, id); if (enabled == 1) { publishTask(context, id, hour, minutes, repeat, task); } cursor.moveToNext(); } } cursor.close(); db.close(); }
Example 4
Source File: TasksUtils.java From FreezeYou with Apache License 2.0 | 5 votes |
public static void onUFApplications(Context context, String pkgNameString) { DataStatisticsUtils.addUFreezeTimes(context, pkgNameString); final SQLiteDatabase db = context.openOrCreateDatabase("scheduledTriggerTasks", Context.MODE_PRIVATE, null); db.execSQL( "create table if not exists tasks(_id integer primary key autoincrement,tg varchar,tgextra varchar,enabled integer(1),label varchar,task varchar,column1 varchar,column2 varchar)" ); Cursor cursor = db.query("tasks", null, null, null, null, null, null); if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { String tgExtra = cursor.getString(cursor.getColumnIndex("tgextra")); if (tgExtra == null) { tgExtra = ""; } String tg = cursor.getString(cursor.getColumnIndex("tg")); int enabled = cursor.getInt(cursor.getColumnIndex("enabled")); if (enabled == 1 && "onUFApplications".equals(tg) && ("".equals(tgExtra) || Arrays.asList(OneKeyListUtils.decodeUserListsInPackageNames(context, tgExtra.split(","))).contains(pkgNameString))) { String task = cursor.getString(cursor.getColumnIndex("task")); if (task != null && !"".equals(task)) { runTask(task.replace("[cpkgn]", pkgNameString), context, null); } } cursor.moveToNext(); } } cursor.close(); db.close(); }
Example 5
Source File: AccountDb.java From google-authenticator-android with Apache License 2.0 | 5 votes |
private SQLiteDatabase openDatabase(Context context) { for (int count = 0; true; count++) { try { return context.openOrCreateDatabase(FileUtilities.DATABASES_PATH, Context.MODE_PRIVATE, null); } catch (SQLiteException e) { if (count < 2) { continue; } else { throw new AccountDbOpenException("Failed to open AccountDb database in three tries.\n" + FileUtilities.getFilesystemInfoForErrorString(context), e); } } } }
Example 6
Source File: TasksUtils.java From FreezeYou with Apache License 2.0 | 5 votes |
public static void checkTriggerTasks(Context context) { //事件触发器 final SQLiteDatabase db = context.openOrCreateDatabase("scheduledTriggerTasks", Context.MODE_PRIVATE, null); db.execSQL( "create table if not exists tasks(_id integer primary key autoincrement,tg varchar,tgextra varchar,enabled integer(1),label varchar,task varchar,column1 varchar,column2 varchar)" ); final Cursor cursor = db.query("tasks", null, null, null, null, null, null); if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { String tg = cursor.getString(cursor.getColumnIndex("tg")); int enabled = cursor.getInt(cursor.getColumnIndex("enabled")); if (enabled == 1) { if (tg == null) { tg = ""; } switch (tg) { case "onScreenOn": startService(context, new Intent(context, TriggerTasksService.class) .putExtra("OnScreenOn", true)); break; case "onScreenOff": startService(context, new Intent(context, TriggerTasksService.class) .putExtra("OnScreenOff", true)); break; default: break; } } cursor.moveToNext(); } } cursor.close(); db.close(); }
Example 7
Source File: TasksUtils.java From FreezeYou with Apache License 2.0 | 5 votes |
public static void onFApplications(Context context, String pkgNameString) { DataStatisticsUtils.addFreezeTimes(context, pkgNameString); final SQLiteDatabase db = context.openOrCreateDatabase("scheduledTriggerTasks", Context.MODE_PRIVATE, null); db.execSQL( "create table if not exists tasks(_id integer primary key autoincrement,tg varchar,tgextra varchar,enabled integer(1),label varchar,task varchar,column1 varchar,column2 varchar)" ); Cursor cursor = db.query("tasks", null, null, null, null, null, null); if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { String tg = cursor.getString(cursor.getColumnIndex("tg")); String tgExtra = cursor.getString(cursor.getColumnIndex("tgextra")); int enabled = cursor.getInt(cursor.getColumnIndex("enabled")); if (tgExtra == null) { tgExtra = ""; } if (enabled == 1 && "onFApplications".equals(tg) && ("".equals(tgExtra) || Arrays.asList(OneKeyListUtils.decodeUserListsInPackageNames(context, tgExtra.split(","))).contains(pkgNameString))) { String task = cursor.getString(cursor.getColumnIndex("task")); if (task != null && !"".equals(task)) { runTask(task.replace("[cpkgn]", pkgNameString), context, null); } } cursor.moveToNext(); } } cursor.close(); db.close(); }
Example 8
Source File: DatabaseUtils.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Creates a db and populates it with the sql statements in sqlStatements. * * @param context the context to use to create the db * @param dbName the name of the db to create * @param dbVersion the version to set on the db * @param sqlStatements the statements to use to populate the db. This should be a single string * of the form returned by sqlite3's <tt>.dump</tt> command (statements separated by * semicolons) */ static public void createDbFromSqlStatements( Context context, String dbName, int dbVersion, String sqlStatements) { SQLiteDatabase db = context.openOrCreateDatabase(dbName, 0, null); // TODO: this is not quite safe since it assumes that all semicolons at the end of a line // terminate statements. It is possible that a text field contains ;\n. We will have to fix // this if that turns out to be a problem. String[] statements = TextUtils.split(sqlStatements, ";\n"); for (String statement : statements) { if (TextUtils.isEmpty(statement)) continue; db.execSQL(statement); } db.setVersion(dbVersion); db.close(); }
Example 9
Source File: HttpAuthDatabase.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
/** * Opens the database, and upgrades it if necessary. * * @param context the Context to use for opening the database * @param databaseFile Name of the file to be initialized. */ private void initDatabase(Context context, String databaseFile) { try { mDatabase = context.openOrCreateDatabase(databaseFile, 0, null); } catch (SQLiteException e) { // try again by deleting the old db and create a new one if (context.deleteDatabase(databaseFile)) { mDatabase = context.openOrCreateDatabase(databaseFile, 0, null); } } if (mDatabase == null) { // Not much we can do to recover at this point Log.e(LOGTAG, "Unable to open or create " + databaseFile); return; } if (mDatabase.getVersion() != DATABASE_VERSION) { mDatabase.beginTransactionNonExclusive(); try { createTable(); mDatabase.setTransactionSuccessful(); } finally { mDatabase.endTransaction(); } } }
Example 10
Source File: HttpAuthDatabase.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
/** * Opens the database, and upgrades it if necessary. * * @param context the Context to use for opening the database * @param databaseFile Name of the file to be initialized. */ private void initDatabase(Context context, String databaseFile) { try { mDatabase = context.openOrCreateDatabase(databaseFile, 0, null); } catch (SQLiteException e) { // try again by deleting the old db and create a new one if (context.deleteDatabase(databaseFile)) { mDatabase = context.openOrCreateDatabase(databaseFile, 0, null); } } if (mDatabase == null) { // Not much we can do to recover at this point Log.e(LOGTAG, "Unable to open or create " + databaseFile); return; } if (mDatabase.getVersion() != DATABASE_VERSION) { mDatabase.beginTransactionNonExclusive(); try { createTable(); mDatabase.setTransactionSuccessful(); } finally { mDatabase.endTransaction(); } } }
Example 11
Source File: CityProvider.java From WayHoo with Apache License 2.0 | 5 votes |
public static void createTmpCityTable(Context context) { SQLiteDatabase db = context.openOrCreateDatabase( SystemUtils.getDBFilePath(context), Context.MODE_PRIVATE, null); L.i("liweiping", "create table tmpcity ...."); db.execSQL("CREATE table IF NOT EXISTS " + TMPCITY_TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, postID TEXT," + " refreshTime TEXT, isLocation TEXT, pubTime TEXT, weatherInfo TEXT, orderIndex INTEGER)"); }
Example 12
Source File: DataStatisticsUtils.java From FreezeYou with Apache License 2.0 | 4 votes |
public static void addUseTimes(Context context, String pkgNameString) { SQLiteDatabase db = context.openOrCreateDatabase("ApplicationsUseTimes", Context.MODE_PRIVATE, null); addTimes(db, pkgNameString); db.close(); }
Example 13
Source File: DataStatisticsUtils.java From FreezeYou with Apache License 2.0 | 4 votes |
public static void addUFreezeTimes(Context context, String pkgNameString) { SQLiteDatabase db = context.openOrCreateDatabase("ApplicationsUFreezeTimes", Context.MODE_PRIVATE, null); addTimes(db, pkgNameString); db.close(); }
Example 14
Source File: DataStatisticsUtils.java From FreezeYou with Apache License 2.0 | 4 votes |
public static void addFreezeTimes(Context context, String pkgNameString) { SQLiteDatabase db = context.openOrCreateDatabase("ApplicationsFreezeTimes", Context.MODE_PRIVATE, null); addTimes(db, pkgNameString); db.close(); }
Example 15
Source File: BackupUtils.java From FreezeYou with Apache License 2.0 | 4 votes |
private static boolean importUserDefinedCategoriesJSONArray(Context context, JSONObject jsonObject) { JSONArray userDefinedCategoriesJSONArray = jsonObject.optJSONArray("userDefinedCategories"); if (userDefinedCategoriesJSONArray == null) { return false; } SQLiteDatabase db = context.openOrCreateDatabase("userDefinedCategories", MODE_PRIVATE, null); db.execSQL( "create table if not exists categories(_id integer primary key autoincrement,label varchar,packages varchar)" ); ArrayList<String> existedLabels = new ArrayList<>(); Cursor cursor = db.query("categories", new String[]{"label"}, null, null, null, null, null); if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { existedLabels.add(cursor.getString(cursor.getColumnIndex("label"))); cursor.moveToNext(); } } cursor.close(); JSONObject oneUserDefinedCategoriesJSONObject; boolean isCompletelySuccess = true; for (int i = 0; i < userDefinedCategoriesJSONArray.length(); ++i) { try { oneUserDefinedCategoriesJSONObject = userDefinedCategoriesJSONArray.optJSONObject(i); if (oneUserDefinedCategoriesJSONObject == null) { isCompletelySuccess = false; continue; } if (oneUserDefinedCategoriesJSONObject.optBoolean("doNotImport", false)) { continue; } String label = oneUserDefinedCategoriesJSONObject.getString("label"); if (existedLabels.contains(label)) { db.execSQL( "update categories set packages = '" + oneUserDefinedCategoriesJSONObject.getString("packages") + "' where label = '" + label + "';" ); } else { db.execSQL( "insert into categories(_id,label,packages) VALUES ( " + null + ",'" + label + "','" + oneUserDefinedCategoriesJSONObject.getString("packages") + "');" ); } } catch (JSONException e) { isCompletelySuccess = false; } } db.close(); return isCompletelySuccess; }
Example 16
Source File: BackupUtils.java From FreezeYou with Apache License 2.0 | 4 votes |
private static boolean importUserTriggerTasksJSONArray(Context context, JSONObject jsonObject) { JSONArray userTriggerScheduledTasksJSONArray = jsonObject.optJSONArray("userTriggerScheduledTasks"); if (userTriggerScheduledTasksJSONArray == null) { return false; } SQLiteDatabase db = context.openOrCreateDatabase("scheduledTriggerTasks", MODE_PRIVATE, null); db.execSQL( "create table if not exists tasks(_id integer primary key autoincrement,tg varchar,tgextra varchar,enabled integer(1),label varchar,task varchar,column1 varchar,column2 varchar)" ); JSONObject oneUserTriggerScheduledTaskJSONObject; boolean isCompletelySuccess = true; for (int i = 0; i < userTriggerScheduledTasksJSONArray.length(); ++i) { try { oneUserTriggerScheduledTaskJSONObject = userTriggerScheduledTasksJSONArray.optJSONObject(i); if (oneUserTriggerScheduledTaskJSONObject == null) { isCompletelySuccess = false; continue; } if (oneUserTriggerScheduledTaskJSONObject.optBoolean("doNotImport", false)) { continue; } db.execSQL( "insert into tasks(_id,tg,tgextra,enabled,label,task,column1,column2) VALUES (null," + "'" + oneUserTriggerScheduledTaskJSONObject.getString("tg") + "'" + "," + "'" + oneUserTriggerScheduledTaskJSONObject.getString("tgextra") + "'" + "," + oneUserTriggerScheduledTaskJSONObject.getInt("enabled") + "," + "'" + oneUserTriggerScheduledTaskJSONObject.getString("label") + "'" + "," + "'" + oneUserTriggerScheduledTaskJSONObject.getString("task") + "'" + ",'','')" ); } catch (JSONException e) { isCompletelySuccess = false; } } db.close(); TasksUtils.checkTriggerTasks(context); return isCompletelySuccess; }
Example 17
Source File: OneKeyListUtils.java From FreezeYou with Apache License 2.0 | 4 votes |
public static String[] decodeUserListsInPackageNames(Context context, String[] pkgs) { StringBuilder result = new StringBuilder(); SQLiteDatabase userDefinedDb = context.openOrCreateDatabase("userDefinedCategories", Context.MODE_PRIVATE, null); for (String pkg : pkgs) { if ("".equals(pkg.trim())) { continue; } if (pkg.startsWith("@")) { try { String labelBase64 = Base64.encodeToString( Base64.decode(pkg.substring(1), Base64.DEFAULT), Base64.DEFAULT ); userDefinedDb.execSQL( "create table if not exists categories(_id integer primary key autoincrement,label varchar,packages varchar)" ); Cursor cursor = userDefinedDb.query( "categories", new String[]{"packages"}, "label = '" + labelBase64 + "'", null, null, null, null ); if (cursor.moveToFirst()) { result.append(cursor.getString(cursor.getColumnIndex("packages"))); } cursor.close(); userDefinedDb.close(); } catch (Exception e) { e.printStackTrace(); } } else { result.append(pkg); } if (result.length() != 0 && result.charAt(result.length() - 1) != ',') { result.append(","); } } return result.toString().split(","); }
Example 18
Source File: BackupUtils.java From FreezeYou with Apache License 2.0 | 4 votes |
private static boolean importUserTimeTasksJSONArray(Context context, JSONObject jsonObject) { JSONArray userTimeScheduledTasksJSONArray = jsonObject.optJSONArray("userTimeScheduledTasks"); if (userTimeScheduledTasksJSONArray == null) { return false; } SQLiteDatabase db = context.openOrCreateDatabase("scheduledTasks", MODE_PRIVATE, null); db.execSQL( "create table if not exists tasks(_id integer primary key autoincrement,hour integer(2),minutes integer(2),repeat varchar,enabled integer(1),label varchar,task varchar,column1 varchar,column2 varchar)" ); boolean isCompletelySuccess = true; JSONObject oneUserTimeScheduledTaskJSONObject; for (int i = 0; i < userTimeScheduledTasksJSONArray.length(); ++i) { try { oneUserTimeScheduledTaskJSONObject = userTimeScheduledTasksJSONArray.optJSONObject(i); if (oneUserTimeScheduledTaskJSONObject == null) { isCompletelySuccess = false; continue; } if (oneUserTimeScheduledTaskJSONObject.optBoolean("doNotImport", false)) { continue; } db.execSQL( "insert into tasks(_id,hour,minutes,repeat,enabled,label,task,column1,column2) values(null," + oneUserTimeScheduledTaskJSONObject.getInt("hour") + "," + oneUserTimeScheduledTaskJSONObject.getInt("minutes") + "," + oneUserTimeScheduledTaskJSONObject.getString("repeat") + "," + oneUserTimeScheduledTaskJSONObject.getInt("enabled") + "," + "'" + oneUserTimeScheduledTaskJSONObject.getString("label") + "'" + "," + "'" + oneUserTimeScheduledTaskJSONObject.getString("task") + "'" + ",'','')" ); } catch (JSONException e) { isCompletelySuccess = false; } } db.close(); TasksUtils.checkTimeTasks(context); return isCompletelySuccess; }
Example 19
Source File: SQLiteHelper.java From SQLite with Apache License 2.0 | 4 votes |
public SQLiteHelper(Context context, @NonNull SQLiteConfig config, @NonNull SQLiteSchema schema) { super(context, context.getDatabasePath(config.getDatabaseName()).getPath(), null, schema.calculateVersion()); mSchema = schema; context.openOrCreateDatabase(config.getDatabaseName(), 0, null, null); }
Example 20
Source File: KVStore.java From AndroidKeyValueStore with Apache License 2.0 | 2 votes |
/** * Init component. * * @param context used to open or create the database * @param databaseName database name for opening or creating */ public static KVManger init(Context context, String databaseName) { SQLiteDatabase database = context.openOrCreateDatabase(databaseName, Context.MODE_PRIVATE, null); return init(database); }