]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/BackupsActivity.java
gray out the backup controls if there is nothing to backup
[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 import net.ktnx.mobileledger.model.Data;
37
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;
43
44 public class BackupsActivity extends AppCompatActivity {
45     private FragmentBackupsBinding b;
46     private ActivityResultLauncher<String> backupChooserLauncher;
47     private ActivityResultLauncher<String[]> restoreChooserLauncher;
48     @Override
49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         b = FragmentBackupsBinding.inflate(getLayoutInflater());
52         setContentView(b.getRoot());
53
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);
59         }
60
61         b.backupButton.setOnClickListener(this::backupClicked);
62         b.restoreButton.setOnClickListener(this::restoreClicked);
63
64
65         backupChooserLauncher =
66                 registerForActivityResult(new ActivityResultContracts.CreateDocument(),
67                         this::storeConfig);
68         restoreChooserLauncher =
69                 registerForActivityResult(new ActivityResultContracts.OpenDocument(),
70                         this::readConfig);
71
72         Data.observeProfile(this, p -> {
73             if (p == null) {
74                 b.backupButton.setEnabled(false);
75                 b.backupExplanationText.setEnabled(false);
76             }
77             else {
78                 b.backupButton.setEnabled(true);
79                 b.backupExplanationText.setEnabled(true);
80             }
81         });
82     }
83     @Override
84     public boolean onOptionsItemSelected(MenuItem item) {
85         if (item.getItemId() == android.R.id.home) {
86             finish();
87
88             return true;
89         }
90         return super.onOptionsItemSelected(item);
91     }
92     private void storeConfig(Uri result) {
93         if (result == null)
94             return;
95
96         try {
97             ConfigWriter saver =
98                     new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
99                         @Override
100                         public void error(Exception e) {
101                             Snackbar.make(b.backupButton, e.toString(),
102                                     BaseTransientBottomBar.LENGTH_LONG)
103                                     .show();
104                         }
105                     }, new ConfigWriter.OnDoneListener() {
106                         public void done() {
107                             Snackbar.make(b.backupButton, R.string.config_saved,
108                                     Snackbar.LENGTH_LONG)
109                                     .show();
110                         }
111                     });
112             saver.start();
113         }
114         catch (IOException e) {
115             e.printStackTrace();
116         }
117
118     }
119     private void readConfig(Uri result) {
120         if (result == null)
121             return;
122
123         try {
124             ConfigReader reader =
125                     new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
126                         @Override
127                         public void error(Exception e) {
128                             Snackbar.make(b.backupButton, e.toString(),
129                                     BaseTransientBottomBar.LENGTH_LONG)
130                                     .show();
131                         }
132                     }, new ConfigReader.OnDoneListener() {
133                         public void done() {
134                             Snackbar.make(b.backupButton, R.string.config_restored,
135                                     Snackbar.LENGTH_LONG)
136                                     .show();
137                         }
138                     });
139             reader.start();
140         }
141         catch (IOException e) {
142             e.printStackTrace();
143         }
144
145     }
146     private void backupClicked(View view) {
147         final Date now = new Date();
148         DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault());
149         df.format(now);
150         backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now)));
151     }
152     private void restoreClicked(View view) {
153         restoreChooserLauncher.launch(new String[]{"application/json"});
154     }
155
156 }