Add a button to change the backup storage location
This commit is contained in:
parent
b3c744b872
commit
b8e9e60666
4 changed files with 78 additions and 28 deletions
app/src/main
java/com/stevesoltys/backup/activity
res
|
@ -5,26 +5,46 @@ import android.content.Intent;
|
|||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.stevesoltys.backup.R;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
|
||||
public class MainActivity extends Activity implements View.OnClickListener {
|
||||
|
||||
public static final int OPEN_DOCUMENT_TREE_REQUEST_CODE = 1;
|
||||
|
||||
public static final int LOAD_DOCUMENT_REQUEST_CODE = 2;
|
||||
public static final int OPEN_DOCUMENT_TREE_BACKUP_REQUEST_CODE = 2;
|
||||
|
||||
public static final int LOAD_DOCUMENT_REQUEST_CODE = 3;
|
||||
|
||||
private MainActivityController controller;
|
||||
private Button changeLocationButton;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
controller = new MainActivityController();
|
||||
|
||||
findViewById(R.id.create_backup_button).setOnClickListener(this);
|
||||
findViewById(R.id.restore_backup_button).setOnClickListener(this);
|
||||
|
||||
controller = new MainActivityController();
|
||||
changeLocationButton = findViewById(R.id.change_backup_location_button);
|
||||
changeLocationButton.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
if (controller.isChangeBackupLocationButtonVisible(this)) {
|
||||
changeLocationButton.setVisibility(VISIBLE);
|
||||
} else {
|
||||
changeLocationButton.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -40,6 +60,10 @@ public class MainActivity extends Activity implements View.OnClickListener {
|
|||
case R.id.restore_backup_button:
|
||||
controller.showLoadDocumentActivity(this);
|
||||
break;
|
||||
|
||||
case R.id.change_backup_location_button:
|
||||
controller.onChangeBackupLocationButtonClicked(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,7 +78,11 @@ public class MainActivity extends Activity implements View.OnClickListener {
|
|||
switch (requestCode) {
|
||||
|
||||
case OPEN_DOCUMENT_TREE_REQUEST_CODE:
|
||||
controller.handleChooseFolderResult(result, this);
|
||||
controller.handleChooseFolderResult(result, this, false);
|
||||
break;
|
||||
|
||||
case OPEN_DOCUMENT_TREE_BACKUP_REQUEST_CODE:
|
||||
controller.handleChooseFolderResult(result, this, true);
|
||||
break;
|
||||
|
||||
case LOAD_DOCUMENT_REQUEST_CODE:
|
||||
|
|
|
@ -24,6 +24,7 @@ import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
|
|||
import static android.provider.DocumentsContract.buildDocumentUriUsingTree;
|
||||
import static android.provider.DocumentsContract.createDocument;
|
||||
import static android.provider.DocumentsContract.getTreeDocumentId;
|
||||
import static com.stevesoltys.backup.activity.MainActivity.OPEN_DOCUMENT_TREE_BACKUP_REQUEST_CODE;
|
||||
import static com.stevesoltys.backup.activity.MainActivity.OPEN_DOCUMENT_TREE_REQUEST_CODE;
|
||||
import static com.stevesoltys.backup.settings.SettingsManager.getBackupFolderUri;
|
||||
import static com.stevesoltys.backup.settings.SettingsManager.setBackupFolderUri;
|
||||
|
@ -40,26 +41,32 @@ class MainActivityController {
|
|||
void onBackupButtonClicked(Activity parent) {
|
||||
Uri folderUri = getBackupFolderUri(parent);
|
||||
if (folderUri == null) {
|
||||
showChooseFolderActivity(parent);
|
||||
showChooseFolderActivity(parent, true);
|
||||
} else {
|
||||
try {
|
||||
Uri fileUri = createBackupFile(parent.getContentResolver(), folderUri);
|
||||
showCreateBackupActivity(parent, fileUri);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
showChooseFolderActivity(parent);
|
||||
showChooseFolderActivity(parent, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showChooseFolderActivity(Activity parent) {
|
||||
Intent createDocumentIntent = new Intent(ACTION_OPEN_DOCUMENT_TREE);
|
||||
createDocumentIntent.addFlags(FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
|
||||
boolean isChangeBackupLocationButtonVisible(Activity parent) {
|
||||
return getBackupFolderUri(parent) != null;
|
||||
}
|
||||
|
||||
private void showChooseFolderActivity(Activity parent, boolean continueToBackup) {
|
||||
Intent openTreeIntent = new Intent(ACTION_OPEN_DOCUMENT_TREE);
|
||||
openTreeIntent.addFlags(FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
|
||||
FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
|
||||
try {
|
||||
Intent documentChooser = Intent.createChooser(createDocumentIntent, "Select the backup location");
|
||||
parent.startActivityForResult(documentChooser, OPEN_DOCUMENT_TREE_REQUEST_CODE);
|
||||
Intent documentChooser = Intent.createChooser(openTreeIntent, "Select the backup location");
|
||||
int requestCode = continueToBackup ? OPEN_DOCUMENT_TREE_BACKUP_REQUEST_CODE : OPEN_DOCUMENT_TREE_REQUEST_CODE;
|
||||
parent.startActivityForResult(documentChooser, requestCode);
|
||||
|
||||
} catch (ActivityNotFoundException ex) {
|
||||
Toast.makeText(parent, "Please install a file manager.", Toast.LENGTH_SHORT).show();
|
||||
|
@ -80,7 +87,11 @@ class MainActivityController {
|
|||
}
|
||||
}
|
||||
|
||||
void handleChooseFolderResult(Intent result, Activity parent) {
|
||||
void onChangeBackupLocationButtonClicked(Activity parent) {
|
||||
showChooseFolderActivity(parent, false);
|
||||
}
|
||||
|
||||
void handleChooseFolderResult(Intent result, Activity parent, boolean continueToBackup) {
|
||||
|
||||
if (result == null || result.getData() == null) {
|
||||
return;
|
||||
|
@ -97,18 +108,19 @@ class MainActivityController {
|
|||
// store backup folder location in settings
|
||||
setBackupFolderUri(parent, folderUri);
|
||||
|
||||
// create a new backup file in folder
|
||||
Uri fileUri;
|
||||
if (!continueToBackup) return;
|
||||
|
||||
try {
|
||||
fileUri = createBackupFile(contentResolver, folderUri);
|
||||
// create a new backup file in folder
|
||||
Uri fileUri = createBackupFile(contentResolver, folderUri);
|
||||
|
||||
showCreateBackupActivity(parent, fileUri);
|
||||
|
||||
} catch (IOException e) {
|
||||
// TODO show better error message once more infrastructure is in place
|
||||
Toast.makeText(parent, "Error creating backup file", Toast.LENGTH_SHORT).show();
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
showCreateBackupActivity(parent, fileUri);
|
||||
}
|
||||
|
||||
private void showCreateBackupActivity(Activity parent, Uri fileUri) {
|
||||
|
@ -134,6 +146,7 @@ class MainActivityController {
|
|||
Uri fileUri = createDocument(contentResolver, documentUri, DOCUMENT_MIME_TYPE, getBackupFileName());
|
||||
if (fileUri == null) throw new IOException();
|
||||
return fileUri;
|
||||
|
||||
} catch (SecurityException e) {
|
||||
// happens when folder was deleted and thus Uri permission don't exist anymore
|
||||
throw new IOException(e);
|
||||
|
|
|
@ -1,29 +1,37 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/activity_main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:weightSum="1"
|
||||
tools:context="com.stevesoltys.backup.activity.MainActivity">
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/activity_main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context="com.stevesoltys.backup.activity.MainActivity">
|
||||
|
||||
<Button
|
||||
android:id="@+id/create_backup_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/button_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/button_horizontal_margin"
|
||||
android:layout_marginTop="@dimen/button_vertical_margin"
|
||||
android:text="@string/create_backup_button"/>
|
||||
android:layout_marginRight="@dimen/button_horizontal_margin"
|
||||
android:text="@string/create_backup_button" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/restore_backup_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/button_horizontal_margin"
|
||||
android:layout_marginRight="@dimen/button_horizontal_margin"
|
||||
android:layout_marginTop="@dimen/button_vertical_margin"
|
||||
android:text="@string/restore_backup_button"/>
|
||||
android:layout_marginRight="@dimen/button_horizontal_margin"
|
||||
android:text="@string/restore_backup_button" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/change_backup_location_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/button_horizontal_margin"
|
||||
android:layout_marginTop="72dp"
|
||||
android:layout_marginRight="@dimen/button_horizontal_margin"
|
||||
android:text="@string/change_backup_location_button" />
|
||||
|
||||
</LinearLayout>
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
<string name="create_backup_button">Create backup</string>
|
||||
<string name="restore_backup_button">Restore backup</string>
|
||||
<string name="change_backup_location_button">Change backup location</string>
|
||||
|
||||
<string name="backup_success">Backup success</string>
|
||||
<string name="backup_failure">Backup failure</string>
|
||||
|
|
Loading…
Add table
Reference in a new issue