Implement import function

This commit is contained in:
Jakob Nixdorf 2017-07-01 16:32:43 +02:00
parent 3d18fde9cf
commit c7a75f6612
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
5 changed files with 106 additions and 14 deletions

View file

@ -23,7 +23,6 @@ goes to Bruno.
## TODO
* Re-implement renaming of entries
* Import from JSON
* Export the secret key (maybe)
<!--

View file

@ -53,6 +53,10 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntriesCardAdapter.
return entries.get(i);
}
public void setEntries(ArrayList<Entry> e) {
entries = e;
}
@Override
public void onBindViewHolder(EntryViewHolder entryViewHolder, int i) {
Entry entry = entries.get(i);

View file

@ -1,4 +1,5 @@
/*
* Copyright (C) 2017 Jakob Nixdorf
* Copyright (C) 2015 Bruno Bierbaumer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@ -30,6 +31,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
@ -67,6 +69,9 @@ public class MainActivity extends AppCompatActivity {
private static final int PERMISSIONS_REQUEST_CAMERA = 42;
private static final int PERMISSIONS_REQUEST_WRITE_EXPORT = 24;
private static final int PERMISSIONS_REQUEST_READ_IMPORT = 12;
private static final int INTENT_OPEN_DOCUMENT = 42;
private void doScanQRCode(){
new IntentIntegrator(MainActivity.this)
@ -145,6 +150,39 @@ public class MainActivity extends AppCompatActivity {
.show();
}
private void doImportJSON(Uri uri) {
if (StorageHelper.isExternalStorageReadable()) {
boolean success = SettingsHelper.importFromJSON(this, uri);
if (success) {
entries = SettingsHelper.load(this);
adapter.setEntries(entries);
adapter.notifyDataSetChanged();
showSimpleSnackbar(R.string.msg_import_success);
} else {
showSimpleSnackbar(R.string.msg_import_failed);
}
} else {
showSimpleSnackbar(R.string.msg_storage_not_accessible);
}
}
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 importJSON() {
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);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
if(requestCode == PERMISSIONS_REQUEST_CAMERA) {
@ -159,6 +197,12 @@ public class MainActivity extends AppCompatActivity {
} else {
showSimpleSnackbar(R.string.msg_storage_permissions);
}
} else if (requestCode == PERMISSIONS_REQUEST_READ_IMPORT) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
importJSONWithSelector();
} else {
showSimpleSnackbar(R.string.msg_storage_permissions);
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@ -314,6 +358,12 @@ public class MainActivity extends AppCompatActivity {
return;
}
} else if (requestCode == INTENT_OPEN_DOCUMENT && resultCode == Activity.RESULT_OK) {
Uri file = null;
if (intent != null) {
file = intent.getData();
doImportJSON(file);
}
}
if(entries.isEmpty()){
@ -337,7 +387,7 @@ public class MainActivity extends AppCompatActivity {
return true;
} else if (id == R.id.action_import) {
SettingsHelper.importFromJSON(this);
importJSON();
return true;
} else if (id == R.id.action_about){

View file

@ -23,13 +23,17 @@
package org.shadowice.flocke.andotp;
import android.content.Context;
import android.net.Uri;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;
@ -45,6 +49,19 @@ public class SettingsHelper {
public static final String EXPORT_FILE = "otp_accounts.json";
public static void store(Context context, JSONArray json) {
try {
byte[] data = json.toString().getBytes();
SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE));
data = EncryptionHelper.encrypt(key,data);
writeFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE), data);
} catch (Exception e) {
}
}
public static void store(Context context, ArrayList<Entry> entries){
JSONArray a = new JSONArray();
@ -55,17 +72,7 @@ public class SettingsHelper {
}
}
try {
byte[] data = a.toString().getBytes();
SecretKey key = EncryptionHelper.loadOrGenerateKeys(context, new File(context.getFilesDir() + "/" + KEY_FILE));
data = EncryptionHelper.encrypt(key,data);
writeFully(new File(context.getFilesDir() + "/" + SETTINGS_FILE), data);
} catch (Exception e) {
}
store(context, a);
}
public static ArrayList<Entry> load(Context context){
@ -124,7 +131,37 @@ public class SettingsHelper {
return success;
}
public static void importFromJSON(Context context) {
public static boolean importFromJSON(Context context, Uri file) {
boolean success = true;
StringBuilder stringBuilder = new StringBuilder();
try {
InputStream inputStream = context.getContentResolver().openInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
reader.close();
inputStream.close();
} catch (Exception e) {
success = false;
e.printStackTrace();
}
String content = stringBuilder.toString();
JSONArray json = null;
try {
json = new JSONArray(content);
} catch (Exception e) {
success = false;
e.printStackTrace();
}
store(context, json);
return success;
}
}

View file

@ -28,6 +28,8 @@
</string>
<string name="msg_export_success">Export to external storage successful</string>
<string name="msg_export_failed">Export to external storage failed</string>
<string name="msg_import_success">Import from external storage successful</string>
<string name="msg_import_failed">Import from external storage failed</string>
<string name="msg_storage_not_accessible">External storage currently not accessible</string>
<!-- About dialog -->