@@ -23,8 +23,8 @@ import android.content.ComponentName
23
23
import android.content.Intent
24
24
import android.content.pm.PackageManager
25
25
import android.os.Build
26
- import android.os.Bundle
27
26
import android.os.Handler
27
+ import android.provider.Settings
28
28
import android.support.annotation.NonNull
29
29
import android.support.v4.app.ActivityCompat
30
30
import android.support.v4.content.LocalBroadcastManager
@@ -42,7 +42,6 @@ import com.thanksmister.iot.mqtt.alarmpanel.network.MQTTOptions
42
42
import com.thanksmister.iot.mqtt.alarmpanel.persistence.Configuration
43
43
import com.thanksmister.iot.mqtt.alarmpanel.persistence.DarkSkyDao
44
44
import com.thanksmister.iot.mqtt.alarmpanel.ui.activities.MainActivity
45
- import com.thanksmister.iot.mqtt.alarmpanel.utils.AlarmUtils
46
45
import com.thanksmister.iot.mqtt.alarmpanel.utils.DialogUtils
47
46
import dagger.android.support.DaggerAppCompatActivity
48
47
import io.reactivex.disposables.CompositeDisposable
@@ -59,20 +58,10 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
59
58
@Inject lateinit var dialogUtils: DialogUtils
60
59
@Inject lateinit var darkSkyDataSource: DarkSkyDao
61
60
62
- private val inactivityHandler: Handler = Handler ()
63
61
private var hasNetwork = AtomicBoolean (true )
64
- private var userPresent: Boolean = false
65
62
private var connectionLiveData: ConnectionLiveData ? = null
66
63
67
64
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
- }
76
65
77
66
override fun onStart (){
78
67
super .onStart()
@@ -84,6 +73,7 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
84
73
handleNetworkDisconnect()
85
74
}
86
75
})
76
+ resetScreenBrightness()
87
77
}
88
78
89
79
private fun checkPermissions () {
@@ -117,33 +107,10 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
117
107
118
108
override fun onDestroy () {
119
109
super .onDestroy()
120
- inactivityHandler.removeCallbacks(inactivityCallback )
110
+ Timber .d( " onDestroy " )
121
111
disposable.dispose()
122
112
}
123
113
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
-
147
114
public override fun onResume () {
148
115
super .onResume()
149
116
checkPermissions()
@@ -162,44 +129,135 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
162
129
}
163
130
164
131
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()
169
138
AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
170
139
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()
173
143
AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
174
144
recreate()
175
145
}
176
146
}
177
147
178
148
private fun dayNightModeChanged () {
179
149
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( )
183
153
AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
184
154
recreate()
185
155
}
186
156
}
187
157
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
+
188
246
/* *
189
247
* Show the screen saver only if the alarm isn't triggered. This shouldn't be an issue
190
248
* with the alarm disabled because the disable time will be longer than this.
191
249
*/
192
- open fun showScreenSaver () {
250
+ fun showScreenSaver () {
251
+ Timber .d(" showScreenSaver" )
193
252
if (! configuration.isAlarmTriggeredMode() && configuration.hasScreenSaver() && ! isFinishing) {
194
- inactivityHandler.removeCallbacks(inactivityCallback)
195
253
val hasWeather = (configuration.showWeatherModule() && darkSkyOptions.isValid)
196
254
try {
197
255
dialogUtils.showScreenSaver(this @BaseActivity,
198
256
configuration.showPhotoScreenSaver(),
199
257
imageOptions,
200
258
View .OnClickListener {
201
259
dialogUtils.hideScreenSaverDialog()
202
- resetInactivityTimer ()
260
+ onUserInteraction ()
203
261
}, darkSkyDataSource, hasWeather)
204
262
} catch (e: Exception ) {
205
263
Timber .e(e.message)
@@ -208,8 +266,8 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
208
266
}
209
267
210
268
open fun hideScreenSaver () {
269
+ Timber .d(" hideScreenSaver" )
211
270
dialogUtils.hideScreenSaverDialog()
212
- screenSaverDialog = null
213
271
window.clearFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON )
214
272
}
215
273
@@ -221,7 +279,6 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
221
279
*/
222
280
open fun handleNetworkDisconnect () {
223
281
hideScreenSaver()
224
- bringApplicationToForegroundIfNeeded()
225
282
dialogUtils.showAlertDialogToDismiss(this @BaseActivity, getString(R .string.text_notification_network_title),
226
283
getString(R .string.text_notification_network_description))
227
284
hasNetwork.set(false )
@@ -240,15 +297,15 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
240
297
return hasNetwork.get()
241
298
}
242
299
243
- fun bringApplicationToForegroundIfNeeded () {
300
+ /* fun bringApplicationToForegroundIfNeeded() {
244
301
if (!LifecycleHandler.isApplicationInForeground()) {
245
302
Timber.d("bringApplicationToForegroundIfNeeded")
246
303
val intent = Intent("intent.alarm.action")
247
304
intent.component = ComponentName(this@BaseActivity.packageName, MainActivity::class.java.name)
248
305
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
249
306
startActivity(intent)
250
307
}
251
- }
308
+ }*/
252
309
253
310
companion object {
254
311
const val REQUEST_PERMISSIONS = 88
0 commit comments