This commit is contained in:
gdlbo
2022-04-28 20:57:57 +03:00
parent ee652593c0
commit cdcf218160
5 changed files with 45 additions and 11 deletions
@@ -2,6 +2,7 @@ package ru.vtosters.lite.ui.fragments;
import static ru.vtosters.lite.utils.SettBackup.backupSettings;
import static ru.vtosters.lite.utils.SettBackup.deletePrefs;
import static ru.vtosters.lite.utils.SettBackup.restoreBackup;
import android.content.ClipData;
import android.content.ClipboardManager;
@@ -143,6 +144,17 @@ public class OtherFragment extends MaterialPreferenceToolbarFragment {
}
}
public class restoreprefs implements Preference.c {
@Override // android.support.v7.preference.Preference.c
public boolean a(Preference preference) {
restoreBackup();
return true;
}
restoreprefs() {
}
}
private void prefs() {
a("firebasefix").a((Preference.c) new a());
a("applicationstop").a((Preference.c) new b());
@@ -150,8 +162,9 @@ public class OtherFragment extends MaterialPreferenceToolbarFragment {
a("applicationrestart").a((Preference.c) new d());
a("clearcache").a((Preference.c) new e());
a("stickfix").a((Preference.c) new f());
a("saveprefs").a((Preference.c) new saveprefs());
a("deleteprefs").a((Preference.c) new deleteprefs());
// a("saveprefs").a((Preference.c) new saveprefs());
// a("restoreprefs").a((Preference.c) new restoreprefs());
}
}
@@ -1,7 +1,17 @@
package ru.vtosters.lite.utils;
import static android.app.Activity.RESULT_OK;
import static android.os.Environment.*;
import static android.widget.Toast.*;
import static androidx.core.app.ActivityCompat.startActivityForResult;
import static ru.vtosters.lite.utils.Helper.*;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.widget.Toast;
@@ -15,18 +25,18 @@ import java.util.Locale;
import java.util.Map;
public class SettBackup {
static SharedPreferences prefs = Helper.GetContext().getSharedPreferences("com.vtosters.lite_preferences", Context.MODE_PRIVATE);
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(Helper.GetContext().getFilesDir().getParent(), "shared_prefs"), "com.vtosters.lite_preferences");
File file = new File(new File(GetContext().getFilesDir().getParent(), "shared_prefs"), "com.vtosters.lite_preferences");
if (file.exists()) file.delete();
}
public static void backupSettings() {
SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
String currentDateandTime = date.format(new Date());
var directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/VTLBackup_" + currentDateandTime + ".txt";
var directory = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) + "/VTLBackup_" + currentDateandTime + ".txt";
var file = new File(directory);
try {
var exists = file.createNewFile();
@@ -39,15 +49,15 @@ public class SettBackup {
FileWriter out = new FileWriter(file);
out.write(getAllPrefs());
out.close();
Toast.makeText(Helper.GetContext(), "Сохранено в файл " + directory, Toast.LENGTH_LONG).show();
makeText(GetContext(), "Сохранено в файл " + directory, LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Helper.GetContext(), "Не удалось сохранить файл", Toast.LENGTH_SHORT).show();
makeText(GetContext(), "Не удалось сохранить файл", LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(Helper.GetContext(), "Не удалось сохранить файл", Toast.LENGTH_SHORT).show();
makeText(GetContext(), "Не удалось сохранить файл", LENGTH_SHORT).show();
}
}
@@ -57,5 +67,9 @@ public class SettBackup {
}
public static void restoreBackup() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("text/plain");
GetContext().startActivity(intent);
}
}