Skip to content

Commit c62a561

Browse files
committed
Disabled email addresses which don't have any private keys.| #378
1 parent 0576091 commit c62a561

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/base/CreateMessageFragment.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.flowcrypt.email.database.dao.source.AccountDao;
5454
import com.flowcrypt.email.database.dao.source.AccountDaoSource;
5555
import com.flowcrypt.email.database.dao.source.ContactsDaoSource;
56+
import com.flowcrypt.email.database.dao.source.UserIdEmailsKeysDaoSource;
5657
import com.flowcrypt.email.js.Js;
5758
import com.flowcrypt.email.js.JsForUiManager;
5859
import com.flowcrypt.email.js.PgpContact;
@@ -66,6 +67,7 @@
6667
import com.flowcrypt.email.ui.activity.SelectContactsActivity;
6768
import com.flowcrypt.email.ui.activity.fragment.dialog.NoPgpFoundDialogFragment;
6869
import com.flowcrypt.email.ui.activity.listeners.OnChangeMessageEncryptedTypeListener;
70+
import com.flowcrypt.email.ui.adapter.FromAddressesAdapter;
6971
import com.flowcrypt.email.ui.adapter.PgpContactAdapter;
7072
import com.flowcrypt.email.ui.loader.LoadGmailAliasesLoader;
7173
import com.flowcrypt.email.ui.loader.UpdateInfoAboutPgpContactsAsyncTaskLoader;
@@ -76,6 +78,7 @@
7678
import com.flowcrypt.email.util.GeneralUtil;
7779
import com.flowcrypt.email.util.UIUtil;
7880
import com.flowcrypt.email.util.exception.FlowCryptException;
81+
import com.google.android.gms.common.util.CollectionUtils;
7982
import com.google.android.material.snackbar.Snackbar;
8083
import com.google.android.material.textfield.TextInputLayout;
8184
import com.hootsuite.nachos.NachoTextView;
@@ -135,7 +138,7 @@ public class CreateMessageFragment extends BaseSyncFragment implements View.OnFo
135138
private IncomingMessageInfo incomingMessageInfo;
136139
private ServiceInfo serviceInfo;
137140
private AccountDao accountDao;
138-
private ArrayAdapter<String> fromAddressesArrayAdapter;
141+
private FromAddressesAdapter<String> fromAddressesArrayAdapter;
139142
private PgpContact pgpContactWithNoPublicKey;
140143
private ExtraActionInfo extraActionInfo;
141144
private MessageType messageType = MessageType.NEW;
@@ -166,6 +169,7 @@ public class CreateMessageFragment extends BaseSyncFragment implements View.OnFo
166169
private boolean isUpdateInfoAboutBccCompleted = true;
167170
private boolean isIncomingMessageInfoUsed;
168171
private boolean isMessageSentToQueue;
172+
private int originalColor;
169173

170174
public CreateMessageFragment() {
171175
pgpContactsTo = new ArrayList<>();
@@ -197,11 +201,15 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
197201
initDraftCacheDirectory();
198202

199203
accountDao = new AccountDaoSource().getActiveAccountInformation(getContext());
200-
fromAddressesArrayAdapter = new ArrayAdapter<>(getContext(),
204+
fromAddressesArrayAdapter = new FromAddressesAdapter<>(getContext(),
201205
android.R.layout.simple_list_item_1, android.R.id.text1, new ArrayList<String>());
202206
fromAddressesArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
207+
fromAddressesArrayAdapter.setUseKeysInfo(onChangeMessageEncryptedTypeListener.getMessageEncryptionType()
208+
== MessageEncryptionType.ENCRYPTED);
203209
if (accountDao != null) {
204210
fromAddressesArrayAdapter.add(accountDao.getEmail());
211+
fromAddressesArrayAdapter.updateKeyAvailable(accountDao.getEmail(), !CollectionUtils.isEmpty(
212+
new UserIdEmailsKeysDaoSource().getLongIdsByEmail(getContext(), accountDao.getEmail())));
205213
}
206214

207215
js = JsForUiManager.getInstance(getContext()).getJs();
@@ -553,6 +561,11 @@ public void handleSuccessLoaderResult(int loaderId, Object result) {
553561
fromAddressesArrayAdapter.clear();
554562
fromAddressesArrayAdapter.addAll(aliases);
555563

564+
for (String email : aliases) {
565+
fromAddressesArrayAdapter.updateKeyAvailable(email, !CollectionUtils.isEmpty(
566+
new UserIdEmailsKeysDaoSource().getLongIdsByEmail(getContext(), email)));
567+
}
568+
556569
prepareAliasForReplyIfNeed(aliases);
557570

558571
if (fromAddressesArrayAdapter.getCount() == 1) {
@@ -655,6 +668,13 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
655668
switch (parent.getId()) {
656669
case R.id.spinnerFrom:
657670
editTextFrom.setText((CharSequence) parent.getAdapter().getItem(position));
671+
if (onChangeMessageEncryptedTypeListener.getMessageEncryptionType() == MessageEncryptionType.ENCRYPTED) {
672+
ArrayAdapter adapter = (ArrayAdapter) parent.getAdapter();
673+
int colorGray = UIUtil.getColor(getContext(), R.color.gray);
674+
editTextFrom.setTextColor(adapter.isEnabled(position) ? originalColor : colorGray);
675+
} else {
676+
editTextFrom.setTextColor(originalColor);
677+
}
658678
break;
659679
}
660680
}
@@ -700,6 +720,12 @@ public void onMessageEncryptionTypeChange(MessageEncryptionType messageEncryptio
700720
editTextRecipientsTo.getOnFocusChangeListener().onFocusChange(editTextRecipientsTo, false);
701721
editTextRecipientsCc.getOnFocusChangeListener().onFocusChange(editTextRecipientsCc, false);
702722
editTextRecipientsBcc.getOnFocusChangeListener().onFocusChange(editTextRecipientsBcc, false);
723+
fromAddressesArrayAdapter.setUseKeysInfo(true);
724+
725+
int colorGray = UIUtil.getColor(getContext(), R.color.gray);
726+
boolean isItemEnabled = fromAddressesArrayAdapter.isEnabled(spinnerFrom.getSelectedItemPosition());
727+
editTextFrom.setTextColor(isItemEnabled ? originalColor : colorGray);
728+
703729
break;
704730

705731
case STANDARD:
@@ -710,6 +736,8 @@ public void onMessageEncryptionTypeChange(MessageEncryptionType messageEncryptio
710736
isUpdateInfoAboutToCompleted = true;
711737
isUpdateInfoAboutCcCompleted = true;
712738
isUpdateInfoAboutBccCompleted = true;
739+
fromAddressesArrayAdapter.setUseKeysInfo(false);
740+
editTextFrom.setTextColor(originalColor);
713741
break;
714742
}
715743
}
@@ -1026,6 +1054,11 @@ private boolean isAllInformationCorrect() {
10261054
editTextRecipientsCc.chipifyAllUnterminatedTokens();
10271055
editTextRecipientsBcc.chipifyAllUnterminatedTokens();
10281056

1057+
if (!fromAddressesArrayAdapter.isEnabled(spinnerFrom.getSelectedItemPosition())) {
1058+
showInfoSnackbar(editTextRecipientsTo, getString(R.string.no_key_available));
1059+
return false;
1060+
}
1061+
10291062
if (TextUtils.isEmpty(editTextRecipientsTo.getText().toString())) {
10301063
showInfoSnackbar(editTextRecipientsTo, getString(R.string.text_must_not_be_empty,
10311064
getString(R.string.prompt_recipients_to)));
@@ -1166,6 +1199,7 @@ private void initViews(View view) {
11661199
spinnerFrom.setAdapter(fromAddressesArrayAdapter);
11671200

11681201
editTextFrom = view.findViewById(R.id.editTextFrom);
1202+
originalColor = editTextFrom.getCurrentTextColor();
11691203

11701204
imageButtonAliases = view.findViewById(R.id.imageButtonAliases);
11711205
if (imageButtonAliases != null) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* © 2016-2018 FlowCrypt Limited. Limitations apply. Contact [email protected]
3+
* Contributors: DenBond7
4+
*/
5+
6+
package com.flowcrypt.email.ui.adapter;
7+
8+
import android.content.Context;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import android.widget.ArrayAdapter;
13+
import android.widget.TextView;
14+
15+
import com.flowcrypt.email.R;
16+
import com.flowcrypt.email.util.UIUtil;
17+
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import androidx.annotation.NonNull;
23+
import androidx.annotation.Nullable;
24+
25+
/**
26+
* This is a custom realization of {@link ArrayAdapter} which can be used for showing the sender addresses.
27+
*
28+
* @author Denis Bondarenko
29+
* Date: 28.11.2018
30+
* Time: 5:22 PM
31+
32+
*/
33+
public class FromAddressesAdapter<T> extends ArrayAdapter<T> {
34+
private Map<String, Boolean> infoAboutKeysAvailable = new HashMap<>();
35+
private int originalColor;
36+
private boolean useKeysInfo;
37+
38+
public FromAddressesAdapter(@NonNull Context context, int resource, int textViewResId, @NonNull List<T> objects) {
39+
super(context, resource, textViewResId, objects);
40+
}
41+
42+
@Override
43+
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
44+
View view = super.getDropDownView(position, convertView, parent);
45+
46+
TextView textView = view.findViewById(android.R.id.text1);
47+
48+
if (textView != null) {
49+
textView.setTextColor(isEnabled(position) ? originalColor : UIUtil.getColor(getContext(), R.color.gray));
50+
}
51+
52+
return view;
53+
}
54+
55+
@Override
56+
public boolean isEnabled(int position) {
57+
if (useKeysInfo && getItem(position) instanceof String) {
58+
String email = (String) getItem(position);
59+
return infoAboutKeysAvailable.get(email);
60+
} else return super.isEnabled(position);
61+
}
62+
63+
@Override
64+
public void setDropDownViewResource(int resource) {
65+
super.setDropDownViewResource(resource);
66+
TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(resource, null);
67+
originalColor = textView.getCurrentTextColor();
68+
}
69+
70+
/**
71+
* This method can be used to disable the keys checking.
72+
*
73+
* @param useKeysInfo true if we want to check the key available, otherwise false.
74+
*/
75+
public void setUseKeysInfo(boolean useKeysInfo) {
76+
this.useKeysInfo = useKeysInfo;
77+
notifyDataSetChanged();
78+
}
79+
80+
/**
81+
* Update information about the key availability for the given email.
82+
*
83+
* @param emailAddress The given email address
84+
* @param hasPgp true if we have a private key for the given email address, otherwise false
85+
*/
86+
public void updateKeyAvailable(String emailAddress, boolean hasPgp) {
87+
if (infoAboutKeysAvailable != null) {
88+
infoAboutKeysAvailable.put(emailAddress, hasPgp);
89+
}
90+
}
91+
}

FlowCrypt/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,5 @@ icelanders prefer real sauna</font>
416416
<string name="see_backups_to_save_your_private_keys">Not implemented yet, see \"Backups\" to save your private key to file</string>
417417
<string name="copied">Copied</string>
418418
<string name="saved">Saved</string>
419+
<string name="no_key_available">No key available for your email account</string>
419420
</resources>

0 commit comments

Comments
 (0)