Add battery level handler
This commit is contained in:
parent
b81461f725
commit
233761a169
@ -116,6 +116,8 @@
|
||||
|
||||
<service android:name=".services.EventListenerJob" android:permission="android.permission.BIND_JOB_SERVICE" />
|
||||
|
||||
<service android:name=".services.BatteryListenerJob" android:permission="android.permission.BIND_JOB_SERVICE" />
|
||||
|
||||
<service android:name=".receivers.MusicNotificationListener"
|
||||
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
|
||||
<intent-filter>
|
||||
@ -131,6 +133,7 @@
|
||||
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
|
||||
<action android:name="android.intent.action.BATTERY_LOW"/>
|
||||
<action android:name="android.intent.action.BATTERY_OKAY"/>
|
||||
<action android:name="android.intent.action.BATTERY_CHANGED"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
|
@ -91,6 +91,7 @@ object Preferences : KotprefModel() {
|
||||
var showNextAlarm by booleanPref(default = true)
|
||||
var showBatteryCharging by booleanPref(default = false)
|
||||
var isBatteryLevelLow by booleanPref(default = false)
|
||||
var isCharging by booleanPref(default = false)
|
||||
var googleFitSteps by longPref(default = -1)
|
||||
var showDailySteps by booleanPref(default = false)
|
||||
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.tommasoberlose.anotherwidget.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Context.BATTERY_SERVICE
|
||||
import android.os.BatteryManager
|
||||
import androidx.core.content.ContextCompat.getSystemService
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
|
||||
|
||||
object BatteryHelper {
|
||||
fun updateBatteryInfo(context: Context) {
|
||||
with(context.getSystemService(BATTERY_SERVICE) as BatteryManager) {
|
||||
Preferences.isBatteryLevelLow = getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) <= 15
|
||||
Preferences.isCharging = isCharging
|
||||
}
|
||||
}
|
||||
|
||||
fun getBatteryLevel(context: Context): Int {
|
||||
with(context.getSystemService(BATTERY_SERVICE) as BatteryManager) {
|
||||
return getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
|
||||
}
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@ object GlanceProviderHelper {
|
||||
val enabledProviders = Preferences.enabledGlanceProviderOrder.split(",").filter { it != "" }
|
||||
|
||||
val providers = Constants.GlanceProviderId.values()
|
||||
.filter { it != Constants.GlanceProviderId.BATTERY_LEVEL_LOW }
|
||||
.filter {
|
||||
context.checkIfFitInstalled() || it != Constants.GlanceProviderId.GOOGLE_FIT_STEPS
|
||||
}.toTypedArray()
|
||||
@ -80,10 +79,11 @@ object GlanceProviderHelper {
|
||||
|
||||
fun showGlanceProviders(context: Context): Boolean {
|
||||
val eventRepository = EventRepository(context)
|
||||
BatteryHelper.updateBatteryInfo(context)
|
||||
val showGlance = Preferences.showGlance && eventRepository.getEventsCount() == 0 && (
|
||||
(Preferences.showNextAlarm && AlarmHelper.getNextAlarm(context) != "") ||
|
||||
(MediaPlayerHelper.isSomeonePlaying(context)) ||
|
||||
(Preferences.isBatteryLevelLow) ||
|
||||
(Preferences.showBatteryCharging && Preferences.isCharging || Preferences.isBatteryLevelLow) ||
|
||||
(Preferences.customNotes.isNotEmpty()) ||
|
||||
(Preferences.showDailySteps && Preferences.googleFitSteps > 0)
|
||||
)
|
||||
|
@ -182,6 +182,10 @@ object IntentHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun getBatteryIntent(context: Context): Intent {
|
||||
return Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
|
||||
}
|
||||
|
||||
fun getMusicIntent(context: Context): Intent {
|
||||
return when (Preferences.mediaPlayerPackage) {
|
||||
"" -> {
|
||||
|
@ -7,12 +7,15 @@ import android.os.BatteryManager
|
||||
import android.util.Log
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
import com.tommasoberlose.anotherwidget.utils.toast
|
||||
|
||||
class BatteryLevelReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
when(intent.action) {
|
||||
Intent.ACTION_BATTERY_LOW -> Preferences.isBatteryLevelLow = true
|
||||
Intent.ACTION_BATTERY_OKAY -> Preferences.isBatteryLevelLow = false
|
||||
Intent.ACTION_POWER_CONNECTED -> Preferences.isCharging = true
|
||||
Intent.ACTION_POWER_DISCONNECTED -> Preferences.isCharging = false
|
||||
}
|
||||
MainWidget.updateWidget(context)
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import com.tommasoberlose.anotherwidget.db.EventRepository
|
||||
import com.tommasoberlose.anotherwidget.global.Actions
|
||||
import com.tommasoberlose.anotherwidget.global.Constants
|
||||
import com.tommasoberlose.anotherwidget.global.Preferences
|
||||
import com.tommasoberlose.anotherwidget.helpers.BatteryHelper
|
||||
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper
|
||||
import com.tommasoberlose.anotherwidget.models.Event
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
@ -28,7 +29,9 @@ class UpdatesReceiver : BroadcastReceiver() {
|
||||
Intent.ACTION_TIME_CHANGED,
|
||||
Intent.ACTION_TIMEZONE_CHANGED,
|
||||
Intent.ACTION_LOCALE_CHANGED,
|
||||
Actions.ACTION_CALENDAR_UPDATE -> CalendarHelper.updateEventList(context)
|
||||
Actions.ACTION_CALENDAR_UPDATE -> {
|
||||
CalendarHelper.updateEventList(context)
|
||||
}
|
||||
|
||||
"com.sec.android.widgetapp.APPWIDGET_RESIZE",
|
||||
Intent.ACTION_DATE_CHANGED,
|
||||
|
@ -0,0 +1,61 @@
|
||||
package com.tommasoberlose.anotherwidget.services
|
||||
|
||||
import android.app.job.JobInfo
|
||||
import android.app.job.JobParameters
|
||||
import android.app.job.JobScheduler
|
||||
import android.app.job.JobService
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.provider.CalendarContract
|
||||
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
|
||||
class BatteryListenerJob : JobService() {
|
||||
override fun onStartJob(params: JobParameters): Boolean {
|
||||
MainWidget.updateWidget(this)
|
||||
schedule(
|
||||
this
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun onStopJob(params: JobParameters): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val chargingJobId = 1006
|
||||
private const val notChargingJobId = 1007
|
||||
fun schedule(context: Context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
remove(context)
|
||||
val componentName = ComponentName(
|
||||
context,
|
||||
EventListenerJob::class.java
|
||||
)
|
||||
with(context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler) {
|
||||
schedule(
|
||||
JobInfo.Builder(chargingJobId, componentName)
|
||||
.setRequiresCharging(true)
|
||||
.setPersisted(true)
|
||||
.build()
|
||||
)
|
||||
schedule(
|
||||
JobInfo.Builder(notChargingJobId, componentName)
|
||||
.setRequiresCharging(false)
|
||||
.setPersisted(true)
|
||||
.build()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun remove(context: Context) {
|
||||
val js = context.getSystemService(JobScheduler::class.java)
|
||||
js?.cancel(chargingJobId)
|
||||
js?.cancel(notChargingJobId)
|
||||
}
|
||||
}
|
||||
}
|
@ -36,6 +36,7 @@ import com.tommasoberlose.anotherwidget.helpers.BitmapHelper
|
||||
import com.tommasoberlose.anotherwidget.helpers.ColorHelper
|
||||
import com.tommasoberlose.anotherwidget.helpers.ColorHelper.isColorDark
|
||||
import com.tommasoberlose.anotherwidget.ui.activities.MainActivity
|
||||
import com.tommasoberlose.anotherwidget.ui.activities.SupportDevActivity
|
||||
import com.tommasoberlose.anotherwidget.ui.adapters.ViewPagerAdapter
|
||||
import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel
|
||||
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
|
||||
@ -268,8 +269,8 @@ class MainFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList
|
||||
widget_loader?.animate()?.scaleX(0f)?.scaleY(0f)?.alpha(0f)?.setDuration(200L)?.start()
|
||||
bitmap_container?.apply {
|
||||
setImageBitmap(bitmap)
|
||||
// scaleX = 0.9f
|
||||
// scaleY = 0.9f
|
||||
scaleX = 0.9f
|
||||
scaleY = 0.9f
|
||||
}
|
||||
widget?.animate()?.alpha(1f)?.start()
|
||||
}
|
||||
|
@ -291,15 +291,18 @@ class MainWidget : AppWidgetProvider() {
|
||||
}
|
||||
}
|
||||
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {
|
||||
if (Preferences.isBatteryLevelLow) {
|
||||
val alarmIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getClockIntent(context),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, alarmIntent)
|
||||
break@loop
|
||||
if (Preferences.showBatteryCharging) {
|
||||
BatteryHelper.updateBatteryInfo(context)
|
||||
if (Preferences.isCharging || Preferences.isBatteryLevelLow) {
|
||||
val batteryIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
widgetID,
|
||||
IntentHelper.getBatteryIntent(context),
|
||||
0
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.second_row_rect, batteryIntent)
|
||||
break@loop
|
||||
}
|
||||
}
|
||||
}
|
||||
Constants.GlanceProviderId.CUSTOM_INFO -> {
|
||||
@ -562,15 +565,24 @@ class MainWidget : AppWidgetProvider() {
|
||||
}
|
||||
}
|
||||
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {
|
||||
if (Preferences.isBatteryLevelLow) {
|
||||
v.second_row_icon.setImageDrawable(
|
||||
ContextCompat.getDrawable(
|
||||
context,
|
||||
R.drawable.round_battery_charging_full
|
||||
)
|
||||
)
|
||||
v.next_event_date.text = context.getString(R.string.battery_low_warning)
|
||||
break@loop
|
||||
Log.d("ciao", "isChargin: ${Preferences.isCharging} ")
|
||||
if (Preferences.showBatteryCharging) {
|
||||
BatteryHelper.updateBatteryInfo(context)
|
||||
if (Preferences.isCharging) {
|
||||
v.second_row_icon.isVisible = false
|
||||
val batteryLevel = BatteryHelper.getBatteryLevel(context)
|
||||
if (batteryLevel == 100) {
|
||||
v.next_event_date.text = "%s - %d%%".format(context.getString(R.string.charging), batteryLevel)
|
||||
} else {
|
||||
v.next_event_date.text = context.getString(R.string.charging)
|
||||
}
|
||||
break@loop
|
||||
} else if (Preferences.isBatteryLevelLow) {
|
||||
v.second_row_icon.isVisible = false
|
||||
v.next_event_date.text =
|
||||
context.getString(R.string.battery_low_warning)
|
||||
break@loop
|
||||
}
|
||||
}
|
||||
}
|
||||
Constants.GlanceProviderId.CUSTOM_INFO -> {
|
||||
|
@ -298,27 +298,27 @@
|
||||
style="@style/AnotherWidget.Settings.Subtitle"/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/small_clock_warning"
|
||||
android:orientation="vertical">
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingLeft="24dp"
|
||||
android:paddingRight="24dp"
|
||||
android:paddingBottom="32dp"
|
||||
android:duplicateParentState="true"
|
||||
android:textSize="14sp"
|
||||
android:text="@string/clock_warning"
|
||||
android:textColor="@color/colorPrimaryText"
|
||||
android:letterSpacing="0"
|
||||
android:fontFamily="@font/product_sans"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||
app:textAllCaps="false" />
|
||||
android:id="@+id/small_clock_warning"
|
||||
android:orientation="vertical">
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingLeft="24dp"
|
||||
android:paddingRight="24dp"
|
||||
android:paddingBottom="32dp"
|
||||
android:duplicateParentState="true"
|
||||
android:textSize="14sp"
|
||||
android:text="@string/clock_warning"
|
||||
android:textColor="@color/colorPrimaryText"
|
||||
android:letterSpacing="0"
|
||||
android:fontFamily="@font/product_sans"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||
app:textAllCaps="false" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
@ -74,6 +74,56 @@
|
||||
android:id="@+id/calendar_settings"
|
||||
android:alpha="@{isGlanceVisible ? 1f : 0.2f, default=1}"
|
||||
android:orientation="vertical">
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingBottom="16dp"
|
||||
android:paddingLeft="8dp"
|
||||
android:paddingRight="8dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:gravity="center_vertical"
|
||||
android:id="@+id/action_sort_glance_providers"
|
||||
android:orientation="horizontal">
|
||||
<ImageView
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
android:padding="10dp"
|
||||
android:src="@drawable/round_flip_to_front"
|
||||
android:tint="@color/colorPrimaryText"/>
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingLeft="8dp"
|
||||
android:paddingRight="8dp"
|
||||
android:orientation="vertical">
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/AnotherWidget.Settings.Title"
|
||||
android:text="@string/settings_sort_glance_providers_title"/>
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/settings_sort_glance_providers_subtitle"
|
||||
style="@style/AnotherWidget.Settings.Subtitle"/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="16dp"
|
||||
android:text="Providers"
|
||||
android:textAlignment="viewStart"
|
||||
android:paddingLeft="20dp"
|
||||
android:paddingRight="20dp"
|
||||
android:id="@+id/label"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@color/colorAccent"
|
||||
android:textAppearance="@style/AnotherWidget.Settings.Header"
|
||||
app:textAllCaps="false" />
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
@ -234,7 +284,6 @@
|
||||
android:paddingRight="8dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:visibility="gone"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:gravity="center_vertical"
|
||||
android:id="@+id/action_show_low_battery_level_warning"
|
||||
@ -300,43 +349,6 @@
|
||||
style="@style/AnotherWidget.Settings.Subtitle"/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingBottom="16dp"
|
||||
android:paddingLeft="8dp"
|
||||
android:paddingRight="8dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:gravity="center_vertical"
|
||||
android:id="@+id/action_sort_glance_providers"
|
||||
android:orientation="horizontal">
|
||||
<ImageView
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
android:padding="10dp"
|
||||
android:src="@drawable/round_flip_to_front"
|
||||
android:tint="@color/colorPrimaryText"/>
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingLeft="8dp"
|
||||
android:paddingRight="8dp"
|
||||
android:orientation="vertical">
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/AnotherWidget.Settings.Title"
|
||||
android:text="@string/settings_sort_glance_providers_title"/>
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/settings_sort_glance_providers_subtitle"
|
||||
style="@style/AnotherWidget.Settings.Subtitle"/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
@ -118,7 +118,7 @@
|
||||
<string name="show_clock_visible">Clock visibility</string>
|
||||
<string name="show_clock_not_visible">Clock is hidden</string>
|
||||
<string name="settings_clock_text_size_title">Clock text size</string>
|
||||
<string name="settings_show_next_alarm_title">Next clock Alarm</string>
|
||||
<string name="settings_show_next_alarm_title">Next clock alarm</string>
|
||||
<string name="settings_show_next_alarm_subtitle">Show the next clock alarm</string>
|
||||
<string name="title_text_shadow">Text shadow</string>
|
||||
<string name="settings_text_shadow_subtitle_none">None</string>
|
||||
@ -213,14 +213,15 @@
|
||||
<string name="settings_title_integrations">Integrations</string>
|
||||
<string name="label_count_installed_integrations">%d installed integrations</string>
|
||||
<string name="title_show_glance">Show at a glance info</string>
|
||||
<string name="description_show_glance">Show multiple provider data when there are no events displayed.</string>
|
||||
<string name="description_show_glance">Show some useful info</string>
|
||||
<string name="settings_show_dividers_title">Show text dividers</string>
|
||||
<string name="settings_sort_glance_providers_title">Data source priority</string>
|
||||
<string name="settings_sort_glance_providers_subtitle">Change the data provider importance</string>
|
||||
<string name="settings_custom_notes_title">Custom notes</string>
|
||||
<string name="settings_low_battery_level_title">Battery level</string>
|
||||
<string name="settings_low_battery_level_title">Battery</string>
|
||||
<string name="settings_daily_steps_title">Daily steps</string>
|
||||
<string name="battery_low_warning">Low battery level</string>
|
||||
<string name="charging">Charging</string>
|
||||
<string name="settings_ampm_indicator_title">Show AM/PM Indicator</string>
|
||||
<string name="daily_steps_counter">%d steps so far</string>
|
||||
<string name="soon">soon</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user