From ecbdb8319891e198b4dc212e3169b74ebcf481e6 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 28 Sep 2021 19:14:27 +0200 Subject: [PATCH] Select account to imports vCards for --- .../eu/faircode/email/FragmentContacts.java | 52 ++++++++----- .../email/FragmentDialogSelectAccount.java | 74 +++++++++++++++++++ .../eu/faircode/email/FragmentMessages.java | 42 +---------- 3 files changed, 110 insertions(+), 58 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java diff --git a/app/src/main/java/eu/faircode/email/FragmentContacts.java b/app/src/main/java/eu/faircode/email/FragmentContacts.java index b0f8cff82e..7b5195947a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentContacts.java +++ b/app/src/main/java/eu/faircode/email/FragmentContacts.java @@ -74,11 +74,13 @@ public class FragmentContacts extends FragmentBase { private ContentLoadingProgressBar pbWait; private Group grpReady; + private long account; private boolean junk = false; private String searching = null; private AdapterContact adapter; - static final int REQUEST_IMPORT = 1; + private static final int REQUEST_ACCOUNT = 1; + private static final int REQUEST_IMPORT = 2; @Override @Nullable @@ -111,6 +113,7 @@ public class FragmentContacts extends FragmentBase { @Override public void onSaveInstanceState(Bundle outState) { + outState.putLong("fair:account", account); outState.putBoolean("fair:junk", junk); outState.putString("fair:searching", searching); super.onSaveInstanceState(outState); @@ -121,6 +124,7 @@ public class FragmentContacts extends FragmentBase { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null) { + account = savedInstanceState.getLong("fair:account"); junk = savedInstanceState.getBoolean("fair:junk"); searching = savedInstanceState.getString("fair:searching"); } @@ -216,16 +220,10 @@ public class FragmentContacts extends FragmentBase { } private void onMenuImport() { - final Context context = getContext(); - PackageManager pm = context.getPackageManager(); - - Intent open = new Intent(Intent.ACTION_GET_CONTENT); - open.addCategory(Intent.CATEGORY_OPENABLE); - open.setType("*/*"); - if (open.resolveActivity(pm) == null) // system whitelisted - ToastEx.makeText(context, R.string.title_no_saf, Toast.LENGTH_LONG).show(); - else - startActivityForResult(Helper.getChooser(context, open), REQUEST_IMPORT); + FragmentDialogSelectAccount fragment = new FragmentDialogSelectAccount(); + fragment.setArguments(new Bundle()); + fragment.setTargetFragment(this, REQUEST_ACCOUNT); + fragment.show(getParentFragmentManager(), "messages:accounts"); } private void onMenuDelete() { @@ -238,6 +236,10 @@ public class FragmentContacts extends FragmentBase { try { switch (requestCode) { + case REQUEST_ACCOUNT: + if (resultCode == RESULT_OK && data != null) + onAccountSelected(data.getBundleExtra("args")); + break; case REQUEST_IMPORT: if (resultCode == RESULT_OK && data != null) handleImport(data); @@ -248,11 +250,27 @@ public class FragmentContacts extends FragmentBase { } } + private void onAccountSelected(Bundle args) { + account = args.getLong("account"); + + final Context context = getContext(); + PackageManager pm = context.getPackageManager(); + + Intent open = new Intent(Intent.ACTION_GET_CONTENT); + open.addCategory(Intent.CATEGORY_OPENABLE); + open.setType("*/*"); + if (open.resolveActivity(pm) == null) // system whitelisted + ToastEx.makeText(context, R.string.title_no_saf, Toast.LENGTH_LONG).show(); + else + startActivityForResult(Helper.getChooser(context, open), REQUEST_IMPORT); + } + private void handleImport(Intent data) { Uri uri = data.getData(); Bundle args = new Bundle(); args.putParcelable("uri", uri); + args.putLong("account", account); new SimpleTask() { @Override @@ -263,6 +281,7 @@ public class FragmentContacts extends FragmentBase { @Override protected Void onExecute(Context context, Bundle args) throws Throwable { Uri uri = args.getParcelable("uri"); + long account = args.getLong("account"); if (uri == null) throw new FileNotFoundException(); @@ -298,12 +317,11 @@ public class FragmentContacts extends FragmentBase { addresses.add(new InternetAddress(address, name, StandardCharsets.UTF_8.name())); } - for (EntityAccount account : accounts) - EntityContact.update(context, - account.id, - addresses.toArray(new Address[0]), - EntityContact.TYPE_TO, - now); + EntityContact.update(context, + account, + addresses.toArray(new Address[0]), + EntityContact.TYPE_TO, + now); } } diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java b/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java new file mode 100644 index 0000000000..03992c6d1b --- /dev/null +++ b/app/src/main/java/eu/faircode/email/FragmentDialogSelectAccount.java @@ -0,0 +1,74 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + 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. + + FairEmail 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 FairEmail. If not, see . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +import static android.app.Activity.RESULT_OK; + +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.widget.ArrayAdapter; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; + +import java.util.List; + +public class FragmentDialogSelectAccount extends FragmentDialogBase { + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + final ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1); + + // TODO: spinner + new SimpleTask>() { + @Override + protected List onExecute(Context context, Bundle args) { + DB db = DB.getInstance(context); + return db.account().getSynchronizingAccounts(); + } + + @Override + protected void onExecuted(Bundle args, List accounts) { + adapter.addAll(accounts); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }.execute(this, new Bundle(), "messages:accounts"); + + return new AlertDialog.Builder(getContext()) + .setTitle(R.string.title_list_accounts) + .setAdapter(adapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + EntityAccount account = adapter.getItem(which); + getArguments().putLong("account", account.id); + sendResult(RESULT_OK); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .create(); + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index d895caca1b..bd1b5a0852 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -4353,7 +4353,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. ib.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - FragmentDialogAccount fragment = new FragmentDialogAccount(); + FragmentDialogSelectAccount fragment = new FragmentDialogSelectAccount(); fragment.setArguments(new Bundle()); fragment.setTargetFragment(FragmentMessages.this, REQUEST_ACCOUNT); fragment.show(getParentFragmentManager(), "messages:accounts"); @@ -8848,46 +8848,6 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } } - public static class FragmentDialogAccount extends FragmentDialogBase { - @NonNull - @Override - public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { - final ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1); - - // TODO: spinner - new SimpleTask>() { - @Override - protected List onExecute(Context context, Bundle args) { - DB db = DB.getInstance(context); - return db.account().getSynchronizingAccounts(); - } - - @Override - protected void onExecuted(Bundle args, List accounts) { - adapter.addAll(accounts); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }.execute(this, new Bundle(), "messages:accounts"); - - return new AlertDialog.Builder(getContext()) - .setTitle(R.string.title_list_accounts) - .setAdapter(adapter, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - EntityAccount account = adapter.getItem(which); - getArguments().putLong("account", account.id); - sendResult(RESULT_OK); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .create(); - } - } - public static class FragmentDialogBoundaryError extends FragmentDialogBase { @NonNull @Override