2015-04-20 22:39:35 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-01-22 22:49:50 +01:00
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-09-20 22:04:53 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
2015-01-22 22:49:50 +01:00
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
|
2015-04-20 22:39:35 +02:00
|
|
|
|
2015-02-07 12:58:18 +01:00
|
|
|
|
2015-01-22 22:49:50 +01:00
|
|
|
public class PhoneCallReceiver extends BroadcastReceiver {
|
|
|
|
|
2015-02-07 12:58:18 +01:00
|
|
|
private static int mLastState = TelephonyManager.CALL_STATE_IDLE;
|
|
|
|
private static String mSavedNumber;
|
|
|
|
|
2015-01-22 22:49:50 +01:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-02-07 12:58:18 +01:00
|
|
|
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
|
|
|
|
mSavedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
|
|
|
|
} else {
|
|
|
|
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
|
|
|
|
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
|
|
|
|
int state = 0;
|
2015-09-20 22:04:53 +02:00
|
|
|
if (TelephonyManager.EXTRA_STATE_IDLE.equals(stateStr)) {
|
2015-02-07 12:58:18 +01:00
|
|
|
state = TelephonyManager.CALL_STATE_IDLE;
|
2015-09-20 22:04:53 +02:00
|
|
|
} else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(stateStr)) {
|
2015-02-07 12:58:18 +01:00
|
|
|
state = TelephonyManager.CALL_STATE_OFFHOOK;
|
2015-09-20 22:04:53 +02:00
|
|
|
} else if (TelephonyManager.EXTRA_STATE_RINGING.equals(stateStr)) {
|
2015-02-07 12:58:18 +01:00
|
|
|
state = TelephonyManager.CALL_STATE_RINGING;
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallStateChanged(context, state, number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onCallStateChanged(Context context, int state, String number) {
|
|
|
|
if (mLastState == state) {
|
|
|
|
return;
|
2015-01-22 22:49:50 +01:00
|
|
|
}
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
ServiceCommand callCommand = null;
|
2015-02-07 12:58:18 +01:00
|
|
|
switch (state) {
|
|
|
|
case TelephonyManager.CALL_STATE_RINGING:
|
|
|
|
mSavedNumber = number;
|
2015-08-03 23:09:49 +02:00
|
|
|
callCommand = ServiceCommand.CALL_INCOMING;
|
2015-02-07 12:58:18 +01:00
|
|
|
break;
|
|
|
|
case TelephonyManager.CALL_STATE_OFFHOOK:
|
|
|
|
if (mLastState == TelephonyManager.CALL_STATE_RINGING) {
|
2015-08-03 23:09:49 +02:00
|
|
|
callCommand = ServiceCommand.CALL_START;
|
2015-02-07 12:58:18 +01:00
|
|
|
} else {
|
2015-08-03 23:09:49 +02:00
|
|
|
callCommand = ServiceCommand.CALL_OUTGOING;
|
2015-02-07 12:58:18 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TelephonyManager.CALL_STATE_IDLE:
|
2015-02-07 13:20:38 +01:00
|
|
|
if (mLastState == TelephonyManager.CALL_STATE_RINGING) {
|
2015-02-12 16:00:45 +01:00
|
|
|
//missed call would be correct here
|
2015-08-03 23:09:49 +02:00
|
|
|
callCommand = ServiceCommand.CALL_END;
|
2015-02-08 23:53:40 +01:00
|
|
|
} else {
|
2015-08-03 23:09:49 +02:00
|
|
|
callCommand = ServiceCommand.CALL_END;
|
2015-02-07 13:20:38 +01:00
|
|
|
}
|
2015-02-07 12:58:18 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-02-12 16:00:45 +01:00
|
|
|
if (callCommand != null) {
|
2015-09-20 22:04:53 +02:00
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
if ("never".equals(sharedPrefs.getString("notification_mode_calls", "always"))) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 00:58:18 +02:00
|
|
|
GBApplication.deviceService().onSetCallState(mSavedNumber, null, callCommand);
|
2015-01-22 22:49:50 +01:00
|
|
|
}
|
2015-02-07 12:58:18 +01:00
|
|
|
mLastState = state;
|
2015-01-22 22:49:50 +01:00
|
|
|
}
|
|
|
|
}
|