import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
+import android.os.SystemClock;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
-import android.text.TextUtils;
+import android.util.Log;
import com.enqualcomm.support.provider.ICDatabaseHelper;
import com.enqualcomm.support.provider.ICNotificationProvider;
*/
public class PhoneCallListener extends PhoneStateListener {
+ private static final String TAG = "PhoneCallListener";
private final Context mContext;
private String mLastPhoneNumber;
@Override
public void onServiceStateChanged(ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
- ICLogger.d("onServiceStateChanged == > " + serviceState);
+ Log.d(TAG, "onServiceStateChanged == > " + serviceState);
}
+ private final Object mLock = new Object();
+
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
ICLogger.d("state == > " + state + " incomingNumber ==> " + incomingNumber);
- if (!TextUtils.isEmpty(incomingNumber)) {
- this.mLastPhoneNumber = incomingNumber;
- }
//挂断的时候,去查询一下是不是未接电话即可
if (state == CALL_STATE_IDLE) {
- //去查询是否有未接电话
- queryCallState(this.mLastPhoneNumber);
+ new Thread(() -> {
+ synchronized (mLock) {
+ SystemClock.sleep(1000);
+ //查询整个通话记录
+ queryCallState();
+ }
+ }).start();
}
}
- private void queryCallState(String phoneNum) {
- if (TextUtils.isEmpty(phoneNum)) {
- return;
- }
+ private void queryCallState() {
String[] projection = {
CallLog.Calls.NUMBER,
CallLog.Calls.TYPE
};
- String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE + " and " +
- CallLog.Calls.NUMBER + "=" + phoneNum;
+ String where = "type=? and is_read=?";
Cursor c = mContext.getContentResolver().query(
Uri.parse("content://call_log/calls"),
projection,
where,
- null,
+ new String[]{"3", "0"},
null
);
- ICLogger.d("where is == > " + where);
+ Log.d(TAG, "where is == > " + where);
if (c != null) {
- if (c.moveToNext()) {
+ int count = c.getCount();
+ Log.d(TAG, "int count ==> " + count);
+ if (count == 0) {
+ updateSettingsState(0);
+ } else {
+ //修改顶部的状态
+ updateSettingsState(1);
+ }
+ while (c.moveToNext()) {
//创建通知,如果大于1的知,说明有未接电话
+ String phoneNum = c.getString(0);
createNotification(phoneNum);
- //修改顶部的状态
- updateSettingsState();
}
c.close();
}
+ //修改电话状态为已读,这样子以后就不会再创建通知了
+ ContentValues updateValues = new ContentValues();
+ //更新为已读
+ updateValues.put("is_read", 1);
+ int update = mContext.getContentResolver().update(Uri.parse("content://call_log/calls"),
+ updateValues,
+ where,
+ new String[]{"3", "0"});
+ Log.d(TAG, "update ==> " + update);
this.mLastPhoneNumber = null;
}
- private void updateSettingsState() {
+ private void updateSettingsState(int value) {
ContentResolver resolver = mContext.getContentResolver();
ContentValues values = new ContentValues();
values.put(ICDatabaseHelper.sKey, "has_missed_phone");
- values.put(ICDatabaseHelper.sValue, 1);
+ values.put(ICDatabaseHelper.sValue, value);
resolver.insert(Uri.parse(SETTINGS), values);
}
private void createNotification(String phoneNum) {
+ Log.d(TAG, "createNotification ===> " + phoneNum);
//先查询是否有,如果已经有的话,就不创建了
Cursor query = mContext.getContentResolver().query(Uri.parse(ICNotificationProvider.NOTIFICATIONS),
null, "category=? and type=? and title=?",
new String[]{"com.ic.phone", "missed_phone", phoneNum}, null);
if (query != null) {
if (query.moveToNext()) {
- ICLogger.d("has one in there...");
+ Log.d(TAG, "has one in there...");
return;
}
query.close();
}
- ICLogger.d("createNotification==> " + phoneNum);
+ Log.d(TAG, "createNotification==> " + phoneNum);
ContentValues contentValues = new ContentValues();
contentValues.put(ICDatabaseHelper.notification_category, "com.ic.phone");
contentValues.put(ICDatabaseHelper.notification_title, phoneNum);
if (Config.custom_name.equals("YT")) {
ICLogger.i("=====add hotline 96885 ");
- contactArray.add(new Contact(Contact.CATEGORY_CALL, runtime.getContextProxy().context.getResources().getString(R.string.hotline), "96885"));
+ contactArray.add(new Contact(Contact.CATEGORY_CALL,
+ runtime.getContextProxy().context.getResources().getString(R.string.hotline),
+ "96885"));
}
ContentResolver resolver = runtime.getContextProxy().getResolver();
//添加之前先删除白名单的内容
ContentValues contentValues = new ContentValues();
contentValues.put(ICDatabaseHelper.fType, ICDatabaseHelper.MemberType.WHITE_LIST);
contentValues.put(ICDatabaseHelper.fPhoneNum, phonem);
- contentValues.put(ICDatabaseHelper.fID, phonem);
contentValues.put(ICDatabaseHelper.fUserName, name);
contentValues.put(ICDatabaseHelper.fCreateTime, System.currentTimeMillis());
contentValues.put(ICDatabaseHelper.fUpdateTime, System.currentTimeMillis());
alarmName = context.getString(R.string.alarm_sit_too_long_tips_text);
}
alarmList.add(new Alarm("ST" + alarmtime.substring(0, 2) + "." + alarmtime.substring(2),
- "W" + repeat, "CON", alarmswitch == 1 ? "ON" : "OFF", alarmName));
+ "W" + repeat, "CON",
+ alarmswitch == 1 ? "ON" : "OFF", alarmName));
} catch (Exception e) {
ICLogger.e(e.getMessage(), e);
}