Added option to show local contacts in addition to Android contacts

pull/162/head
M66B 5 years ago
parent 6d68db87ec
commit 84d80ce1b8

@ -35,7 +35,8 @@ import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
@ -453,24 +454,19 @@ public class FragmentCompose extends FragmentBase {
Helper.setViewsEnabled(view, false);
final DB db = DB.getInstance(getContext());
final boolean contacts = Helper.hasPermission(getContext(), Manifest.permission.READ_CONTACTS);
SimpleCursorAdapter cadapter = new SimpleCursorAdapter(
getContext(),
R.layout.spinner_item2_dropdown,
null,
contacts
? new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA}
: new String[]{"name", "email"},
new String[]{"name", "email"},
new int[]{android.R.id.text1, android.R.id.text2},
0);
cadapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
public CharSequence convertToString(Cursor cursor) {
int colName = cursor.getColumnIndex(contacts ? ContactsContract.Contacts.DISPLAY_NAME : "name");
int colEmail = cursor.getColumnIndex(contacts ? ContactsContract.CommonDataKinds.Email.DATA : "email");
int colName = cursor.getColumnIndex("name");
int colEmail = cursor.getColumnIndex("email");
String name = cursor.getString(colName);
String email = cursor.getString(colEmail);
StringBuilder sb = new StringBuilder();
@ -484,20 +480,19 @@ public class FragmentCompose extends FragmentBase {
}
});
etTo.setAdapter(cadapter);
etCc.setAdapter(cadapter);
etBcc.setAdapter(cadapter);
etTo.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
etCc.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
etBcc.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean suggest_local = prefs.getBoolean("suggest_local", false);
if (contacts)
cadapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence typed) {
Log.i("Searching provided contact=" + typed);
String wildcard = "%" + typed + "%";
return new CursorWrapper(resolver.query(
boolean contacts = Helper.hasPermission(getContext(), Manifest.permission.READ_CONTACTS);
MatrixCursor provided = new MatrixCursor(new String[]{"_id", "name", "email"});
if (contacts) {
Cursor cursor = resolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
@ -510,47 +505,29 @@ public class FragmentCompose extends FragmentBase {
new String[]{wildcard, wildcard},
"CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME + " NOT LIKE '%@%' THEN 0 ELSE 1 END" +
", " + ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE" +
", " + ContactsContract.CommonDataKinds.Email.DATA + " COLLATE NOCASE")) {
", " + ContactsContract.CommonDataKinds.Email.DATA + " COLLATE NOCASE");
while (cursor != null && cursor.moveToNext())
provided.newRow()
.add(cursor.getLong(0))
.add(cursor.getString(1))
.add(cursor.getString(2));
@Override
public String[] getColumnNames() {
String[] names = super.getColumnNames();
names[0] = "_id";
return names;
if (!suggest_local)
return provided;
}
@Override
public String getColumnName(int index) {
if (index == 0)
return "_id";
return super.getColumnName(index);
Cursor local = db.contact().searchContacts(null, null, wildcard);
return new MergeCursor(new Cursor[]{provided, local});
}
});
@Override
public int getColumnIndex(String name) {
if ("_id".equals(name))
return 0;
return super.getColumnIndex(name);
}
etTo.setAdapter(cadapter);
etCc.setAdapter(cadapter);
etBcc.setAdapter(cadapter);
@Override
public int getColumnIndexOrThrow(String name) throws IllegalArgumentException {
if ("_id".equals(name))
return 0;
return super.getColumnIndexOrThrow(name);
}
};
}
});
else
cadapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence typed) {
Log.i("Searching local contact=" + typed);
String wildcard = "%" + typed + "%";
return db.contact().searchContacts(null, null, wildcard);
}
});
etTo.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
etCc.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
etBcc.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
rvAttachment.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());

@ -40,6 +40,7 @@ import androidx.preference.PreferenceManager;
public class FragmentOptionsSend extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swKeyboard;
private SwitchCompat swSuggestLocal;
private SwitchCompat swPrefixOnce;
private SwitchCompat swPlainOnly;
private SwitchCompat swUsenetSignature;
@ -51,7 +52,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
private Spinner spSendDelayed;
private final static String[] RESET_OPTIONS = new String[]{
"keyboard", "prefix_once", "plain_only", "usenet_signature", "autoresize", "resize", "lookup_mx", "autosend", "send_delayed"
"keyboard", "suggest_local", "prefix_once", "plain_only", "usenet_signature", "autoresize", "resize", "lookup_mx", "autosend", "send_delayed"
};
@Override
@ -65,6 +66,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
// Get controls
swKeyboard = view.findViewById(R.id.swKeyboard);
swSuggestLocal = view.findViewById(R.id.swSuggestLocal);
swPrefixOnce = view.findViewById(R.id.swPrefixOnce);
swPlainOnly = view.findViewById(R.id.swPlainOnly);
swUsenetSignature = view.findViewById(R.id.swUsenetSignature);
@ -88,6 +90,13 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
}
});
swSuggestLocal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("suggest_local", checked).apply();
}
});
swPrefixOnce.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -204,6 +213,7 @@ public class FragmentOptionsSend extends FragmentBase implements SharedPreferenc
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swKeyboard.setChecked(prefs.getBoolean("keyboard", true));
swSuggestLocal.setChecked(prefs.getBoolean("suggest_local", false));
swPrefixOnce.setChecked(prefs.getBoolean("prefix_once", true));
swPlainOnly.setChecked(prefs.getBoolean("plain_only", false));
swUsenetSignature.setChecked(prefs.getBoolean("usenet_signature", false));

@ -31,6 +31,29 @@
app:layout_constraintTop_toTopOf="parent"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swSuggestLocal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_suggest_local"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swKeyboard"
app:switchPadding="12dp" />
<TextView
android:id="@+id/tvSuggestLocalHint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_suggest_local_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swSuggestLocal" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swPrefixOnce"
android:layout_width="0dp"
@ -40,7 +63,7 @@
android:text="@string/title_advanced_prefix_once"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swKeyboard"
app:layout_constraintTop_toBottomOf="@id/tvSuggestLocalHint"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat

@ -207,8 +207,11 @@
<string name="title_advanced_delete_unseen">Delete old unread messages</string>
<string name="title_advanced_kept_removed">Check if old messages were removed from the server</string>
<string name="title_advanced_sync_folders">Synchronize folder list</string>
<string name="title_advanced_subscriptions">Manage folder subscriptions</string>
<string name="title_advanced_sync_subscribed">Synchronize subscribed folders only</string>
<string name="title_advanced_keyboard">Show keyboard by default</string>
<string name="title_advanced_suggest_local">Suggest locally stored contacts</string>
<string name="title_advanced_prefix_once">Prefix subject only once on replying or forwarding</string>
<string name="title_advanced_plain_only">Send plain text only by default</string>
<string name="title_advanced_usenet_signature">Usenet signature convention</string>
@ -303,11 +306,10 @@
<string name="title_advanced_unseen_hint">Some providers don\'t support this properly, which, for example, may result in all messages being synchronized</string>
<string name="title_advanced_sync_kept_hint">This will transfer extra data and consume extra battery power, especially if a lot of messages are stored on the device</string>
<string name="title_advanced_sync_folders_hint">Disabling this will reduce data and battery usage somewhat, but will disable updating the list of folders too</string>
<string name="title_advanced_subscriptions">Manage folder subscriptions</string>
<string name="title_advanced_sync_subscribed">Synchronize subscribed folders only</string>
<string name="title_advanced_sync_delay_hint">This will slow down synchronizing messages</string>
<string name="title_advanced_suggest_local_hint">In addition to contacts provided by Android</string>
<string name="title_advanced_lookup_mx_hint">This will check if DNS MX records exist</string>
<string name="title_advanced_sync_delay_hint">This will slow down synchronizing messages</string>
<string name="title_advanced_metered_hint">Metered connections are generally mobile connections or paid Wi-Fi hotspots</string>
<string name="title_advanced_metered_warning">Disabling this option will disable receiving and sending messages on mobile internet connections</string>

Loading…
Cancel
Save