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.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.view.MenuItem;
25 import android.view.View;
27 import androidx.activity.result.ActivityResultLauncher;
28 import androidx.activity.result.contract.ActivityResultContracts;
29 import androidx.appcompat.app.ActionBar;
30 import androidx.appcompat.app.AppCompatActivity;
32 import com.google.android.material.snackbar.BaseTransientBottomBar;
33 import com.google.android.material.snackbar.Snackbar;
35 import net.ktnx.mobileledger.backup.ConfigReader;
36 import net.ktnx.mobileledger.backup.ConfigWriter;
37 import net.ktnx.mobileledger.databinding.FragmentBackupsBinding;
38 import net.ktnx.mobileledger.model.Data;
40 import java.io.IOException;
41 import java.text.DateFormat;
42 import java.text.SimpleDateFormat;
43 import java.util.Date;
44 import java.util.Locale;
46 public class BackupsActivity extends AppCompatActivity {
47 private FragmentBackupsBinding b;
48 private ActivityResultLauncher<String> backupChooserLauncher;
49 private ActivityResultLauncher<String[]> restoreChooserLauncher;
50 public static void start(Context context) {
51 Intent starter = new Intent(context, BackupsActivity.class);
52 context.startActivity(starter);
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
57 b = FragmentBackupsBinding.inflate(getLayoutInflater());
58 setContentView(b.getRoot());
60 setSupportActionBar(b.toolbar);
61 // Show the Up button in the action bar.
62 ActionBar actionBar = getSupportActionBar();
63 if (actionBar != null) {
64 actionBar.setDisplayHomeAsUpEnabled(true);
67 b.backupButton.setOnClickListener(this::backupClicked);
68 b.restoreButton.setOnClickListener(this::restoreClicked);
71 backupChooserLauncher =
72 registerForActivityResult(new ActivityResultContracts.CreateDocument(),
74 restoreChooserLauncher =
75 registerForActivityResult(new ActivityResultContracts.OpenDocument(),
78 Data.observeProfile(this, p -> {
80 b.backupButton.setEnabled(false);
81 b.backupExplanationText.setEnabled(false);
84 b.backupButton.setEnabled(true);
85 b.backupExplanationText.setEnabled(true);
90 public boolean onOptionsItemSelected(MenuItem item) {
91 if (item.getItemId() == android.R.id.home) {
96 return super.onOptionsItemSelected(item);
98 private void storeConfig(Uri result) {
104 new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
106 public void error(Exception e) {
107 Snackbar.make(b.backupButton, e.toString(),
108 BaseTransientBottomBar.LENGTH_LONG)
111 }, new ConfigWriter.OnDoneListener() {
113 Snackbar.make(b.backupButton, R.string.config_saved,
114 Snackbar.LENGTH_LONG)
120 catch (IOException e) {
125 private void readConfig(Uri result) {
130 ConfigReader reader =
131 new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
133 public void error(Exception e) {
134 Snackbar.make(b.backupButton, e.toString(),
135 BaseTransientBottomBar.LENGTH_LONG)
138 }, new ConfigReader.OnDoneListener() {
140 Snackbar.make(b.backupButton, R.string.config_restored,
141 Snackbar.LENGTH_LONG)
147 catch (IOException e) {
152 private void backupClicked(View view) {
153 final Date now = new Date();
154 DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault());
156 backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now)));
158 private void restoreClicked(View view) {
159 restoreChooserLauncher.launch(new String[]{"application/json"});