added backup settings in test mode
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ru.vtosters.lite.ui.fragments;
|
||||
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static android.widget.Toast.makeText;
|
||||
import static ru.vtosters.lite.utils.CacheUtils.deleteCache;
|
||||
@@ -16,6 +17,7 @@ import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
@@ -29,6 +31,8 @@ import com.vtosters.lite.auth.VKAccountManager;
|
||||
import com.vtosters.lite.general.fragments.MaterialPreferenceToolbarFragment;
|
||||
import com.vtosters.lite.im.ImEngineProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import b.h.g.m.FileUtils;
|
||||
import ru.vtosters.lite.utils.Globals;
|
||||
|
||||
@@ -50,8 +54,8 @@ public class OtherFragment extends MaterialPreferenceToolbarFragment{
|
||||
findPreference("stickfix").setOnPreferenceClickListener(new f());
|
||||
findPreference("deleteprefs").setOnPreferenceClickListener(new deleteprefs());
|
||||
|
||||
// a("saveprefs").a((Preference.c) new saveprefs());
|
||||
// a("restoreprefs").a((Preference.c) new restoreprefs());
|
||||
findPreference("saveprefs").setOnPreferenceClickListener(new saveprefs());
|
||||
findPreference("restoreprefs").setOnPreferenceClickListener(new restoreprefs());
|
||||
}
|
||||
|
||||
public static class d implements Preference.OnPreferenceClickListener{
|
||||
@@ -179,7 +183,13 @@ public class OtherFragment extends MaterialPreferenceToolbarFragment{
|
||||
|
||||
@Override // android.support.v7.preference.Preference.c
|
||||
public boolean onPreferenceClick(Preference preference){
|
||||
restoreBackup();
|
||||
try {
|
||||
restoreBackup();
|
||||
Toast.makeText(getContext(), "Настройки восстановлены", LENGTH_LONG).show();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(getContext(), "Ошибка, Настройки не восстановлены", LENGTH_LONG).show();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ import com.vtosters.lite.im.ImEngineProvider;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -87,11 +88,11 @@ public class Globals{
|
||||
// VKIDProtection.alert(activity);
|
||||
}
|
||||
|
||||
public static byte[] readFileFully(File file) throws IOException{
|
||||
public static byte[] readFileFully(File file) throws IOException {
|
||||
return readFully(new FileInputStream(file));
|
||||
}
|
||||
|
||||
public static byte[] readFully(InputStream is) throws IOException{
|
||||
public static byte[] readFully(InputStream is) throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
@@ -102,6 +103,15 @@ public class Globals{
|
||||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
public static void writeToFile(File file, String content) throws IOException {
|
||||
writeToFile(file, content.getBytes());
|
||||
}
|
||||
|
||||
public static void writeToFile(File file, byte[] content) throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
fos.write(content);
|
||||
}
|
||||
|
||||
public static void componentSwitcher(String component, Boolean enabled){
|
||||
var packageManager = Objects.requireNonNull(getContext()).getPackageManager();
|
||||
packageManager.setComponentEnabledSetting(new ComponentName(getPackageName(), getPackageName() + "." + component), enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
|
||||
|
||||
@@ -4,66 +4,130 @@ import static android.os.Environment.DIRECTORY_DOWNLOADS;
|
||||
import static android.os.Environment.getExternalStoragePublicDirectory;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static android.widget.Toast.makeText;
|
||||
import static ru.vtosters.lite.utils.Globals.getContext;
|
||||
import static ru.vtosters.lite.utils.Globals.readFileFully;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.HashSet;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SettBackup{
|
||||
static SharedPreferences prefs = getContext().getSharedPreferences("com.vtosters.lite_preferences", Context.MODE_PRIVATE);
|
||||
|
||||
public static void deletePrefs(){
|
||||
prefs.edit().clear().commit();
|
||||
File file = new File(new File(getContext().getFilesDir().getParent(), "shared_prefs"), "com.vtosters.lite_preferences");
|
||||
if(file.exists()) file.delete();
|
||||
private static SharedPreferences getPrefs() {
|
||||
return getContext().getSharedPreferences("com.vtosters.lite_preferences", Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public static void backupSettings(){
|
||||
SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
|
||||
String currentDateandTime = date.format(new Date());
|
||||
var directory = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS + "/VTLBackups/") + "/Backup_" + currentDateandTime + ".txt";
|
||||
var file = new File(directory);
|
||||
public static void deletePrefs(){
|
||||
getPrefs().edit().clear().commit();
|
||||
File file = new File(new File(getContext().getFilesDir().getParent(), "shared_prefs"), "com.vtosters.lite_preferences");
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public static void backupSettings() {
|
||||
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
|
||||
var dir = new File(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS), "/VTLBackups/");
|
||||
var file = new File(dir,
|
||||
"Backup_"
|
||||
// + dateFormat.format(new Date())
|
||||
+ "test"
|
||||
+ ".xml");
|
||||
try {
|
||||
var exists = file.createNewFile();
|
||||
|
||||
if(!exists){
|
||||
file.delete();
|
||||
file.createNewFile();
|
||||
}
|
||||
try {
|
||||
FileWriter out = new FileWriter(file);
|
||||
out.write(getAllPrefs());
|
||||
out.close();
|
||||
makeText(getContext(), "Сохранено в файл " + directory, LENGTH_LONG).show();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
makeText(getContext(), "Не удалось сохранить файл", LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
file.delete();
|
||||
FileWriter out = new FileWriter(file);
|
||||
out.write(getPrefContent());
|
||||
out.close();
|
||||
Toast.makeText(getContext(), "Сохранено в файл " + file.getAbsolutePath(), LENGTH_LONG).show();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
makeText(getContext(), "Не удалось сохранить файл", LENGTH_SHORT).show();
|
||||
Toast.makeText(getContext(), "Не удалось сохранить файл", LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getAllPrefs(){
|
||||
Map<String, ?> allPrefs = prefs.getAll();
|
||||
return Base64.encodeToString(allPrefs.toString().getBytes(), 1);
|
||||
public static String getPrefContent() throws IOException {
|
||||
File prefsDir = new File(getContext().getFilesDir().getParentFile(), "shared_prefs");
|
||||
if (!prefsDir.exists())
|
||||
return null;
|
||||
File pref = new File(prefsDir, "com.vtosters.lite_preferences.xml");
|
||||
return new String(readFileFully(pref));
|
||||
}
|
||||
|
||||
public static void restoreBackup(){
|
||||
public static void restoreBackup() throws IOException {
|
||||
SharedPreferences.Editor editor = getPrefs().edit();
|
||||
File dir = new File(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS + "/VTLBackups/").getPath());
|
||||
;
|
||||
// TODO Make restore backup from file list
|
||||
File pref = new File(dir, "Backup_test.xml");
|
||||
Scanner scanner = new Scanner(new String(readFileFully(pref)));
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine().trim();
|
||||
if (line.contains("<?xml") || line.contains("<map>") || line.contains("</map>"))
|
||||
continue;
|
||||
if (line.startsWith("<set")) {
|
||||
Pattern pattern = Pattern.compile("<set.+name=\"(.+)\">");
|
||||
Matcher matcher = pattern.matcher(line);
|
||||
if (!matcher.find()) continue;
|
||||
String name = matcher.group(1);
|
||||
Set<String> set = new HashSet<>();
|
||||
String _line;
|
||||
while (!(_line = scanner.nextLine().trim()).equals("</set>")) {
|
||||
set.add(_line.substring(_line.indexOf(">") + 1, _line.lastIndexOf("<")));
|
||||
}
|
||||
editor.putStringSet(name, set)
|
||||
.commit();
|
||||
if (!Preferences.dev()) continue;
|
||||
Log.d("Prefs", "Append " + name + " : " + set.toString());
|
||||
} else if (line.startsWith("<")){
|
||||
Pattern pattern = Pattern.compile("^<(.+) name=\"(.+)\">(.+)</.+>$");
|
||||
Matcher matcher = pattern.matcher(line);
|
||||
String type = "", name = "", value = "";
|
||||
if (!matcher.find()) {
|
||||
pattern = Pattern.compile("^<(.+) name=\"(.+?)\" value=\"(.+?)\" />$");
|
||||
matcher = pattern.matcher(line);
|
||||
if (!matcher.find()) continue;
|
||||
}
|
||||
type = matcher.group(1);
|
||||
name = matcher.group(2);
|
||||
value = matcher.group(3);
|
||||
if (type.equals("string"))
|
||||
editor.putString(
|
||||
name,
|
||||
value
|
||||
).commit();
|
||||
else if (type.equals("boolean"))
|
||||
editor.putBoolean(
|
||||
name,
|
||||
Boolean.parseBoolean(value)
|
||||
).commit();
|
||||
else if (type.equals("float"))
|
||||
editor.putFloat(
|
||||
name,
|
||||
Float.parseFloat(value)
|
||||
).commit();
|
||||
else if (type.equals("int"))
|
||||
editor.putInt(
|
||||
name,
|
||||
Integer.parseInt(value)
|
||||
).commit();
|
||||
else if (type.equals("long"))
|
||||
editor.putLong(
|
||||
name,
|
||||
Long.parseLong(value)
|
||||
).commit();
|
||||
if (!Preferences.dev()) continue;
|
||||
Log.d("Prefs", "Append " + name + " : " + value);
|
||||
}
|
||||
}
|
||||
if (!Preferences.dev()) return;
|
||||
File prefsDir = new File(getContext().getFilesDir().getParentFile(), "shared_prefs");
|
||||
File file = new File(prefsDir, "com.vtosters.lite_preferences.xml");
|
||||
Log.d("TAG", new String(readFileFully(file)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user