Skip to content

Commit

Permalink
Gradle update + Wear Os always on display
Browse files Browse the repository at this point in the history
Changes:
- gradle updated to 7.0.4
- wear os measurement can use always on screen flag
- comment added to phone app about the battery options on wear os
  • Loading branch information
Foxpace committed Dec 13, 2021
1 parent e54d5dc commit 230f38a
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 45 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
minSdkVersion 19
targetSdkVersion 31
versionCode 63
versionName "4.2.3"
versionName "4.2.4"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -70,14 +70,14 @@ dependencies {
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

implementation 'com.google.android.gms:play-services-maps:18.0.0'
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.google.android.gms:play-services-basement:17.6.0'
implementation 'com.google.android.gms:play-services-maps:18.0.1'
implementation 'com.google.android.gms:play-services-location:19.0.0'
implementation 'com.google.android.gms:play-services-basement:18.0.0'
implementation 'com.google.android.gms:play-services-wearable:17.1.0'
implementation 'androidx.preference:preference-ktx:1.1.1'

implementation 'com.google.firebase:firebase-analytics-ktx:20.0.0'
implementation 'com.google.firebase:firebase-crashlytics-ktx:18.2.5'
implementation 'com.google.firebase:firebase-analytics-ktx:20.0.1'
implementation 'com.google.firebase:firebase-crashlytics-ktx:18.2.6'

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2-native-mt'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ class SettingsFragment : PreferenceFragmentCompat() {
true
}

val wearOsBatterytips: Preference? = findPreference(WEAR_OS_SETTINGS_TIPS)
wearOsBatterytips?.setOnPreferenceClickListener {
dialog = MaterialDialog(this@SettingsFragment.requireContext()).show{
cornerRadius(16f)
title(R.string.settings_wear_os_battery_title)
message(R.string.settings_wear_os_battery_text)
negativeButton(R.string.cancel) {
it.dismiss()
}
}
true
}

// settings for the service - number parameters
for(key in PREFS) {
val pref: EditTextPreference? = findPreference(key)
Expand Down Expand Up @@ -166,6 +179,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
const val STORAGE_DETAILS = "storage_details"
const val ANDROID_SETTINGS = "android_settings"
const val BATTERY_SETTINGS = "battery_settings"
const val WEAR_OS_SETTINGS_TIPS = "wear_os_battery"
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@
<string name="settings_android_open">Open the app in settings</string>
<string name="settings_wrong_input_number">Wrong input - use only digits</string>
<string name="settings_android_battery_optimization">Ignore battery optimization</string>
<string name="settings_wear_os_battery_title">About Wear Os battery optimization</string>
<string name="settings_wear_os_battery_text">Wear Os has aggressive battery saving routines, which prohibit to record sensors for longer period of time.
You can try it without any additional options, but the measurement will not provide longer measurements, then 2 mins in general. \n\nSolutions:
\n\u2022 use wake lock, but usually it does not to lead to reasonable solution\n\u2022 turn on always on display and
leave the screen on red measurement screen of the watch. The screen will become black after 1 min, but is still reactive as it would not be asleep.
This should be enough to keep the device alive and data flow intact. However, it can increase battery drainage.\n\nThese options can be found in settings of the Wear Os app.</string>
<string name="settings_no_optimisations_required">No optimisations required under Android M</string>
<string name="settings_annotations">Annotations</string>
<string name="settings_csv">Data format</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,8 @@
<Preference
app:key="battery_settings"
app:title="@string/settings_android_battery_optimization" />
<Preference
app:key="wear_os_battery"
app:title="@string/settings_wear_os_battery_title" />
</PreferenceCategory>
</PreferenceScreen>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
Expand Down
4 changes: 2 additions & 2 deletions sensorservices/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.google.android.gms:play-services-maps:18.0.0'
implementation 'com.google.android.gms:play-services-location:19.0.0'
implementation 'com.google.android.gms:play-services-maps:18.0.1'
implementation 'androidx.preference:preference-ktx:1.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
12 changes: 6 additions & 6 deletions wear/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdkVersion 25
targetSdkVersion 31
versionCode 1000024
versionName "3.3.0"
versionName "3.4.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

def secureProps = new Properties()
Expand Down Expand Up @@ -50,8 +50,8 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2-native-mt'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt'

implementation 'com.google.firebase:firebase-analytics-ktx:20.0.0'
implementation 'com.google.firebase:firebase-crashlytics-ktx:18.2.5'
implementation 'com.google.firebase:firebase-analytics-ktx:20.0.1'
implementation 'com.google.firebase:firebase-crashlytics-ktx:18.2.6'

implementation 'androidx.percentlayout:percentlayout:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
Expand All @@ -63,9 +63,9 @@ dependencies {

implementation 'com.google.android.support:wearable:2.8.1'
implementation 'com.google.android.gms:play-services-wearable:17.1.0'
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.google.android.gms:play-services-basement:17.6.0'
implementation 'com.google.android.gms:play-services-maps:18.0.0'
implementation 'com.google.android.gms:play-services-location:19.0.0'
implementation 'com.google.android.gms:play-services-basement:18.0.0'
implementation 'com.google.android.gms:play-services-maps:18.0.1'
implementation 'com.google.android.material:material:1.4.0'

implementation 'com.github.GrenderG:Toasty:1.5.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class MainSettings : AppCompatActivity(), SettingsAdapter.ClickListenerInterface
lastChange = position
when (key) {
// negate the preference
BATTERY_RESTRICTION, WAKE_LOCK -> {
BATTERY_RESTRICTION, WAKE_LOCK, ALWAYS_ON_DISPLAY -> {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val b = !sharedPreferences.getBoolean(key, true)
val b = !sharedPreferences.getBoolean(key, SettingsAdapter.defaultValues.getOrDefault(key, false))
val editor = sharedPreferences.edit()
editor.putBoolean(key, b)
editor.apply()
Expand Down Expand Up @@ -87,6 +87,6 @@ class MainSettings : AppCompatActivity(), SettingsAdapter.ClickListenerInterface
companion object {
const val BATTERY_RESTRICTION = "BATTERY_RESTRICTION_WEAR"
const val WAKE_LOCK = "WAKE_LOCK_WEAR"
const val BATTERY_WHITELIST = "BATTERY_WHITE_LIST"
const val ALWAYS_ON_DISPLAY = "ALWAYS_ON_DISPLAY"
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.motionapps.sensorbox.activities

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.*
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import androidx.wear.ambient.AmbientModeSupport
import androidx.wear.widget.BoxInsetLayout
import com.motionapps.sensorbox.communication.MsgListener
Expand All @@ -25,14 +26,17 @@ import kotlinx.coroutines.*
/**
* launched, when the measurementService is active
*/
class StopActivity : AppCompatActivity(), WearOsListener, AmbientModeSupport.AmbientCallbackProvider {
class StopActivity : AppCompatActivity(), WearOsListener,
AmbientModeSupport.AmbientCallbackProvider {

private val wearOsHandler: WearOsHandler = WearOsHandler()
private var wearOsState: WearOsStates.PresenceResult? = null
private var job: Job? = null

private lateinit var ambientController: AmbientModeSupport.AmbientController

private val handler: Handler = Handler(Looper.getMainLooper())

private var running = false
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Expand All @@ -49,7 +53,7 @@ class StopActivity : AppCompatActivity(), WearOsListener, AmbientModeSupport.Amb
// whole screen is button
val view = findViewById<View>(R.id.stopButton)

view.setOnClickListener{
view.setOnClickListener {
// stops the MeasurementActivity
var intent = Intent(MeasurementService.STOP_SERVICE)
sendBroadcast(intent)
Expand All @@ -58,7 +62,7 @@ class StopActivity : AppCompatActivity(), WearOsListener, AmbientModeSupport.Amb

job?.cancel()
wearOsState?.let {
if(it.present){
if (it.present) {
MsgListener.sendStatusStartingService(this, wearOsHandler, false)
}
}
Expand All @@ -67,31 +71,54 @@ class StopActivity : AppCompatActivity(), WearOsListener, AmbientModeSupport.Amb
startActivity(intent)
}

// check the always on display option + sets up the black screen after 1 min
val sharedPreferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this)
if (sharedPreferences.getBoolean(MainSettings.ALWAYS_ON_DISPLAY, false)) {
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
findViewById<TextView>(R.id.stop_activity_text)?.let {
it.text = getString(R.string.tap_to_save_screen_on)
}
handler.postDelayed(Runnable {
turnScreenBlack()
}, 60_000)
}


// changes colour to black, when inactive - as watch
ambientController = AmbientModeSupport.attach(this)

// cancel from the phone by communication channel -> receiver
registerReceiver(broadcastReceiver, IntentFilter(MeasurementService.STOP_ACTIVITY))
running = true

// searches for phone
job = CoroutineScope(Dispatchers.Main).launch {
wearOsHandler.searchForWearOs(this@StopActivity, this@StopActivity, WearOsConstants.PHONE_APP_CAPABILITY)
wearOsHandler.searchForWearOs(
this@StopActivity,
this@StopActivity,
WearOsConstants.PHONE_APP_CAPABILITY
)
}
}

override fun onDestroy() {
super.onDestroy()

if (running) {
unregisterReceiver(broadcastReceiver)
}
running = false

window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
handler.removeCallbacksAndMessages(null)

job?.cancel()
wearOsHandler.onDestroy()
}

override suspend fun onWearOsStates(wearOsStates: WearOsStates) {
if(wearOsStates is WearOsStates.PresenceResult){
if (wearOsStates is WearOsStates.PresenceResult) {
this.wearOsState = wearOsStates
}
}
Expand All @@ -102,28 +129,37 @@ class StopActivity : AppCompatActivity(), WearOsListener, AmbientModeSupport.Amb
* @param ambientDetails
*/
override fun onEnterAmbient(ambientDetails: Bundle?) {
val view: BoxInsetLayout = this@StopActivity.findViewById(R.id.stopButton)
view.backgroundTintList = ContextCompat.getColorStateList(
this@StopActivity,
R.color.black_list_color
)
turnScreenBlack()
}

/**
* turns it back to red, when active
*/
override fun onExitAmbient() {
val view: BoxInsetLayout = findViewById(R.id.stopButton)
view.backgroundTintList = ContextCompat.getColorStateList(
this@StopActivity,
R.color.red_list_color
)
turnScreenRed()
}

override fun onUpdateAmbient() {
// Update the content
}
}

private fun turnScreenRed() {
val view: BoxInsetLayout = findViewById(R.id.stopButton)
view.backgroundTintList = ContextCompat.getColorStateList(
this@StopActivity,
R.color.red_list_color
)
}

private fun turnScreenBlack() {
val view: BoxInsetLayout = this@StopActivity.findViewById(R.id.stopButton)
view.backgroundTintList = ContextCompat.getColorStateList(
this@StopActivity,
R.color.black_list_color
)
}

override fun getAmbientCallback(): AmbientModeSupport.AmbientCallback {
return StopAmbientCallback()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SettingsAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
init {
add(R.string.battery_limit)
add(R.string.wake_lock)
add(R.string.keep_screen_on)
add(R.string.wear_sampling_text)
add(R.string.activity_gps_time)
add(R.string.activity_gps_distance)
Expand All @@ -42,6 +43,7 @@ class SettingsAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
init {
add(R.drawable.ic_battery_full)
add(R.drawable.ic_cpu)
add(R.drawable.ic_brightness)
add(R.drawable.ic_sampling)
add(R.drawable.ic_baseline_timer)
add(R.drawable.ic_baseline_location)
Expand All @@ -53,14 +55,16 @@ class SettingsAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
init {
add(MainSettings.BATTERY_RESTRICTION)
add(MainSettings.WAKE_LOCK)
add(MainSettings.ALWAYS_ON_DISPLAY)
add(SettingsPickerAdapter.SAMPLING_PREFERENCE)
add(MeasurementService.GPS_TIME)
add(MeasurementService.GPS_DISTANCE)
}
}

private val stringGpsTime: Array<String> = context.resources.getStringArray(R.array.GPS_times)
private val stringGpsDistance: Array<String> = context.resources.getStringArray(R.array.GPS_distances)
private val stringGpsDistance: Array<String> =
context.resources.getStringArray(R.array.GPS_distances)
private val mInflater: LayoutInflater = LayoutInflater.from(context)
private val sharedPreferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
Expand Down Expand Up @@ -96,8 +100,8 @@ class SettingsAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
private fun updateValueText(textView: TextView, key: String) {
when (key) {

MainSettings.BATTERY_RESTRICTION, MainSettings.WAKE_LOCK -> {
if (sharedPreferences.getBoolean(key, true)) {
MainSettings.BATTERY_RESTRICTION, MainSettings.WAKE_LOCK, MainSettings.ALWAYS_ON_DISPLAY -> {
if (sharedPreferences.getBoolean(key, defaultValues.getOrDefault(key, false))) {
textView.setText(R.string.settings_on)
} else {
textView.setText(R.string.settings_off)
Expand All @@ -114,12 +118,18 @@ class SettingsAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
}

MeasurementService.GPS_TIME -> {
val position = SettingsPickerAdapter.PREFERENCES[MeasurementService.GPS_TIME]!!.indexOf(sharedPreferences.getInt(MeasurementService.GPS_TIME, 1))
val position =
SettingsPickerAdapter.PREFERENCES[MeasurementService.GPS_TIME]!!.indexOf(
sharedPreferences.getInt(MeasurementService.GPS_TIME, 1)
)
textView.text = stringGpsTime[position]
}

MeasurementService.GPS_DISTANCE -> {
val position = SettingsPickerAdapter.PREFERENCES[MeasurementService.GPS_DISTANCE]!!.indexOf(sharedPreferences.getInt(MeasurementService.GPS_DISTANCE, 1))
val position =
SettingsPickerAdapter.PREFERENCES[MeasurementService.GPS_DISTANCE]!!.indexOf(
sharedPreferences.getInt(MeasurementService.GPS_DISTANCE, 1)
)
textView.text = stringGpsDistance[position]
}
}
Expand Down Expand Up @@ -155,4 +165,12 @@ class SettingsAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
fun onClick(position: Int, key: String)
}

companion object{
val defaultValues: Map<String, Boolean> = mapOf(
MainSettings.BATTERY_RESTRICTION to true,
MainSettings.WAKE_LOCK to false,
MainSettings.ALWAYS_ON_DISPLAY to false
)
}

}
Loading

0 comments on commit 230f38a

Please sign in to comment.