mirror of
https://github.com/woheller69/solxpect.git
synced 2026-02-02 06:51:46 +01:00
add backup/restore
This commit is contained in:
parent
67027dc8e6
commit
7a2edcf814
12 changed files with 297 additions and 2 deletions
69
app/src/main/java/org/woheller69/weather/Backup.java
Normal file
69
app/src/main/java/org/woheller69/weather/Backup.java
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package org.woheller69.weather;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class Backup {
|
||||
public static final int PERMISSION_REQUEST_CODE = 123;
|
||||
|
||||
public static boolean checkPermissionStorage (Context context) {
|
||||
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||
int result1 = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
public static void requestPermission(Activity activity) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
builder.setIcon(R.drawable.ic_warning_amber_black_24dp);
|
||||
builder.setTitle(activity.getResources().getString(R.string.permission_required));
|
||||
builder.setMessage(activity.getResources().getString(R.string.permission_message,activity.getResources().getString(R.string.app_name)));
|
||||
builder.setPositiveButton(R.string.dialog_OK_button, (dialog, which) -> {
|
||||
dialog.cancel();
|
||||
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
|
||||
});
|
||||
builder.setNegativeButton(R.string.dialog_NO_button, (dialog, whichButton) -> dialog.cancel());
|
||||
AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
public static void zipExtract(Context context, File targetDir, Uri zipFile) {
|
||||
ZipEntry zipEntry;
|
||||
int readLen;
|
||||
byte[] readBuffer = new byte[4096];
|
||||
try {
|
||||
InputStream src = context.getContentResolver().openInputStream(zipFile);
|
||||
try {
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(src)) {
|
||||
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||
File extractedFile = new File(targetDir ,zipEntry.getName());
|
||||
try (OutputStream outputStream = new FileOutputStream(extractedFile)) {
|
||||
while ((readLen = zipInputStream.read(readBuffer)) != -1) {
|
||||
outputStream.write(readBuffer, 0, readLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ioException) {
|
||||
ioException.printStackTrace();
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
package org.woheller69.weather.activities;
|
||||
|
||||
import static android.os.Environment.DIRECTORY_DOCUMENTS;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import net.lingala.zip4j.ZipFile;
|
||||
import net.lingala.zip4j.exception.ZipException;
|
||||
|
||||
import org.woheller69.weather.Backup;
|
||||
import org.woheller69.weather.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public class BackupRestoreActivity extends NavigationActivity{
|
||||
ActivityResultLauncher<Intent> mRestore;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_backuprestore);
|
||||
|
||||
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
mRestore = registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
File intData = new File(Environment.getDataDirectory() + "//databases//" + this.getPackageName());
|
||||
if (result.getData()!=null && result.getData().getData()!=null) Backup.zipExtract(this, intData, result.getData().getData());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getNavigationDrawerID() {
|
||||
return R.id.nav_backuprestore;
|
||||
}
|
||||
|
||||
public void performBackup(View view) {
|
||||
File extStorage;
|
||||
File intData;
|
||||
intData = new File(Environment.getDataDirectory()+"//data//" + this.getPackageName() + "//databases//");
|
||||
extStorage = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS);
|
||||
String filesBackup = getResources().getString(R.string.app_name)+".zip";
|
||||
final File dbBackup = new File(extStorage, filesBackup);
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setMessage(getResources().getString(R.string.backup_database) +" -> " + dbBackup.toString());
|
||||
builder.setPositiveButton(R.string.dialog_OK_button, (dialog, whichButton) -> {
|
||||
if (!Backup.checkPermissionStorage(this)) {
|
||||
Backup.requestPermission(this);
|
||||
} else {
|
||||
if (dbBackup.exists()){
|
||||
if (!dbBackup.delete()){
|
||||
Toast.makeText(this,getResources().getString(R.string.toast_delete), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
try {
|
||||
new ZipFile(dbBackup).addFolder(intData);
|
||||
} catch (ZipException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton(R.string.dialog_NO_button, (dialog, whichButton) -> dialog.cancel());
|
||||
AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.BOTTOM);
|
||||
|
||||
}
|
||||
|
||||
public void performRestore(View view) {
|
||||
File extStorage;
|
||||
File intData;
|
||||
intData = new File(Environment.getDataDirectory() + "//data//" + this.getPackageName());
|
||||
extStorage = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS);
|
||||
String filesBackup = getResources().getString(R.string.app_name)+".zip";
|
||||
final File zipFileBackup = new File(extStorage, filesBackup);
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setMessage(getResources().getString(R.string.restore_database));
|
||||
builder.setPositiveButton(R.string.dialog_OK_button, (dialog, whichButton) -> {
|
||||
if (!Backup.checkPermissionStorage(this)) {
|
||||
Backup.requestPermission(this);
|
||||
} else {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
intent.setType("application/zip");
|
||||
mRestore.launch(intent);
|
||||
} else {
|
||||
Backup.zipExtract(this, intData, Uri.fromFile(zipFileBackup));
|
||||
}
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton(R.string.dialog_NO_button, (dialog, whichButton) -> dialog.cancel());
|
||||
AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.BOTTOM);
|
||||
}
|
||||
}
|
||||
|
|
@ -169,6 +169,9 @@ public class NavigationActivity extends AppCompatActivity implements OnNavigatio
|
|||
}else if(itemId==R.id.nav_help) {
|
||||
intent = new Intent(this, HelpActivity.class);
|
||||
startActivity(intent);
|
||||
}else if(itemId==R.id.nav_backuprestore) {
|
||||
intent = new Intent(this, BackupRestoreActivity.class);
|
||||
startActivity(intent);
|
||||
}else if (itemId==R.id.star_on_github){
|
||||
startActivity(new Intent(Intent.ACTION_VIEW,
|
||||
Uri.parse(BuildConfig.GITHUB_URL)));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue