Skip to content

Commit

Permalink
Added backup of app preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilkedia committed Aug 8, 2017
1 parent ea1b684 commit fd53eab
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ android {
minSdkVersion 14
//noinspection OldTargetApi
targetSdkVersion 23
versionCode 7
versionName "1.3.0"
versionCode 8
versionName "1.4.0"
}
buildTypes {
release {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/akhil/alltrans/AppListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void onStart() {
super.onStart();
context = this.getActivity();
//noinspection deprecation,deprecation
BackupSharedPreferences.backupSharedPreferences(this.getActivity());
settings = this.getActivity().getSharedPreferences("AllTransPref", MODE_WORLD_READABLE);
mFirebaseAnalytics = FirebaseAnalytics.getInstance(context);

Expand Down
67 changes: 67 additions & 0 deletions app/src/main/java/akhil/alltrans/BackupSharedPreferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2017 Akhil Kedia
* This file is part of AllTrans.
*
* AllTrans is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AllTrans is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AllTrans. If not, see <http://www.gnu.org/licenses/>.
*
*/

package akhil.alltrans;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import java.io.DataOutputStream;


public class BackupSharedPreferences {
public static void backupSharedPreferences(Context context) {
@SuppressLint("SdCardPath")
String sharedPath = "/data/data/akhil.alltrans/shared_prefs/";
String externalPath = Environment.getExternalStorageDirectory().getAbsolutePath();
if (!isExternalStorageWritable()) {
return;
}

try {
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
utils.debugLog("cp -R" + " " + sharedPath + " " + externalPath + "/AllTransBackup/" + "\n");
outputStream.writeBytes("cp -R" + " " + sharedPath + " " + externalPath + "/AllTransBackup/" + "\n");
outputStream.writeBytes("exit\n");
outputStream.flush();
outputStream.close();
su.waitFor();

CharSequence text = context.getString(R.string.backup_pref_success);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} catch (Exception e) {
CharSequence text = context.getString(R.string.backup_pref_error);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}

/* Checks if external storage is available for read and write */
private static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
}
13 changes: 7 additions & 6 deletions app/src/main/java/akhil/alltrans/LocalPreferenceFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ public boolean onPreferenceClick(Preference preference) {

outputStream.writeBytes("exit\n");
outputStream.flush();
outputStream.close();
su.waitFor();

Context context = preference.getContext();
CharSequence text = getString(R.string.clear_cache_success);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} catch (Exception e) {
Context context = preference.getContext();
CharSequence text = getString(R.string.clear_cache_error);
Expand All @@ -112,12 +119,6 @@ public boolean onPreferenceClick(Preference preference) {
toast.show();
}

Context context = preference.getContext();
CharSequence text = getString(R.string.clear_cache_success);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();

return false;
}
});
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<string name="loading">"Loading List of Applications - Please Wait"</string>
<string name="clear_cache_error">"Some Error. Could not clear translation cache!"</string>
<string name="clear_cache_success">"Translate Cache for this app has been erased!"</string>
<string name="backup_pref_error">"Some Error. Could not backup preferences! Did you grant SuperUser Access?"</string>
<string name="backup_pref_success">"Preferences have been backed up to External Storage!"</string>
<string name="wut_why_null">"Could not load the Per-app preference page. Close and re-open application again."</string>

<string name="app_icon">"App Icon"</string>
Expand Down

0 comments on commit fd53eab

Please sign in to comment.