universal7885: parts-aidl: Just send callback through setSwapOn()

- Set callback on JNI, since Java class cant be sent through, use the ndk backend to implement callback and send to swapon()
This commit is contained in:
roynatech2544 2022-10-12 01:54:08 +09:00
commit ef97fdeb77
18 changed files with 209 additions and 30 deletions

View file

@ -5,7 +5,10 @@ android_app {
],
platform_apis: true,
certificate: "platform",
jni_libs: ["libSwapStorageHelper"],
jni_libs: [
"libStorageHelper",
"libSwapCallback",
],
static_libs: [
"androidx.preference_preference",
"vendor.eureka.hardware.parts-V1-java",

View file

@ -1,5 +1,5 @@
cc_library_shared {
name: "libSwapStorageHelper",
name: "libStorageHelper",
defaults: ["eureka_defaults"],
cflags: [
"-Wall",
@ -7,7 +7,22 @@ cc_library_shared {
"-Wno-unused-parameter",
"-Werror",
],
srcs: ["*.cpp"],
srcs: ["Storage.cpp"],
header_libs: ["jni_headers"],
static_libs: ["libc++fs"],
}
cc_library_shared {
name: "libSwapCallback",
defaults: ["eureka_defaults"],
srcs: [
"SwapCallbackImpl.cpp",
"SwapCallbackNative.cpp",
],
header_libs: ["jni_headers"],
shared_libs: [
"vendor.eureka.hardware.parts-V1-ndk",
"libbase",
"libbinder_ndk",
],
}

View file

@ -0,0 +1,21 @@
// Copyright (C) 2021 Eureka Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "SwapCallbackImpl.h"
::ndk::ScopedAStatus SwapCallbackImpl::respondToBool(bool result) {
jmethodID mid_callback = env->GetMethodID(cls, "reactToCallbackNative", "()V");
env->CallBooleanMethod(cls, mid_callback, result);
return ::ndk::ScopedAStatus::ok();
}

View file

@ -0,0 +1,36 @@
// Copyright (C) 2021 Eureka Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <aidl/vendor/eureka/hardware/parts/BnBoolCallback.h>
#include <jni.h>
using namespace aidl::vendor::eureka::hardware::parts;
struct SwapCallbackImpl : public BnBoolCallback {
public:
SwapCallbackImpl(JNIEnv *_env, jclass _cls) {
env = _env;
cls = _cls;
};
// Methods from aidl::vendor::eureka::hardware::parts::IBoolCallback follow.
::ndk::ScopedAStatus respondToBool(bool result) override;
private:
JNIEnv *env;
jclass cls;
};

View file

@ -0,0 +1,12 @@
#include "SwapCallbackImpl.h"
#include <android/binder_manager.h>
#include <aidl/vendor/eureka/hardware/parts/BnSwapOnData.h>
extern "C" JNIEXPORT void JNICALL
Java_com_eurekateam_samsungextras_interfaces_Swap_setSwapOn
(JNIEnv *env, jclass clazz, jboolean cb) {
auto svc = ISwapOnData::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService("vendor.eureka.hardware.parts.ISwapOnData/default")));
svc->setSwapOn(cb ? ndk::SharedRefBase::make<SwapCallbackImpl>(env, clazz) : nullptr);
}

View file

@ -19,8 +19,9 @@ class BootReceiver : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
val mSharedPreferences = p0?.let { PreferenceManager.getDefaultSharedPreferences(it) }
if (p1 != null && mSharedPreferences != null) {
if (p1.action == Intent.ACTION_BOOT_COMPLETED) {
System.loadLibrary("SwapStorageHelper")
if (p1.action == Intent.ACTION_LOCKED_BOOT_COMPLETED) {
System.loadLibrary("StorageHelper")
System.loadLibrary("SwapCallback")
// Battery
val mBattery = Battery()
mBattery.Charge = mSharedPreferences.getBoolean(BatteryFragment.PREF_CHARGE, true)
@ -41,7 +42,8 @@ class BootReceiver : BroadcastReceiver() {
// ZRAM
val mSwap = Swap()
mSwap.setSwapOn(mSharedPreferences.getBoolean(SwapFragment.PREF_SWAP_ENABLE, false))
if (mSharedPreferences.getBoolean(SwapFragment.PREF_SWAP_ENABLE, false))
mSwap.setSwapOn(false) else mSwap.setSwapOff()
// Display
val mDisplay = Display()

View file

@ -33,7 +33,8 @@ class DeviceSettings : PreferenceFragmentCompat(), Preference.OnPreferenceChange
private lateinit var mPrefs: SharedPreferences
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
System.loadLibrary("SwapStorageHelper")
System.loadLibrary("StorageHelper")
System.loadLibrary("SwapCallback")
setPreferencesFromResource(R.xml.preferences_samsung_parts, rootKey)
mPrefs = PreferenceManager.getDefaultSharedPreferences(requireContext())
val mClearSpeakerPref = findPreference<Preference>(PREF_CLEAR_SPEAKER)!!

View file

@ -17,6 +17,7 @@
package com.eurekateam.samsungextras.interfaces
import vendor.eureka.hardware.parts.ISwapOnData
import vendor.eureka.hardware.parts.IBoolCallback
import android.os.ServiceManager
@ -26,7 +27,8 @@ class Swap {
mSwap = ISwapOnData.Stub.asInterface(ServiceManager.waitForDeclaredService("vendor.eureka.hardware.parts.ISwapOnData/default"))
}
fun setSwapOn(mEnabled: Boolean) = if (mEnabled) mSwap.setSwapOn() else mSwap.setSwapOff()
external fun setSwapOn(mCallBackEnabled: Boolean)
fun setSwapOff() = mSwap.setSwapOff()
fun mkFile(mSize: Int) = mSwap.makeSwapFile(mSize)
fun delFile() = mSwap.removeSwapFile()
fun isLocked() : Boolean = mSwap.isMutexLocked()

View file

@ -31,6 +31,7 @@ import com.eurekateam.samsungextras.interfaces.Swap
import java.lang.Thread
import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.TimeUnit
import vendor.eureka.hardware.parts.IBoolCallback
class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener, OnMainSwitchChangeListener {
private lateinit var mSwapSizePref: SeekBarPreference
@ -77,11 +78,26 @@ class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeLi
return false
}
// This is called from native - DO NOT CHANGE SIGNATURE
fun reactToCallbackNative(res : Boolean) {
if (!res) {
mSwap.delFile()
mSharedPreferences.edit().putBoolean(PREF_SWAP_ENABLE, false).apply()
mSwapSizePref.isEnabled = true
}
mSwapFileSize.summary = "${mSwap.getSwapSize()} MB"
}
override fun onSwitchChanged(switchView: Switch, isChecked: Boolean) {
mSwapEnable.isEnabled = false
val mSwap = Swap()
if (isChecked) mSwap.mkFile(mSwapSize) else mSwap.delFile()
mSwap.setSwapOn(isChecked)
if (isChecked) {
mSwap.mkFile(mSwapSize)
mSwap.setSwapOn(true)
} else {
mSwap.setSwapOff()
mSwap.delFile()
}
mSwapEnable.isEnabled = true
mSharedPreferences.edit().putBoolean(PREF_SWAP_ENABLE, isChecked).apply()
mSwapSizePref.isEnabled = !isChecked