Added import result dialog

pull/206/head
M66B 3 years ago
parent c24a4cb4a1
commit eb915d20b4

@ -35,18 +35,23 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -777,6 +782,18 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
Log.w(ex);
}
View dview = LayoutInflater.from(this).inflate(R.layout.dialog_import_progress, null);
TextView tvLog = dview.findViewById(R.id.tvLog);
tvLog.setText(null);
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(dview)
.setPositiveButton(android.R.string.ok, null)
.show();
Button ok = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
ok.setEnabled(false);
Bundle args = new Bundle();
args.putParcelable("uri", uri);
args.putString("password", this.password);
@ -787,9 +804,17 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
args.putBoolean("import_settings", this.import_settings);
new SimpleTask<Void>() {
private SpannableStringBuilder ssb = new SpannableStringBuilder();
@Override
protected void onPreExecute(Bundle args) {
ToastEx.makeText(ActivitySetup.this, R.string.title_executing, Toast.LENGTH_LONG).show();
protected void onProgress(CharSequence status, Bundle data) {
ssb.append(status).append("\n");
tvLog.setText(ssb);
}
@Override
protected void onPostExecute(Bundle args) {
ok.setEnabled(true);
}
@Override
@ -867,6 +892,8 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
List<EntityRule> rules = new ArrayList<>();
if (import_answers) {
postProgress(context.getString(R.string.title_setup_import_answers), null);
// Answers
JSONArray janswers = jimport.getJSONArray("answers");
for (int a = 0; a < janswers.length(); a++) {
@ -890,19 +917,28 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
for (int a = 0; a < jaccounts.length(); a++) {
JSONObject jaccount = (JSONObject) jaccounts.get(a);
EntityAccount account = EntityAccount.fromJSON(jaccount);
postProgress(context.getString(R.string.title_importing_account, account.name));
EntityAccount existing = db.account().getAccountByUUID(account.uuid);
if (existing != null) {
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(context.getString(R.string.title_importing_exists));
ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, ssb.length(), 0);
postProgress(ssb);
EntityLog.log(context, "Existing account=" + account.name +
"id=" + account.id);
continue;
}
if (account.auth_type == AUTH_TYPE_GMAIL) {
if (GmailState.getAccount(context, account.user) == null) {
Log.i("Google account not found user=" + account.user);
continue;
}
if (account.auth_type == AUTH_TYPE_GMAIL &&
GmailState.getAccount(context, account.user) == null) {
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(context.getString(R.string.title_importing_wizard));
ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, ssb.length(), 0);
postProgress(ssb);
EntityLog.log(context, "Run wizard account=" + account.name +
"id=" + account.id);
account.synchronize = false;
}
Long aid = account.id;
@ -948,6 +984,8 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
for (int i = 0; i < jidentities.length(); i++) {
JSONObject jidentity = (JSONObject) jidentities.get(i);
EntityIdentity identity = EntityIdentity.fromJSON(jidentity);
postProgress(context.getString(R.string.title_importing_identity, identity.email));
long id = identity.id;
identity.id = null;
@ -1008,6 +1046,8 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
if (import_contacts) {
// Contacts
if (jaccount.has("contacts")) {
postProgress(context.getString(R.string.title_setup_import_contacts), null);
JSONArray jcontacts = jaccount.getJSONArray("contacts");
for (int c = 0; c < jcontacts.length(); c++) {
JSONObject jcontact = (JSONObject) jcontacts.get(c);
@ -1024,7 +1064,8 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
db.account().updateAccount(account);
}
if (import_rules)
if (import_rules) {
postProgress(context.getString(R.string.title_setup_import_rules), null);
for (EntityRule rule : rules) {
try {
JSONObject jaction = new JSONObject(rule.action);
@ -1054,9 +1095,12 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
db.rule().insertRule(rule);
}
}
}
if (import_settings) {
postProgress(context.getString(R.string.title_setup_import_settings), null);
// Certificates
if (jimport.has("certificates")) {
JSONArray jcertificates = jimport.getJSONArray("certificates");
@ -1180,28 +1224,26 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
ServiceSynchronize.eval(context, "import");
Log.i("Imported data");
postProgress(context.getString(R.string.title_setup_imported), null);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(ActivitySetup.this, R.string.title_setup_imported, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex.getCause() instanceof BadPaddingException)
ToastEx.makeText(ActivitySetup.this, R.string.title_setup_password_invalid, Toast.LENGTH_LONG).show();
else if (ex instanceof IOException && ex.getCause() instanceof IllegalBlockSizeException)
ToastEx.makeText(ActivitySetup.this, R.string.title_setup_import_invalid, Toast.LENGTH_LONG).show();
else {
boolean expected =
(ex instanceof IllegalArgumentException ||
ex instanceof IOException ||
ex instanceof FileNotFoundException ||
ex instanceof JSONException ||
ex instanceof SecurityException);
Log.unexpectedError(getSupportFragmentManager(), ex, !expected);
if (ex.getCause() instanceof BadPaddingException) {
onProgress(getString(R.string.title_setup_password_invalid), null);
onProgress("\n" + ex.toString(), null);
} else if (ex instanceof IOException && ex.getCause() instanceof IllegalBlockSizeException) {
onProgress(getString(R.string.title_setup_import_invalid), null);
onProgress("\n" + ex.toString(), null);
} else if (ex instanceof IllegalArgumentException ||
ex instanceof IOException ||
ex instanceof JSONException ||
ex instanceof SecurityException) {
onProgress(ex.toString(), null);
} else {
dialog.dismiss();
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}
}.execute(this, args, "setup:import");

@ -298,6 +298,26 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
protected abstract T onExecute(Context context, Bundle args) throws Throwable;
protected void postProgress(CharSequence status) {
postProgress(status, null);
}
protected void postProgress(CharSequence status, Bundle data) {
ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
try {
onProgress(status, data);
} catch (Throwable ex) {
Log.e(ex);
}
}
});
}
protected void onProgress(CharSequence status, Bundle data) {
}
protected void onExecuted(Bundle args, T data) {
}

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<eu.faircode.email.ScrollViewEx xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp"
android:scrollbarStyle="outsideOverlay">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<eu.faircode.email.FixedTextView
android:id="@+id/tvCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/twotone_unarchive_24"
android:drawablePadding="6dp"
android:text="@string/title_setup_import"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvLog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Log"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvCaption" />
</androidx.constraintlayout.widget.ConstraintLayout>
</eu.faircode.email.ScrollViewEx>

@ -269,6 +269,10 @@
<string name="title_setup_exported">Settings exported</string>
<string name="title_setup_imported">Settings imported</string>
<string name="title_setup_import_invalid">Invalid settings file</string>
<string name="title_importing_account">Account %1$s</string>
<string name="title_importing_identity">Identity %1$s</string>
<string name="title_importing_exists">Skipped: already exists</string>
<string name="title_importing_wizard">Run wizard to authenticate again</string>
<string name="title_setup_reorder_accounts">Order accounts</string>
<string name="title_setup_reorder_folders">Order folders</string>

Loading…
Cancel
Save