Add battery level handler

This commit is contained in:
Tommaso Berlose 2020-05-16 14:34:04 +02:00
parent b81461f725
commit 233761a169
13 changed files with 207 additions and 83 deletions

View File

@ -116,6 +116,8 @@
<service android:name=".services.EventListenerJob" android:permission="android.permission.BIND_JOB_SERVICE" /> <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" <service android:name=".receivers.MusicNotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter> <intent-filter>
@ -131,6 +133,7 @@
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.BATTERY_LOW"/> <action android:name="android.intent.action.BATTERY_LOW"/>
<action android:name="android.intent.action.BATTERY_OKAY"/> <action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
</intent-filter> </intent-filter>
</receiver> </receiver>

View File

@ -91,6 +91,7 @@ object Preferences : KotprefModel() {
var showNextAlarm by booleanPref(default = true) var showNextAlarm by booleanPref(default = true)
var showBatteryCharging by booleanPref(default = false) var showBatteryCharging by booleanPref(default = false)
var isBatteryLevelLow by booleanPref(default = false) var isBatteryLevelLow by booleanPref(default = false)
var isCharging by booleanPref(default = false)
var googleFitSteps by longPref(default = -1) var googleFitSteps by longPref(default = -1)
var showDailySteps by booleanPref(default = false) var showDailySteps by booleanPref(default = false)

View File

@ -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)
}
}
}

View File

@ -14,7 +14,6 @@ object GlanceProviderHelper {
val enabledProviders = Preferences.enabledGlanceProviderOrder.split(",").filter { it != "" } val enabledProviders = Preferences.enabledGlanceProviderOrder.split(",").filter { it != "" }
val providers = Constants.GlanceProviderId.values() val providers = Constants.GlanceProviderId.values()
.filter { it != Constants.GlanceProviderId.BATTERY_LEVEL_LOW }
.filter { .filter {
context.checkIfFitInstalled() || it != Constants.GlanceProviderId.GOOGLE_FIT_STEPS context.checkIfFitInstalled() || it != Constants.GlanceProviderId.GOOGLE_FIT_STEPS
}.toTypedArray() }.toTypedArray()
@ -80,10 +79,11 @@ object GlanceProviderHelper {
fun showGlanceProviders(context: Context): Boolean { fun showGlanceProviders(context: Context): Boolean {
val eventRepository = EventRepository(context) val eventRepository = EventRepository(context)
BatteryHelper.updateBatteryInfo(context)
val showGlance = Preferences.showGlance && eventRepository.getEventsCount() == 0 && ( val showGlance = Preferences.showGlance && eventRepository.getEventsCount() == 0 && (
(Preferences.showNextAlarm && AlarmHelper.getNextAlarm(context) != "") || (Preferences.showNextAlarm && AlarmHelper.getNextAlarm(context) != "") ||
(MediaPlayerHelper.isSomeonePlaying(context)) || (MediaPlayerHelper.isSomeonePlaying(context)) ||
(Preferences.isBatteryLevelLow) || (Preferences.showBatteryCharging && Preferences.isCharging || Preferences.isBatteryLevelLow) ||
(Preferences.customNotes.isNotEmpty()) || (Preferences.customNotes.isNotEmpty()) ||
(Preferences.showDailySteps && Preferences.googleFitSteps > 0) (Preferences.showDailySteps && Preferences.googleFitSteps > 0)
) )

View File

@ -182,6 +182,10 @@ object IntentHelper {
} }
} }
fun getBatteryIntent(context: Context): Intent {
return Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
}
fun getMusicIntent(context: Context): Intent { fun getMusicIntent(context: Context): Intent {
return when (Preferences.mediaPlayerPackage) { return when (Preferences.mediaPlayerPackage) {
"" -> { "" -> {

View File

@ -7,12 +7,15 @@ import android.os.BatteryManager
import android.util.Log import android.util.Log
import com.tommasoberlose.anotherwidget.global.Preferences import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
import com.tommasoberlose.anotherwidget.utils.toast
class BatteryLevelReceiver : BroadcastReceiver() { class BatteryLevelReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
when(intent.action) { when(intent.action) {
Intent.ACTION_BATTERY_LOW -> Preferences.isBatteryLevelLow = true Intent.ACTION_BATTERY_LOW -> Preferences.isBatteryLevelLow = true
Intent.ACTION_BATTERY_OKAY -> Preferences.isBatteryLevelLow = false Intent.ACTION_BATTERY_OKAY -> Preferences.isBatteryLevelLow = false
Intent.ACTION_POWER_CONNECTED -> Preferences.isCharging = true
Intent.ACTION_POWER_DISCONNECTED -> Preferences.isCharging = false
} }
MainWidget.updateWidget(context) MainWidget.updateWidget(context)
} }

View File

@ -12,6 +12,7 @@ import com.tommasoberlose.anotherwidget.db.EventRepository
import com.tommasoberlose.anotherwidget.global.Actions import com.tommasoberlose.anotherwidget.global.Actions
import com.tommasoberlose.anotherwidget.global.Constants import com.tommasoberlose.anotherwidget.global.Constants
import com.tommasoberlose.anotherwidget.global.Preferences import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.helpers.BatteryHelper
import com.tommasoberlose.anotherwidget.helpers.CalendarHelper import com.tommasoberlose.anotherwidget.helpers.CalendarHelper
import com.tommasoberlose.anotherwidget.models.Event import com.tommasoberlose.anotherwidget.models.Event
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget
@ -28,7 +29,9 @@ class UpdatesReceiver : BroadcastReceiver() {
Intent.ACTION_TIME_CHANGED, Intent.ACTION_TIME_CHANGED,
Intent.ACTION_TIMEZONE_CHANGED, Intent.ACTION_TIMEZONE_CHANGED,
Intent.ACTION_LOCALE_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", "com.sec.android.widgetapp.APPWIDGET_RESIZE",
Intent.ACTION_DATE_CHANGED, Intent.ACTION_DATE_CHANGED,

View File

@ -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)
}
}
}

View File

@ -36,6 +36,7 @@ import com.tommasoberlose.anotherwidget.helpers.BitmapHelper
import com.tommasoberlose.anotherwidget.helpers.ColorHelper import com.tommasoberlose.anotherwidget.helpers.ColorHelper
import com.tommasoberlose.anotherwidget.helpers.ColorHelper.isColorDark import com.tommasoberlose.anotherwidget.helpers.ColorHelper.isColorDark
import com.tommasoberlose.anotherwidget.ui.activities.MainActivity 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.adapters.ViewPagerAdapter
import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel import com.tommasoberlose.anotherwidget.ui.viewmodels.MainViewModel
import com.tommasoberlose.anotherwidget.ui.widgets.MainWidget 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() widget_loader?.animate()?.scaleX(0f)?.scaleY(0f)?.alpha(0f)?.setDuration(200L)?.start()
bitmap_container?.apply { bitmap_container?.apply {
setImageBitmap(bitmap) setImageBitmap(bitmap)
// scaleX = 0.9f scaleX = 0.9f
// scaleY = 0.9f scaleY = 0.9f
} }
widget?.animate()?.alpha(1f)?.start() widget?.animate()?.alpha(1f)?.start()
} }

View File

@ -291,15 +291,18 @@ class MainWidget : AppWidgetProvider() {
} }
} }
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {
if (Preferences.isBatteryLevelLow) { if (Preferences.showBatteryCharging) {
val alarmIntent = PendingIntent.getActivity( BatteryHelper.updateBatteryInfo(context)
context, if (Preferences.isCharging || Preferences.isBatteryLevelLow) {
widgetID, val batteryIntent = PendingIntent.getActivity(
IntentHelper.getClockIntent(context), context,
0 widgetID,
) IntentHelper.getBatteryIntent(context),
views.setOnClickPendingIntent(R.id.second_row_rect, alarmIntent) 0
break@loop )
views.setOnClickPendingIntent(R.id.second_row_rect, batteryIntent)
break@loop
}
} }
} }
Constants.GlanceProviderId.CUSTOM_INFO -> { Constants.GlanceProviderId.CUSTOM_INFO -> {
@ -562,15 +565,24 @@ class MainWidget : AppWidgetProvider() {
} }
} }
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {
if (Preferences.isBatteryLevelLow) { Log.d("ciao", "isChargin: ${Preferences.isCharging} ")
v.second_row_icon.setImageDrawable( if (Preferences.showBatteryCharging) {
ContextCompat.getDrawable( BatteryHelper.updateBatteryInfo(context)
context, if (Preferences.isCharging) {
R.drawable.round_battery_charging_full v.second_row_icon.isVisible = false
) val batteryLevel = BatteryHelper.getBatteryLevel(context)
) if (batteryLevel == 100) {
v.next_event_date.text = context.getString(R.string.battery_low_warning) v.next_event_date.text = "%s - %d%%".format(context.getString(R.string.charging), batteryLevel)
break@loop } 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 -> { Constants.GlanceProviderId.CUSTOM_INFO -> {

View File

@ -298,27 +298,27 @@
style="@style/AnotherWidget.Settings.Subtitle"/> style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout> </LinearLayout>
</LinearLayout> </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
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="16dp" android:id="@+id/small_clock_warning"
android:paddingLeft="24dp" android:orientation="vertical">
android:paddingRight="24dp" <androidx.appcompat.widget.AppCompatTextView
android:paddingBottom="32dp" android:layout_width="match_parent"
android:duplicateParentState="true" android:layout_height="wrap_content"
android:textSize="14sp" android:paddingTop="16dp"
android:text="@string/clock_warning" android:paddingLeft="24dp"
android:textColor="@color/colorPrimaryText" android:paddingRight="24dp"
android:letterSpacing="0" android:paddingBottom="32dp"
android:fontFamily="@font/product_sans" android:duplicateParentState="true"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textSize="14sp"
app:textAllCaps="false" /> 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>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View File

@ -74,6 +74,56 @@
android:id="@+id/calendar_settings" android:id="@+id/calendar_settings"
android:alpha="@{isGlanceVisible ? 1f : 0.2f, default=1}" android:alpha="@{isGlanceVisible ? 1f : 0.2f, default=1}"
android:orientation="vertical"> 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 <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -234,7 +284,6 @@
android:paddingRight="8dp" android:paddingRight="8dp"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"
android:visibility="gone"
android:background="?android:attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical" android:gravity="center_vertical"
android:id="@+id/action_show_low_battery_level_warning" android:id="@+id/action_show_low_battery_level_warning"
@ -300,43 +349,6 @@
style="@style/AnotherWidget.Settings.Subtitle"/> style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout> </LinearLayout>
</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>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View File

@ -118,7 +118,7 @@
<string name="show_clock_visible">Clock visibility</string> <string name="show_clock_visible">Clock visibility</string>
<string name="show_clock_not_visible">Clock is hidden</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_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="settings_show_next_alarm_subtitle">Show the next clock alarm</string>
<string name="title_text_shadow">Text shadow</string> <string name="title_text_shadow">Text shadow</string>
<string name="settings_text_shadow_subtitle_none">None</string> <string name="settings_text_shadow_subtitle_none">None</string>
@ -213,14 +213,15 @@
<string name="settings_title_integrations">Integrations</string> <string name="settings_title_integrations">Integrations</string>
<string name="label_count_installed_integrations">%d installed 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="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_show_dividers_title">Show text dividers</string>
<string name="settings_sort_glance_providers_title">Data source priority</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_sort_glance_providers_subtitle">Change the data provider importance</string>
<string name="settings_custom_notes_title">Custom notes</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="settings_daily_steps_title">Daily steps</string>
<string name="battery_low_warning">Low battery level</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="settings_ampm_indicator_title">Show AM/PM Indicator</string>
<string name="daily_steps_counter">%d steps so far</string> <string name="daily_steps_counter">%d steps so far</string>
<string name="soon">soon</string> <string name="soon">soon</string>