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, platform_apis: true,
certificate: "platform", certificate: "platform",
jni_libs: ["libSwapStorageHelper"], jni_libs: [
"libStorageHelper",
"libSwapCallback",
],
static_libs: [ static_libs: [
"androidx.preference_preference", "androidx.preference_preference",
"vendor.eureka.hardware.parts-V1-java", "vendor.eureka.hardware.parts-V1-java",

View file

@ -1,5 +1,5 @@
cc_library_shared { cc_library_shared {
name: "libSwapStorageHelper", name: "libStorageHelper",
defaults: ["eureka_defaults"], defaults: ["eureka_defaults"],
cflags: [ cflags: [
"-Wall", "-Wall",
@ -7,7 +7,22 @@ cc_library_shared {
"-Wno-unused-parameter", "-Wno-unused-parameter",
"-Werror", "-Werror",
], ],
srcs: ["*.cpp"], srcs: ["Storage.cpp"],
header_libs: ["jni_headers"], header_libs: ["jni_headers"],
static_libs: ["libc++fs"], 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?) { override fun onReceive(p0: Context?, p1: Intent?) {
val mSharedPreferences = p0?.let { PreferenceManager.getDefaultSharedPreferences(it) } val mSharedPreferences = p0?.let { PreferenceManager.getDefaultSharedPreferences(it) }
if (p1 != null && mSharedPreferences != null) { if (p1 != null && mSharedPreferences != null) {
if (p1.action == Intent.ACTION_BOOT_COMPLETED) { if (p1.action == Intent.ACTION_LOCKED_BOOT_COMPLETED) {
System.loadLibrary("SwapStorageHelper") System.loadLibrary("StorageHelper")
System.loadLibrary("SwapCallback")
// Battery // Battery
val mBattery = Battery() val mBattery = Battery()
mBattery.Charge = mSharedPreferences.getBoolean(BatteryFragment.PREF_CHARGE, true) mBattery.Charge = mSharedPreferences.getBoolean(BatteryFragment.PREF_CHARGE, true)
@ -41,7 +42,8 @@ class BootReceiver : BroadcastReceiver() {
// ZRAM // ZRAM
val mSwap = Swap() 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 // Display
val mDisplay = Display() val mDisplay = Display()

View file

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

View file

@ -17,6 +17,7 @@
package com.eurekateam.samsungextras.interfaces package com.eurekateam.samsungextras.interfaces
import vendor.eureka.hardware.parts.ISwapOnData import vendor.eureka.hardware.parts.ISwapOnData
import vendor.eureka.hardware.parts.IBoolCallback
import android.os.ServiceManager import android.os.ServiceManager
@ -26,7 +27,8 @@ class Swap {
mSwap = ISwapOnData.Stub.asInterface(ServiceManager.waitForDeclaredService("vendor.eureka.hardware.parts.ISwapOnData/default")) 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 mkFile(mSize: Int) = mSwap.makeSwapFile(mSize)
fun delFile() = mSwap.removeSwapFile() fun delFile() = mSwap.removeSwapFile()
fun isLocked() : Boolean = mSwap.isMutexLocked() fun isLocked() : Boolean = mSwap.isMutexLocked()

View file

@ -31,6 +31,7 @@ import com.eurekateam.samsungextras.interfaces.Swap
import java.lang.Thread import java.lang.Thread
import java.util.concurrent.ScheduledThreadPoolExecutor import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import vendor.eureka.hardware.parts.IBoolCallback
class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener, OnMainSwitchChangeListener { class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener, OnMainSwitchChangeListener {
private lateinit var mSwapSizePref: SeekBarPreference private lateinit var mSwapSizePref: SeekBarPreference
@ -77,11 +78,26 @@ class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeLi
return false 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) { override fun onSwitchChanged(switchView: Switch, isChecked: Boolean) {
mSwapEnable.isEnabled = false mSwapEnable.isEnabled = false
val mSwap = Swap() val mSwap = Swap()
if (isChecked) mSwap.mkFile(mSwapSize) else mSwap.delFile() if (isChecked) {
mSwap.setSwapOn(isChecked) mSwap.mkFile(mSwapSize)
mSwap.setSwapOn(true)
} else {
mSwap.setSwapOff()
mSwap.delFile()
}
mSwapEnable.isEnabled = true mSwapEnable.isEnabled = true
mSharedPreferences.edit().putBoolean(PREF_SWAP_ENABLE, isChecked).apply() mSharedPreferences.edit().putBoolean(PREF_SWAP_ENABLE, isChecked).apply()
mSwapSizePref.isEnabled = !isChecked mSwapSizePref.isEnabled = !isChecked

View file

@ -1 +1 @@
d1cebfb943ba21a23e9c502b43d6c2766f6e608d 48bdd89bd2780de4c4254e0d52a8c4e53e416d00

View file

@ -0,0 +1,20 @@
// Copyright (C) 2022 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.
package vendor.eureka.hardware.parts;
@VintfStability
interface IBoolCallback {
void respondToBool(in boolean result);
}

View file

@ -14,9 +14,10 @@
package vendor.eureka.hardware.parts; package vendor.eureka.hardware.parts;
import vendor.eureka.hardware.parts.IBoolCallback;
@VintfStability @VintfStability
interface ISwapOnData { interface ISwapOnData {
boolean getSwapOnResult();
boolean isMutexLocked(); boolean isMutexLocked();
@ -24,7 +25,7 @@ interface ISwapOnData {
oneway void setSwapOff(); oneway void setSwapOff();
oneway void setSwapOn(); oneway void setSwapOn(in IBoolCallback cb);
oneway void makeSwapFile(in int size); oneway void makeSwapFile(in int size);
} }

View file

@ -0,0 +1,36 @@
// Copyright (C) 2022 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.
///////////////////////////////////////////////////////////////////////////////
// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. //
///////////////////////////////////////////////////////////////////////////////
// This file is a snapshot of an AIDL file. Do not edit it manually. There are
// two cases:
// 1). this is a frozen version file - do not edit this in any case.
// 2). this is a 'current' file. If you make a backwards compatible change to
// the interface (from the latest frozen version), the build system will
// prompt you to update this file with `m <name>-update-api`.
//
// You must not make a backward incompatible change to any AIDL file built
// with the aidl_interface module type with versions property set. The module
// type is used to build AIDL files in a way that they can be used across
// independently updatable components of the system. If a device is shipped
// with such a backward incompatible change, it has a high risk of breaking
// later when a module using the interface is updated, e.g., Mainline modules.
package vendor.eureka.hardware.parts;
@VintfStability
interface IBoolCallback {
void respondToBool(in boolean result);
}

View file

@ -32,10 +32,9 @@
package vendor.eureka.hardware.parts; package vendor.eureka.hardware.parts;
@VintfStability @VintfStability
interface ISwapOnData { interface ISwapOnData {
boolean getSwapOnResult();
boolean isMutexLocked(); boolean isMutexLocked();
oneway void removeSwapFile(); oneway void removeSwapFile();
oneway void setSwapOff(); oneway void setSwapOff();
oneway void setSwapOn(); oneway void setSwapOn(in vendor.eureka.hardware.parts.IBoolCallback cb);
oneway void makeSwapFile(in int size); oneway void makeSwapFile(in int size);
} }

View file

@ -29,8 +29,6 @@ namespace aidl::vendor::eureka::hardware::parts {
static std::mutex thread_lock; static std::mutex thread_lock;
static bool swapOnRes = false;
static inline bool swapfile_exist(void) { static inline bool swapfile_exist(void) {
return access(SWAP_PATH, F_OK) == 0; return access(SWAP_PATH, F_OK) == 0;
} }
@ -61,15 +59,15 @@ static void rmswap(void) {
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
static void swapon_func(void) { static void swapon_func(cb_t cb) {
const std::lock_guard<std::mutex> lock(thread_lock); const std::lock_guard<std::mutex> lock(thread_lock);
int res = swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK); int res = swapon(SWAP_PATH, (10 << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK);
swapOnRes = res == 0; if (cb != nullptr) cb->respondToBool(res == 0);
} }
::ndk::ScopedAStatus SwapOnData::setSwapOn() { ::ndk::ScopedAStatus SwapOnData::setSwapOn(cb_t cb) {
if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok(); if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok();
std::thread swapon_thread(swapon_func); std::thread swapon_thread(swapon_func, cb);
swapon_thread.detach(); swapon_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
@ -86,11 +84,6 @@ static void swapoff_func(void) {
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus SwapOnData::getSwapOnResult(bool *_aidl_return) {
*_aidl_return = swapOnRes;
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus SwapOnData::isMutexLocked(bool *_aidl_return) { ::ndk::ScopedAStatus SwapOnData::isMutexLocked(bool *_aidl_return) {
if (thread_lock.try_lock()) { if (thread_lock.try_lock()) {
thread_lock.unlock(); thread_lock.unlock();

View file

@ -18,14 +18,15 @@
namespace aidl::vendor::eureka::hardware::parts { namespace aidl::vendor::eureka::hardware::parts {
using cb_t = const std::shared_ptr<IBoolCallback>&;
struct SwapOnData : public BnSwapOnData { struct SwapOnData : public BnSwapOnData {
// Methods from ::aidl::vendor::eureka::hardware::parts::ISwapOnData // Methods from ::aidl::vendor::eureka::hardware::parts::ISwapOnData
// follow. // follow.
::ndk::ScopedAStatus makeSwapFile(int32_t size); ::ndk::ScopedAStatus makeSwapFile(int32_t size);
::ndk::ScopedAStatus setSwapOn(void); ::ndk::ScopedAStatus setSwapOn(cb_t cb);
::ndk::ScopedAStatus removeSwapFile(void); ::ndk::ScopedAStatus removeSwapFile(void);
::ndk::ScopedAStatus setSwapOff(void); ::ndk::ScopedAStatus setSwapOff(void);
::ndk::ScopedAStatus getSwapOnResult(bool *_aidl_return);
::ndk::ScopedAStatus isMutexLocked(bool *_aidl_return); ::ndk::ScopedAStatus isMutexLocked(bool *_aidl_return);
}; };
} // namespace vendor::eureka::hardware::parts::V1_0 } // namespace vendor::eureka::hardware::parts::V1_0

View file

@ -0,0 +1,20 @@
// Copyright (C) 2022 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.
package vendor.eureka.hardware.parts;
@VintfStability
interface IBoolCallback {
void respondToBool(in boolean result);
}

View file

@ -14,9 +14,10 @@
package vendor.eureka.hardware.parts; package vendor.eureka.hardware.parts;
import vendor.eureka.hardware.parts.IBoolCallback;
@VintfStability @VintfStability
interface ISwapOnData { interface ISwapOnData {
boolean getSwapOnResult();
boolean isMutexLocked(); boolean isMutexLocked();
@ -24,7 +25,7 @@ interface ISwapOnData {
oneway void setSwapOff(); oneway void setSwapOff();
oneway void setSwapOn(); oneway void setSwapOn(in IBoolCallback cb);
oneway void makeSwapFile(in int size); oneway void makeSwapFile(in int size);
} }