FairEmail/app/src/main/java/eu/faircode/email/ActivitySetup.java

811 lines
34 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2018-08-04 15:46:22 +00:00
import android.content.BroadcastReceiver;
2019-01-14 10:29:47 +00:00
import android.content.ContentResolver;
2018-08-04 15:46:22 +00:00
import android.content.Context;
2019-01-14 10:29:47 +00:00
import android.content.DialogInterface;
2018-08-04 15:46:22 +00:00
import android.content.Intent;
import android.content.IntentFilter;
2019-01-14 10:29:47 +00:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.Configuration;
import android.net.Uri;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2019-01-14 10:29:47 +00:00
import android.preference.PreferenceManager;
2019-01-14 12:01:33 +00:00
import android.provider.Settings;
2019-01-14 10:29:47 +00:00
import android.text.TextUtils;
import android.view.LayoutInflater;
2018-08-04 16:54:57 +00:00
import android.view.MenuItem;
2019-01-14 10:29:47 +00:00
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
2019-01-27 09:32:35 +00:00
import android.widget.TextView;
2018-08-02 13:33:06 +00:00
2019-01-14 10:29:47 +00:00
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputLayout;
import org.json.JSONArray;
import org.json.JSONObject;
2019-01-27 09:32:35 +00:00
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
2019-01-14 10:29:47 +00:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.text.SimpleDateFormat;
2019-03-01 09:07:15 +00:00
import java.util.ArrayList;
2019-01-14 10:29:47 +00:00
import java.util.Date;
2018-08-14 08:31:43 +00:00
import java.util.List;
2019-02-26 10:05:21 +00:00
import java.util.Objects;
2018-08-14 08:31:43 +00:00
2019-01-14 10:29:47 +00:00
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import androidx.appcompat.app.ActionBarDrawerToggle;
2019-01-27 09:32:35 +00:00
import androidx.documentfile.provider.DocumentFile;
2019-01-14 10:29:47 +00:00
import androidx.drawerlayout.widget.DrawerLayout;
2018-08-08 06:55:47 +00:00
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
2018-08-17 05:34:46 +00:00
import androidx.lifecycle.Lifecycle;
2018-08-14 08:31:43 +00:00
import androidx.lifecycle.Observer;
2018-08-08 06:55:47 +00:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2018-09-07 08:44:48 +00:00
public class ActivitySetup extends ActivityBilling implements FragmentManager.OnBackStackChangedListener {
2019-01-14 10:29:47 +00:00
private View view;
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
private boolean hasAccount;
2019-01-27 09:32:35 +00:00
private String password;
2018-08-14 08:31:43 +00:00
2019-01-14 10:29:47 +00:00
private static final int KEY_ITERATIONS = 65536;
private static final int KEY_LENGTH = 256;
2018-08-27 14:31:45 +00:00
static final int REQUEST_PERMISSION = 1;
static final int REQUEST_CHOOSE_ACCOUNT = 2;
static final int REQUEST_SOUND = 3;
2018-09-14 11:33:22 +00:00
static final int REQUEST_EXPORT = 4;
static final int REQUEST_IMPORT = 5;
static final int REQUEST_ERROR = 6;
2018-11-19 13:14:02 +00:00
2018-08-04 15:46:22 +00:00
static final String ACTION_EDIT_ACCOUNT = BuildConfig.APPLICATION_ID + ".EDIT_ACCOUNT";
static final String ACTION_EDIT_IDENTITY = BuildConfig.APPLICATION_ID + ".EDIT_IDENTITY";
2018-08-02 13:33:06 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2019-01-14 10:29:47 +00:00
view = LayoutInflater.from(this).inflate(R.layout.activity_setup, null);
setContentView(view);
2018-08-02 13:33:06 +00:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2018-08-04 16:54:57 +00:00
2019-01-14 10:29:47 +00:00
drawerLayout = findViewById(R.id.drawer_layout);
drawerLayout.setScrimColor(Helper.resolveColor(this, R.attr.colorDrawerScrim));
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(getString(R.string.app_name));
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(getString(R.string.app_name));
}
};
drawerLayout.addDrawerListener(drawerToggle);
drawerList = findViewById(R.id.drawer_list);
2019-02-26 17:44:15 +00:00
final DrawerAdapter drawerArray = new DrawerAdapter(this);
drawerList.setAdapter(drawerArray);
2019-01-14 10:29:47 +00:00
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2019-02-26 17:44:15 +00:00
DrawerItem item = drawerArray.getItem(position);
2019-03-01 09:07:15 +00:00
if (item == null)
return;
2019-02-26 17:44:15 +00:00
switch (item.getMenuId()) {
2019-01-14 10:29:47 +00:00
case R.string.title_setup_export:
onMenuExport();
break;
case R.string.title_setup_import:
onMenuImport();
break;
2019-01-14 12:01:33 +00:00
case R.string.title_setup_light_theme:
case R.string.title_setup_dark_theme:
case R.string.title_setup_black_theme:
case R.string.title_setup_system_theme:
2019-02-26 17:44:15 +00:00
onMenuTheme(item.getMenuId());
2019-01-14 12:01:33 +00:00
break;
2019-01-14 13:20:47 +00:00
case R.string.title_setup_notifications:
onManageNotifications();
break;
2019-01-14 12:01:33 +00:00
case R.string.title_setup_advanced:
onMenuOptions();
break;
2019-01-14 10:29:47 +00:00
case R.string.menu_legend:
onMenuLegend();
break;
case R.string.menu_faq:
onMenuFAQ();
break;
case R.string.menu_privacy:
onMenuPrivacy();
break;
case R.string.menu_about:
onMenuAbout();
break;
}
drawerLayout.closeDrawer(drawerList);
}
});
2019-02-17 16:45:19 +00:00
drawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
2019-02-26 17:44:15 +00:00
DrawerItem item = drawerArray.getItem(position);
2019-03-01 09:07:15 +00:00
if (item == null)
return false;
2019-02-26 17:44:15 +00:00
if (item.getMenuId() == R.string.menu_privacy) {
2019-02-17 16:45:19 +00:00
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
int count = DB.getInstance(context).contact().clearContacts();
Log.i("Cleared contacts=" + count);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(ActivitySetup.this, ActivitySetup.this, ex);
}
}.execute(ActivitySetup.this, new Bundle(), "setup:privacy");
return true;
}
return false;
}
});
2019-03-01 09:07:15 +00:00
List<DrawerItem> items = new ArrayList<>();
2019-01-14 10:29:47 +00:00
PackageManager pm = getPackageManager();
if (getIntentExport().resolveActivity(pm) != null)
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-1, R.string.title_setup_export, R.drawable.baseline_archive_24));
2019-01-14 10:29:47 +00:00
if (getIntentImport().resolveActivity(pm) != null)
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-2, R.string.title_setup_import, R.drawable.baseline_unarchive_24));
2019-01-14 10:29:47 +00:00
2019-01-14 12:01:33 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String theme = prefs.getString("theme", "system");
if ("light".equals(theme))
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-3, R.string.title_setup_dark_theme, R.drawable.baseline_palette_24));
else if ("dark".equals(theme))
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-4, R.string.title_setup_black_theme, R.drawable.baseline_palette_24));
2019-01-14 12:01:33 +00:00
else if ("black".equals(theme))
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-5, R.string.title_setup_system_theme, R.drawable.baseline_palette_24));
2019-01-14 12:01:33 +00:00
else
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-6, R.string.title_setup_light_theme, R.drawable.baseline_palette_24));
2019-01-14 12:01:33 +00:00
2019-01-14 13:20:47 +00:00
if (getIntentNotifications(this).resolveActivity(pm) != null)
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-7, R.string.title_setup_notifications, R.drawable.baseline_notifications_24));
2019-01-14 13:20:47 +00:00
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-8, R.string.title_setup_advanced, R.drawable.baseline_settings_applications_24));
2019-01-14 12:01:33 +00:00
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-9));
2019-01-14 10:29:47 +00:00
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-10, R.string.menu_legend, R.drawable.baseline_help_24));
2019-01-14 10:29:47 +00:00
if (Helper.getIntentFAQ().resolveActivity(getPackageManager()) != null)
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-11, R.string.menu_faq, R.drawable.baseline_question_answer_24));
2019-01-14 10:29:47 +00:00
if (Helper.getIntentPrivacy().resolveActivity(getPackageManager()) != null)
2019-03-01 09:07:15 +00:00
items.add(new DrawerItem(-12, R.string.menu_privacy, R.drawable.baseline_account_box_24));
items.add(new DrawerItem(-13, R.string.menu_about, R.drawable.baseline_info_24));
drawerArray.set(items);
2019-01-14 10:29:47 +00:00
2018-08-02 13:33:06 +00:00
getSupportFragmentManager().addOnBackStackChangedListener(this);
if (getSupportFragmentManager().getFragments().size() == 0) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentSetup()).addToBackStack("setup");
fragmentTransaction.commit();
}
2018-08-14 08:31:43 +00:00
2019-01-14 10:29:47 +00:00
if (savedInstanceState != null)
drawerToggle.setDrawerIndicatorEnabled(savedInstanceState.getBoolean("toggle"));
2019-02-17 18:29:00 +00:00
DB.getInstance(this).account().liveSynchronizingAccounts().observe(this, new Observer<List<EntityAccount>>() {
2018-08-14 08:31:43 +00:00
@Override
public void onChanged(List<EntityAccount> accounts) {
2018-08-14 08:31:43 +00:00
hasAccount = (accounts != null && accounts.size() > 0);
}
});
2018-08-02 13:33:06 +00:00
}
2019-01-14 10:29:47 +00:00
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("toggle", drawerToggle.isDrawerIndicatorEnabled());
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
2018-08-04 15:46:22 +00:00
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
IntentFilter iff = new IntentFilter();
iff.addAction(ACTION_EDIT_ACCOUNT);
iff.addAction(ACTION_EDIT_IDENTITY);
lbm.registerReceiver(receiver, iff);
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.unregisterReceiver(receiver);
}
2019-01-14 10:29:47 +00:00
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(drawerList))
drawerLayout.closeDrawer(drawerList);
else
super.onBackPressed();
}
@Override
public void onBackStackChanged() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
if (hasAccount)
startActivity(new Intent(this, ActivityView.class));
finish();
} else {
if (drawerLayout.isDrawerOpen(drawerList))
drawerLayout.closeDrawer(drawerList);
drawerToggle.setDrawerIndicatorEnabled(count == 1);
}
}
2018-08-04 16:54:57 +00:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2019-01-14 10:29:47 +00:00
if (drawerToggle.onOptionsItemSelected(item))
return true;
2018-08-04 16:54:57 +00:00
switch (item.getItemId()) {
case android.R.id.home:
2018-08-17 05:34:46 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
getSupportFragmentManager().popBackStack();
2018-08-04 16:54:57 +00:00
return true;
}
return super.onOptionsItemSelected(item);
}
2018-08-02 13:33:06 +00:00
@Override
2019-01-14 11:11:03 +00:00
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
2019-01-27 09:32:35 +00:00
if (resultCode == RESULT_OK && data != null)
if (requestCode == REQUEST_EXPORT)
handleExport(data, this.password);
else if (requestCode == REQUEST_IMPORT)
handleImport(data, this.password);
2019-01-14 10:29:47 +00:00
}
2019-01-14 12:01:33 +00:00
private void onManageNotifications() {
startActivity(getIntentNotifications(this));
}
2019-01-14 10:29:47 +00:00
private void onMenuExport() {
if (Helper.isPro(this))
try {
2019-01-27 09:32:35 +00:00
askPassword(true);
2019-01-14 10:29:47 +00:00
} catch (Throwable ex) {
Helper.unexpectedError(this, this, ex);
}
else {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
}
private void onMenuImport() {
try {
2019-01-27 09:32:35 +00:00
askPassword(false);
2019-01-14 10:29:47 +00:00
} catch (Throwable ex) {
Helper.unexpectedError(this, this, ex);
}
}
2019-01-27 09:32:35 +00:00
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();
}
2019-01-14 12:01:33 +00:00
private void onMenuTheme(int id) {
2019-01-22 15:12:20 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
switch (id) {
case R.string.title_setup_light_theme:
prefs.edit().putString("theme", "light").apply();
break;
case R.string.title_setup_dark_theme:
prefs.edit().putString("theme", "dark").apply();
break;
case R.string.title_setup_black_theme:
prefs.edit().putString("theme", "black").apply();
break;
case R.string.title_setup_system_theme:
prefs.edit().putString("theme", "system").apply();
break;
2019-01-14 12:01:33 +00:00
}
}
private void onMenuOptions() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentOptions()).addToBackStack("options");
fragmentTransaction.commit();
}
2019-01-14 10:29:47 +00:00
private void onMenuLegend() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentLegend()).addToBackStack("legend");
fragmentTransaction.commit();
}
private void onMenuFAQ() {
Helper.view(this, this, Helper.getIntentFAQ());
}
private void onMenuPrivacy() {
Helper.view(this, this, Helper.getIntentPrivacy());
}
private void onMenuAbout() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentAbout()).addToBackStack("about");
fragmentTransaction.commit();
}
2019-01-14 12:01:33 +00:00
private static Intent getIntentNotifications(Context context) {
return new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.putExtra("app_package", context.getPackageName())
.putExtra("app_uid", context.getApplicationInfo().uid)
.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
}
2019-01-14 10:29:47 +00:00
private static Intent getIntentExport() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
2019-01-27 09:32:35 +00:00
intent.putExtra(Intent.EXTRA_TITLE, "fairemail_" +
new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".backup");
2019-01-14 10:29:47 +00:00
return intent;
}
private static Intent getIntentImport() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
return intent;
}
private void handleExport(Intent data, String password) {
Bundle args = new Bundle();
args.putParcelable("uri", data.getData());
args.putString("password", password);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
String password = args.getString("password");
if ("file".equals(uri.getScheme())) {
Log.w("Export uri=" + uri);
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
}
2019-01-27 09:32:35 +00:00
Log.i("Collecting data");
DB db = DB.getInstance(context);
// Accounts
JSONArray jaccounts = new JSONArray();
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);
2019-01-14 10:29:47 +00:00
}
2019-01-27 09:32:35 +00:00
jaccount.put("folders", jfolders);
2019-01-14 10:29:47 +00:00
2019-01-27 09:32:35 +00:00
jaccounts.put(jaccount);
}
2019-01-14 10:29:47 +00:00
2019-02-13 09:24:06 +00:00
// Contacts
JSONArray jcontacts = new JSONArray();
for (EntityContact contact : db.contact().getContacts())
jcontacts.put(contact.toJSON());
2019-01-27 09:32:35 +00:00
// Answers
JSONArray janswers = new JSONArray();
for (EntityAnswer answer : db.answer().getAnswers())
janswers.put(answer.toJSON());
// Settings
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
JSONArray jsettings = new JSONArray();
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);
2019-02-13 09:24:06 +00:00
jexport.put("contacts", jcontacts);
2019-01-27 09:32:35 +00:00
jexport.put("answers", janswers);
jexport.put("settings", jsettings);
2019-01-14 10:29:47 +00:00
2019-01-27 09:32:35 +00:00
ContentResolver resolver = context.getContentResolver();
DocumentFile file = DocumentFile.fromSingleUri(context, uri);
2019-02-22 15:59:23 +00:00
try (OutputStream raw = new BufferedOutputStream(resolver.openOutputStream(uri))) {
2019-01-27 09:32:35 +00:00
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();
}
2019-01-14 10:29:47 +00:00
2019-01-27 09:32:35 +00:00
raw.flush();
2019-01-14 10:29:47 +00:00
Log.i("Exported data");
}
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
Snackbar.make(view, R.string.title_setup_exported, Snackbar.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivitySetup.this, ActivitySetup.this, ex);
}
}.execute(this, args, "setup:export");
}
private void handleImport(Intent data, String password) {
Bundle args = new Bundle();
args.putParcelable("uri", data.getData());
args.putString("password", password);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
String password = args.getString("password");
if ("file".equals(uri.getScheme())) {
Log.w("Import uri=" + uri);
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
}
2019-01-27 09:32:35 +00:00
StringBuilder data = new StringBuilder();
2019-02-22 15:59:23 +00:00
Log.i("Reading URI=" + uri);
ContentResolver resolver = context.getContentResolver();
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null);
try (InputStream raw = new BufferedInputStream(descriptor.createInputStream())) {
2019-01-27 09:32:35 +00:00
InputStream in;
if (TextUtils.isEmpty(password))
in = raw;
else {
byte[] salt = new byte[16];
byte[] prefix = new byte[16];
if (raw.read(salt) != salt.length)
throw new IOException("length");
if (raw.read(prefix) != prefix.length)
throw new IOException("length");
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");
IvParameterSpec iv = new IvParameterSpec(prefix);
cipher.init(Cipher.DECRYPT_MODE, secret, iv);
in = new CipherInputStream(raw, cipher);
}
2019-01-14 10:29:47 +00:00
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null)
2019-01-27 09:32:35 +00:00
data.append(line);
}
2019-01-14 10:29:47 +00:00
2019-01-27 09:32:35 +00:00
Log.i("Importing data");
JSONObject jimport = new JSONObject(data.toString());
2019-01-14 10:29:47 +00:00
2019-01-27 09:32:35 +00:00
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2019-02-13 09:24:06 +00:00
// Accounts
2019-01-27 09:32:35 +00:00
JSONArray jaccounts = jimport.getJSONArray("accounts");
for (int a = 0; a < jaccounts.length(); a++) {
JSONObject jaccount = (JSONObject) jaccounts.get(a);
EntityAccount account = EntityAccount.fromJSON(jaccount);
// Forward referenced
Long swipe_left = account.swipe_left;
Long swipe_right = account.swipe_right;
account.swipe_left = null;
account.swipe_right = null;
2019-01-27 09:32:35 +00:00
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);
2019-01-14 10:29:47 +00:00
}
2019-01-27 09:32:35 +00:00
JSONArray jfolders = (JSONArray) jaccount.get("folders");
for (int f = 0; f < jfolders.length(); f++) {
JSONObject jfolder = (JSONObject) jfolders.get(f);
EntityFolder folder = EntityFolder.fromJSON(jfolder);
long id = folder.id;
folder.id = null;
2019-01-27 09:32:35 +00:00
folder.account = account.id;
folder.id = db.folder().insertFolder(folder);
2019-02-26 10:05:21 +00:00
if (Objects.equals(swipe_left, id))
account.swipe_left = folder.id;
2019-02-26 10:05:21 +00:00
if (Objects.equals(swipe_right, id))
account.swipe_right = folder.id;
2019-01-27 09:32:35 +00:00
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);
}
2019-01-14 10:29:47 +00:00
}
2019-01-27 09:32:35 +00:00
Log.i("Imported folder=" + folder.name);
2019-01-14 10:29:47 +00:00
}
// Update swipe left/right
db.account().updateAccount(account);
2019-01-27 09:32:35 +00:00
}
2019-01-14 10:29:47 +00:00
2019-02-13 09:24:06 +00:00
// Contacts
JSONArray jcontacts = jimport.getJSONArray("contacts");
for (int c = 0; c < jcontacts.length(); c++) {
JSONObject jcontact = (JSONObject) jcontacts.get(c);
EntityContact contact = EntityContact.fromJSON(jcontact);
if (db.contact().getContacts(contact.type, contact.email).size() == 0) {
contact.id = db.contact().insertContact(contact);
Log.i("Imported contact=" + contact);
}
}
// Answers
2019-01-27 09:32:35 +00:00
JSONArray janswers = jimport.getJSONArray("answers");
for (int a = 0; a < janswers.length(); a++) {
JSONObject janswer = (JSONObject) janswers.get(a);
EntityAnswer answer = EntityAnswer.fromJSON(janswer);
answer.id = db.answer().insertAnswer(answer);
Log.i("Imported answer=" + answer.name);
2019-01-14 10:29:47 +00:00
}
2019-02-13 09:24:06 +00:00
// Settings
2019-01-27 09:32:35 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
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();
2019-01-14 10:29:47 +00:00
} finally {
2019-01-27 09:32:35 +00:00
db.endTransaction();
2019-01-14 10:29:47 +00:00
}
2019-01-27 09:32:35 +00:00
Log.i("Imported data");
ServiceSynchronize.reload(context, "import");
2019-01-14 10:29:47 +00:00
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
Snackbar.make(view, R.string.title_setup_imported, Snackbar.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex.getCause() instanceof BadPaddingException)
Snackbar.make(view, R.string.title_setup_password_invalid, Snackbar.LENGTH_LONG).show();
else if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivitySetup.this, ActivitySetup.this, ex);
}
}.execute(this, args, "setup:import");
}
2019-01-14 12:58:33 +00:00
private void onEditAccount(Intent intent) {
FragmentAccount fragment = new FragmentAccount();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("account");
fragmentTransaction.commit();
}
private void onEditIdentity(Intent intent) {
FragmentIdentity fragment = new FragmentIdentity();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("identity");
fragmentTransaction.commit();
}
2018-08-04 15:46:22 +00:00
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
2019-01-14 12:58:33 +00:00
if (ACTION_EDIT_ACCOUNT.equals(intent.getAction()))
onEditAccount(intent);
else if (ACTION_EDIT_IDENTITY.equals(intent.getAction()))
onEditIdentity(intent);
2018-08-04 15:46:22 +00:00
}
};
2018-08-02 13:33:06 +00:00
}