Skip to content

Commit

Permalink
backup files locally after selecting folder
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Jul 11, 2020
1 parent 543d744 commit 496cf27
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 98 deletions.
129 changes: 67 additions & 62 deletions LimeStudio/app/src/main/java/net/toload/main/hd/DBServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -79,32 +81,13 @@ 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);
//loadLimeDB();
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 {

Expand Down Expand Up @@ -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<String> 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];
Expand Down Expand Up @@ -285,7 +330,6 @@ public static void backupDefaultSharedPreference(File sharePrefs) {
ex.printStackTrace();
}
}

}


Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,22 @@

package net.toload.main.hd.ui;

import android.net.Uri;
import android.os.RemoteException;

import net.toload.main.hd.DBServer;
import net.toload.main.hd.Lime;
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
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://developer.android.com/design/patterns/navigation-drawer.html#Interaction">
Expand Down Expand Up @@ -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");

}
});

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -739,26 +745,30 @@ public void restoreLocalDrive(){
}

public void initialThreadTask(String action, String type) {

// Default Setting
mLIMEPref.setParameter("dbtarget", Lime.DEVICE);

if (action.equals(Lime.BACKUP)) {
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);
Expand Down

0 comments on commit 496cf27

Please sign in to comment.