Skip to content

Commit 7ed30aa

Browse files
committed
* Fix for clock screensaver sometimes appearing offscreen
* Added feature to adjust screen brightness when application uses night mode * Fixed missing fonts in UI for some dialogs * Changed font color of screensaver text to be more tranquil
1 parent 4be29dd commit 7ed30aa

29 files changed

+408
-179
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ repositories {
3838
def versionMajor = 0
3939
def versionMinor = 8
4040
def versionPatch = 5
41-
def versionBuild = 6 // bump for dog food builds, public betas, etc.
41+
def versionBuild = 7 // bump for dog food builds, public betas, etc.
4242

4343
def ALARM_CODE() {
4444
Properties properties = new Properties()

app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
3434
<uses-permission android:name="com.fingerprints.service.ACCESS_FINGERPRINT_MANAGER"/>
3535
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY"/>
36+
<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
3637

3738
<uses-feature android:name="android.hardware.camera" android:required="false" />
3839
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/BaseActivity.kt

+109-52
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import android.content.ComponentName
2323
import android.content.Intent
2424
import android.content.pm.PackageManager
2525
import android.os.Build
26-
import android.os.Bundle
2726
import android.os.Handler
27+
import android.provider.Settings
2828
import android.support.annotation.NonNull
2929
import android.support.v4.app.ActivityCompat
3030
import android.support.v4.content.LocalBroadcastManager
@@ -42,7 +42,6 @@ import com.thanksmister.iot.mqtt.alarmpanel.network.MQTTOptions
4242
import com.thanksmister.iot.mqtt.alarmpanel.persistence.Configuration
4343
import com.thanksmister.iot.mqtt.alarmpanel.persistence.DarkSkyDao
4444
import com.thanksmister.iot.mqtt.alarmpanel.ui.activities.MainActivity
45-
import com.thanksmister.iot.mqtt.alarmpanel.utils.AlarmUtils
4645
import com.thanksmister.iot.mqtt.alarmpanel.utils.DialogUtils
4746
import dagger.android.support.DaggerAppCompatActivity
4847
import io.reactivex.disposables.CompositeDisposable
@@ -59,20 +58,10 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
5958
@Inject lateinit var dialogUtils: DialogUtils
6059
@Inject lateinit var darkSkyDataSource: DarkSkyDao
6160

62-
private val inactivityHandler: Handler = Handler()
6361
private var hasNetwork = AtomicBoolean(true)
64-
private var userPresent: Boolean = false
6562
private var connectionLiveData: ConnectionLiveData? = null
6663

6764
val disposable = CompositeDisposable()
68-
private var screenSaverDialog : Dialog? = null
69-
70-
private val inactivityCallback = Runnable {
71-
Timber.d("inactivityCallback")
72-
dialogUtils.clearDialogs()
73-
userPresent = false
74-
showScreenSaver()
75-
}
7665

7766
override fun onStart(){
7867
super.onStart()
@@ -84,6 +73,7 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
8473
handleNetworkDisconnect()
8574
}
8675
})
76+
resetScreenBrightness()
8777
}
8878

8979
private fun checkPermissions() {
@@ -117,33 +107,10 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
117107

118108
override fun onDestroy() {
119109
super.onDestroy()
120-
inactivityHandler.removeCallbacks(inactivityCallback)
110+
Timber.d("onDestroy")
121111
disposable.dispose()
122112
}
123113

124-
fun resetInactivityTimer() {
125-
Timber.d("resetInactivityTimer")
126-
hideScreenSaver()
127-
inactivityHandler.removeCallbacks(inactivityCallback)
128-
inactivityHandler.postDelayed(inactivityCallback, configuration.inactivityTime)
129-
}
130-
131-
fun stopDisconnectTimer() {
132-
hideScreenSaver()
133-
inactivityHandler.removeCallbacks(inactivityCallback)
134-
}
135-
136-
override fun onUserInteraction() {
137-
Timber.d("onUserInteraction")
138-
onWindowFocusChanged(true)
139-
userPresent = true
140-
resetInactivityTimer()
141-
val intent = Intent(AlarmPanelService.BROADCAST_EVENT_SCREEN_TOUCH)
142-
intent.putExtra(AlarmPanelService.BROADCAST_EVENT_SCREEN_TOUCH, true)
143-
val bm = LocalBroadcastManager.getInstance(applicationContext)
144-
bm.sendBroadcast(intent)
145-
}
146-
147114
public override fun onResume() {
148115
super.onResume()
149116
checkPermissions()
@@ -162,44 +129,135 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
162129
}
163130

164131
open fun dayNightModeCheck(dayNightMode:String?) {
165-
Timber.d("dayNightModeCheck")
166-
val uiMode = resources.configuration.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK;
167-
if(dayNightMode == Configuration.DISPLAY_MODE_NIGHT && uiMode == android.content.res.Configuration.UI_MODE_NIGHT_NO) {
168-
Timber.d("Tis the night!")
132+
133+
Timber.d("dayNightModeCheck $dayNightMode")
134+
135+
if(dayNightMode == Configuration.DISPLAY_MODE_NIGHT && tisTheDay()) {
136+
hideScreenSaver()
137+
resetScreenBrightness()
169138
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
170139
recreate()
171-
} else if (dayNightMode == Configuration.DISPLAY_MODE_DAY && uiMode == android.content.res.Configuration.UI_MODE_NIGHT_YES) {
172-
Timber.d("Tis the day!")
140+
} else if (dayNightMode == Configuration.DISPLAY_MODE_DAY && !tisTheDay()) {
141+
hideScreenSaver()
142+
resetScreenBrightness()
173143
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
174144
recreate()
175145
}
176146
}
177147

178148
private fun dayNightModeChanged() {
179149
Timber.d("dayNightModeChanged")
180-
val uiMode = resources.configuration.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK;
181-
if (!configuration.useNightDayMode && uiMode == android.content.res.Configuration.UI_MODE_NIGHT_YES) {
182-
Timber.d("Tis the day!")
150+
if (!configuration.useNightDayMode && !tisTheDay()) {
151+
hideScreenSaver()
152+
resetScreenBrightness()
183153
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
184154
recreate()
185155
}
186156
}
187157

158+
private fun tisTheDay(): Boolean {
159+
val uiMode = resources.configuration.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK;
160+
if (uiMode == android.content.res.Configuration.UI_MODE_NIGHT_YES) {
161+
Timber.d("Tis the night!")
162+
return false
163+
} else if (uiMode == android.content.res.Configuration.UI_MODE_NIGHT_NO) {
164+
Timber.d("Tis the day!")
165+
return true
166+
}
167+
return true
168+
}
169+
170+
open fun setScreenBrightness() {
171+
Timber.d("changeScreenBrightness")
172+
var brightness = configuration.screenBrightness
173+
if (!tisTheDay()) {
174+
brightness = configuration.screenNightBrightness
175+
}
176+
Timber.d("calculated brightness ${brightness}")
177+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.System.canWrite(applicationContext) && configuration.useScreenBrightness) {
178+
var mode = -1
179+
try {
180+
mode = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE) //this will return integer (0 or 1)
181+
} catch (e: Settings.SettingNotFoundException) {
182+
Timber.e(e.message)
183+
}
184+
try {
185+
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
186+
//Automatic mode, need to be in manual to change brightness
187+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)
188+
}
189+
if (brightness in 1..255) {
190+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness)
191+
}
192+
} catch (e: SecurityException) {
193+
Timber.e(e.message)
194+
}
195+
} else {
196+
try {
197+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)
198+
if (brightness in 1..255) {
199+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness)
200+
}
201+
} catch (e: SecurityException) {
202+
Timber.e(e.message)
203+
}
204+
}
205+
}
206+
207+
open fun resetScreenBrightness() {
208+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.System.canWrite(applicationContext)) {
209+
var mode = -1
210+
try {
211+
mode = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE) //this will return integer (0 or 1)
212+
} catch (e: Settings.SettingNotFoundException) {
213+
Timber.e(e.message)
214+
}
215+
try {
216+
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
217+
//Automatic mode, need to be in manual to change brightness
218+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)
219+
}
220+
if(tisTheDay() && configuration.screenBrightness in 1..255) {
221+
Timber.d("calculated brightness ${configuration.screenBrightness}")
222+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, configuration.screenBrightness)
223+
} else if (!tisTheDay() && configuration.screenNightBrightness in 1..255) {
224+
Timber.d("calculated brightness ${configuration.screenNightBrightness}")
225+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, configuration.screenNightBrightness)
226+
}
227+
} catch (e: SecurityException) {
228+
Timber.e(e.message)
229+
}
230+
} else {
231+
try {
232+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)
233+
if(tisTheDay() && configuration.screenBrightness in 1..255) {
234+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, configuration.screenBrightness)
235+
Timber.d("calculated brightness ${configuration.screenBrightness}")
236+
} else if (!tisTheDay() && configuration.screenNightBrightness in 1..255) {
237+
Timber.d("calculated brightness ${configuration.screenNightBrightness}")
238+
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, configuration.screenNightBrightness)
239+
}
240+
} catch (e: SecurityException) {
241+
Timber.e(e.message)
242+
}
243+
}
244+
}
245+
188246
/**
189247
* Show the screen saver only if the alarm isn't triggered. This shouldn't be an issue
190248
* with the alarm disabled because the disable time will be longer than this.
191249
*/
192-
open fun showScreenSaver() {
250+
fun showScreenSaver() {
251+
Timber.d("showScreenSaver")
193252
if (!configuration.isAlarmTriggeredMode() && configuration.hasScreenSaver() && !isFinishing) {
194-
inactivityHandler.removeCallbacks(inactivityCallback)
195253
val hasWeather = (configuration.showWeatherModule() && darkSkyOptions.isValid)
196254
try {
197255
dialogUtils.showScreenSaver(this@BaseActivity,
198256
configuration.showPhotoScreenSaver(),
199257
imageOptions,
200258
View.OnClickListener {
201259
dialogUtils.hideScreenSaverDialog()
202-
resetInactivityTimer()
260+
onUserInteraction()
203261
}, darkSkyDataSource, hasWeather)
204262
} catch (e: Exception) {
205263
Timber.e(e.message)
@@ -208,8 +266,8 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
208266
}
209267

210268
open fun hideScreenSaver() {
269+
Timber.d("hideScreenSaver")
211270
dialogUtils.hideScreenSaverDialog()
212-
screenSaverDialog = null
213271
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
214272
}
215273

@@ -221,7 +279,6 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
221279
*/
222280
open fun handleNetworkDisconnect() {
223281
hideScreenSaver()
224-
bringApplicationToForegroundIfNeeded()
225282
dialogUtils.showAlertDialogToDismiss(this@BaseActivity, getString(R.string.text_notification_network_title),
226283
getString(R.string.text_notification_network_description))
227284
hasNetwork.set(false)
@@ -240,15 +297,15 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
240297
return hasNetwork.get()
241298
}
242299

243-
fun bringApplicationToForegroundIfNeeded() {
300+
/*fun bringApplicationToForegroundIfNeeded() {
244301
if (!LifecycleHandler.isApplicationInForeground()) {
245302
Timber.d("bringApplicationToForegroundIfNeeded")
246303
val intent = Intent("intent.alarm.action")
247304
intent.component = ComponentName(this@BaseActivity.packageName, MainActivity::class.java.name)
248305
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
249306
startActivity(intent)
250307
}
251-
}
308+
}*/
252309

253310
companion object {
254311
const val REQUEST_PERMISSIONS = 88

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/managers/DayNightAlarmLiveData.kt

+1-36
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616

1717
package com.thanksmister.iot.mqtt.alarmpanel.managers
1818

19-
import android.app.PendingIntent
2019
import android.arch.lifecycle.MutableLiveData
21-
import android.content.BroadcastReceiver
2220
import android.content.Context
23-
import android.content.Intent
2421
import android.os.Handler
2522
import android.os.Looper
2623
import com.thanksmister.iot.mqtt.alarmpanel.persistence.Configuration
@@ -31,9 +28,6 @@ import java.util.*
3128

3229
class DayNightAlarmLiveData(private val context: Context, private val configuration: Configuration) : MutableLiveData<String>() {
3330

34-
//private var pendingIntent: PendingIntent? = null
35-
//private val intentFilter = IntentFilter(ALARM_ACTION)
36-
//private val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
3731
private var timeHandler: Handler? = null
3832

3933
private val timeRunnable = object : Runnable {
@@ -49,13 +43,11 @@ class DayNightAlarmLiveData(private val context: Context, private val configurat
4943

5044
override fun onActive() {
5145
super.onActive()
52-
//context.registerReceiver(alarmReceiver, IntentFilter(intentFilter))
53-
startDayNightMode()
46+
startDayNightMode()
5447
}
5548

5649
override fun onInactive() {
5750
super.onInactive()
58-
//context.unregisterReceiver(alarmReceiver)
5951
cancelDayNightMode()
6052
}
6153

@@ -65,13 +57,6 @@ class DayNightAlarmLiveData(private val context: Context, private val configurat
6557
timeHandler!!.removeCallbacks(timeRunnable)
6658
timeHandler = null
6759
}
68-
/*if(pendingIntent != null) {
69-
Timber.d("cancelDayNightMode")
70-
pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, Intent(ALARM_ACTION), PendingIntent.FLAG_CANCEL_CURRENT);
71-
alarmManager.cancel(pendingIntent)
72-
pendingIntent?.cancel()
73-
pendingIntent = null
74-
}*/
7560
}
7661

7762
private fun startDayNightMode() {
@@ -81,24 +66,6 @@ class DayNightAlarmLiveData(private val context: Context, private val configurat
8166
timeHandler = Handler(Looper.getMainLooper())
8267
timeHandler!!.postDelayed(timeRunnable, firstTime)
8368
}
84-
/*if(pendingIntent == null && configuration.useNightDayMode) {
85-
configuration.dayNightMode = Configuration.DISPLAY_MODE_DAY
86-
pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, Intent(ALARM_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
87-
val firstTime = SystemClock.elapsedRealtime() + 30 * 1000;
88-
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, INTERVAL_FIFTEEN_MINUTES, pendingIntent)
89-
}*/
90-
}
91-
92-
private fun getPendingIntent(): PendingIntent {
93-
val pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, Intent(ALARM_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);
94-
return pendingIntent
95-
}
96-
97-
private val alarmReceiver = object : BroadcastReceiver() {
98-
override fun onReceive(context: Context, intent: Intent) {
99-
Timber.d("alarmReceiver")
100-
setNightDayMode()
101-
}
10269
}
10370

10471
private fun setNightDayMode() {
@@ -135,7 +102,5 @@ class DayNightAlarmLiveData(private val context: Context, private val configurat
135102

136103
companion object {
137104
const val INTERVAL_FIFTEEN_MINUTES = (15 * 60 * 1000).toLong()
138-
const val ALARM_ACTION = "com.thanksmister.iot.mqtt.alarmpanel.DayNightAlarmReceiver"
139-
const val REQUEST_CODE = 888
140105
}
141106
}

0 commit comments

Comments
 (0)