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

844 lines
35 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
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
2018-09-04 18:06:22 +00:00
import android.annotation.TargetApi;
2018-09-14 11:33:22 +00:00
import android.content.ContentResolver;
import android.content.Context;
2018-09-04 18:06:22 +00:00
import android.content.DialogInterface;
import android.content.Intent;
2018-08-04 15:37:42 +00:00
import android.content.SharedPreferences;
2018-08-02 13:33:06 +00:00
import android.content.pm.PackageManager;
2018-09-14 11:33:22 +00:00
import android.content.res.AssetFileDescriptor;
2018-09-07 12:34:54 +00:00
import android.graphics.drawable.Drawable;
2018-09-04 18:06:22 +00:00
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2018-09-04 18:06:22 +00:00
import android.os.PowerManager;
2018-08-04 15:37:42 +00:00
import android.preference.PreferenceManager;
2018-09-04 18:06:22 +00:00
import android.provider.Settings;
2018-11-14 19:07:06 +00:00
import android.text.TextUtils;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
2018-09-14 11:33:22 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
2018-11-03 10:53:37 +00:00
import android.widget.CheckBox;
2018-08-04 15:37:42 +00:00
import android.widget.CompoundButton;
2018-11-14 18:58:01 +00:00
import android.widget.EditText;
2018-09-19 06:38:29 +00:00
import android.widget.ImageButton;
2018-08-02 13:33:06 +00:00
import android.widget.TextView;
2018-09-07 12:34:54 +00:00
import android.widget.ToggleButton;
2018-08-02 13:33:06 +00:00
2018-09-14 11:33:22 +00:00
import com.google.android.material.snackbar.Snackbar;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
2018-12-09 14:49:43 +00:00
import java.io.IOException;
2018-09-14 11:33:22 +00:00
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
2018-11-14 18:58:01 +00:00
import java.security.SecureRandom;
import java.security.spec.KeySpec;
2018-09-14 11:33:22 +00:00
import java.text.SimpleDateFormat;
import java.util.Date;
2018-08-02 13:33:06 +00:00
import java.util.List;
2018-11-17 08:07:24 +00:00
import javax.crypto.BadPaddingException;
2018-11-14 18:58:01 +00:00
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;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.LiveData;
2018-08-08 06:55:47 +00:00
import androidx.lifecycle.Observer;
2018-09-14 11:33:22 +00:00
import static android.app.Activity.RESULT_OK;
public class FragmentSetup extends FragmentEx {
2018-09-14 11:33:22 +00:00
private ViewGroup view;
2018-09-19 06:38:29 +00:00
private ImageButton ibHelp;
2018-08-02 13:33:06 +00:00
private Button btnAccount;
private TextView tvAccountDone;
private TextView tvNoPrimaryDrafts;
private TextView tvNoPrimaryArchive;
2018-08-04 15:13:19 +00:00
private Button btnIdentity;
2018-08-02 13:33:06 +00:00
private TextView tvIdentityDone;
2018-08-04 15:13:19 +00:00
private Button btnPermissions;
2018-08-02 13:33:06 +00:00
private TextView tvPermissionsDone;
2018-09-04 18:06:22 +00:00
private Button btnDoze;
private TextView tvDozeDone;
private Button btnData;
2018-10-29 10:43:45 +00:00
private Button btnNotifications;
2018-09-07 12:34:54 +00:00
private ToggleButton tbDarkTheme;
2018-11-03 10:53:37 +00:00
private CheckBox cbBlackTheme;
private Button btnOptions;
2018-08-05 18:46:36 +00:00
2018-09-07 12:34:54 +00:00
private Drawable check;
2018-11-14 18:58:01 +00:00
private static final int KEY_ITERATIONS = 65536;
private static final int KEY_LENGTH = 256;
2018-08-02 13:33:06 +00:00
private static final String[] permissions = new String[]{
Manifest.permission.READ_CONTACTS
};
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_setup);
2018-09-14 11:33:22 +00:00
setHasOptionsMenu(true);
2018-09-07 12:34:54 +00:00
check = getResources().getDrawable(R.drawable.baseline_check_24, getContext().getTheme());
2018-09-14 11:33:22 +00:00
view = (ViewGroup) inflater.inflate(R.layout.fragment_setup, container, false);
2018-08-02 13:33:06 +00:00
// Get controls
2018-09-19 06:38:29 +00:00
ibHelp = view.findViewById(R.id.ibHelp);
2018-08-02 13:33:06 +00:00
btnAccount = view.findViewById(R.id.btnAccount);
tvAccountDone = view.findViewById(R.id.tvAccountDone);
tvNoPrimaryDrafts = view.findViewById(R.id.tvNoPrimaryDrafts);
tvNoPrimaryArchive = view.findViewById(R.id.tvNoPrimaryArchive);
2018-08-04 15:13:19 +00:00
btnIdentity = view.findViewById(R.id.btnIdentity);
2018-08-02 13:33:06 +00:00
tvIdentityDone = view.findViewById(R.id.tvIdentityDone);
2018-08-04 15:13:19 +00:00
btnPermissions = view.findViewById(R.id.btnPermissions);
2018-08-02 13:33:06 +00:00
tvPermissionsDone = view.findViewById(R.id.tvPermissionsDone);
2018-09-04 18:06:22 +00:00
btnDoze = view.findViewById(R.id.btnDoze);
tvDozeDone = view.findViewById(R.id.tvDozeDone);
2018-10-29 10:43:45 +00:00
btnNotifications = view.findViewById(R.id.btnNotifications);
2018-09-04 18:06:22 +00:00
btnData = view.findViewById(R.id.btnData);
2018-09-07 12:34:54 +00:00
tbDarkTheme = view.findViewById(R.id.tbDarkTheme);
2018-11-03 10:53:37 +00:00
cbBlackTheme = view.findViewById(R.id.cbBlackTheme);
btnOptions = view.findViewById(R.id.btnOptions);
2018-08-05 18:46:36 +00:00
2018-08-02 13:33:06 +00:00
// Wire controls
2018-09-19 06:38:29 +00:00
ibHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(getIntentHelp());
}
});
2018-08-02 13:33:06 +00:00
btnAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2018-08-06 12:29:14 +00:00
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentAccounts()).addToBackStack("accounts");
fragmentTransaction.commit();
2018-08-02 13:33:06 +00:00
}
});
btnIdentity.setOnClickListener(new View.OnClickListener() {
2018-08-05 18:46:36 +00:00
@Override
public void onClick(View view) {
2018-08-04 15:13:19 +00:00
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
2018-08-06 12:29:14 +00:00
fragmentTransaction.replace(R.id.content_frame, new FragmentIdentities()).addToBackStack("identities");
2018-08-04 15:13:19 +00:00
fragmentTransaction.commit();
}
});
2018-08-06 12:29:14 +00:00
btnPermissions.setOnClickListener(new View.OnClickListener() {
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View view) {
2018-08-10 16:06:39 +00:00
btnPermissions.setEnabled(false);
2018-08-06 12:29:14 +00:00
requestPermissions(permissions, 1);
2018-08-02 13:33:06 +00:00
}
});
2018-09-04 18:06:22 +00:00
btnDoze.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2018-09-20 16:47:28 +00:00
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
2018-09-04 18:06:22 +00:00
.setMessage(R.string.title_setup_doze_instructions)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-09-04 18:06:22 +00:00
}
}
})
.create()
.show();
}
});
btnData.setOnClickListener(new View.OnClickListener() {
@Override
@TargetApi(Build.VERSION_CODES.N)
public void onClick(View v) {
try {
startActivity(new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS,
Uri.parse("package:" + BuildConfig.APPLICATION_ID)));
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-09-04 18:06:22 +00:00
}
}
});
2018-10-29 10:43:45 +00:00
PackageManager pm = getContext().getPackageManager();
btnNotifications.setVisibility(getIntentNotifications(getContext()).resolveActivity(pm) == null ? View.GONE : View.VISIBLE);
btnNotifications.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(getIntentNotifications(getContext()));
}
});
2018-08-04 15:37:42 +00:00
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2018-08-06 12:29:14 +00:00
String theme = prefs.getString("theme", "light");
2018-11-03 10:53:37 +00:00
boolean light = "light".equals(theme);
tbDarkTheme.setTag(!light);
tbDarkTheme.setChecked(!light);
2018-09-07 12:34:54 +00:00
tbDarkTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2018-08-04 15:37:42 +00:00
@Override
public void onCheckedChanged(CompoundButton button, boolean checked) {
2018-09-17 06:15:54 +00:00
if (Helper.isPro(getContext())) {
2018-09-16 06:27:31 +00:00
if (checked != (Boolean) button.getTag()) {
button.setTag(checked);
tbDarkTheme.setChecked(checked);
prefs.edit().putString("theme", checked ? "dark" : "light").apply();
}
} else {
2018-10-17 18:41:43 +00:00
prefs.edit().remove("theme").apply();
2018-09-18 18:15:14 +00:00
if (checked) {
tbDarkTheme.setChecked(false);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
2018-08-04 15:37:42 +00:00
}
2018-11-03 10:53:37 +00:00
cbBlackTheme.setVisibility(tbDarkTheme.isChecked() ? View.VISIBLE : View.GONE);
2018-08-04 15:37:42 +00:00
}
});
2018-11-03 10:53:37 +00:00
cbBlackTheme.setChecked("black".equals(theme));
cbBlackTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
prefs.edit().putString("theme", checked ? "black" : "dark").apply();
}
});
cbBlackTheme.setVisibility(tbDarkTheme.isChecked() ? View.VISIBLE : View.GONE);
btnOptions.setOnClickListener(new View.OnClickListener() {
2018-08-06 12:29:14 +00:00
@Override
public void onClick(View view) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentOptions()).addToBackStack("options");
fragmentTransaction.commit();
2018-08-06 12:29:14 +00:00
}
});
2018-08-02 13:33:06 +00:00
// Initialize
2018-09-19 06:38:29 +00:00
ibHelp.setVisibility(View.GONE);
2018-08-02 13:33:06 +00:00
2018-09-04 18:06:22 +00:00
tvAccountDone.setText(null);
2018-09-07 12:34:54 +00:00
tvAccountDone.setCompoundDrawables(null, null, null, null);
tvNoPrimaryDrafts.setVisibility(View.GONE);
tvNoPrimaryArchive.setVisibility(View.GONE);
2018-09-07 12:34:54 +00:00
btnIdentity.setEnabled(false);
2018-09-04 18:06:22 +00:00
tvIdentityDone.setText(null);
2018-09-07 12:34:54 +00:00
tvIdentityDone.setCompoundDrawables(null, null, null, null);
2018-09-04 18:06:22 +00:00
tvPermissionsDone.setText(null);
2018-09-07 12:34:54 +00:00
tvPermissionsDone.setCompoundDrawables(null, null, null, null);
2018-09-04 18:06:22 +00:00
btnDoze.setEnabled(false);
tvDozeDone.setText(null);
2018-09-07 12:34:54 +00:00
tvDozeDone.setCompoundDrawables(null, null, null, null);
2018-09-04 18:06:22 +00:00
btnData.setVisibility(View.GONE);
2018-08-02 13:33:06 +00:00
int[] grantResults = new int[permissions.length];
for (int i = 0; i < permissions.length; i++)
grantResults[i] = ContextCompat.checkSelfPermission(getActivity(), permissions[i]);
checkPermissions(permissions, grantResults, true);
2018-08-02 13:33:06 +00:00
2018-08-06 12:29:14 +00:00
// Create outbox
new SimpleTask<Void>() {
@Override
2018-08-12 16:14:20 +00:00
protected Void onLoad(Context context, Bundle args) {
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = new EntityFolder();
outbox.name = "OUTBOX";
outbox.type = EntityFolder.OUTBOX;
2018-12-04 08:01:08 +00:00
outbox.level = 0;
outbox.synchronize = false;
2018-11-14 09:49:59 +00:00
outbox.sync_days = 0;
outbox.keep_days = 0;
outbox.id = db.folder().insertFolder(outbox);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
}
}.load(this, new Bundle());
2018-08-02 13:33:06 +00:00
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
2018-09-19 06:38:29 +00:00
PackageManager pm = getContext().getPackageManager();
ibHelp.setVisibility(getIntentHelp().resolveActivity(pm) == null ? View.GONE : View.VISIBLE);
final DB db = DB.getInstance(getContext());
db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
private boolean done = false;
private LiveData<EntityFolder> livePrimaryDrafts = null;
private LiveData<EntityFolder> livePrimaryArchive = null;
@Override
public void onChanged(@Nullable List<EntityAccount> accounts) {
done = (accounts != null && accounts.size() > 0);
2018-09-07 12:34:54 +00:00
btnIdentity.setEnabled(done);
tvAccountDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do);
tvAccountDone.setCompoundDrawablesWithIntrinsicBounds(done ? check : null, null, null, null);
if (livePrimaryDrafts == null)
livePrimaryDrafts = db.folder().livePrimaryDrafts();
else
livePrimaryDrafts.removeObservers(getViewLifecycleOwner());
if (livePrimaryArchive == null)
2018-12-16 07:40:41 +00:00
livePrimaryArchive = db.folder().livePrimaryArchive();
else
livePrimaryArchive.removeObservers(getViewLifecycleOwner());
livePrimaryDrafts.observe(getViewLifecycleOwner(), new Observer<EntityFolder>() {
@Override
public void onChanged(EntityFolder drafts) {
tvNoPrimaryDrafts.setVisibility(done && drafts == null ? View.VISIBLE : View.GONE);
}
});
livePrimaryArchive.observe(getViewLifecycleOwner(), new Observer<EntityFolder>() {
@Override
public void onChanged(EntityFolder archive) {
tvNoPrimaryArchive.setVisibility(done && archive == null ? View.VISIBLE : View.GONE);
}
});
}
});
2018-11-09 12:21:12 +00:00
db.identity().liveIdentities(null, true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
@Override
public void onChanged(@Nullable List<EntityIdentity> identities) {
2018-09-07 12:34:54 +00:00
boolean done = (identities != null && identities.size() > 0);
tvIdentityDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do);
tvIdentityDone.setCompoundDrawablesWithIntrinsicBounds(done ? check : null, null, null, null);
}
});
}
2018-09-04 18:06:22 +00:00
@Override
public void onResume() {
super.onResume();
2018-12-09 17:49:52 +00:00
PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
boolean ignoring = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
ignoring = pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
2018-09-04 18:06:22 +00:00
btnDoze.setEnabled(!ignoring);
tvDozeDone.setText(ignoring ? R.string.title_setup_done : R.string.title_setup_to_do);
2018-09-07 12:34:54 +00:00
tvDozeDone.setCompoundDrawablesWithIntrinsicBounds(ignoring ? check : null, null, null, null);
2018-09-04 18:06:22 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
2018-12-09 17:49:52 +00:00
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
2018-09-04 18:06:22 +00:00
boolean saving = (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
btnData.setVisibility(saving ? View.VISIBLE : View.GONE);
}
}
2018-09-14 11:33:22 +00:00
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_setup, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
PackageManager pm = getContext().getPackageManager();
menu.findItem(R.id.menu_export).setEnabled(getIntentExport().resolveActivity(pm) != null);
menu.findItem(R.id.menu_import).setEnabled(getIntentImport().resolveActivity(pm) != null);
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_legend:
onMenuLegend();
return true;
2018-09-14 11:33:22 +00:00
case R.id.menu_export:
2018-09-23 05:18:12 +00:00
onMenuExport();
2018-09-14 11:33:22 +00:00
return true;
case R.id.menu_import:
2018-09-23 05:18:12 +00:00
onMenuImport();
2018-09-14 11:33:22 +00:00
return true;
case R.id.menu_privacy:
onMenuPrivacy();
return true;
case R.id.menu_about:
onMenuAbout();
return true;
2018-09-14 11:33:22 +00:00
default:
return super.onOptionsItemSelected(item);
}
}
2018-08-02 13:33:06 +00:00
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
checkPermissions(permissions, grantResults, false);
}
private void checkPermissions(String[] permissions, @NonNull int[] grantResults, boolean init) {
2018-08-02 13:33:06 +00:00
boolean has = (grantResults.length > 0);
for (int result : grantResults)
if (result != PackageManager.PERMISSION_GRANTED) {
has = false;
break;
}
btnPermissions.setEnabled(!has);
tvPermissionsDone.setText(has ? R.string.title_setup_done : R.string.title_setup_to_do);
2018-09-07 12:34:54 +00:00
tvPermissionsDone.setCompoundDrawablesWithIntrinsicBounds(has ? check : null, null, null, null);
2018-12-08 08:17:18 +00:00
if (has && !init)
2018-12-08 08:17:18 +00:00
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
DB db = DB.getInstance(context);
for (EntityFolder folder : db.folder().getFoldersSynchronizing())
EntityOperation.sync(db, folder.id);
return null;
}
2018-12-11 10:31:31 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
}
2018-12-08 08:17:18 +00:00
}.load(FragmentSetup.this, new Bundle());
2018-08-02 13:33:06 +00:00
}
2018-09-14 11:33:22 +00:00
@Override
2018-11-14 18:58:01 +00:00
public void onActivityResult(final int requestCode, int resultCode, final Intent data) {
if (requestCode == ActivitySetup.REQUEST_EXPORT || requestCode == ActivitySetup.REQUEST_IMPORT)
if (resultCode == RESULT_OK && data != null) {
final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_password, null);
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText etPassword1 = dview.findViewById(R.id.etPassword1);
EditText etPassword2 = dview.findViewById(R.id.etPassword2);
String password1 = etPassword1.getText().toString();
String password2 = etPassword2.getText().toString();
if (TextUtils.isEmpty(password1))
Snackbar.make(view, R.string.title_setup_password_missing, Snackbar.LENGTH_LONG).show();
else {
if (password1.equals(password2)) {
2018-11-14 19:07:06 +00:00
if (requestCode == ActivitySetup.REQUEST_EXPORT)
handleExport(data, password1);
else
handleImport(data, password1);
} else
Snackbar.make(view, R.string.title_setup_password_different, Snackbar.LENGTH_LONG).show();
}
2018-11-14 18:58:01 +00:00
}
})
.show();
}
2018-09-14 11:33:22 +00:00
}
private void onMenuPrivacy() {
Helper.view(getContext(), getViewLifecycleOwner(), Helper.getIntentPrivacy());
}
private void onMenuLegend() {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentLegend()).addToBackStack("legend");
fragmentTransaction.commit();
}
2018-09-23 05:18:12 +00:00
private void onMenuExport() {
if (Helper.isPro(getContext()))
2018-11-14 18:58:01 +00:00
try {
startActivityForResult(Helper.getChooser(getContext(), getIntentExport()), ActivitySetup.REQUEST_EXPORT);
2018-11-14 18:58:01 +00:00
} catch (Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
2018-11-14 18:58:01 +00:00
}
2018-09-23 05:18:12 +00:00
else {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
}
private void onMenuImport() {
2018-11-14 18:58:01 +00:00
try {
startActivityForResult(Helper.getChooser(getContext(), getIntentImport()), ActivitySetup.REQUEST_IMPORT);
2018-11-14 18:58:01 +00:00
} catch (Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
2018-11-14 18:58:01 +00:00
}
2018-09-23 05:18:12 +00:00
}
private void onMenuAbout() {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentAbout()).addToBackStack("about");
fragmentTransaction.commit();
}
2018-09-19 06:38:29 +00:00
private Intent getIntentHelp() {
Intent intent = new Intent(Intent.ACTION_VIEW);
2018-11-08 11:08:53 +00:00
intent.setData(Uri.parse("https://github.com/M66B/open-source-email/blob/master/SETUP.md#setup-help"));
2018-09-19 06:38:29 +00:00
return intent;
}
2018-09-14 11:33:22 +00:00
private static Intent getIntentExport() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, "fairemail_backup_" +
new SimpleDateFormat("yyyyMMdd").format(new Date().getTime()) + ".json");
return intent;
}
private static Intent getIntentImport() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
return intent;
}
2018-10-29 10:43:45 +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());
}
2018-11-14 18:58:01 +00:00
private void handleExport(Intent data, String password) {
2018-09-14 11:33:22 +00:00
Bundle args = new Bundle();
args.putParcelable("uri", data.getData());
2018-11-14 18:58:01 +00:00
args.putString("password", password);
2018-09-14 11:33:22 +00:00
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
2018-11-14 18:58:01 +00:00
String password = args.getString("password");
2018-09-14 11:33:22 +00:00
2018-12-19 18:12:37 +00:00
if ("file".equals(uri.getScheme()))
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
2018-09-14 11:33:22 +00:00
OutputStream out = null;
try {
2018-12-24 12:27:45 +00:00
Log.i("Writing URI=" + uri);
2018-11-14 18:58:01 +00:00
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);
OutputStream raw = getContext().getContentResolver().openOutputStream(uri);
raw.write(salt);
raw.write(cipher.getIV());
out = new CipherOutputStream(raw, cipher);
2018-09-14 11:33:22 +00:00
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))
jfolders.put(folder.toJSON());
jaccount.put("folders", jfolders);
jaccounts.put(jaccount);
}
// 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())
2018-11-10 10:45:03 +00:00
if (!"pro".equals(key)) {
2018-09-14 11:33:22 +00:00
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);
out.write(jexport.toString(2).getBytes());
2018-12-24 12:27:45 +00:00
Log.i("Exported data");
2018-09-14 11:33:22 +00:00
} finally {
if (out != null)
out.close();
}
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
Snackbar.make(view, R.string.title_setup_exported, Snackbar.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
2018-12-19 18:12:37 +00:00
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
2018-09-14 11:33:22 +00:00
}
}.load(this, args);
}
2018-11-14 18:58:01 +00:00
private void handleImport(Intent data, String password) {
2018-09-14 11:33:22 +00:00
Bundle args = new Bundle();
args.putParcelable("uri", data.getData());
2018-11-14 18:58:01 +00:00
args.putString("password", password);
2018-09-14 11:33:22 +00:00
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
2018-11-14 18:58:01 +00:00
String password = args.getString("password");
2018-12-19 18:12:37 +00:00
if ("file".equals(uri.getScheme()))
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
2018-09-14 11:33:22 +00:00
InputStream in = null;
try {
2018-12-24 12:27:45 +00:00
Log.i("Reading URI=" + uri);
2018-09-14 11:33:22 +00:00
ContentResolver resolver = getContext().getContentResolver();
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null);
2018-11-14 18:58:01 +00:00
InputStream raw = descriptor.createInputStream();
byte[] salt = new byte[16];
byte[] prefix = new byte[16];
2018-12-09 14:49:43 +00:00
if (raw.read(salt) != salt.length)
throw new IOException("length");
if (raw.read(prefix) != prefix.length)
throw new IOException("length");
2018-11-14 18:58:01 +00:00
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);
2018-09-14 11:33:22 +00:00
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
response.append(line);
2018-12-24 12:27:45 +00:00
Log.i("Importing " + resolver.toString());
2018-09-14 11:33:22 +00:00
JSONObject jimport = new JSONObject(response.toString());
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();
2018-09-14 11:33:22 +00:00
account.id = db.account().insertAccount(account);
2018-12-24 12:27:45 +00:00
Log.i("Imported account=" + account.name);
2018-09-14 11:33:22 +00:00
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);
2018-12-24 12:27:45 +00:00
Log.i("Imported identity=" + identity.email);
2018-09-14 11:33:22 +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);
folder.account = account.id;
folder.id = db.folder().insertFolder(folder);
2018-12-24 12:27:45 +00:00
Log.i("Imported folder=" + folder.name);
2018-09-14 11:33:22 +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);
2018-12-24 12:27:45 +00:00
Log.i("Imported answer=" + answer.name);
2018-09-14 11:33:22 +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");
2018-11-10 10:45:03 +00:00
if (!"pro".equals(key)) {
2018-09-14 11:36:49 +00:00
Object value = jsetting.get("value");
if (value instanceof Boolean)
editor.putBoolean(key, (Boolean) value);
2018-11-10 10:45:03 +00:00
else if (value instanceof Integer)
editor.putInt(key, (Integer) value);
else if (value instanceof Long)
editor.putLong(key, (Long) value);
2018-09-14 11:36:49 +00:00
else if (value instanceof String)
editor.putString(key, (String) value);
else
throw new IllegalArgumentException("Unknown settings type key=" + key);
2018-12-24 12:27:45 +00:00
Log.i("Imported setting=" + key);
2018-09-14 11:36:49 +00:00
}
2018-09-14 11:33:22 +00:00
}
editor.apply();
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2018-12-24 12:27:45 +00:00
Log.i("Imported data");
2018-11-14 19:14:40 +00:00
ServiceSynchronize.reload(context, "import");
2018-09-14 11:33:22 +00:00
} finally {
if (in != null)
in.close();
}
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
Snackbar.make(view, R.string.title_setup_imported, Snackbar.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
2018-11-17 08:07:24 +00:00
if (ex.getCause() instanceof BadPaddingException)
Snackbar.make(view, R.string.title_setup_password_invalid, Snackbar.LENGTH_LONG).show();
2018-12-19 18:12:37 +00:00
else if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
2018-11-17 08:07:24 +00:00
else
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
2018-09-14 11:33:22 +00:00
}
}.load(this, args);
}
2018-08-02 13:33:06 +00:00
}