DbUtils.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.chinaappsremover.dbhandler;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import com.chinaappsremover.AppController;
  5. import com.chinaappsremover.utils.Preference;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. public class DbUtils {
  11. public File getDatabasePath(Context context, String str) {
  12. return context.getDatabasePath(str);
  13. }
  14. public static void attachDB(Context context) {
  15. File databasePath = context.getDatabasePath("dest.sqLiteDatabase");
  16. if (getDbFile(context, DataBaseHelper.DATABASE_NAME, databasePath.getAbsolutePath()) && AppController.getDbHelper().attach(databasePath, false)) {
  17. Preference.setAttachedDb();
  18. }
  19. }
  20. public static boolean getDbFile(Context context, String str, String str2) {
  21. try {
  22. InputStream open = context.getAssets().open(str);
  23. FileOutputStream fileOutputStream = new FileOutputStream(str2);
  24. Log.v("Tag assets", fileOutputStream.toString());
  25. byte[] bArr = new byte[1024];
  26. while (true) {
  27. int read = open.read(bArr);
  28. if (read > 0) {
  29. fileOutputStream.write(bArr, 0, read);
  30. } else {
  31. open.close();
  32. fileOutputStream.flush();
  33. fileOutputStream.close();
  34. return true;
  35. }
  36. }
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. return false;
  40. }
  41. }
  42. public boolean isDatabaseAttached(Context context, String str) {
  43. if (getDatabasePath(context, str) == null) {
  44. return false;
  45. }
  46. return Preference.isDBAttached();
  47. }
  48. }