2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.view.MenuItem;
23 import android.view.View;
25 import androidx.activity.result.ActivityResultLauncher;
26 import androidx.activity.result.contract.ActivityResultContracts;
27 import androidx.appcompat.app.ActionBar;
28 import androidx.appcompat.app.AppCompatActivity;
30 import com.google.android.material.snackbar.BaseTransientBottomBar;
31 import com.google.android.material.snackbar.Snackbar;
33 import net.ktnx.mobileledger.async.ConfigReader;
34 import net.ktnx.mobileledger.async.ConfigWriter;
35 import net.ktnx.mobileledger.databinding.FragmentBackupsBinding;
37 import java.io.IOException;
38 import java.text.DateFormat;
39 import java.text.SimpleDateFormat;
40 import java.util.Date;
41 import java.util.Locale;
43 public class BackupsActivity extends AppCompatActivity {
44 private FragmentBackupsBinding b;
45 private ActivityResultLauncher<String> backupChooserLauncher;
46 private ActivityResultLauncher<String[]> restoreChooserLauncher;
48 protected void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50 b = FragmentBackupsBinding.inflate(getLayoutInflater());
51 setContentView(b.getRoot());
53 setSupportActionBar(b.toolbar);
54 // Show the Up button in the action bar.
55 ActionBar actionBar = getSupportActionBar();
56 if (actionBar != null) {
57 actionBar.setDisplayHomeAsUpEnabled(true);
60 b.backupButton.setOnClickListener(this::backupClicked);
61 b.restoreButton.setOnClickListener(this::restoreClicked);
64 backupChooserLauncher =
65 registerForActivityResult(new ActivityResultContracts.CreateDocument(),
67 restoreChooserLauncher =
68 registerForActivityResult(new ActivityResultContracts.OpenDocument(),
72 public boolean onOptionsItemSelected(MenuItem item) {
73 if (item.getItemId() == android.R.id.home) {
78 return super.onOptionsItemSelected(item);
80 private void storeConfig(Uri result) {
86 new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
88 public void error(Exception e) {
89 Snackbar.make(b.backupButton, e.toString(),
90 BaseTransientBottomBar.LENGTH_LONG)
93 }, new ConfigWriter.OnDoneListener() {
95 Snackbar.make(b.backupButton, R.string.config_saved,
102 catch (IOException e) {
107 private void readConfig(Uri result) {
112 ConfigReader reader =
113 new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
115 public void error(Exception e) {
116 Snackbar.make(b.backupButton, e.toString(),
117 BaseTransientBottomBar.LENGTH_LONG)
120 }, new ConfigReader.OnDoneListener() {
122 Snackbar.make(b.backupButton, R.string.config_restored,
123 Snackbar.LENGTH_LONG)
129 catch (IOException e) {
134 private void backupClicked(View view) {
135 final Date now = new Date();
136 DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault());
138 backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now)));
140 private void restoreClicked(View view) {
141 restoreChooserLauncher.launch(new String[]{"application/json"});