DataBaseHelper.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.chinaappsremover.dbhandler;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.util.Log;
  8. import com.chinaappsremover.wrapper.AppInfo;
  9. import java.io.File;
  10. import java.util.List;
  11. public class DataBaseHelper extends SQLiteOpenHelper {
  12. public static final String DATABASE_NAME = "rca.db";
  13. public static final int DATABASE_VERSION = 1;
  14. private static final String SQL_CREATE_ENTRIES =
  15. "CREATE TABLE " + AppInfoEntry.TABLE_NAME + " (" +
  16. AppInfoEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
  17. AppInfoEntry.COLUMN_PACKAGE_NAME + " TEXT," +
  18. AppInfoEntry.COLUMN_APP_NAME + " TEXT)";
  19. private static final String SQL_DELETE_ENTRIES =
  20. "DROP TABLE IF EXISTS " + AppInfoEntry.TABLE_NAME;
  21. public DataBaseHelper(Context context) {
  22. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  23. }
  24. @Override
  25. public void onCreate(SQLiteDatabase db) {
  26. Log.w("Db oncrate called", "Db oncreate called" + SQL_CREATE_ENTRIES);
  27. db.execSQL(SQL_CREATE_ENTRIES);
  28. }
  29. @Override
  30. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  31. if (newVersion != oldVersion) {
  32. db.execSQL(SQL_DELETE_ENTRIES);
  33. onCreate(db);
  34. }
  35. }
  36. @Override
  37. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  38. onUpgrade(db, oldVersion, newVersion);
  39. }
  40. public List<AppInfo> isExist(List<AppInfo> allInstalledAppInfoList, List<AppInfo> chinaAppInfoList) {
  41. SQLiteDatabase db = getWritableDatabase();
  42. chinaAppInfoList.clear();
  43. for (AppInfo next : allInstalledAppInfoList) {
  44. Cursor rawQuery = db.rawQuery("select * from apps where p_name ='" + next.packageName + "'", null);
  45. if (rawQuery != null && rawQuery.moveToFirst()) {
  46. chinaAppInfoList.add(next);
  47. while (rawQuery.moveToNext()) {
  48. chinaAppInfoList.add(next);
  49. }
  50. }
  51. if (rawQuery != null) {
  52. rawQuery.close();
  53. }
  54. }
  55. return chinaAppInfoList;
  56. }
  57. public boolean refreshAppInfos(List<AppInfo> appInfoEntries) {
  58. SQLiteDatabase db = getWritableDatabase();
  59. db.beginTransaction();
  60. try {
  61. db.delete(AppInfoEntry.TABLE_NAME, null, null);
  62. for (AppInfo appInfo : appInfoEntries) {
  63. ContentValues values = new ContentValues();
  64. values.put(AppInfoEntry.COLUMN_APP_NAME, appInfo.appName
  65. );
  66. values.put(AppInfoEntry.COLUMN_PACKAGE_NAME, appInfo.packageName);
  67. db.insertOrThrow(AppInfoEntry.TABLE_NAME, null, values);
  68. }
  69. db.setTransactionSuccessful();
  70. return true;
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. } finally {
  74. db.endTransaction();
  75. }
  76. return false;
  77. }
  78. }