]> git.ktnx.net Git - mobile-ledger.git/blob - mobileledger/BackupsActivity.java
bump androidx.constraintlayout library version
[mobile-ledger.git] / 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.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;
26
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;
31
32 import com.google.android.material.snackbar.BaseTransientBottomBar;
33 import com.google.android.material.snackbar.Snackbar;
34
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;
39
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;
45
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);
53     }
54     @Override
55     protected void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57         b = FragmentBackupsBinding.inflate(getLayoutInflater());
58         setContentView(b.getRoot());
59
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);
65         }
66
67         b.backupButton.setOnClickListener(this::backupClicked);
68         b.restoreButton.setOnClickListener(this::restoreClicked);
69
70
71         backupChooserLauncher =
72                 registerForActivityResult(new ActivityResultContracts.CreateDocument(),
73                         this::storeConfig);
74         restoreChooserLauncher =
75                 registerForActivityResult(new ActivityResultContracts.OpenDocument(),
76                         this::readConfig);
77
78         Data.observeProfile(this, p -> {
79             if (p == null) {
80                 b.backupButton.setEnabled(false);
81                 b.backupExplanationText.setEnabled(false);
82             }
83             else {
84                 b.backupButton.setEnabled(true);
85                 b.backupExplanationText.setEnabled(true);
86             }
87         });
88     }
89     @Override
90     public boolean onOptionsItemSelected(MenuItem item) {
91         if (item.getItemId() == android.R.id.home) {
92             finish();
93
94             return true;
95         }
96         return super.onOptionsItemSelected(item);
97     }
98     private void storeConfig(Uri result) {
99         if (result == null)
100             return;
101
102         try {
103             ConfigWriter saver =
104                     new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
105                         @Override
106                         public void error(Exception e) {
107                             Snackbar.make(b.backupButton, e.toString(),
108                                     BaseTransientBottomBar.LENGTH_LONG)
109                                     .show();
110                         }
111                     }, new ConfigWriter.OnDoneListener() {
112                         public void done() {
113                             Snackbar.make(b.backupButton, R.string.config_saved,
114                                     Snackbar.LENGTH_LONG)
115                                     .show();
116                         }
117                     });
118             saver.start();
119         }
120         catch (IOException e) {
121             e.printStackTrace();
122         }
123
124     }
125     private void readConfig(Uri result) {
126         if (result == null)
127             return;
128
129         try {
130             ConfigReader reader =
131                     new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() {
132                         @Override
133                         public void error(Exception e) {
134                             Snackbar.make(b.backupButton, e.toString(),
135                                     BaseTransientBottomBar.LENGTH_LONG)
136                                     .show();
137                         }
138                     }, new ConfigReader.OnDoneListener() {
139                         public void done() {
140                             Snackbar.make(b.backupButton, R.string.config_restored,
141                                     Snackbar.LENGTH_LONG)
142                                     .show();
143                         }
144                     });
145             reader.start();
146         }
147         catch (IOException e) {
148             e.printStackTrace();
149         }
150
151     }
152     private void backupClicked(View view) {
153         final Date now = new Date();
154         DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault());
155         df.format(now);
156         backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now)));
157     }
158     private void restoreClicked(View view) {
159         restoreChooserLauncher.launch(new String[]{"application/json"});
160     }
161
162 }