Skip to content

Commit

Permalink
feat(save-backup): use ACTION_CREATE_DOCUMENT instead of "Sharing" th…
Browse files Browse the repository at this point in the history
…e backup file
  • Loading branch information
LucasGGamerM committed May 11, 2024
1 parent 95ba52b commit 96c1c03
Showing 1 changed file with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.joinmastodon.android.ui.Snackbar;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.updater.GithubSelfUpdater;
import org.joinmastodon.android.utils.FileProvider;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -46,6 +45,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
Expand All @@ -63,6 +63,7 @@
public class SettingsAboutAppFragment extends BaseSettingsFragment<Void> implements HasAccountID{
private static final String TAG="SettingsAboutAppFragment";
private static final int IMPORT_RESULT=314;
private static final int EXPORT_RESULT=271;
private ListItem<Void> mediaCacheItem, copyCrashLogItem;
private CheckableListItem<Void> enablePreReleasesItem;
private AccountSession session;
Expand Down Expand Up @@ -161,40 +162,11 @@ private void onClearRecentEmojisClick(ListItem<?> item){
}

private void onExportClick(ListItem<?> item){
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("versionName", BuildConfig.VERSION_NAME);
jsonObject.addProperty("versionCode", BuildConfig.VERSION_CODE);

// GlobalUserPreferences
//TODO: remove prefs that should not be exported
JsonElement je = gson.toJsonTree(GlobalUserPreferences.getPrefs().getAll());
jsonObject.add("GlobalUserPreferences", je);

// add account local prefs
for(AccountSession accountSession: AccountSessionManager.getInstance().getLoggedInAccounts()) {
Map<String, ?> prefs = accountSession.getRawLocalPreferences().getAll();
//TODO: remove prefs that should not be exported
JsonElement accountPrefs = gson.toJsonTree(prefs);
jsonObject.add(accountSession.self.id, accountPrefs);
}

try {
File file = new File(getContext().getCacheDir(), "moshidon-exported-settings.json");
FileWriter writer = new FileWriter(file);
writer.write(jsonObject.toString());
writer.flush();
writer.close();

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/json");
Uri outputUri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".fileprovider", file);
intent.putExtra(Intent.EXTRA_STREAM, outputUri);
startActivity(Intent.createChooser(intent, getContext().getString(R.string.export_settings_share)));
} catch (IOException e) {
Toast.makeText(getContext(), getContext().getString(R.string.export_settings_fail), Toast.LENGTH_SHORT).show();
Log.w(TAG, e);
}
// The magic will happen on the onActivityResult Method
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_TITLE,"moshidon-exported-settings.json");
startActivityForResult(intent, EXPORT_RESULT);
}

private void onImportClick(ListItem<?> item){
Expand Down Expand Up @@ -292,6 +264,46 @@ public void onActivityResult(int requestCode, int resultCode, Intent data){
Toast.makeText(getContext(), getContext().getString(R.string.import_settings_failed), Toast.LENGTH_SHORT).show();
}
}

if(requestCode == EXPORT_RESULT && resultCode==Activity.RESULT_OK) {
try{
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("versionName", BuildConfig.VERSION_NAME);
jsonObject.addProperty("versionCode", BuildConfig.VERSION_CODE);

// GlobalUserPreferences
//TODO: remove prefs that should not be exported
JsonElement je = gson.toJsonTree(GlobalUserPreferences.getPrefs().getAll());
jsonObject.add("GlobalUserPreferences", je);

// add account local prefs
for(AccountSession accountSession: AccountSessionManager.getInstance().getLoggedInAccounts()) {
Map<String, ?> prefs = accountSession.getRawLocalPreferences().getAll();
//TODO: remove prefs that should not be exported
JsonElement accountPrefs = gson.toJsonTree(prefs);
jsonObject.add(accountSession.self.id, accountPrefs);
}

File file = new File(getContext().getCacheDir(), "moshidon-exported-settings.json");
FileWriter writer = new FileWriter(file);
writer.write(jsonObject.toString());
writer.flush();
writer.close();

// Got this from stackoverflow at https://stackoverflow.com/a/67046741
InputStream is = new FileInputStream(file);
OutputStream os = getContext().getContentResolver().openOutputStream(data.getData());

byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
}catch(IOException e){
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT);
}
}
}

private void savePrefValue(SharedPreferences.Editor editor, String key, Object value) {
Expand Down

0 comments on commit 96c1c03

Please sign in to comment.