android_device_xiaomi_sdm66.../CloverParts/src/io/alcatraz/cloverparts/SharedPreferenceUtil.java
Alcatraz323 9ab5292b0b sdm660-common: introduce my simple battery management system
* settings page located in Settings -> Battery
* soc-based step charger, jeita thermal charger switch (also done something in kernel)
* add a mode for user always connected with chagrging cable to limit battery around 40% - 60%
* add a switch for user to limit max charge at around 80%

Signed-off-by: pix106 <sbordenave@gmail.com>
2023-08-22 08:45:13 +02:00

74 lines
2.9 KiB
Java

/*
* Copyright (c) 2021, Alcatraz323 <alcatraz32323@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/
package io.alcatraz.cloverparts;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.Nullable;
public final class SharedPreferenceUtil {
private static final String FILE_NAME = "io.alcatraz.cloverparts_preferences";
private static SharedPreferenceUtil mInstance;
public static SharedPreferenceUtil getInstance() {
if (mInstance == null) {
synchronized (SharedPreferenceUtil.class) {
if (mInstance == null) {
mInstance = new SharedPreferenceUtil();
}
}
}
return mInstance;
}
public boolean put(Context context, String key, Object value) {
String type = value.getClass().getSimpleName();
SharedPreferences sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if ("Integer".equals(type)) {
editor.putInt(key, (Integer) value);
} else if ("Boolean".equals(type)) {
editor.putBoolean(key, (Boolean) value);
} else if ("Float".equals(type)) {
editor.putFloat(key, (Float) value);
} else if ("Long".equals(type)) {
editor.putLong(key, (Long) value);
} else if ("String".equals(type)) {
editor.putString(key, (String) value);
}
editor.apply();
return false;
}
@Nullable
public Object get(Context context, String key, Object defValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
String type = defValue.getClass().getSimpleName();
if ("Integer".equals(type)) {
return sharedPreferences.getInt(key, (Integer) defValue);
} else if ("Boolean".equals(type)) {
return sharedPreferences.getBoolean(key, (Boolean) defValue);
} else if ("Float".equals(type)) {
return sharedPreferences.getFloat(key, (Float) defValue);
} else if ("Long".equals(type)) {
return sharedPreferences.getLong(key, (Long) defValue);
} else if ("String".equals(type)) {
return sharedPreferences.getString(key, (String) defValue);
}
return null;
}
}