62 lines
2.0 KiB
Java
62 lines
2.0 KiB
Java
package com.avapp.phoneStateBroadcastReceiver;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.IntentFilter;
|
|
|
|
import com.facebook.react.bridge.Promise;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
import com.facebook.react.bridge.ReactMethod;
|
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Bundle;
|
|
import android.telephony.PhoneStateListener;
|
|
import android.telephony.TelephonyManager;
|
|
import android.util.Log;
|
|
|
|
public class PhoneStateModule extends ReactContextBaseJavaModule {
|
|
private final ReactApplicationContext reactContext;
|
|
private TelephonyManager telephonyManager;
|
|
private String currentCallState = "UNKNOWN";
|
|
|
|
public PhoneStateModule(ReactApplicationContext reactContext) {
|
|
super(reactContext);
|
|
this.reactContext = reactContext;
|
|
telephonyManager = (TelephonyManager) reactContext.getSystemService(Context.TELEPHONY_SERVICE);
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "PhoneStateModule";
|
|
}
|
|
|
|
@ReactMethod
|
|
public void getCurrentCallState(Promise promise) {
|
|
if (telephonyManager == null) {
|
|
promise.resolve("UNKNOWN");
|
|
return;
|
|
}
|
|
|
|
int state = telephonyManager.getCallState();
|
|
String currentCallState;
|
|
|
|
switch (state) {
|
|
case TelephonyManager.CALL_STATE_IDLE:
|
|
currentCallState = "IDLE";
|
|
break;
|
|
case TelephonyManager.CALL_STATE_RINGING:
|
|
currentCallState = "RINGING";
|
|
break;
|
|
case TelephonyManager.CALL_STATE_OFFHOOK:
|
|
currentCallState = "OFFHOOK";
|
|
break;
|
|
default:
|
|
currentCallState = "UNKNOWN";
|
|
break;
|
|
}
|
|
|
|
promise.resolve(currentCallState);
|
|
}
|
|
} |