fixing sdk31 listhern for calls

This commit is contained in:
Alexey Kuznetsov 2023-07-24 13:11:57 +03:00
commit c3ac921cf6
3 changed files with 39 additions and 39 deletions

View file

@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
apply plugin: 'com.github.axet.dxplugin'
android {
compileSdkVersion 30
compileSdkVersion 31
ndkVersion "16.1.4479499" // strip debug symbols
defaultConfig {

View file

@ -1,6 +1,7 @@
package com.github.axet.audiorecorder.activities;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
@ -302,6 +303,40 @@ public class RecordingActivity extends AppCompatThemeActivity {
class PhoneStateChangeListener extends PhoneStateListener {
public boolean wasRinging;
public boolean pausedByCall;
@TargetApi(31)
TelephonyCallback e;
@TargetApi(31)
public class TelephonyCallback extends android.telephony.TelephonyCallback implements android.telephony.TelephonyCallback.CallStateListener {
@Override
public void onCallStateChanged(int i) {
PhoneStateChangeListener.this.onCallStateChanged(i, "");
}
}
public void create() {
Context context = RecordingActivity.this;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= 31 && getApplicationInfo().targetSdkVersion >= 31) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
e = new TelephonyCallback();
tm.registerTelephonyCallback(getMainExecutor(), e);
}
} else {
tm.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
}
}
public void close() {
Context context = RecordingActivity.this;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= 31 && getApplicationInfo().targetSdkVersion >= 31) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)
tm.unregisterTelephonyCallback(e);
} else {
tm.listen(this, PhoneStateListener.LISTEN_NONE);
}
}
@Override
public void onCallStateChanged(int s, String incomingNumber) {
@ -319,7 +354,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
case TelephonyManager.CALL_STATE_IDLE:
if (pausedByCall) {
if (receiver.isRecordingReady())
startRecording();
; // startRecording();
}
wasRinging = false;
pausedByCall = false;
@ -406,37 +441,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
final SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);
if (shared.getBoolean(AudioApplication.PREFERENCE_CALL, false)) {
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= 31 && getApplicationInfo().targetSdkVersion >= 31) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
try {
Class TelephonyManager = TelephonyManager.class;
Class CallStateListener = Class.forName("android.telephony.TelephonyCallback$CallStateListener");
InvocationHandler e = new InvocationHandler() {
public void onCallStateChanged(int state) {
pscl.onCallStateChanged(state, "");
}
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
switch (method.getName()) {
case "onCallStateChanged":
onCallStateChanged((int) objects[0]);
break;
}
return null;
}
};
Class TelephonyCallback = Class.forName("android.telephony.TelephonyCallback");
TelephonyManager.getDeclaredMethod("registerTelephonyCallback", Executor.class, TelephonyCallback).invoke(tm, getMainExecutor(), Proxy.newProxyInstance(CallStateListener.getClassLoader(), new Class[]{CallStateListener}, e));
} catch (ClassNotFoundException | InvocationTargetException |
IllegalAccessException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
} else {
tm.listen(pscl, PhoneStateListener.LISTEN_CALL_STATE);
}
pscl.create();
}
final View cancel = findViewById(R.id.recording_cancel);
@ -951,8 +956,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
ControlsService.startIfEnabled(this);
if (pscl != null) {
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(pscl, PhoneStateListener.LISTEN_NONE);
pscl.close();
pscl = null;
}

View file

@ -148,10 +148,6 @@ public class SettingsActivity extends AppCompatSettingsThemeActivity implements
s.setStorageAccessFramework(this, RESULT_STORAGE);
SwitchPreferenceCompat p = (SwitchPreferenceCompat) pm.findPreference(AudioApplication.PREFERENCE_CALL);
if (Build.VERSION.SDK_INT >= 31 && context.getApplicationInfo().targetSdkVersion >= 31) {
p.setVisible(false); // TODO: disable until registerTelephonyCallback fixed
p.setChecked(false);
}
p.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {