Basic logic for intercepting missed calls

pull/14/head
Sam Bosley 13 years ago
parent bdec8356ca
commit 040aca6d7c

@ -32,6 +32,8 @@
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!-- for push notifications -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- for missed call reminders -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- ============================================== Exported Permissions = -->
@ -199,6 +201,12 @@
</intent-filter>
</receiver>
<receiver android:name="com.todoroo.astrid.calls.PhoneStateChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<!-- ======================================================== Services = -->
<service android:name="com.todoroo.astrid.widget.TasksWidget$WidgetUpdateService" />

@ -0,0 +1,56 @@
package com.todoroo.astrid.calls;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.CallLog.Calls;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.todoroo.andlib.utility.Preferences;
@SuppressWarnings("nls")
public class PhoneStateChangedReceiver extends BroadcastReceiver {
private static final String PREF_LAST_INCOMING_NUMBER = "last_incoming_number";
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.err.println("Ringing: " + number);
if (TextUtils.isEmpty(number))
return;
Preferences.setString(PREF_LAST_INCOMING_NUMBER, number);
} else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
String lastNumber = Preferences.getStringValue(PREF_LAST_INCOMING_NUMBER);
if (TextUtils.isEmpty(lastNumber))
return;
Preferences.clear(PREF_LAST_INCOMING_NUMBER);
Cursor calls = context.getContentResolver().query(
Calls.CONTENT_URI,
null,
Calls.TYPE + " = ? AND " + Calls.NEW + " = ? AND " + Calls.NUMBER + " = ?" ,
new String[] { Integer.toString(Calls.MISSED_TYPE), "1", lastNumber },
Calls.DATE + " DESC"
);
try {
if (calls.getCount() > 0) {
calls.moveToFirst();
System.err.println("Should display notification for number: " + lastNumber);
}
} finally {
calls.close();
}
} else {
System.err.println("Other state: " + state);
}
}
}
Loading…
Cancel
Save