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;
36 import net.ktnx.mobileledger.model.Data;
38 import java.io.IOException;
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.Date;
42 import java.util.Locale;
44 public class BackupsActivity extends AppCompatActivity {
45 private FragmentBackupsBinding b;
46 private ActivityResultLauncher<String> backupChooserLauncher;
47 private ActivityResultLauncher<String[]> restoreChooserLauncher;
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 b = FragmentBackupsBinding.inflate(getLayoutInflater());
52 setContentView(b.getRoot());
54 setSupportActionBar(b.toolbar);
55 // Show the Up button in the action bar.
56 ActionBar actionBar = getSupportActionBar();
57 if (actionBar != null) {
58 actionBar.setDisplayHomeAsUpEnabled(true);
61 b.backupButton.setOnClickListener(this::backupClicked);
62 b.restoreButton.setOnClickListener(this::restoreClicked);
65 backupChooserLauncher =
66 registerForActivityResult(new ActivityResultContracts.CreateDocument(),
68 restoreChooserLauncher =
69 registerForActivityResult(new ActivityResultContracts.OpenDocument(),
72 Data.observeProfile(this, p -> {
74 b.backupButton.setEnabled(false);
75 b.backupExplanationText.setEnabled(false);
78 b.backupButton.setEnabled(true);
79 b.backupExplanationText.setEnabled(true);
84 public boolean onOptionsItemSelected(MenuItem item) {
85 if (item.getItemId() == android.R.id.home) {
90 return super.onOptionsItemSelected(item);
92 private void storeConfig(Uri result) {
98 new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
100 public void error(Exception e) {
101 Snackbar.make(b.backupButton, e.toString(),
102 BaseTransientBottomBar.LENGTH_LONG)
105 }, new ConfigWriter.OnDoneListener() {
107 Snackbar.make(b.backupButton, R.string.config_saved,
108 Snackbar.LENGTH_LONG)
114 catch (IOException e) {
119 private void readConfig(Uri result) {
124 ConfigReader reader =
125 new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
127 public void error(Exception e) {
128 Snackbar.make(b.backupButton, e.toString(),
129 BaseTransientBottomBar.LENGTH_LONG)
132 }, new ConfigReader.OnDoneListener() {
134 Snackbar.make(b.backupButton, R.string.config_restored,
135 Snackbar.LENGTH_LONG)
141 catch (IOException e) {
146 private void backupClicked(View view) {
147 final Date now = new Date();
148 DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault());
150 backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now)));
152 private void restoreClicked(View view) {
153 restoreChooserLauncher.launch(new String[]{"application/json"});