]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/BackupsActivity.java
9a1f706a718264f90cc2df3e39cc39802719df35
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / BackupsActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger;
19
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.view.MenuItem;
23 import android.view.View;
24
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;
29
30 import com.google.android.material.snackbar.BaseTransientBottomBar;
31 import com.google.android.material.snackbar.Snackbar;
32
33 import net.ktnx.mobileledger.async.ConfigReader;
34 import net.ktnx.mobileledger.async.ConfigWriter;
35 import net.ktnx.mobileledger.databinding.FragmentBackupsBinding;
36
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;
42
43 public class BackupsActivity extends AppCompatActivity {
44     private FragmentBackupsBinding b;
45     private ActivityResultLauncher<String> backupChooserLauncher;
46     private ActivityResultLauncher<String[]> restoreChooserLauncher;
47     @Override
48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         b = FragmentBackupsBinding.inflate(getLayoutInflater());
51         setContentView(b.getRoot());
52
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);
58         }
59
60         b.backupButton.setOnClickListener(this::backupClicked);
61         b.restoreButton.setOnClickListener(this::restoreClicked);
62
63
64         backupChooserLauncher =
65                 registerForActivityResult(new ActivityResultContracts.CreateDocument(),
66                         this::storeConfig);
67         restoreChooserLauncher =
68                 registerForActivityResult(new ActivityResultContracts.OpenDocument(),
69                         this::readConfig);
70     }
71     @Override
72     public boolean onOptionsItemSelected(MenuItem item) {
73         if (item.getItemId() == android.R.id.home) {
74             finish();
75
76             return true;
77         }
78         return super.onOptionsItemSelected(item);
79     }
80     private void storeConfig(Uri result) {
81         if (result == null)
82             return;
83
84         try {
85             ConfigWriter saver =
86                     new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
87                         @Override
88                         public void error(Exception e) {
89                             Snackbar.make(b.backupButton, e.toString(),
90                                     BaseTransientBottomBar.LENGTH_LONG)
91                                     .show();
92                         }
93                     }, new ConfigWriter.OnDoneListener() {
94                         public void done() {
95                             Snackbar.make(b.backupButton, R.string.config_saved,
96                                     Snackbar.LENGTH_LONG)
97                                     .show();
98                         }
99                     });
100             saver.start();
101         }
102         catch (IOException e) {
103             e.printStackTrace();
104         }
105
106     }
107     private void readConfig(Uri result) {
108         if (result == null)
109             return;
110
111         try {
112             ConfigReader reader =
113                     new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
114                         @Override
115                         public void error(Exception e) {
116                             Snackbar.make(b.backupButton, e.toString(),
117                                     BaseTransientBottomBar.LENGTH_LONG)
118                                     .show();
119                         }
120                     }, new ConfigReader.OnDoneListener() {
121                         public void done() {
122                             Snackbar.make(b.backupButton, R.string.config_restored,
123                                     Snackbar.LENGTH_LONG)
124                                     .show();
125                         }
126                     });
127             reader.start();
128         }
129         catch (IOException e) {
130             e.printStackTrace();
131         }
132
133     }
134     private void backupClicked(View view) {
135         final Date now = new Date();
136         DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault());
137         df.format(now);
138         backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now)));
139     }
140     private void restoreClicked(View view) {
141         restoreChooserLauncher.launch(new String[]{"application/json"});
142     }
143
144 }