X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FBackupsActivity.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FBackupsActivity.java;h=9a1f706a718264f90cc2df3e39cc39802719df35;hb=39eca7a036963be2e6dc4e6403961177f2c5267d;hp=0000000000000000000000000000000000000000;hpb=1595c5ff0973f34eca8b9830589b2c982caf26c7;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/BackupsActivity.java b/app/src/main/java/net/ktnx/mobileledger/BackupsActivity.java new file mode 100644 index 00000000..9a1f706a --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/BackupsActivity.java @@ -0,0 +1,144 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger; + +import android.net.Uri; +import android.os.Bundle; +import android.view.MenuItem; +import android.view.View; + +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; + +import com.google.android.material.snackbar.BaseTransientBottomBar; +import com.google.android.material.snackbar.Snackbar; + +import net.ktnx.mobileledger.async.ConfigReader; +import net.ktnx.mobileledger.async.ConfigWriter; +import net.ktnx.mobileledger.databinding.FragmentBackupsBinding; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +public class BackupsActivity extends AppCompatActivity { + private FragmentBackupsBinding b; + private ActivityResultLauncher backupChooserLauncher; + private ActivityResultLauncher restoreChooserLauncher; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + b = FragmentBackupsBinding.inflate(getLayoutInflater()); + setContentView(b.getRoot()); + + setSupportActionBar(b.toolbar); + // Show the Up button in the action bar. + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + } + + b.backupButton.setOnClickListener(this::backupClicked); + b.restoreButton.setOnClickListener(this::restoreClicked); + + + backupChooserLauncher = + registerForActivityResult(new ActivityResultContracts.CreateDocument(), + this::storeConfig); + restoreChooserLauncher = + registerForActivityResult(new ActivityResultContracts.OpenDocument(), + this::readConfig); + } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + + return true; + } + return super.onOptionsItemSelected(item); + } + private void storeConfig(Uri result) { + if (result == null) + return; + + try { + ConfigWriter saver = + new ConfigWriter(getBaseContext(), result, new ConfigWriter.OnErrorListener() { + @Override + public void error(Exception e) { + Snackbar.make(b.backupButton, e.toString(), + BaseTransientBottomBar.LENGTH_LONG) + .show(); + } + }, new ConfigWriter.OnDoneListener() { + public void done() { + Snackbar.make(b.backupButton, R.string.config_saved, + Snackbar.LENGTH_LONG) + .show(); + } + }); + saver.start(); + } + catch (IOException e) { + e.printStackTrace(); + } + + } + private void readConfig(Uri result) { + if (result == null) + return; + + try { + ConfigReader reader = + new ConfigReader(getBaseContext(), result, new ConfigWriter.OnErrorListener() { + @Override + public void error(Exception e) { + Snackbar.make(b.backupButton, e.toString(), + BaseTransientBottomBar.LENGTH_LONG) + .show(); + } + }, new ConfigReader.OnDoneListener() { + public void done() { + Snackbar.make(b.backupButton, R.string.config_restored, + Snackbar.LENGTH_LONG) + .show(); + } + }); + reader.start(); + } + catch (IOException e) { + e.printStackTrace(); + } + + } + private void backupClicked(View view) { + final Date now = new Date(); + DateFormat df = new SimpleDateFormat("y-MM-dd HH:mm", Locale.getDefault()); + df.format(now); + backupChooserLauncher.launch(String.format("MoLe-%s.json", df.format(now))); + } + private void restoreClicked(View view) { + restoreChooserLauncher.launch(new String[]{"application/json"}); + } + +} \ No newline at end of file