From c3ac921cf620cf9b84f839d0a3dfa42612c1904c Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Mon, 24 Jul 2023 13:11:57 +0300 Subject: [PATCH] fixing sdk31 listhern for calls --- app/build.gradle | 2 +- .../activities/RecordingActivity.java | 72 ++++++++++--------- .../activities/SettingsActivity.java | 4 -- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f6c9c8a..bc32858 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 { diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index 7e524ef..5d0e8b8 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -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; } diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java index 6bb6df4..30c358b 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java @@ -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) {