Skip to content

Commit

Permalink
Multiple fixes
Browse files Browse the repository at this point in the history
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
salowenh committed Oct 13, 2019
1 parent 6429a62 commit a25d6ca
Show file tree
Hide file tree
Showing 23 changed files with 595 additions and 160 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "com.lowenhardt.pickup"
minSdkVersion 16
targetSdkVersion 29
versionCode 2
versionName "1.0"
versionCode 3
versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
setProperty("archivesBaseName", "pickup.vn.${versionName}.vc.${versionCode}")
multiDexEnabled = true
Expand All @@ -39,7 +39,6 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation "com.android.support:support-compat:28.0.0"
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
android:name=".PickUpApplication"
Expand Down
128 changes: 128 additions & 0 deletions app/src/main/java/com/lowenhardt/pickup/AsyncQueryContacts.java
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();
}
}
24 changes: 14 additions & 10 deletions app/src/main/java/com/lowenhardt/pickup/CallReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.telephony.PhoneNumberUtils;
import android.util.Log;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.crashlytics.android.Crashlytics;
import com.lowenhardt.pickup.models.ContactConfig;

import java.util.Calendar;
import java.util.Date;
Expand All @@ -18,14 +19,15 @@ public class CallReceiver extends PhoneCallReceiver {
private static final String TAG = CallReceiver.class.getSimpleName();

@Override
protected void onIncomingCallReceived(Context c, Uri number, Date start) {
protected void onIncomingCallReceived(Context c, String number, Date start) {
Database db = new Database(c);
List<ContactConfig> contactConfigs = db.getAllContactConfigs(c);
ContactConfig matchingContact = null;
for (ContactConfig contactConfig : contactConfigs) {
Uri contactNumber = contactConfig.getPhoneNumberUri();
if (contactNumber.equals(number)) {
String contactNumber = contactConfig.getPhoneNumber();
if (PhoneNumberUtils.compare(contactNumber, number)) {
matchingContact = contactConfig;
break;
}
}

Expand All @@ -37,7 +39,9 @@ protected void onIncomingCallReceived(Context c, Uri number, Date start) {
Crashlytics.log(Log.INFO, TAG, "Incoming phone number matches contact config: "+matchingContact);

matchingContact.addCall(start);
if (!matchingContact.shouldChangeMode()) {
db.addOrUpdate(matchingContact, c);

if (!matchingContact.areCallConditionsMet()) {
return;
}

Expand Down Expand Up @@ -140,27 +144,27 @@ private boolean isDeviceMuted(Context c) {
}

@Override
protected void onIncomingCallAnswered(Context ctx, Uri number, Date start) {
protected void onIncomingCallAnswered(Context ctx, String number, Date start) {
//
}

@Override
protected void onIncomingCallEnded(Context ctx, Uri number, Date start, Date end) {
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
//
}

@Override
protected void onOutgoingCallStarted(Context ctx, Uri number, Date start) {
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
//
}

@Override
protected void onOutgoingCallEnded(Context ctx, Uri number, Date start, Date end) {
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
//
}

@Override
protected void onMissedCall(Context ctx, Uri number, Date start) {
protected void onMissedCall(Context ctx, String number, Date start) {
//
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/lowenhardt/pickup/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Constants {

public static final int REQUEST_PERMISSION_READ_PHONE_STATE = 1003;
public static final int REQUEST_PERMISSION_READ_CALL_LOG = 1004;
public static final int REQUEST_PERMISSION_READ_CONTACTS = 1005;

static public String NOTIFICATION_CHANNEL_GENERAL_ID = "general";
static public String NOTIFICATION_CHANNEL_GENERAL_NAME = "General";
Expand Down
Loading

0 comments on commit a25d6ca

Please sign in to comment.