From 13ce264280ed6e9c934710aca32760cbf460a244 Mon Sep 17 00:00:00 2001 From: hazmi-e205 Date: Thu, 22 Feb 2024 14:36:26 +0700 Subject: [PATCH 1/3] Android 14 Support added - Ported InputManagerGlobalEventInjector to support input event for Android 14 - Fixed crash SonicKeyboard on Android 14 because behavior changes of registerReceiver method - Fixed crash SonicManagerServiceV2 on Android 14 because behavior changes of startForeground method - Updated targetSdk and compileSdk to 34 - Updated AGP to 8.2.2 and Gradle 8.6 - Removed duplicate permissions --- app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 14 ++-- .../sonic/android/keyboard/SonicKeyboard.kt | 7 +- .../touchCompat/InputManagerWrapper.kt | 67 ++++++++++++++++++- .../android/service/SonicManagerServiceV2.kt | 16 +++-- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- lib_socketmanager/build.gradle | 5 +- .../src/main/AndroidManifest.xml | 3 +- 9 files changed, 99 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 458d579..1cfc68f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,12 +5,13 @@ plugins { } android { - compileSdk 31 + compileSdk 34 + namespace "org.cloud.sonic.android" defaultConfig { applicationId "org.cloud.sonic.android" minSdk 21 - targetSdk 31 + targetSdk 34 versionCode 10 versionName "SONIC_VERSION" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6ccc0a2..bfd9fbb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,9 +1,7 @@ + xmlns:tools="http://schemas.android.com/tools"> - @@ -23,8 +21,6 @@ - - @@ -33,7 +29,10 @@ tools:ignore="ProtectedPermissions" /> - + + + + + android:exported="true" + android:foregroundServiceType="mediaPlayback" /> = Build.VERSION_CODES.O) { + registerReceiver(mReceiver, filter, RECEIVER_EXPORTED) + } else { + registerReceiver(mReceiver, filter) + } } mInputView.setOnClickListener { val intent = Intent(Settings.ACTION_INPUT_METHOD_SETTINGS) diff --git a/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt b/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt index 48e9aa4..5149037 100644 --- a/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt +++ b/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt @@ -34,9 +34,13 @@ class InputManagerWrapper { init { eventInjector = try { - InputManagerEventInjector() + InputManagerGlobalEventInjector() } catch (e: UnsupportedOperationException) { - WindowManagerEventInjector() + try { + InputManagerEventInjector() + } catch (e: UnsupportedOperationException) { + WindowManagerEventInjector() + } } } @@ -48,6 +52,65 @@ class InputManagerWrapper { fun injectInputEvent(event: InputEvent?): Boolean } + class InputManagerGlobalEventInjector : EventInjector { + private val injectMode = 0 + private var inputManager: Any? = null + private var injector: Method? = null + + init { + try { + var inputManagerGlobal = getInstance("android.hardware.input.InputManagerGlobal"); + var m = inputManagerGlobal?.javaClass?.getMethod("getInputManagerService"); + inputManager = m?.invoke(inputManagerGlobal); + injector = inputManager?.javaClass + ?.getMethod( + "injectInputEvent", + InputEvent::class.java, + Int::class.javaPrimitiveType + ); + } + catch (e: NoSuchMethodException) { + throw java.lang.UnsupportedOperationException( + "InputManagerEventInjector is not supported in this device! " + + "Please submit your deviceInfo to https://github.com/SonicCloudOrg/sonic-android-apk" + ) + } + catch (e: InvocationTargetException) { + throw java.lang.UnsupportedOperationException( + "InputManagerEventInjector is not supported in this device! " + + "Please submit your deviceInfo to https://github.com/SonicCloudOrg/sonic-android-apk" + ) + } + catch (e: IllegalAccessException) { + throw java.lang.UnsupportedOperationException( + "InputManagerEventInjector is not supported in this device! " + + "Please submit your deviceInfo to https://github.com/SonicCloudOrg/sonic-android-apk" + ) + } + } + private fun getInstance(className: String): Any { + val aClass = Class.forName(className) + val getInstance = aClass.getMethod("getInstance") + return getInstance.invoke(null) + } + override fun injectInputEvent(event: InputEvent?): Boolean { + return try { + injector!!.invoke( + inputManager, + event, + injectMode + ) + true + } catch (e: IllegalAccessException) { + e.printStackTrace() + false + } catch (e: InvocationTargetException) { + e.printStackTrace() + false + } + } + } + class InputManagerEventInjector : EventInjector { private val injectMode = 0 private var inputManager: Any? = null diff --git a/app/src/main/java/org/cloud/sonic/android/service/SonicManagerServiceV2.kt b/app/src/main/java/org/cloud/sonic/android/service/SonicManagerServiceV2.kt index f8f6045..129b2dc 100644 --- a/app/src/main/java/org/cloud/sonic/android/service/SonicManagerServiceV2.kt +++ b/app/src/main/java/org/cloud/sonic/android/service/SonicManagerServiceV2.kt @@ -20,6 +20,7 @@ package org.cloud.sonic.android.service import android.app.* import android.content.Context import android.content.Intent +import android.content.pm.ServiceInfo import android.graphics.drawable.Icon import android.os.* import androidx.annotation.RequiresApi @@ -78,6 +79,14 @@ class SonicManagerServiceV2 : Service(), TcpServerListener { } } + fun StartForeground(id: Int, notification: Notification) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK) + } else { + startForeground(NOTIFICATION_ID, notification) + } + } + override fun onCreate() { super.onCreate() LogUtils.i("onCreate") @@ -91,15 +100,12 @@ class SonicManagerServiceV2 : Service(), TcpServerListener { NotificationManager.IMPORTANCE_NONE ) getNotificationManager().createNotificationChannel(channel) - startForeground( - NOTIFICATION_ID, - notification - ) + StartForeground(NOTIFICATION_ID, notification) } else { // 获取服务通知 val notification: Notification = createNotification(false) //将服务置于启动状态 ,NOTIFICATION_ID指的是创建的通知的ID - startForeground(NOTIFICATION_ID, notification) + StartForeground(NOTIFICATION_ID, notification) } } diff --git a/build.gradle b/build.gradle index 389d653..408499b 100644 --- a/build.gradle +++ b/build.gradle @@ -10,8 +10,8 @@ buildscript { plugins { - id 'com.android.application' version '7.1.2' apply false - id 'com.android.library' version '7.1.2' apply false + id 'com.android.application' version '8.2.2' apply false + id 'com.android.library' version '8.2.2' apply false id 'org.jetbrains.kotlin.android' version '1.6.10' apply false } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index aa991fc..17655d0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/lib_socketmanager/build.gradle b/lib_socketmanager/build.gradle index 588a87b..ea3278c 100644 --- a/lib_socketmanager/build.gradle +++ b/lib_socketmanager/build.gradle @@ -4,11 +4,12 @@ plugins { } android { - compileSdk 32 + compileSdk 34 + namespace "org.cloud.sonic.android.lib.socketmanager" defaultConfig { minSdk 21 - targetSdk 32 + targetSdk 34 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" diff --git a/lib_socketmanager/src/main/AndroidManifest.xml b/lib_socketmanager/src/main/AndroidManifest.xml index 8a3e213..8bdb7e1 100644 --- a/lib_socketmanager/src/main/AndroidManifest.xml +++ b/lib_socketmanager/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - + From 3451e5c1aac914f4f22c21e92e8c65b265d954fd Mon Sep 17 00:00:00 2001 From: hazmi-e205 Date: Thu, 22 Feb 2024 14:59:07 +0700 Subject: [PATCH 2/3] - Update to OpenJDK 17 in CI --- .github/workflows/android.yml | 4 ++-- .github/workflows/deploy.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 516b83e..5cf94d2 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -9,10 +9,10 @@ jobs: steps: - uses: actions/checkout@v2 - - name: set up JDK 11 + - name: set up JDK 17 uses: actions/setup-java@v2 with: - java-version: '11' + java-version: '17' distribution: 'temurin' cache: gradle diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 88c34e9..c8b9fb8 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -26,10 +26,10 @@ jobs: uses: "WyriHaximus/github-action-get-previous-tag@v1" - name: replace version run: ver=${{ steps.previoustag.outputs.tag }} && new_ver=${ver:1} && sed -i "s/SONIC_VERSION/${new_ver}/g" app/build.gradle - - name: set up JDK 11 + - name: set up JDK 17 uses: actions/setup-java@v2 with: - java-version: '11' + java-version: '17' distribution: 'temurin' cache: gradle - name: Grant execute permission for gradlew From f6a4e6fcdbaadbf3c19d99de777a284ce7771af0 Mon Sep 17 00:00:00 2001 From: hazmi-e205 Date: Thu, 22 Feb 2024 15:36:28 +0700 Subject: [PATCH 3/3] * fix throw something else on older android --- .../touchPlugin/touchCompat/InputManagerWrapper.kt | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt b/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt index 5149037..0c6f54e 100644 --- a/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt +++ b/app/src/main/java/org/cloud/sonic/android/plugin/touchPlugin/touchCompat/InputManagerWrapper.kt @@ -69,19 +69,7 @@ class InputManagerWrapper { Int::class.javaPrimitiveType ); } - catch (e: NoSuchMethodException) { - throw java.lang.UnsupportedOperationException( - "InputManagerEventInjector is not supported in this device! " + - "Please submit your deviceInfo to https://github.com/SonicCloudOrg/sonic-android-apk" - ) - } - catch (e: InvocationTargetException) { - throw java.lang.UnsupportedOperationException( - "InputManagerEventInjector is not supported in this device! " + - "Please submit your deviceInfo to https://github.com/SonicCloudOrg/sonic-android-apk" - ) - } - catch (e: IllegalAccessException) { + catch (e: Exception) { throw java.lang.UnsupportedOperationException( "InputManagerEventInjector is not supported in this device! " + "Please submit your deviceInfo to https://github.com/SonicCloudOrg/sonic-android-apk"