@@ -34,7 +34,10 @@ import android.widget.TextView
3434import android.widget.Toast
3535import androidx.activity.result.contract.ActivityResultContracts
3636import androidx.appcompat.app.AppCompatActivity
37+ import androidx.appcompat.app.AppCompatDelegate
3738import androidx.core.view.WindowCompat
39+ import androidx.lifecycle.lifecycleScope
40+ import androidx.preference.PreferenceManager
3841import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
3942import retrofit2.Call
4043import retrofit2.Callback
@@ -103,14 +106,17 @@ class MainActivity : AppCompatActivity() {
103106 }
104107
105108 override fun onCreate (savedInstanceState : Bundle ? ) {
109+ // force dark mode
110+ AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
106111 super .onCreate(savedInstanceState)
107112
108113 window.addFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON )
109114 setContentView(R .layout.main_view)
110115 hideSystemUI()
111116
112117 webView = findViewById(R .id.webView)
113- // webView.setBackgroundColor(Color.TRANSPARENT)
118+ webView.setBackgroundColor(Color .BLACK )
119+ webView.loadUrl(" about:blank" )
114120 imageView1 = findViewById(R .id.imageView1)
115121 imageView2 = findViewById(R .id.imageView2)
116122 txtPhotoInfo = findViewById(R .id.txtPhotoInfo)
@@ -125,6 +131,7 @@ class MainActivity : AppCompatActivity() {
125131 swipeRefreshLayout.isRefreshing = false
126132 settingsAction()
127133 }
134+
128135 btnPrevious.setOnClickListener {
129136 val toast = Toast .makeText(this , " Previous" , Toast .LENGTH_SHORT )
130137 toast.setGravity(Gravity .CENTER_VERTICAL or Gravity .START , 0 , 0 )
@@ -148,21 +155,28 @@ class MainActivity : AppCompatActivity() {
148155
149156 rcpServer = RpcHttpServer (
150157 onDimCommand = { dim -> runOnUiThread { screenDim(dim) } },
151- onNextCommand = { runOnUiThread {nextAction()} },
152- onPreviousCommand = { runOnUiThread {previousAction()} },
153- onPauseCommand = { runOnUiThread {pauseAction()} },
154- onSettingsCommand = { runOnUiThread {settingsAction()} },
155- onBrightnessCommand = { brightness -> runOnUiThread { screenBrightnessAction(brightness) }},
158+ onNextCommand = { runOnUiThread { nextAction() } },
159+ onPreviousCommand = { runOnUiThread { previousAction() } },
160+ onPauseCommand = { runOnUiThread { pauseAction() } },
161+ onSettingsCommand = { runOnUiThread { settingsAction() } },
162+ onBrightnessCommand = { brightness -> runOnUiThread { screenBrightnessAction(brightness) } },
156163 )
157164 rcpServer.start()
158165
159- val savedUrl = getSharedPreferences(" ImmichFramePrefs" , MODE_PRIVATE ).getString(" webview_url" , " " ) ? : " "
160- if (savedUrl.isBlank()) {
161- val intent = Intent (this , SettingsActivity ::class .java)
162- settingsLauncher.launch(intent)
163- }
164- else {
165- loadSettings()
166+ // wait for network connection
167+ lifecycleScope.launch {
168+ if (! Helpers .isNetworkAvailable(this @MainActivity)) {
169+ Helpers .waitForNetwork(this @MainActivity)
170+ Toast .makeText(this @MainActivity, " Connected!" , Toast .LENGTH_SHORT ).show()
171+ }
172+ val prefs = PreferenceManager .getDefaultSharedPreferences(applicationContext)
173+ val savedUrl = prefs.getString(" webview_url" , " " ) ? : " "
174+ if (savedUrl.isBlank()) {
175+ val intent = Intent (this @MainActivity, SettingsActivity ::class .java)
176+ settingsLauncher.launch(intent)
177+ } else {
178+ loadSettings()
179+ }
166180 }
167181 }
168182
@@ -429,6 +443,9 @@ class MainActivity : AppCompatActivity() {
429443 var retryCount = 0
430444
431445 fun attemptFetch () {
446+ if (useWebView) {
447+ return
448+ }
432449 apiService.getServerSettings().enqueue(object : Callback <Helpers .ServerSettings > {
433450 override fun onResponse (
434451 call : Call <Helpers .ServerSettings >,
@@ -451,6 +468,9 @@ class MainActivity : AppCompatActivity() {
451468 }
452469
453470 private fun handleFailure (t : Throwable ) {
471+ if (useWebView) {
472+ return
473+ }
454474 if (retryCount < maxRetries) {
455475 retryCount++
456476 Toast .makeText(
@@ -473,14 +493,14 @@ class MainActivity : AppCompatActivity() {
473493
474494 @SuppressLint(" SetJavaScriptEnabled" )
475495 private fun loadSettings () {
476- val sharedPreferences = getSharedPreferences( " ImmichFramePrefs " , MODE_PRIVATE )
477- blurredBackground = sharedPreferences .getBoolean(" blurredBackground" , true )
478- showCurrentDate = sharedPreferences .getBoolean(" showCurrentDate" , true )
479- var savedUrl = sharedPreferences .getString(" webview_url" , " " ) ? : " "
480- useWebView = sharedPreferences .getBoolean(" useWebView" , true )
481- keepScreenOn = sharedPreferences .getBoolean(" keepScreenOn" , true )
482- val authSecret = sharedPreferences .getString(" authSecret" , " " ) ? : " "
483- val screenDim = sharedPreferences .getBoolean(" screenDim" , false )
496+ val prefs = PreferenceManager .getDefaultSharedPreferences(applicationContext )
497+ blurredBackground = prefs .getBoolean(" blurredBackground" , true )
498+ showCurrentDate = prefs .getBoolean(" showCurrentDate" , true )
499+ var savedUrl = prefs .getString(" webview_url" , " " ) ? : " "
500+ useWebView = prefs .getBoolean(" useWebView" , true )
501+ keepScreenOn = prefs .getBoolean(" keepScreenOn" , true )
502+ val authSecret = prefs .getString(" authSecret" , " " ) ? : " "
503+ val screenDim = prefs .getBoolean(" screenDim" , false )
484504
485505 webView.visibility = if (useWebView) View .VISIBLE else View .GONE
486506 imageView1.visibility = if (useWebView) View .GONE else View .VISIBLE
@@ -515,8 +535,6 @@ class MainActivity : AppCompatActivity() {
515535 } else {
516536 savedUrl
517537 }
518-
519- // handler.removeCallbacksAndMessages(null)
520538 handler.removeCallbacks(imageRunnable)
521539 handler.removeCallbacks(weatherRunnable)
522540
@@ -541,9 +559,19 @@ class MainActivity : AppCompatActivity() {
541559 error : WebResourceError ?
542560 ) {
543561 super .onReceivedError(view, request, error)
562+
563+ if (request?.isForMainFrame == true && error != null ) {
564+ view?.loadUrl(" file:///android_asset/error_page.html" )
565+
566+ Handler (Looper .getMainLooper()).postDelayed({
567+ val errorCode = error.errorCode
568+ val errorDescription = error.description.toString().replace(" '" , " \\ '" )
569+ view?.evaluateJavascript(" showError('$errorCode ', '$errorDescription ')" , null )
570+ }, 500 )
571+ }
544572 Handler (Looper .getMainLooper()).postDelayed({
545- view?.reload( )
546- }, 3000 )
573+ webView.loadUrl(savedUrl )
574+ }, 5000 )
547575 }
548576 }
549577 webView.settings.javaScriptEnabled = true
@@ -567,15 +595,13 @@ class MainActivity : AppCompatActivity() {
567595 }
568596 )
569597 }
570-
571598 }
572599
573600 private fun onSettingsLoaded () {
574- if (serverSettings.imageFill){
601+ if (serverSettings.imageFill) {
575602 imageView1.scaleType = ImageView .ScaleType .CENTER_CROP
576603 imageView2.scaleType = ImageView .ScaleType .CENTER_CROP
577- }
578- else {
604+ } else {
579605 imageView1.scaleType = ImageView .ScaleType .FIT_CENTER
580606 imageView2.scaleType = ImageView .ScaleType .FIT_CENTER
581607 }
@@ -613,14 +639,13 @@ class MainActivity : AppCompatActivity() {
613639 }
614640 }
615641
616- private fun previousAction (){
642+ private fun previousAction () {
617643 if (useWebView) {
618644 // Simulate a key press
619645 webView.requestFocus()
620646 val event = KeyEvent (KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_DPAD_LEFT )
621647 dispatchKeyEvent(event)
622- }
623- else {
648+ } else {
624649 val safePreviousImage = previousImage
625650 if (safePreviousImage != null ) {
626651 stopImageTimer()
@@ -630,14 +655,13 @@ class MainActivity : AppCompatActivity() {
630655 }
631656 }
632657
633- private fun nextAction (){
658+ private fun nextAction () {
634659 if (useWebView) {
635660 // Simulate a key press
636661 webView.requestFocus()
637662 val event = KeyEvent (KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_DPAD_RIGHT )
638663 dispatchKeyEvent(event)
639- }
640- else {
664+ } else {
641665 stopImageTimer()
642666 getNextImage()
643667 startImageTimer()
@@ -698,7 +722,7 @@ class MainActivity : AppCompatActivity() {
698722 return true
699723 }
700724
701- KeyEvent .KEYCODE_SPACE -> {
725+ KeyEvent .KEYCODE_SPACE -> {
702726 pauseAction()
703727 return true
704728 }
@@ -741,10 +765,9 @@ class MainActivity : AppCompatActivity() {
741765 dimOverlay.apply {
742766 visibility = View .VISIBLE
743767 alpha = 0f
744- if (useWebView){
768+ if (useWebView) {
745769 webView.loadUrl(" about:blank" )
746- }
747- else {
770+ } else {
748771 stopImageTimer()
749772 stopWeatherTimer()
750773 }
@@ -771,7 +794,7 @@ class MainActivity : AppCompatActivity() {
771794 }
772795
773796 private fun checkDimTime () {
774- val prefs = getSharedPreferences( " ImmichFramePrefs " , MODE_PRIVATE )
797+ val prefs = PreferenceManager .getDefaultSharedPreferences(applicationContext )
775798 val startHour = prefs.getInt(" dimStartHour" , 22 )
776799 val startMinute = prefs.getInt(" dimStartMinute" , 0 )
777800 val endHour = prefs.getInt(" dimEndHour" , 6 )
0 commit comments