Use a separate activity for the backup functions

This commit is contained in:
Jakob Nixdorf 2017-07-12 17:51:15 +02:00
parent e2c1050c50
commit d693d331ae
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
7 changed files with 320 additions and 167 deletions

View file

@ -23,6 +23,10 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".BackupActivity"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity <activity
android:name=".SettingsActivity" android:name=".SettingsActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"

View file

@ -0,0 +1,212 @@
package org.shadowice.flocke.andotp;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.Toast;
public class BackupActivity extends AppCompatActivity {
private final static int INTENT_OPEN_DOCUMENT_PLAIN = 100;
private final static int INTENT_SAVE_DOCUMENT_PLAIN = 101;
private final static int PERMISSIONS_REQUEST_READ_IMPORT_PLAIN = 110;
private final static int PERMISSIONS_REQUEST_WRITE_EXPORT_PLAIN = 111;
private static final String DEFAULT_BACKUP_FILENAME = "otp_accounts.json";
private static final String DEFAULT_BACKUP_MIMETYPE = "application/json";
private boolean reload = false;
LinearLayout exportPlain;
LinearLayout importPlain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.backup_activity_title);
setContentView(R.layout.activity_container);
Toolbar toolbar = (Toolbar) findViewById(R.id.container_toolbar);
setSupportActionBar(toolbar);
ViewStub stub = (ViewStub) findViewById(R.id.container_stub);
stub.setLayoutResource(R.layout.content_backup);
View v = stub.inflate();
exportPlain = (LinearLayout) v.findViewById(R.id.button_export_plain);
importPlain = (LinearLayout) v.findViewById(R.id.button_import_plain);
exportPlain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exportJSONWithWarning();
}
});
importPlain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
importJSONWithPermissions();
}
});
}
// End with a result
public void finishWithResult() {
Intent data = new Intent();
data.putExtra("reload", reload);
setResult(RESULT_OK, data);
finish();
}
// Go back to the main activity
@Override
public boolean onSupportNavigateUp() {
finishWithResult();
return true;
}
@Override
public void onBackPressed() {
finishWithResult();
super.onBackPressed();
}
// Get the result from permission requests
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_IMPORT_PLAIN) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
importJSONWithSelector();
} else {
Toast.makeText(this, R.string.backup_toast_storage_permissions, Toast.LENGTH_LONG).show();
}
} else if (requestCode == PERMISSIONS_REQUEST_WRITE_EXPORT_PLAIN) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
exportJSONWithSelector();
} else {
Toast.makeText(this, R.string.backup_toast_storage_permissions, Toast.LENGTH_LONG).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
// Get the result from external activities
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == INTENT_OPEN_DOCUMENT_PLAIN && resultCode == Activity.RESULT_OK) {
Uri file;
if (intent != null) {
file = intent.getData();
doImportJSON(file);
}
} else if (requestCode == INTENT_SAVE_DOCUMENT_PLAIN && resultCode == Activity.RESULT_OK) {
Uri file;
if (intent != null) {
file = intent.getData();
doExportJSON(file);
}
}
}
// Import from JSON
private void doImportJSON(Uri uri) {
if (StorageHelper.isExternalStorageReadable()) {
boolean success = DatabaseHelper.importFromJSON(this, uri);
if (success) {
reload = true;
Toast.makeText(this, R.string.backup_toast_import_success, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, R.string.backup_toast_import_failed, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, R.string.backup_toast_storage_not_accessible, Toast.LENGTH_LONG).show();
}
finishWithResult();
}
private void importJSONWithSelector() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, INTENT_OPEN_DOCUMENT_PLAIN);
}
private void importJSONWithPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
importJSONWithSelector();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_IMPORT_PLAIN);
}
}
// Export to JSON
private void doExportJSON(Uri uri) {
if (StorageHelper.isExternalStorageWritable()) {
boolean success = DatabaseHelper.exportAsJSON(this, uri);
if (success)
Toast.makeText(this, R.string.backup_toast_export_success, Toast.LENGTH_LONG).show();
else
Toast.makeText(this, R.string.backup_toast_export_failed, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, R.string.backup_toast_storage_not_accessible, Toast.LENGTH_LONG).show();
}
finishWithResult();
}
private void exportJSONWithPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
exportJSONWithSelector();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_WRITE_EXPORT_PLAIN);
}
}
private void exportJSONWithSelector() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(DEFAULT_BACKUP_MIMETYPE);
intent.putExtra(Intent.EXTRA_TITLE, DEFAULT_BACKUP_FILENAME);
startActivityForResult(intent, INTENT_SAVE_DOCUMENT_PLAIN);
}
private void exportJSONWithWarning() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.backup_dialog_title_security_warning)
.setMessage(R.string.backup_dialog_msg_export_warning)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
exportJSONWithPermissions();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.create()
.show();
}
}

View file

@ -23,9 +23,7 @@
package org.shadowice.flocke.andotp; package org.shadowice.flocke.andotp;
import android.Manifest;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.KeyguardManager; import android.app.KeyguardManager;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -33,15 +31,11 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout; import android.support.constraint.ConstraintLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
@ -75,16 +69,9 @@ public class MainActivity extends AppCompatActivity {
private Handler handler; private Handler handler;
private Runnable handlerTask; private Runnable handlerTask;
private static final int PERMISSIONS_REQUEST_WRITE_EXPORT = 42; private static final int INTENT_AUTHENTICATE = 1;
private static final int PERMISSIONS_REQUEST_READ_IMPORT = 41; private static final int INTENT_INTERNAL_SETTINGS = 2;
private static final int INTENT_INTERNAL_BACKUP = 3;
private static final int INTENT_OPEN_DOCUMENT = 24;
private static final int INTENT_SAVE_DOCUMENT= 23;
private static final int INTENT_SETTINGS = 22;
private static final int INTENT_AUTHENTICATE = 21;
private static final String DEFAULT_BACKUP_FILENAME = "otp_accounts.json";
private static final String DEFAULT_BACKUP_MIMETYPE = "application/json";
// QR code scanning // QR code scanning
private void scanQRCode(){ private void scanQRCode(){
@ -164,107 +151,6 @@ public class MainActivity extends AppCompatActivity {
.show(); .show();
} }
// Export to JSON
private void doExportJSON(Uri uri) {
if (StorageHelper.isExternalStorageWritable()) {
boolean success = DatabaseHelper.exportAsJSON(this, uri);
if (success)
Toast.makeText(this, R.string.toast_export_success, Toast.LENGTH_LONG).show();
else
Toast.makeText(this, R.string.toast_export_failed, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, R.string.toast_storage_not_accessible, Toast.LENGTH_LONG).show();
}
}
private void exportJSONWithPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
exportJSONWithSelector();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_WRITE_EXPORT);
}
}
private void exportJSONWithSelector() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(DEFAULT_BACKUP_MIMETYPE);
intent.putExtra(Intent.EXTRA_TITLE, DEFAULT_BACKUP_FILENAME);
startActivityForResult(intent, INTENT_SAVE_DOCUMENT);
}
private void exportJSONWithWarning() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.dialog_title_security_warning)
.setMessage(R.string.dialog_msg_export_warning)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
exportJSONWithPermissions();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.create()
.show();
}
// Import from JSON
private void doImportJSON(Uri uri) {
if (StorageHelper.isExternalStorageReadable()) {
boolean success = DatabaseHelper.importFromJSON(this, uri);
if (success) {
adapter.loadEntries();
Toast.makeText(this, R.string.toast_import_success, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, R.string.toast_import_failed, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, R.string.toast_storage_not_accessible, Toast.LENGTH_LONG).show();
}
}
private void importJSONWithSelector() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, INTENT_OPEN_DOCUMENT);
}
private void importJSONWithPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
importJSONWithSelector();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_IMPORT);
}
}
// Permission results
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_WRITE_EXPORT) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
exportJSONWithSelector();
} else {
Toast.makeText(this, R.string.toast_storage_permissions, Toast.LENGTH_LONG).show();
}
} else if (requestCode == PERMISSIONS_REQUEST_READ_IMPORT) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
importJSONWithSelector();
} else {
Toast.makeText(this, R.string.toast_storage_permissions, Toast.LENGTH_LONG).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
// Initialize the main application // Initialize the main application
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -396,20 +282,11 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(this, R.string.toast_invalid_qr_code, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.toast_invalid_qr_code, Toast.LENGTH_LONG).show();
} }
} }
} else if (requestCode == INTENT_OPEN_DOCUMENT && resultCode == Activity.RESULT_OK) { } else if (requestCode == INTENT_INTERNAL_SETTINGS && resultCode == RESULT_OK) {
Uri file;
if (intent != null) {
file = intent.getData();
doImportJSON(file);
}
} else if (requestCode == INTENT_SAVE_DOCUMENT && resultCode == Activity.RESULT_OK) {
Uri file;
if (intent != null) {
file = intent.getData();
doExportJSON(file);
}
} else if (requestCode == INTENT_SETTINGS && resultCode == RESULT_OK) {
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} else if (requestCode == INTENT_INTERNAL_BACKUP && resultCode == RESULT_OK) {
if (intent.getBooleanExtra("reload", false))
adapter.loadEntries();
} else if (requestCode == INTENT_AUTHENTICATE && resultCode != RESULT_OK) { } else if (requestCode == INTENT_AUTHENTICATE && resultCode != RESULT_OK) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
finishAndRemoveTask(); finishAndRemoveTask();
@ -460,15 +337,12 @@ public class MainActivity extends AppCompatActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); int id = item.getItemId();
if (id == R.id.action_export) { if (id == R.id.action_backup) {
exportJSONWithWarning(); Intent intent = new Intent(this, BackupActivity.class);
return true; startActivityForResult(intent, INTENT_INTERNAL_BACKUP);
} else if (id == R.id.action_import) {
importJSONWithPermissions();
return true;
} else if (id == R.id.action_settings) { } else if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class); Intent intent = new Intent(this, SettingsActivity.class);
startActivityForResult(intent, INTENT_SETTINGS); startActivityForResult(intent, INTENT_INTERNAL_SETTINGS);
} else if (id == R.id.action_about){ } else if (id == R.id.action_about){
showAbout(); showAbout();
return true; return true;

View file

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:textColor="@color/colorAccent"
android:textStyle="bold"
android:text="@string/backup_category_plain" />
<LinearLayout
android:id="@+id/button_export_plain"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_margin"
android:background="?android:attr/selectableItemBackground" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItem"
android:text="@string/backup_title_export_plain" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/backup_desc_export_plain"/>
</LinearLayout>
<LinearLayout
android:id="@+id/button_import_plain"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_margin"
android:background="?android:attr/selectableItemBackground" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItem"
android:text="@string/backup_title_import_plain" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/backup_desc_import_plain"/>
</LinearLayout>
</LinearLayout>

View file

@ -9,23 +9,10 @@
app:actionViewClass="android.support.v7.widget.SearchView" /> app:actionViewClass="android.support.v7.widget.SearchView" />
<item <item
android:id="@+id/submenu_backup" android:id="@+id/action_backup"
android:title="@string/menu_main_submenu_backup"> android:orderInCategory="100"
android:title="@string/menu_main_backup"
<menu> app:showAsAction="never" />
<item
android:id="@+id/action_export"
android:orderInCategory="100"
android:title="@string/menu_main_export"
app:showAsAction="never" />
<item
android:id="@+id/action_import"
android:orderInCategory="100"
android:title="@string/menu_main_import"
app:showAsAction="never" />
</menu>
</item>
<item <item
android:id="@+id/action_settings" android:id="@+id/action_settings"

View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="backup_activity_title">Backups</string>
<string name="backup_category_plain">Plain-text backups</string>
<string name="backup_title_export_plain">Export to JSON</string>
<string name="backup_title_import_plain">Import from JSON</string>
<string name="backup_desc_export_plain">Export all accounts to a plain-text JSON file.</string>
<string name="backup_desc_import_plain">Import accounts from a plain-text JSON file.</string>
<!-- Dialogs -->
<string name="backup_dialog_title_security_warning">Security warning</string>
<string name="backup_dialog_msg_export_warning">Do you really want to export the database as
plain-text JSON file? This file contains all your secret keys, please <b>keep it safe</b>!
</string>
<!-- Toast messages -->
<string name="backup_toast_export_success">Export to external storage successful</string>
<string name="backup_toast_export_failed">Export to external storage failed</string>
<string name="backup_toast_import_success">Import from external storage successful</string>
<string name="backup_toast_import_failed">Import from external storage failed</string>
<string name="backup_toast_storage_not_accessible">External storage currently not accessible</string>
<string name="backup_toast_storage_permissions">Storage permissions not granted</string>
</resources>

View file

@ -25,24 +25,16 @@
<!-- Menu --> <!-- Menu -->
<string name="menu_main_about">About</string> <string name="menu_main_about">About</string>
<string name="menu_main_export">Export (JSON)</string> <string name="menu_main_backup">Backup</string>
<string name="menu_main_import">Import (JSON)</string>
<string name="menu_main_search">Search</string> <string name="menu_main_search">Search</string>
<string name="menu_main_settings">Settings</string> <string name="menu_main_settings">Settings</string>
<string name="menu_main_submenu_backup">Import / Export</string>
<string name="menu_popup_edit_label">Edit label</string> <string name="menu_popup_edit_label">Edit label</string>
<string name="menu_popup_remove">Remove</string> <string name="menu_popup_remove">Remove</string>
<!-- Toast messages --> <!-- Toast messages -->
<string name="toast_copied_to_clipboard">Copied to clipboard</string> <string name="toast_copied_to_clipboard">Copied to clipboard</string>
<string name="toast_export_success">Export to external storage successful</string>
<string name="toast_export_failed">Export to external storage failed</string>
<string name="toast_import_success">Import from external storage successful</string>
<string name="toast_import_failed">Import from external storage failed</string>
<string name="toast_invalid_qr_code">Invalid QR Code</string> <string name="toast_invalid_qr_code">Invalid QR Code</string>
<string name="toast_storage_not_accessible">External storage currently not accessible</string>
<string name="toast_storage_permissions">Storage permissions not granted</string>
<string name="toast_tmp_hotp">HOTP is not yet implemented</string> <string name="toast_tmp_hotp">HOTP is not yet implemented</string>
@ -51,13 +43,9 @@
<string name="dialog_title_manual_entry">Enter details</string> <string name="dialog_title_manual_entry">Enter details</string>
<string name="dialog_title_remove">Remove</string> <string name="dialog_title_remove">Remove</string>
<string name="dialog_title_rename">Rename</string> <string name="dialog_title_rename">Rename</string>
<string name="dialog_title_security_warning">Security warning</string>
<string name="dialog_msg_auth">Enter your device credentials to start andOTP.</string> <string name="dialog_msg_auth">Enter your device credentials to start andOTP.</string>
<string name="dialog_msg_confirm_delete">Are you sure you want do remove this account?</string> <string name="dialog_msg_confirm_delete">Are you sure you want do remove this account?</string>
<string name="dialog_msg_export_warning">Do you really want to export the database as plain-text
JSON file? This file contains all your secret keys, please <b>keep it safe</b>!
</string>
<!-- About dialog --> <!-- About dialog -->
<string name="about_description"> <string name="about_description">