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.
|
|
|
|
|
|
|
|
NetGuard is distributed in the hope that it will be useful,
|
|
|
|
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
|
|
|
|
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
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;
|
2018-08-12 07:51:33 +00:00
|
|
|
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;
|
|
|
|
import android.util.Log;
|
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-08-04 15:37:42 +00:00
|
|
|
import android.widget.CompoundButton;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.widget.TextView;
|
2018-08-11 16:50:30 +00:00
|
|
|
import android.widget.Toast;
|
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;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Date;
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2018-09-04 18:06:22 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.fragment.app.FragmentTransaction;
|
|
|
|
import androidx.lifecycle.Observer;
|
|
|
|
|
2018-09-14 11:33:22 +00:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
|
|
|
|
2018-08-05 12:09:46 +00:00
|
|
|
public class FragmentSetup extends FragmentEx {
|
2018-09-14 11:33:22 +00:00
|
|
|
private ViewGroup view;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
private Button btnAccount;
|
|
|
|
private TextView tvAccountDone;
|
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-09-07 12:34:54 +00:00
|
|
|
private ToggleButton tbDarkTheme;
|
2018-08-16 18:19:09 +00:00
|
|
|
|
|
|
|
private Button btnOptions;
|
2018-08-05 18:46:36 +00:00
|
|
|
|
2018-09-07 12:34:54 +00:00
|
|
|
private Drawable check;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
private static final String[] permissions = new String[]{
|
|
|
|
Manifest.permission.READ_CONTACTS
|
|
|
|
};
|
|
|
|
|
2018-09-14 11:33:22 +00:00
|
|
|
static final List<String> EXPORT_SETTINGS = Arrays.asList(
|
|
|
|
"compress",
|
|
|
|
"avatars",
|
|
|
|
"theme"
|
|
|
|
);
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2018-08-05 12:09:46 +00:00
|
|
|
setSubtitle(R.string.title_setup);
|
2018-09-14 11:33:22 +00:00
|
|
|
setHasOptionsMenu(true);
|
2018-08-05 12:09:46 +00:00
|
|
|
|
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
|
|
|
|
btnAccount = view.findViewById(R.id.btnAccount);
|
|
|
|
tvAccountDone = view.findViewById(R.id.tvAccountDone);
|
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);
|
|
|
|
|
|
|
|
btnData = view.findViewById(R.id.btnData);
|
|
|
|
|
2018-09-07 12:34:54 +00:00
|
|
|
tbDarkTheme = view.findViewById(R.id.tbDarkTheme);
|
2018-08-16 18:19:09 +00:00
|
|
|
btnOptions = view.findViewById(R.id.btnOptions);
|
2018-08-05 18:46:36 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Wire controls
|
|
|
|
|
|
|
|
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) {
|
|
|
|
new AlertDialog.Builder(getContext())
|
|
|
|
.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) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-04 15:37:42 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
2018-08-06 12:29:14 +00:00
|
|
|
|
2018-08-08 05:52:57 +00:00
|
|
|
String theme = prefs.getString("theme", "light");
|
2018-08-04 15:37:42 +00:00
|
|
|
boolean dark = "dark".equals(theme);
|
2018-09-07 12:34:54 +00:00
|
|
|
tbDarkTheme.setTag(dark);
|
|
|
|
tbDarkTheme.setChecked(dark);
|
|
|
|
tbDarkTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
2018-08-04 15:37:42 +00:00
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton button, boolean checked) {
|
|
|
|
if (checked != (Boolean) button.getTag()) {
|
|
|
|
button.setTag(checked);
|
2018-09-07 12:34:54 +00:00
|
|
|
tbDarkTheme.setChecked(checked);
|
2018-08-08 05:52:57 +00:00
|
|
|
prefs.edit().putString("theme", checked ? "dark" : "light").apply();
|
2018-08-04 15:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-16 18:19:09 +00:00
|
|
|
btnOptions.setOnClickListener(new View.OnClickListener() {
|
2018-08-06 12:29:14 +00:00
|
|
|
@Override
|
2018-08-16 18:19:09 +00:00
|
|
|
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-04 18:06:22 +00:00
|
|
|
tvAccountDone.setText(null);
|
2018-09-07 12:34:54 +00:00
|
|
|
tvAccountDone.setCompoundDrawables(null, null, null, null);
|
|
|
|
|
2018-09-04 12:32:23 +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]);
|
|
|
|
|
|
|
|
onRequestPermissionsResult(0, permissions, grantResults);
|
|
|
|
|
2018-08-06 12:29:14 +00:00
|
|
|
// Create outbox
|
2018-08-12 07:51:33 +00:00
|
|
|
new SimpleTask<Void>() {
|
2018-08-03 16:21:18 +00:00
|
|
|
@Override
|
2018-08-12 16:14:20 +00:00
|
|
|
protected Void onLoad(Context context, Bundle args) {
|
2018-08-12 07:51:33 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2018-08-12 12:47:52 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
EntityFolder outbox = db.folder().getOutbox();
|
|
|
|
if (outbox == null) {
|
|
|
|
outbox = new EntityFolder();
|
|
|
|
outbox.name = "OUTBOX";
|
|
|
|
outbox.type = EntityFolder.OUTBOX;
|
|
|
|
outbox.synchronize = false;
|
|
|
|
outbox.after = 0;
|
|
|
|
outbox.id = db.folder().insertFolder(outbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2018-08-03 16:21:18 +00:00
|
|
|
}
|
2018-08-12 12:47:52 +00:00
|
|
|
|
2018-08-11 16:50:30 +00:00
|
|
|
return null;
|
2018-08-03 16:21:18 +00:00
|
|
|
}
|
2018-08-11 16:50:30 +00:00
|
|
|
|
|
|
|
@Override
|
2018-08-12 07:51:33 +00:00
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2018-08-11 16:50:30 +00:00
|
|
|
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2018-08-12 07:51:33 +00:00
|
|
|
}.load(this, new Bundle());
|
2018-08-03 16:21:18 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2018-08-08 05:52:57 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
|
|
DB db = DB.getInstance(getContext());
|
|
|
|
|
2018-08-08 07:11:03 +00:00
|
|
|
db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
|
2018-08-08 05:52:57 +00:00
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<EntityAccount> accounts) {
|
2018-09-07 12:34:54 +00:00
|
|
|
boolean done = (accounts != null && accounts.size() > 0);
|
|
|
|
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);
|
2018-08-08 05:52:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-08 07:11:03 +00:00
|
|
|
db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
|
2018-08-08 05:52:57 +00:00
|
|
|
@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-08-08 05:52:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-04 18:06:22 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
PowerManager pm = getContext().getSystemService(PowerManager.class);
|
|
|
|
boolean ignoring = pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
|
|
|
|
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) {
|
|
|
|
ConnectivityManager cm = getContext().getSystemService(ConnectivityManager.class);
|
|
|
|
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_export:
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
if (prefs.getBoolean("pro", false))
|
|
|
|
startActivityForResult(getIntentExport(), ActivitySetup.REQUEST_EXPORT);
|
|
|
|
else {
|
|
|
|
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
|
|
|
|
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
|
|
|
|
fragmentTransaction.commit();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case R.id.menu_import:
|
|
|
|
startActivityForResult(getIntentImport(), ActivitySetup.REQUEST_IMPORT);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
boolean has = (grantResults.length > 0);
|
|
|
|
for (int result : grantResults)
|
|
|
|
if (result != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
has = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
btnPermissions.setEnabled(!has);
|
2018-08-15 11:34:21 +00:00
|
|
|
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-08-02 13:33:06 +00:00
|
|
|
}
|
2018-09-14 11:33:22 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
Log.i(Helper.TAG, "Request=" + requestCode + " result=" + resultCode + " data=" + data);
|
|
|
|
|
|
|
|
if (requestCode == ActivitySetup.REQUEST_EXPORT) {
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
handleExport(data);
|
|
|
|
|
|
|
|
} else if (requestCode == ActivitySetup.REQUEST_IMPORT) {
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
handleImport(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleExport(Intent data) {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putParcelable("uri", data.getData());
|
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void onLoad(Context context, Bundle args) throws Throwable {
|
|
|
|
Uri uri = args.getParcelable("uri");
|
|
|
|
|
|
|
|
OutputStream out = null;
|
|
|
|
try {
|
|
|
|
Log.i(Helper.TAG, "Writing URI=" + uri);
|
|
|
|
out = getContext().getContentResolver().openOutputStream(uri);
|
|
|
|
|
|
|
|
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())
|
|
|
|
if (EXPORT_SETTINGS.contains(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);
|
|
|
|
|
|
|
|
out.write(jexport.toString(2).getBytes());
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Exported data");
|
|
|
|
} 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) {
|
|
|
|
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}.load(this, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleImport(Intent data) {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putParcelable("uri", data.getData());
|
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void onLoad(Context context, Bundle args) throws Throwable {
|
|
|
|
Uri uri = args.getParcelable("uri");
|
|
|
|
|
|
|
|
InputStream in = null;
|
|
|
|
try {
|
|
|
|
Log.i(Helper.TAG, "Reading URI=" + uri);
|
|
|
|
ContentResolver resolver = getContext().getContentResolver();
|
|
|
|
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null);
|
|
|
|
in = descriptor.createInputStream();
|
|
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null)
|
|
|
|
response.append(line);
|
|
|
|
Log.i(Helper.TAG, "Importing " + resolver.toString());
|
|
|
|
|
|
|
|
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.store_sent = false;
|
|
|
|
account.id = db.account().insertAccount(account);
|
|
|
|
Log.i(Helper.TAG, "Imported account=" + account.name);
|
|
|
|
|
|
|
|
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(Helper.TAG, "Imported identity=" + identity.email);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
Log.i(Helper.TAG, "Imported folder=" + folder.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(Helper.TAG, "Imported answer=" + answer.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-14 11:36:49 +00:00
|
|
|
if (EXPORT_SETTINGS.contains(key)) {
|
|
|
|
Object value = jsetting.get("value");
|
|
|
|
if (value instanceof Boolean)
|
|
|
|
editor.putBoolean(key, (Boolean) value);
|
|
|
|
else if (value instanceof String)
|
|
|
|
editor.putString(key, (String) value);
|
|
|
|
else
|
|
|
|
throw new IllegalArgumentException("Unknown settings type key=" + key);
|
|
|
|
Log.i(Helper.TAG, "Imported setting=" + key);
|
|
|
|
}
|
2018-09-14 11:33:22 +00:00
|
|
|
}
|
|
|
|
editor.apply();
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Imported data");
|
|
|
|
} 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();
|
|
|
|
ServiceSynchronize.reload(getContext(), "import");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}.load(this, args);
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|