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