Export/import improvements

pull/147/head
M66B 6 years ago
parent 1dc5a4fad0
commit eb55cc2490

@ -39,6 +39,7 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
@ -46,6 +47,8 @@ import com.google.android.material.textfield.TextInputLayout;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -67,6 +70,7 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEKeySpec;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.documentfile.provider.DocumentFile;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
@ -81,6 +85,7 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
private ActionBarDrawerToggle drawerToggle; private ActionBarDrawerToggle drawerToggle;
private boolean hasAccount; private boolean hasAccount;
private String password;
private static final int KEY_ITERATIONS = 65536; private static final int KEY_ITERATIONS = 65536;
private static final int KEY_LENGTH = 256; private static final int KEY_LENGTH = 256;
@ -290,39 +295,11 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ActivitySetup.REQUEST_EXPORT || requestCode == ActivitySetup.REQUEST_IMPORT) if (resultCode == RESULT_OK && data != null)
if (resultCode == RESULT_OK && data != null) if (requestCode == REQUEST_EXPORT)
fileSelected(requestCode == ActivitySetup.REQUEST_EXPORT, data); handleExport(data, this.password);
} else if (requestCode == REQUEST_IMPORT)
handleImport(data, this.password);
private void fileSelected(final boolean export, final Intent data) {
View dview = LayoutInflater.from(this).inflate(R.layout.dialog_password, null);
final TextInputLayout etPassword1 = dview.findViewById(R.id.tilPassword1);
final TextInputLayout etPassword2 = dview.findViewById(R.id.tilPassword2);
new DialogBuilderLifecycle(this, this)
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String password1 = etPassword1.getEditText().getText().toString();
String password2 = etPassword2.getEditText().getText().toString();
if (TextUtils.isEmpty(password1))
Snackbar.make(view, R.string.title_setup_password_missing, Snackbar.LENGTH_LONG).show();
else {
if (password1.equals(password2)) {
if (export)
handleExport(data, password1);
else
handleImport(data, password1);
} else
Snackbar.make(view, R.string.title_setup_password_different, Snackbar.LENGTH_LONG).show();
}
}
})
.show();
} }
private void onManageNotifications() { private void onManageNotifications() {
@ -332,7 +309,7 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
private void onMenuExport() { private void onMenuExport() {
if (Helper.isPro(this)) if (Helper.isPro(this))
try { try {
startActivityForResult(Helper.getChooser(this, getIntentExport()), ActivitySetup.REQUEST_EXPORT); askPassword(true);
} catch (Throwable ex) { } catch (Throwable ex) {
Helper.unexpectedError(this, this, ex); Helper.unexpectedError(this, this, ex);
} }
@ -345,12 +322,47 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
private void onMenuImport() { private void onMenuImport() {
try { try {
startActivityForResult(Helper.getChooser(this, getIntentImport()), ActivitySetup.REQUEST_IMPORT); askPassword(false);
} catch (Throwable ex) { } catch (Throwable ex) {
Helper.unexpectedError(this, this, ex); Helper.unexpectedError(this, this, ex);
} }
} }
private void askPassword(final boolean export) {
View dview = LayoutInflater.from(this).inflate(R.layout.dialog_password, null);
final TextInputLayout etPassword1 = dview.findViewById(R.id.tilPassword1);
final TextInputLayout etPassword2 = dview.findViewById(R.id.tilPassword2);
TextView tvImportHint = dview.findViewById(R.id.tvImporthint);
etPassword2.setVisibility(export ? View.VISIBLE : View.GONE);
tvImportHint.setVisibility(export ? View.GONE : View.VISIBLE);
new DialogBuilderLifecycle(this, this)
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String password1 = etPassword1.getEditText().getText().toString();
String password2 = etPassword2.getEditText().getText().toString();
if (!BuildConfig.DEBUG && TextUtils.isEmpty(password1))
Snackbar.make(view, R.string.title_setup_password_missing, Snackbar.LENGTH_LONG).show();
else {
if (!export || password1.equals(password2)) {
ActivitySetup.this.password = password1;
startActivityForResult(
Helper.getChooser(
ActivitySetup.this,
export ? getIntentExport() : getIntentImport()),
export ? REQUEST_EXPORT : REQUEST_IMPORT);
} else
Snackbar.make(view, R.string.title_setup_password_different, Snackbar.LENGTH_LONG).show();
}
}
})
.show();
}
private void onMenuTheme(int id) { private void onMenuTheme(int id) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
switch (id) { switch (id) {
@ -403,8 +415,8 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE); intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*"); intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, "fairemail_backup_" + intent.putExtra(Intent.EXTRA_TITLE, "fairemail_" +
new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".json"); new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".backup");
return intent; return intent;
} }
@ -431,82 +443,93 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
throw new IllegalArgumentException(context.getString(R.string.title_no_stream)); throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
} }
OutputStream out = null; Log.i("Collecting data");
try { DB db = DB.getInstance(context);
Log.i("Writing URI=" + uri);
// Accounts
byte[] salt = new byte[16]; JSONArray jaccounts = new JSONArray();
SecureRandom random = new SecureRandom(); for (EntityAccount account : db.account().getAccounts()) {
random.nextBytes(salt); // Account
JSONObject jaccount = account.toJSON();
// https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); // Identities
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, KEY_ITERATIONS, KEY_LENGTH); JSONArray jidentities = new JSONArray();
SecretKey secret = keyFactory.generateSecret(keySpec); for (EntityIdentity identity : db.identity().getIdentities(account.id))
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); jidentities.put(identity.toJSON());
cipher.init(Cipher.ENCRYPT_MODE, secret); jaccount.put("identities", jidentities);
OutputStream raw = context.getContentResolver().openOutputStream(uri); // Folders
raw.write(salt); JSONArray jfolders = new JSONArray();
raw.write(cipher.getIV()); for (EntityFolder folder : db.folder().getFolders(account.id)) {
out = new CipherOutputStream(raw, cipher); JSONObject jfolder = folder.toJSON();
JSONArray jrules = new JSONArray();
DB db = DB.getInstance(context); for (EntityRule rule : db.rule().getRules(folder.id))
jrules.put(rule.toJSON());
// Accounts jfolder.put("rules", jrules);
JSONArray jaccounts = new JSONArray(); jfolders.put(jfolder);
for (EntityAccount account : db.account().getAccounts()) {
// Account
JSONObject jaccount = account.toJSON();
// Identities
JSONArray jidentities = new JSONArray();
for (EntityIdentity identity : db.identity().getIdentities(account.id))
jidentities.put(identity.toJSON());
jaccount.put("identities", jidentities);
// Folders
JSONArray jfolders = new JSONArray();
for (EntityFolder folder : db.folder().getFolders(account.id)) {
JSONObject jfolder = folder.toJSON();
JSONArray jrules = new JSONArray();
for (EntityRule rule : db.rule().getRules(folder.id))
jrules.put(rule.toJSON());
jfolder.put("rules", jrules);
jfolders.put(jfolder);
}
jaccount.put("folders", jfolders);
jaccounts.put(jaccount);
} }
jaccount.put("folders", jfolders);
// Answers jaccounts.put(jaccount);
JSONArray janswers = new JSONArray(); }
for (EntityAnswer answer : db.answer().getAnswers())
janswers.put(answer.toJSON());
// Settings // Answers
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); JSONArray janswers = new JSONArray();
JSONArray jsettings = new JSONArray(); for (EntityAnswer answer : db.answer().getAnswers())
for (String key : prefs.getAll().keySet()) janswers.put(answer.toJSON());
if (!"pro".equals(key)) {
JSONObject jsetting = new JSONObject(); // Settings
jsetting.put("key", key); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
jsetting.put("value", prefs.getAll().get(key)); JSONArray jsettings = new JSONArray();
jsettings.put(jsetting); for (String key : prefs.getAll().keySet())
} if (!"pro".equals(key)) {
JSONObject jsetting = new JSONObject();
jsetting.put("key", key);
jsetting.put("value", prefs.getAll().get(key));
jsettings.put(jsetting);
}
JSONObject jexport = new JSONObject();
jexport.put("accounts", jaccounts);
jexport.put("answers", janswers);
jexport.put("settings", jsettings);
JSONObject jexport = new JSONObject(); ContentResolver resolver = context.getContentResolver();
jexport.put("accounts", jaccounts); DocumentFile file = DocumentFile.fromSingleUri(context, uri);
jexport.put("answers", janswers); OutputStream raw = null;
jexport.put("settings", jsettings); try {
raw = new BufferedOutputStream(resolver.openOutputStream(uri));
Log.i("Writing URI=" + uri + " name=" + file.getName() + " virtual=" + file.isVirtual());
if (TextUtils.isEmpty(password))
raw.write(jexport.toString(2).getBytes());
else {
byte[] salt = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(salt);
// https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, KEY_ITERATIONS, KEY_LENGTH);
SecretKey secret = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
raw.write(salt);
raw.write(cipher.getIV());
OutputStream cout = new CipherOutputStream(raw, cipher);
cout.write(jexport.toString(2).getBytes());
cout.flush();
cout.close();
}
out.write(jexport.toString(2).getBytes()); raw.flush();
Log.i("Exported data"); Log.i("Exported data");
} finally { } finally {
if (out != null) if (raw != null)
out.close(); raw.close();
} }
return null; return null;
@ -543,125 +566,130 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
throw new IllegalArgumentException(context.getString(R.string.title_no_stream)); throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
} }
InputStream in = null; InputStream raw = null;
StringBuilder data = new StringBuilder();
try { try {
Log.i("Reading URI=" + uri); Log.i("Reading URI=" + uri);
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null); AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null);
InputStream raw = descriptor.createInputStream(); raw = new BufferedInputStream(descriptor.createInputStream());
byte[] salt = new byte[16]; InputStream in;
byte[] prefix = new byte[16]; if (TextUtils.isEmpty(password))
if (raw.read(salt) != salt.length) in = raw;
throw new IOException("length"); else {
if (raw.read(prefix) != prefix.length) byte[] salt = new byte[16];
throw new IOException("length"); byte[] prefix = new byte[16];
if (raw.read(salt) != salt.length)
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); throw new IOException("length");
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, KEY_ITERATIONS, KEY_LENGTH); if (raw.read(prefix) != prefix.length)
SecretKey secret = keyFactory.generateSecret(keySpec); throw new IOException("length");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(prefix); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
cipher.init(Cipher.DECRYPT_MODE, secret, iv); KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, KEY_ITERATIONS, KEY_LENGTH);
SecretKey secret = keyFactory.generateSecret(keySpec);
in = new CipherInputStream(raw, cipher); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(prefix);
cipher.init(Cipher.DECRYPT_MODE, secret, iv);
in = new CipherInputStream(raw, cipher);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line; String line;
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
response.append(line); data.append(line);
Log.i("Importing " + resolver.toString()); } finally {
if (raw != null)
JSONObject jimport = new JSONObject(response.toString()); raw.close();
}
DB db = DB.getInstance(context);
try {
db.beginTransaction();
JSONArray jaccounts = jimport.getJSONArray("accounts");
for (int a = 0; a < jaccounts.length(); a++) {
JSONObject jaccount = (JSONObject) jaccounts.get(a);
EntityAccount account = EntityAccount.fromJSON(jaccount);
account.created = new Date().getTime();
account.id = db.account().insertAccount(account);
Log.i("Imported account=" + account.name);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
if (account.notify)
account.createNotificationChannel(context);
JSONArray jidentities = (JSONArray) jaccount.get("identities");
for (int i = 0; i < jidentities.length(); i++) {
JSONObject jidentity = (JSONObject) jidentities.get(i);
EntityIdentity identity = EntityIdentity.fromJSON(jidentity);
identity.account = account.id;
identity.id = db.identity().insertIdentity(identity);
Log.i("Imported identity=" + identity.email);
}
JSONArray jfolders = (JSONArray) jaccount.get("folders"); Log.i("Importing data");
for (int f = 0; f < jfolders.length(); f++) { JSONObject jimport = new JSONObject(data.toString());
JSONObject jfolder = (JSONObject) jfolders.get(f);
EntityFolder folder = EntityFolder.fromJSON(jfolder);
folder.account = account.id;
folder.id = db.folder().insertFolder(folder);
if (jfolder.has("rules")) {
JSONArray jrules = jfolder.getJSONArray("rules");
for (int r = 0; r < jrules.length(); r++) {
JSONObject jrule = (JSONObject) jrules.get(r);
EntityRule rule = EntityRule.fromJSON(jrule);
rule.folder = folder.id;
db.rule().insertRule(rule);
}
}
Log.i("Imported folder=" + folder.name);
}
}
JSONArray janswers = jimport.getJSONArray("answers"); DB db = DB.getInstance(context);
for (int a = 0; a < janswers.length(); a++) { try {
JSONObject janswer = (JSONObject) janswers.get(a); db.beginTransaction();
EntityAnswer answer = EntityAnswer.fromJSON(janswer);
answer.id = db.answer().insertAnswer(answer); JSONArray jaccounts = jimport.getJSONArray("accounts");
Log.i("Imported answer=" + answer.name); for (int a = 0; a < jaccounts.length(); a++) {
JSONObject jaccount = (JSONObject) jaccounts.get(a);
EntityAccount account = EntityAccount.fromJSON(jaccount);
account.created = new Date().getTime();
account.id = db.account().insertAccount(account);
Log.i("Imported account=" + account.name);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
if (account.notify)
account.createNotificationChannel(context);
JSONArray jidentities = (JSONArray) jaccount.get("identities");
for (int i = 0; i < jidentities.length(); i++) {
JSONObject jidentity = (JSONObject) jidentities.get(i);
EntityIdentity identity = EntityIdentity.fromJSON(jidentity);
identity.account = account.id;
identity.id = db.identity().insertIdentity(identity);
Log.i("Imported identity=" + identity.email);
} }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); JSONArray jfolders = (JSONArray) jaccount.get("folders");
SharedPreferences.Editor editor = prefs.edit(); for (int f = 0; f < jfolders.length(); f++) {
JSONArray jsettings = jimport.getJSONArray("settings"); JSONObject jfolder = (JSONObject) jfolders.get(f);
for (int s = 0; s < jsettings.length(); s++) { EntityFolder folder = EntityFolder.fromJSON(jfolder);
JSONObject jsetting = (JSONObject) jsettings.get(s); folder.account = account.id;
String key = jsetting.getString("key"); folder.id = db.folder().insertFolder(folder);
if (!"pro".equals(key)) { if (jfolder.has("rules")) {
Object value = jsetting.get("value"); JSONArray jrules = jfolder.getJSONArray("rules");
if (value instanceof Boolean) for (int r = 0; r < jrules.length(); r++) {
editor.putBoolean(key, (Boolean) value); JSONObject jrule = (JSONObject) jrules.get(r);
else if (value instanceof Integer) EntityRule rule = EntityRule.fromJSON(jrule);
editor.putInt(key, (Integer) value); rule.folder = folder.id;
else if (value instanceof Long) db.rule().insertRule(rule);
editor.putLong(key, (Long) value); }
else if (value instanceof String)
editor.putString(key, (String) value);
else
throw new IllegalArgumentException("Unknown settings type key=" + key);
Log.i("Imported setting=" + key);
} }
Log.i("Imported folder=" + folder.name);
} }
editor.apply(); }
db.setTransactionSuccessful(); JSONArray janswers = jimport.getJSONArray("answers");
} finally { for (int a = 0; a < janswers.length(); a++) {
db.endTransaction(); JSONObject janswer = (JSONObject) janswers.get(a);
EntityAnswer answer = EntityAnswer.fromJSON(janswer);
answer.id = db.answer().insertAnswer(answer);
Log.i("Imported answer=" + answer.name);
} }
Log.i("Imported data"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
ServiceSynchronize.reload(context, "import"); SharedPreferences.Editor editor = prefs.edit();
JSONArray jsettings = jimport.getJSONArray("settings");
for (int s = 0; s < jsettings.length(); s++) {
JSONObject jsetting = (JSONObject) jsettings.get(s);
String key = jsetting.getString("key");
if (!"pro".equals(key)) {
Object value = jsetting.get("value");
if (value instanceof Boolean)
editor.putBoolean(key, (Boolean) value);
else if (value instanceof Integer)
editor.putInt(key, (Integer) value);
else if (value instanceof Long)
editor.putLong(key, (Long) value);
else if (value instanceof String)
editor.putString(key, (String) value);
else
throw new IllegalArgumentException("Unknown settings type key=" + key);
Log.i("Imported setting=" + key);
}
}
editor.apply();
db.setTransactionSuccessful();
} finally { } finally {
if (in != null) db.endTransaction();
in.close();
} }
Log.i("Imported data");
ServiceSynchronize.reload(context, "import");
return null; return null;
} }

@ -39,8 +39,8 @@
android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<TextView <TextView
android:id="@+id/tvImporthint"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="12dp" android:layout_marginTop="12dp"

Loading…
Cancel
Save