1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2025-01-01 04:35:57 +00:00

Select account to imports vCards for

This commit is contained in:
M66B 2021-09-28 19:14:27 +02:00
parent 88336e16f4
commit ecbdb83198
3 changed files with 110 additions and 58 deletions

View file

@ -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<Void>() {
@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);
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
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<EntityAccount> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1);
// TODO: spinner
new SimpleTask<List<EntityAccount>>() {
@Override
protected List<EntityAccount> onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
return db.account().getSynchronizingAccounts();
}
@Override
protected void onExecuted(Bundle args, List<EntityAccount> 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();
}
}

View file

@ -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<EntityAccount> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1);
// TODO: spinner
new SimpleTask<List<EntityAccount>>() {
@Override
protected List<EntityAccount> onExecute(Context context, Bundle args) {
DB db = DB.getInstance(context);
return db.account().getSynchronizingAccounts();
}
@Override
protected void onExecuted(Bundle args, List<EntityAccount> 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