universal7885: parts: Implement SmartCharge settings

Using the interface vendor.eureka.hardware.parts.ISmartCharge, this intends to add simple implementation of smart charge. If the battery percent is above set limit percent, it will stop charging, else if the battery percents goes under the set restart percent, it will start charging again.

While I'm on this parts, I refactored kotlin srcs using ktlint, formatted xml manually.
This commit is contained in:
roynatech2544 2022-10-13 16:04:00 +09:00
commit 8d1811d574
43 changed files with 779 additions and 568 deletions

View file

@ -48,16 +48,18 @@ class FMRadioService : Service() {
ACTION_BEFORE -> {
mPlayState = PlayState.STATE_PLAYING
Log.i("mCurrentIndex $mIndex")
if (mIndex > 0)
if (mIndex > 0) {
mIndex -= 1
}
mNativeFMInterface.setFMFreq(fd, mTracks[mIndex].toInt())
MainFragment.mFreqCurrent = mTracks[mIndex].toInt()
}
ACTION_NEXT -> {
mPlayState = PlayState.STATE_PLAYING
Log.i("mCurrentIndex $mIndex")
if (mIndex < mTracks.size - 1)
if (mIndex < mTracks.size - 1) {
mIndex += 1
}
mNativeFMInterface.setFMFreq(fd, mTracks[mIndex].toInt())
MainFragment.mFreqCurrent = mTracks[mIndex].toInt()
}
@ -118,7 +120,8 @@ class FMRadioService : Service() {
private fun pushNotification(): Notification {
val nm = mContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(
mContext.packageName, "FM Radio Playing",
mContext.packageName,
"FM Radio Playing",
NotificationManager.IMPORTANCE_HIGH
)
nm.createNotificationChannel(channel)
@ -146,7 +149,8 @@ class FMRadioService : Service() {
Icon.createWithResource(this, R.drawable.ic_headphones)
}
},
"Output Configuration", output
"Output Configuration",
output
).build()
val mPausePlayAction: Notification.Action = Notification.Action.Builder(
when (mPlayState) {
@ -157,20 +161,24 @@ class FMRadioService : Service() {
Icon.createWithResource(this, R.drawable.ic_play)
}
},
"Start/Stop", togglePlay
"Start/Stop",
togglePlay
).build()
val mRewindAction: Notification.Action = Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_rewind),
"Rewind", rewind
"Rewind",
rewind
).build()
val mForwardAction: Notification.Action = Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_forward),
"Forward", forward
"Forward",
forward
).build()
val mCloseAction: Notification.Action = Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_close),
"Close", close
"Close",
close
).build()
builder.addAction(mOutputAction)
builder.addAction(mRewindAction)

View file

@ -1,15 +1,14 @@
package com.eurekateam.fmradio
import vendor.eureka.hardware.fmradio.IFMDevControl
import android.os.ServiceManager
import vendor.eureka.hardware.fmradio.GetType
import vendor.eureka.hardware.fmradio.IFMDevControl
import vendor.eureka.hardware.fmradio.SetType
import android.os.ServiceManager
class NativeFMInterface {
private val mDevCtl : IFMDevControl
private val mSysfsCtl : IFMDevControl
private val mDefaultCtl : IFMDevControl
private val mDevCtl: IFMDevControl
private val mSysfsCtl: IFMDevControl
private val mDefaultCtl: IFMDevControl
init {
mDevCtl = IFMDevControl.Stub.asInterface(ServiceManager.waitForDeclaredService("vendor.eureka.hardware.fmradio.IFMDevControl/default"))
@ -26,7 +25,7 @@ class NativeFMInterface {
fun setFMFreq(a: Int, freq: Int) = mDevCtl.setValue(SetType.SET_TYPE_FM_FREQ, freq)
fun setFMVolume(a: Int, volume: Int) = mDevCtl.setValue(SetType.SET_TYPE_FM_VOLUME, volume)
fun setFMMute(a: Int, mute: Boolean) = mDevCtl.setValue(SetType.SET_TYPE_FM_MUTE, if (mute) 1 else 0)
fun getFmUpper(a: Int) : Int = mDevCtl.getValue(GetType.GET_TYPE_FM_UPPER_LIMIT)
fun getFmUpper(a: Int): Int = mDevCtl.getValue(GetType.GET_TYPE_FM_UPPER_LIMIT)
fun getFMLower(a: Int): Int = mDevCtl.getValue(GetType.GET_TYPE_FM_LOWER_LIMIT)
fun getRMSSI(a: Int): Int = mDevCtl.getValue(GetType.GET_TYPE_FM_RMSSI)
fun getFMTracks(fd: Int): IntArray = mDefaultCtl.getFreqsList()
@ -37,5 +36,5 @@ class NativeFMInterface {
fun setFMRSSI(a: Int, rssi: Long) = mDevCtl.setValue(SetType.SET_TYPE_FM_RMSSI, rssi.toInt())
fun closeFMDevice(fd: Int) = mDevCtl.close()
fun getSysfsSupport(): Boolean = mSysfsCtl.getValue(GetType.GET_TYPE_FM_SYSFS_IF) == 0
fun setAudioRoute(speaker: Boolean) = mDevCtl.setValue(SetType.SET_TYPE_FM_SPEAKER_ROUTE, if (speaker) 1 else 0)
fun setAudioRoute(speaker: Boolean) = mDevCtl.setValue(SetType.SET_TYPE_FM_SPEAKER_ROUTE, if (speaker) 1 else 0)
}

View file

@ -45,7 +45,6 @@ class PebbleTextView(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
desiredWidth: Float,
text: String
) {
// Pick a reasonably large value for the test. Larger values produce
// more accurate results, but may cause problems with hardware
// acceleration. But there are workarounds for that, too; refer to

View file

@ -32,7 +32,9 @@ class ListViewAdapter(private val mContext: Context) : BaseAdapter() {
override fun getView(id: Int, mConvertView: View?, parent: ViewGroup?): View {
val mAnotherConvertView = mConvertView
?: (mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(
R.layout.channel_list_items, parent, false
R.layout.channel_list_items,
parent,
false
)
mAnotherConvertView.findViewById<MaterialTextView>(R.id.channel_list_title).text =
String.format(
@ -44,7 +46,8 @@ class ListViewAdapter(private val mContext: Context) : BaseAdapter() {
MainFragment.mFreqCurrent = MainFragment.mTracks[id].toInt()
FileUtilities.writeToFile(
FileUtilities.mFMFreqFileName,
MainFragment.mFreqCurrent.toString(), mContext
MainFragment.mFreqCurrent.toString(),
mContext
)
setCurrentFMChannel(id)
}
@ -52,7 +55,8 @@ class ListViewAdapter(private val mContext: Context) : BaseAdapter() {
val mStar = ResourcesCompat.getDrawable(mContext.resources, R.drawable.ic_star, mContext.theme)
val mStarFilled = ResourcesCompat.getDrawable(
mContext.resources,
R.drawable.ic_star_filled, mContext.theme
R.drawable.ic_star_filled,
mContext.theme
)
val mIndex = MainFragment.mTracks[id].toInt()
if (MainFragment.mFavStats[mIndex] == null) {
@ -77,14 +81,16 @@ class ListViewAdapter(private val mContext: Context) : BaseAdapter() {
mItem.value.setBackgroundColor(
ResourcesCompat.getColor(
mContext.resources,
android.R.color.system_accent2_100, mContext.theme
android.R.color.system_accent2_100,
mContext.theme
)
)
}
mListofViews[mPosition]?.setBackgroundColor(
ResourcesCompat.getColor(
mContext.resources,
android.R.color.system_accent3_400, mContext.theme
android.R.color.system_accent3_400,
mContext.theme
)
)
}

View file

@ -43,12 +43,15 @@ class PebbleLayoutAdapter(private val mContext: Context) : BaseAdapter() {
override fun getView(id: Int, mConvertView: View?, parent: ViewGroup?): View {
val mAnotherConvertView = mConvertView
?: (mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(
R.layout.favorite_channel_items, parent, false
R.layout.favorite_channel_items,
parent,
false
)
mAnotherConvertView.findViewById<PebbleTextView>(R.id.pebble_textview).apply {
mText = (mFavoriteList[id].toFloat() / 1000).toString()
mColor = ResourcesCompat.getColor(
mContext.resources, android.R.color.system_accent1_400,
mContext.resources,
android.R.color.system_accent1_400,
mContext.theme
)
setOnClickListener {
@ -56,7 +59,8 @@ class PebbleLayoutAdapter(private val mContext: Context) : BaseAdapter() {
MainFragment.mFreqCurrent = mFavoriteList[id]
FileUtilities.writeToFile(
FileUtilities.mFMFreqFileName,
MainFragment.mFreqCurrent.toString(), mContext
MainFragment.mFreqCurrent.toString(),
mContext
)
}
}

View file

@ -7,5 +7,5 @@ package com.eurekateam.fmradio.enums
*/
enum class HeadsetState {
HEADSET_STATE_CONNECTED,
HEADSET_STATE_DISCONNECTED,
HEADSET_STATE_DISCONNECTED
}

View file

@ -41,10 +41,11 @@ class ChannelListFragment :
Configuration.UI_MODE_NIGHT_UNDEFINED -> mIsLight = true
}
mRootView.findViewById<FrameLayout>(R.id.channel_list_layout).apply {
if (mIsLight)
if (mIsLight) {
setBackgroundColor(resources.getColor(android.R.color.system_accent1_50, requireContext().theme))
else
} else {
setBackgroundColor(resources.getColor(android.R.color.system_accent1_100, requireContext().theme))
}
}
return mRootView
}

View file

@ -28,10 +28,11 @@ class FavouriteFragment : Fragment(R.layout.fragment_fav_list) {
Configuration.UI_MODE_NIGHT_UNDEFINED -> mIsLight = true
}
mRootView.findViewById<GridView>(R.id.fav_list_grid).apply {
if (mIsLight)
if (mIsLight) {
setBackgroundColor(resources.getColor(android.R.color.system_accent1_50, requireContext().theme))
else
} else {
setBackgroundColor(resources.getColor(android.R.color.system_accent1_100, requireContext().theme))
}
}
return mRootView
}

View file

@ -57,11 +57,13 @@ class MainFragment :
super.onCreate(savedInstanceState)
mStar = ResourcesCompat.getDrawable(
requireContext().resources,
R.drawable.ic_star, requireContext().theme
R.drawable.ic_star,
requireContext().theme
)!!
mStarFilled = ResourcesCompat.getDrawable(
requireContext().resources,
R.drawable.ic_star_filled, requireContext().theme
R.drawable.ic_star_filled,
requireContext().theme
)!!
mAudioManager = requireContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager
mVolumeUp = mRootView.findViewById(R.id.volume_up)
@ -92,27 +94,31 @@ class MainFragment :
val mTextViewList = listOf(R.id.app_banner, R.id.fm_freq, R.id.freq_misc)
for (mResID in mTextViewList) {
mRootView.findViewById<MaterialTextView>(mResID).apply {
if (mIsLight)
if (mIsLight) {
setTextColor(resources.getColor(android.R.color.system_accent2_500, requireContext().theme))
else
} else {
setTextColor(resources.getColor(android.R.color.system_accent2_100, requireContext().theme))
}
}
}
mRootView.findViewById<FrameLayout>(R.id.main_fragment).apply {
if (mIsLight)
if (mIsLight) {
setBackgroundColor(resources.getColor(android.R.color.system_accent1_100, requireContext().theme))
else
} else {
setBackgroundColor(resources.getColor(android.R.color.system_accent1_700, requireContext().theme))
}
}
GlobalScope.launch {
withContext(Dispatchers.IO) {
if (FileUtilities.checkIfExistFile(FileUtilities.mFavouriteChannelFileName, requireContext())) {
val mFavData = FileUtilities.readFromFile(
FileUtilities.mFavouriteChannelFileName, requireContext()
FileUtilities.mFavouriteChannelFileName,
requireContext()
)
for (mItem in mFavData.split("\\r?\\n".toRegex())) {
if (mItem.isNotBlank())
if (mItem.isNotBlank()) {
mFavStats[mItem.toInt()] = true
}
}
}
var mMute = false
@ -157,8 +163,9 @@ class MainFragment :
}
}
}
if (!mMute)
if (!mMute) {
mFMInterface.setFMMute(fd, true)
}
mFMInterface.setFMFreq(fd, mFMInterface.getFMLower(fd))
mRefreshTracks()
if (mFreqCurrent != -1) {
@ -171,10 +178,12 @@ class MainFragment :
withContext(Dispatchers.Main) {
mFMFreq.text = mCleanFormat.format(mFreqCurrent.toFloat() / 1000)
}
if (!mMute)
if (!mMute) {
mFMInterface.setFMMute(fd, false)
if (mFreqCurrent == -1)
}
if (mFreqCurrent == -1) {
mFMInterface.setFMThread(fd, true)
}
withContext(Dispatchers.Main) {
mFavButton.let {
if (mFavStats[mFreqCurrent] == null) {
@ -201,6 +210,7 @@ class MainFragment :
alpha = .7f
if (!mTextView) isEnabled = false
}
/**
* Extension function for [View], for reverting Grayed out, disabled View
* @param mTextView Whether the target view is touchable [FloatingActionButton] or
@ -222,20 +232,25 @@ class MainFragment :
private fun mUpdateEnableDisable(mEnabled: Boolean, mView: View = requireView()) {
val mTextViewList = listOf(R.id.fm_freq, R.id.freq_misc)
val mFloatButtonList = listOf(
R.id.volume_down, R.id.volume_up,
R.id.before_channel, R.id.next_channel, R.id.fm_output_btn
R.id.volume_down,
R.id.volume_up,
R.id.before_channel,
R.id.next_channel,
R.id.fm_output_btn
)
for (i in mTextViewList) {
if (mEnabled)
if (mEnabled) {
mView.findViewById<View>(i).enable(true)
else
} else {
mView.findViewById<View>(i).disable(true)
}
}
for (i in mFloatButtonList) {
if (mEnabled)
if (mEnabled) {
mView.findViewById<View>(i).enable()
else
} else {
mView.findViewById<View>(i).disable()
}
}
}
@ -268,25 +283,29 @@ class MainFragment :
}
}
mVolumeUp.id -> {
if (mVolume < 15)
if (mVolume < 15) {
mVolume += 1
}
mFMInterface.setFMVolume(fd, mVolume)
mSeekBar.progress = mVolume
Toast.makeText(requireContext(), "Volume set to $mVolume", Toast.LENGTH_SHORT).show()
FileUtilities.writeToFile(
FileUtilities.mFMVolumeFileName,
mVolume.toString(), requireContext()
mVolume.toString(),
requireContext()
)
}
mVolumeDown.id -> {
if (mVolume > 0)
if (mVolume > 0) {
mVolume -= 1
}
mFMInterface.setFMVolume(fd, mVolume)
mSeekBar.progress = mVolume
Toast.makeText(requireContext(), "Volume set to $mVolume", Toast.LENGTH_SHORT).show()
FileUtilities.writeToFile(
FileUtilities.mFMVolumeFileName,
mVolume.toString(), requireContext()
mVolume.toString(),
requireContext()
)
}
mBeforeChannelBtn.id -> {
@ -304,7 +323,8 @@ class MainFragment :
mFMInterface.setFMMute(fd, false)
FileUtilities.writeToFile(
FileUtilities.mFMFreqFileName,
mFreqCurrent.toString(), requireContext()
mFreqCurrent.toString(),
requireContext()
)
mFavButton.let {
if (mFavStats[mFreqCurrent] == null) {
@ -343,7 +363,8 @@ class MainFragment :
mFMInterface.setFMMute(fd, false)
FileUtilities.writeToFile(
FileUtilities.mFMFreqFileName,
mFreqCurrent.toString(), requireContext()
mFreqCurrent.toString(),
requireContext()
)
mFavButton.let {
if (mFavStats[mFreqCurrent] == null) {