universal7885: parts-aidl: Refactor swap impl

This commit is contained in:
roynatech2544 2022-10-10 22:16:39 +09:00
commit 50f3bfa31a
9 changed files with 48 additions and 24 deletions

View file

@ -41,7 +41,6 @@ class BootReceiver : BroadcastReceiver() {
// ZRAM // ZRAM
val mSwap = Swap() val mSwap = Swap()
mSwap.setSize(mSharedPreferences.getInt(SwapFragment.PREF_SWAP_SIZE, 50))
mSwap.setSwapOn(mSharedPreferences.getBoolean(SwapFragment.PREF_SWAP_ENABLE, false)) mSwap.setSwapOn(mSharedPreferences.getBoolean(SwapFragment.PREF_SWAP_ENABLE, false))
// Display // Display

View file

@ -27,7 +27,8 @@ class Swap {
} }
fun setSwapOn(mEnabled: Boolean) = if (mEnabled) mSwap.setSwapOn() else mSwap.setSwapOff() fun setSwapOn(mEnabled: Boolean) = if (mEnabled) mSwap.setSwapOn() else mSwap.setSwapOff()
fun setSize(mSize: Int) = mSwap.setSwapSize(mSize) fun mkFile(mSize: Int) = mSwap.makeSwapFile(mSize)
fun delFile() = mSwap.removeSwapFile()
external fun getFreeSpace(): Double external fun getFreeSpace(): Double
external fun getSwapSize(): Long external fun getSwapSize(): Long
} }

View file

@ -17,6 +17,8 @@ package com.eurekateam.samsungextras.swap
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Switch import android.widget.Switch
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
@ -34,6 +36,8 @@ class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeLi
private lateinit var mSwapEnable: MainSwitchPreference private lateinit var mSwapEnable: MainSwitchPreference
private lateinit var mFreeSpace: Preference private lateinit var mFreeSpace: Preference
private lateinit var mSwapFileSize: Preference private lateinit var mSwapFileSize: Preference
private var mSwapSize = 0
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val mSwap = Swap() val mSwap = Swap()
addPreferencesFromResource(R.xml.swap_settings) addPreferencesFromResource(R.xml.swap_settings)
@ -57,10 +61,12 @@ class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeLi
val mSwap = Swap() val mSwap = Swap()
if (preference == mSwapSizePref) { if (preference == mSwapSizePref) {
val value = newValue as Int val value = newValue as Int
mSwap.setSize(value) mSwapSize = value
mSharedPreferences.edit().putInt(PREF_SWAP_SIZE, value).apply() mSharedPreferences.edit().putInt(PREF_SWAP_SIZE, value).apply()
mFreeSpace.summary = "${mSwap.getFreeSpace()} GB" mFreeSpace.summary = "${mSwap.getFreeSpace()} GB"
mSwapFileSize.summary = "${mSwap.getSwapSize()} MB" val mHandler = Handler(Looper.getMainLooper())
mSwapFileSize.summary = "${mSwapSize * 10} MB applied on next enable"
mHandler.postDelayed({ mSwapFileSize.summary = "${mSwap.getSwapSize()} MB" }, 1500)
return true return true
} }
return false return false
@ -69,13 +75,15 @@ class SwapFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeLi
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()
mSwap.setSwapOn(isChecked) mSwap.setSwapOn(isChecked)
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
mFreeSpace.summary = "${mSwap.getFreeSpace()} GB" mFreeSpace.summary = "${mSwap.getFreeSpace()} GB"
mSwapFileSize.summary = "${mSwap.getSwapSize()} MB" mSwapFileSize.summary = "${mSwap.getSwapSize()} MB"
} }
companion object { companion object {
const val PREF_SWAP_SIZE = "swap_size" const val PREF_SWAP_SIZE = "swap_size"
const val PREF_SWAP_ENABLE = "swap_enable" const val PREF_SWAP_ENABLE = "swap_enable"

View file

@ -1 +1 @@
330aae72d01c9055be476b739e8c01df433f0ea2 d1cebfb943ba21a23e9c502b43d6c2766f6e608d

View file

@ -26,5 +26,5 @@ interface ISwapOnData {
oneway void setSwapOn(); oneway void setSwapOn();
oneway void setSwapSize(in int size); oneway void makeSwapFile(in int size);
} }

View file

@ -37,5 +37,5 @@ interface ISwapOnData {
oneway void removeSwapFile(); oneway void removeSwapFile();
oneway void setSwapOff(); oneway void setSwapOff();
oneway void setSwapOn(); oneway void setSwapOn();
oneway void setSwapSize(in int size); oneway void makeSwapFile(in int size);
} }

View file

@ -20,7 +20,6 @@
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
static int mSwapSize = 100;
extern int mkswap(const char *filename); extern int mkswap(const char *filename);
extern void mkfile(int filesize, const char *name); extern void mkfile(int filesize, const char *name);
@ -32,41 +31,58 @@ static std::mutex thread_lock;
static bool swapOnRes = false; static bool swapOnRes = false;
::ndk::ScopedAStatus SwapOnData::setSwapSize(int32_t size) { static inline bool swapfile_exist(void) {
mSwapSize = size; return access(SWAP_PATH, F_OK) == 0;
}
static void makeFile(int32_t mSwapSize) {
const std::lock_guard<std::mutex> lock(thread_lock);
mkfile(mSwapSize * 10, SWAP_PATH);
mkswap(SWAP_PATH);
}
::ndk::ScopedAStatus SwapOnData::makeSwapFile(int32_t size) {
if (swapfile_exist()) return ::ndk::ScopedAStatus::ok();
std::thread makefile_thread(makeFile, size);
makefile_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
static void rmswap(void) {
const std::lock_guard<std::mutex> lock(thread_lock);
std::remove(SWAP_PATH);
}
::ndk::ScopedAStatus SwapOnData::removeSwapFile(void) { ::ndk::ScopedAStatus SwapOnData::removeSwapFile(void) {
const std::lock_guard<std::mutex> lock(thread_lock); if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok();
std::remove(SWAP_PATH); std::thread rmswap_thread(rmswap);
rmswap_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
static void mkfile_swapon_thread(void) { static void swapon_func(void) {
const std::lock_guard<std::mutex> lock(thread_lock); const std::lock_guard<std::mutex> lock(thread_lock);
if (access(SWAP_PATH, F_OK) != 0) {
mkfile(mSwapSize * 10, SWAP_PATH);
mkswap(SWAP_PATH);
}
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; swapOnRes = res == 0;
} }
::ndk::ScopedAStatus SwapOnData::setSwapOn() { ::ndk::ScopedAStatus SwapOnData::setSwapOn() {
const std::lock_guard<std::mutex> lock(thread_lock); if (!swapfile_exist()) return ::ndk::ScopedAStatus::ok();
std::thread mkswapfile(mkfile_swapon_thread); std::thread swapon_thread(swapon_func);
swapon_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
static void swapoff_thread(void) { static void swapoff_func(void) {
if (!swapfile_exist()) return;
const std::lock_guard<std::mutex> lock(thread_lock); const std::lock_guard<std::mutex> lock(thread_lock);
swapoff(SWAP_PATH); swapoff(SWAP_PATH);
} }
::ndk::ScopedAStatus SwapOnData::setSwapOff() { ::ndk::ScopedAStatus SwapOnData::setSwapOff() {
const std::lock_guard<std::mutex> lock(thread_lock); std::thread swapoff_thread(swapoff_func);
std::thread swapoff(swapoff_thread); swapoff_thread.detach();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }

View file

@ -21,7 +21,7 @@ namespace aidl::vendor::eureka::hardware::parts {
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 setSwapSize(int32_t size); ::ndk::ScopedAStatus makeSwapFile(int32_t size);
::ndk::ScopedAStatus setSwapOn(void); ::ndk::ScopedAStatus setSwapOn(void);
::ndk::ScopedAStatus removeSwapFile(void); ::ndk::ScopedAStatus removeSwapFile(void);
::ndk::ScopedAStatus setSwapOff(void); ::ndk::ScopedAStatus setSwapOff(void);

View file

@ -26,5 +26,5 @@ interface ISwapOnData {
oneway void setSwapOn(); oneway void setSwapOn();
oneway void setSwapSize(in int size); oneway void makeSwapFile(in int size);
} }