package com.ic.sos;
+import static android.telephony.TelephonyManager.CALL_STATE_IDLE;
+
import android.annotation.SuppressLint;
-import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
-import android.content.IntentFilter;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import java.util.ArrayList;
import java.util.List;
-import static android.telephony.TelephonyManager.CALL_STATE_IDLE;
-
public class SOSPresenter {
public static final String PATH = "members";
private int callDuration;
private final Handler handler;
private final TelephonyManager telephonyManager;
- private final PhoneStateReceiver stateReceiver;
+ private String targetPhoneNum;
public SOSPresenter(Context context) {
this.mContext = context;
telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction("com.ic.action.PHONE_STATE");
- stateReceiver = new PhoneStateReceiver();
- mContext.registerReceiver(stateReceiver, intentFilter);
HandlerThread handlerThread = new HandlerThread("IC:SOS");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
handler.post(this::sosTask);
}
+ private void checkHasRemoteAnswer() {
+ //hasRemoteAnswer
+ ICLogger.d("targetPhoneNum ==> " + targetPhoneNum);
+ String where = "number=?";
+ Cursor c = mContext.getContentResolver().query(
+ Uri.parse("content://call_log/calls"),
+ null,
+ where,
+ new String[]{targetPhoneNum},
+ "`_id` desc"
+ );
+ ICLogger.d("where is == > " + where);
+ if (c != null) {
+ int count = c.getCount();
+ ICLogger.d("int count ==> " + count);
+ if (c.moveToNext()) {
+ @SuppressLint("Range")
+ int duration = c.getInt(c.getColumnIndex("duration"));
+ ICLogger.d("duration is ==> " + duration);
+ hasRemoteAnswer = duration > 0;
+ }
+ c.close();
+ }
+ }
+
+
/**
* 观察SOS号码的变化
*/
@Override
public void onChange(boolean selfChange) {
handler.post(() -> {
- if (phoneNumList != null && phoneNumList.size() == 0) {
+ if (phoneNumList != null && phoneNumList.size() != 0) {
sosTask();
}
});
//是否有接听
private boolean hasRemoteAnswer = false;
- //通话结束
- public static final int STATE_END = 9;
- //正在挂断
- public static final int STATE_HAND_UP_ING = 8;
- //正在拨号
- public static final int STATE_CALLING = 5;
- //正在通话
- public static final int STATE_TALKING = 2;
- //来电
- public static final int STATE_INCOME = 3;
-
-
- private class PhoneStateReceiver extends BroadcastReceiver {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- int state = intent.getIntExtra("state", -1);
- String callStateLabel = intent.getStringExtra("callStateLabel");
- ICLogger.d("callStateLabel ==> " + callStateLabel);
- ICLogger.d("state ==> " + state);
- //判断case
- switch (state) {
- case STATE_END:
- ICLogger.d("通话结束");
- //在activity onResume的时候再去检查
- //checkNextCall();
- break;
- case STATE_HAND_UP_ING:
- ICLogger.d("正在挂断");
- //删除闹钟
- EndCallAlarmUtil.cancelAlarm(context);
- break;
- case STATE_TALKING:
- ICLogger.d("正在通话中...");
- hasRemoteAnswer = true;
- break;
- case STATE_CALLING:
- ICLogger.d("正在呼叫");
- break;
- case STATE_INCOME:
- ICLogger.d("来电...");
- break;
- }
- }
- }
-
/**
* 根据条件进行下一轮拨号
*/
public void checkNextCall() {
+ //删除闹钟
+ EndCallAlarmUtil.cancelAlarm(mContext);
+ //这个就不切到子线程了,资源耗费不大
+ checkHasRemoteAnswer();
+ startNextCheck();
+ }
+
+ private void startNextCheck() {
boolean doCallNext;
//是否进行下一个拨号,结合策略和是否有接听
//已经接听了
+ ICLogger.d("hasRemoteAnswer ==> " + hasRemoteAnswer);
+ ICLogger.d("loopTime ==> " + loopTime);
//stop,continue,loop
if ("continue".equals(callWay)) {
//如果拨通了,就不再往下轮询了
//是否有拨通hasRemoteAnswer
boolean isLastLoop = currentLoopGroupTime >= loopTime;
ICLogger.d("isLastOne ==> " + isLastOne);
- ICLogger.d("hasRemoteAnswer ==> " + hasRemoteAnswer);
//有接听的,拨完这轮,没接听的,一直拨到最后一轮
if (hasRemoteAnswer) {
doCallNext = !isLastOne;
//拨号超时时长(完成)
//先监听电话的状态,这个在类创建的时候就已经监听的了
if (phoneNumList.size() > currentCallIndex) {
- String targetPhoneNum = phoneNumList.get(currentCallIndex);
+ targetPhoneNum = phoneNumList.get(currentCallIndex);
//开始准备超时
if (callDuration != 0) {
//需要按时挂断
handler.removeCallbacks(this::doSosCall);
hasFirstCall = false;
phoneNumList.clear();
- mContext.unregisterReceiver(stateReceiver);
mContext.getContentResolver().unregisterContentObserver(contentObserver);
}
}