-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #1 Support selecting an existing contact fixes #2 Phone number isn't identified if country code is appended fixes #6 Device isn't unmuted if calls threshold is more than 1
- Loading branch information
Showing
23 changed files
with
595 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
app/src/main/java/com/lowenhardt/pickup/AsyncQueryContacts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.lowenhardt.pickup; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.os.AsyncTask; | ||
import android.os.SystemClock; | ||
import android.provider.ContactsContract; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.crashlytics.android.Crashlytics; | ||
import com.lowenhardt.pickup.models.Contact; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.ArrayList; | ||
|
||
class AsyncQueryContacts extends AsyncTask<Void, Void, ArrayList<Contact>> { | ||
|
||
private static final String TAG = AsyncQueryContacts.class.getSimpleName(); | ||
|
||
private WeakReference<Context> weakContext; | ||
private QueryCB cb; | ||
|
||
interface QueryCB { | ||
void onSuccess(ArrayList<Contact> contacts); | ||
void onFailure(); | ||
} | ||
|
||
AsyncQueryContacts(Context context, QueryCB cb) { | ||
this.weakContext = new WeakReference<>(context); | ||
this.cb = cb; | ||
} | ||
|
||
@Override | ||
protected ArrayList<Contact> doInBackground(Void... voids) { | ||
Crashlytics.log(Log.INFO, TAG, "Loading contacts asynchronously"); | ||
|
||
String[] projection = { | ||
ContactsContract.Contacts._ID, | ||
ContactsContract.Contacts.DISPLAY_NAME | ||
}; | ||
|
||
String selection = "(" + ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1' " + | ||
"AND (" + ContactsContract.Contacts.HAS_PHONE_NUMBER + " != 0 ))"; | ||
|
||
ArrayList<Contact> list = new ArrayList<>(); | ||
Context context = this.weakContext.get(); | ||
if (context == null) { | ||
Crashlytics.log(Log.INFO, TAG, "Context has been cleared, not getting contacts"); | ||
return null; | ||
} | ||
|
||
ContentResolver contentResolver = context.getContentResolver(); | ||
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, projection, selection, null, null); | ||
if (cursor == null) { | ||
Crashlytics.log(Log.INFO, TAG, "null cursor, no contacts loaded"); | ||
cancel(true); | ||
return null; | ||
} | ||
|
||
// no contacts is still success | ||
if (cursor.getCount() == 0) { | ||
Crashlytics.log(Log.INFO, TAG, "No contacts to load"); | ||
cursor.close(); | ||
return list; | ||
} | ||
|
||
while (cursor.moveToNext()) { | ||
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); | ||
Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, | ||
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, | ||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", | ||
new String[]{id}, | ||
null); | ||
|
||
if (cursorInfo == null) { | ||
continue; | ||
} | ||
|
||
while (cursorInfo.moveToNext()) { | ||
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); | ||
String number = cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); | ||
list.add(new Contact(name, number)); | ||
} | ||
|
||
cursorInfo.close(); | ||
} | ||
|
||
cursor.close(); | ||
|
||
Crashlytics.log(Log.INFO, TAG, "Loaded "+list.size()+" contacts"); | ||
|
||
return list; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(ArrayList<Contact> contacts) { | ||
super.onPostExecute(contacts); | ||
if (this.cb == null) { | ||
return; | ||
} | ||
|
||
if (contacts == null) { | ||
cb.onFailure(); | ||
return; | ||
} | ||
|
||
cb.onSuccess(contacts); | ||
} | ||
|
||
@Override | ||
protected void onCancelled(ArrayList<Contact> contacts) { | ||
super.onCancelled(contacts); | ||
onCancelled(); | ||
} | ||
|
||
@Override | ||
protected void onCancelled() { | ||
super.onCancelled(); | ||
if (cb == null) { | ||
return; | ||
} | ||
|
||
cb.onFailure(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.