diff --git a/LimeStudio/app/src/main/java/net/toload/main/hd/DBServer.java b/LimeStudio/app/src/main/java/net/toload/main/hd/DBServer.java index 533825e..f098221 100644 --- a/LimeStudio/app/src/main/java/net/toload/main/hd/DBServer.java +++ b/LimeStudio/app/src/main/java/net/toload/main/hd/DBServer.java @@ -32,6 +32,7 @@ import android.os.Environment; import android.os.RemoteException; import android.support.v4.content.ContextCompat; +import android.support.v4.provider.DocumentFile; import android.util.Log; import net.toload.main.hd.data.KeyboardObj; @@ -52,6 +53,7 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -79,19 +81,6 @@ public class DBServer { protected static Context ctx = null; - // Monitoring thread. - // private Thread thread = null; - - // public class DBServiceImpl extends IDBService.Stub { - // - // Context ctx = null; - // //private Thread thread = null; - // - // DBServiceImpl(Context ctx) { - // this.ctx = ctx; - // mLIMEPref = new LIMEPreferenceManager(ctx); - // loadLimeDB(); - // } public DBServer(Context context) { DBServer.ctx = context; mLIMEPref = new LIMEPreferenceManager(ctx); @@ -99,12 +88,6 @@ public DBServer(Context context) { if (datasource == null) datasource = new LimeDB(ctx); } -/* deprecated by jeremy '12,5,2 - public void loadLimeDB(){ - if(datasource==null) - datasource = new LimeDB(ctx); - } -*/ public void loadMapping(String filename, String tablename, LIMEProgressListener progressListener) throws RemoteException { @@ -173,15 +156,77 @@ public int importMapping(File compressedSourceDB, String imtype) { } else { int count = datasource.importDb(unzipFilePaths.get(0), imtype); - //mLIMEPref.setResetCacheFlag(true); resetCache(); return count; } } + public static void backupDatabase(Uri uri) throws RemoteException { + if (DEBUG) Log.i(TAG, "backupDatabase()"); + + File limedir = ContextCompat.getExternalFilesDirs(ctx, null)[0]; + + if (!limedir.exists()) { + limedir.mkdirs(); + } + + //backup shared preferences + File fileSharedPrefsBackup = new File(LIME.getLimeDataRootFolder(), LIME.SHARED_PREFS_BACKUP_NAME); + if(fileSharedPrefsBackup.exists()) fileSharedPrefsBackup.delete(); + backupDefaultSharedPreference(fileSharedPrefsBackup); + + // create backup file list. + List backupFileList = new ArrayList<>(); + backupFileList.add(LIME.DATABASE_RELATIVE_FOLDER + File.separator + LIME.DATABASE_NAME); + backupFileList.add(LIME.DATABASE_RELATIVE_FOLDER + File.separator + LIME.DATABASE_JOURNAL); + backupFileList.add(LIME.SHARED_PREFS_BACKUP_NAME); + + // hold database connection and close database. + //mLIMEPref.holdDatabaseCoonection(true); + datasource.holdDBConnection(); //Jeremy '15,5,23 + closeDatabse(); + + //ready to zip backup file list + try { + LIMEUtilities.zip( + limedir.getAbsolutePath() + File.separator + LIME.DATABASE_BACKUP_NAME, backupFileList, + LIME.getLimeDataRootFolder(), + true + ); + + DocumentFile pickedDir = DocumentFile.fromTreeUri(ctx, uri); + DocumentFile backupFile = pickedDir.createFile("application/zip", "backup.zip"); + File file = new File(limedir, LIME.DATABASE_BACKUP_NAME); + FileInputStream fileInputStream = new FileInputStream(file); + OutputStream fileOutputStream = ctx.getContentResolver().openOutputStream(backupFile.getUri()); + copyFile(fileInputStream, fileOutputStream); + file.delete(); + } catch (Exception e) { + e.printStackTrace(); + showNotificationMessage(ctx.getText(R.string.l3_initial_backup_error) + ""); + } finally { + showNotificationMessage(ctx.getText(R.string.l3_initial_backup_end) + ""); + } + + // backup finished. unhold the database connection and false reopen the database. + datasource.unHoldDBConnection(); //Jeremy '15,5,23 + //mLIMEPref.holdDatabaseConnection(false); + datasource.openDBConnection(true); + + //cleanup the shared preference backup file. + if( fileSharedPrefsBackup!=null && fileSharedPrefsBackup.exists() ) fileSharedPrefsBackup.delete(); + } + + private static void copyFile(InputStream in, OutputStream out) throws IOException { + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + } + public static void backupDatabase() throws RemoteException { - if (DEBUG) - Log.i(TAG, "backupDatabase()"); + if (DEBUG) Log.i(TAG, "backupDatabase()"); //File limedir = new File(LIME.LIME_SDCARD_FOLDER + File.separator); File limedir = ContextCompat.getExternalFilesDirs(ctx, null)[0]; @@ -285,7 +330,6 @@ public static void backupDefaultSharedPreference(File sharePrefs) { ex.printStackTrace(); } } - } @@ -595,45 +639,6 @@ public int getLoadingMappingPercentageDone() throws RemoteException { if(remoteFileDownloading) return 0; else return datasource.getProgressPercentageDone(); } -/* - @Deprecated //by Jeremy '12,6,6 - public void forceUpgrad() throws RemoteException { - //if (datasource == null) {loadLimeDB();} - datasource.forceUpgrade(); - }*/ - - // } - - - // public IBinder onBind(Intent arg0) { - // return new DBServiceImpl(this); - // } - // - // /* - // * (non-Javadoc) - // * - // * @see android.app.Service#onCreate() - // */ - // - // public void onCreate() { - // super.onCreate(); - // } - // - // /* - // * (non-Javadoc) - // * - // * @see android.app.Service#onDestroy() - // */ - // - // public void onDestroy() { - // if (datasource != null) { - // datasource.close(); - // datasource = null; - // - // } - // notificationMgr.cancelAll(); - // super.onDestroy(); - // } //Jeremy '12,4,23 rewriting using alert notification builder in LIME utilities to replace the deprecated method public static void showNotificationMessage(String message) { diff --git a/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImBackupRunnable.java b/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImBackupRunnable.java index afe0c56..2267f5a 100644 --- a/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImBackupRunnable.java +++ b/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImBackupRunnable.java @@ -24,6 +24,7 @@ package net.toload.main.hd.ui; +import android.net.Uri; import android.os.RemoteException; import net.toload.main.hd.DBServer; @@ -31,17 +32,14 @@ import net.toload.main.hd.R; public class SetupImBackupRunnable implements Runnable { - - // Global - private String mType; private SetupImFragment mFragment; - private SetupImHandler mHandler; + private Uri folderUri; - public SetupImBackupRunnable(SetupImFragment fragment, SetupImHandler handler, String type) { - this.mHandler = handler; - this.mType = type; - this.mFragment = fragment; + public SetupImBackupRunnable(SetupImFragment fragment, SetupImHandler handler, Uri uri) { + mHandler = handler; + mFragment = fragment; + folderUri = uri; } @Override @@ -51,22 +49,14 @@ protected void finalize() throws Throwable { @Override public void run() { - mHandler.showProgress(true, this.mFragment.getResources().getString(R.string.setup_im_backup_message)); - // Preparing the file to be backup - if (mType.equals(Lime.LOCAL) || mType.equals(Lime.GOOGLE) || mType.equals(Lime.DROPBOX)) { - try { - DBServer.backupDatabase(); - } catch (RemoteException e) { - e.printStackTrace(); - } + try { + DBServer.backupDatabase(folderUri); + } catch (RemoteException e) { + e.printStackTrace(); } - switch (mType) { - default: - DBServer.showNotificationMessage(mFragment.getResources().getString(R.string.l3_initial_backup_end)); - mHandler.cancelProgress(); - break; - } + DBServer.showNotificationMessage(mFragment.getResources().getString(R.string.l3_initial_backup_end)); + mHandler.cancelProgress(); } } \ No newline at end of file diff --git a/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImFragment.java b/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImFragment.java index f48578a..adf3bcb 100644 --- a/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImFragment.java +++ b/LimeStudio/app/src/main/java/net/toload/main/hd/ui/SetupImFragment.java @@ -67,6 +67,8 @@ import java.util.HashMap; import java.util.List; +import static android.app.Activity.RESULT_OK; + /** * Fragment used for managing interactions for and presentation of a navigation drawer. * See the @@ -407,7 +409,6 @@ public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); SetupImLoadDialog dialog = SetupImLoadDialog.newInstance(Lime.DB_TABLE_CUSTOM, handler); dialog.show(ft, "loadimdialog"); - } }); @@ -677,18 +678,23 @@ public void onAttach(Context context) { @Override public void onActivityResult(int requestCode, int resultCode, Intent data){ + if (resultCode != RESULT_OK) { + return; + } + if (requestCode == RESTORE_FILE_REQUEST_CODE) { - // The document selected by the user won't be returned in the intent. - // Instead, a URI to that document will be contained in the return intent - // provided to this method as a parameter. - // Pull that URI using resultData.getData(). + Uri uri = data.getData(); + Log.i(TAG, "Uri: " + uri.toString()); + restorethread = new Thread(new SetupImRestoreRunnable(this, handler, getFilePathFromUri(uri))); + restorethread.start(); + } else if (requestCode == BACKUP_FILE_REQUEST_CODE) { + Uri treeUri = data.getData(); if (data != null) { - Uri uri = data.getData(); - Log.i(TAG, "Uri: " + uri.toString()); - restorethread = new Thread(new SetupImRestoreRunnable(this, handler, getFilePathFromUri(uri))); - restorethread.start(); + backupthread = new Thread(new SetupImBackupRunnable(this, handler, treeUri)); + backupthread.start(); } } + } public String getFilePathFromUri(Uri uri) { @@ -739,7 +745,6 @@ public void restoreLocalDrive(){ } public void initialThreadTask(String action, String type) { - // Default Setting mLIMEPref.setParameter("dbtarget", Lime.DEVICE); @@ -747,18 +752,23 @@ public void initialThreadTask(String action, String type) { if(backupthread != null && backupthread.isAlive()){ handler.removeCallbacks(backupthread); } - backupthread = new Thread(new SetupImBackupRunnable(this, handler, type)); - backupthread.start(); + //backupthread = new Thread(new SetupImBackupRunnable(this, handler, type)); + //backupthread.start(); + launchBackupFilePicker(); }else if(action.equals(Lime.RESTORE)){ if(restorethread != null && restorethread.isAlive()){ handler.removeCallbacks(restorethread); } launchRestoreFilePicker(); - //restorethread = new Thread(new SetupImRestoreRunnable(this, handler, type)); - //restorethread.start(); } } + final static int BACKUP_FILE_REQUEST_CODE = 10421; + private void launchBackupFilePicker() { + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); + startActivityForResult(intent, BACKUP_FILE_REQUEST_CODE); + } + final static int RESTORE_FILE_REQUEST_CODE = 0421; private void launchRestoreFilePicker() { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);