Skip to content

Commit

Permalink
Fixed several problems during WebView/GeckoView switch;
Browse files Browse the repository at this point in the history
Version update: v2.0.0
  • Loading branch information
truefedex committed Apr 28, 2023
1 parent facab00 commit 42cae54
Showing 8 changed files with 71 additions and 42 deletions.
15 changes: 12 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -17,15 +17,15 @@ var includeFirebase = true

android {
compileSdk = 33
buildToolsVersion = "33.0.1"
buildToolsVersion = "33.0.2"
namespace = "com.phlox.tvwebbrowser"

defaultConfig {
applicationId = "com.phlox.tvwebbrowser"
minSdk = 23
targetSdk = 33
versionCode = 59
versionName = "1.8.6"
versionCode = 60
versionName = "2.0.0"

javaCompileOptions {
annotationProcessorOptions {
@@ -62,12 +62,18 @@ android {
//important than the size after installation
manifestPlaceholders["extractNativeLibs"] = "true"
packagingOptions.jniLibs.useLegacyPackaging = true
ndk {
abiFilters += listOf("armeabi-v7a")//TODO: implement auto-update logic for selecting right arch during update
}
}
create("google") {
dimension = "appstore"
//now auto-update violates Google Play policies
buildConfigField("Boolean", "BUILT_IN_AUTO_UPDATE", "false")
manifestPlaceholders["extractNativeLibs"] = "false"
ndk {
abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
}
}
create("foss") {
dimension = "appstore"
@@ -77,6 +83,9 @@ android {
manifestPlaceholders["extractNativeLibs"] = "true"
packagingOptions.jniLibs.useLegacyPackaging = true
includeFirebase = false//do not include firebase in the foss build
ndk {
abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
}
}
}

22 changes: 14 additions & 8 deletions app/src/main/java/com/phlox/tvwebbrowser/TVBro.kt
Original file line number Diff line number Diff line change
@@ -4,18 +4,19 @@ import android.app.Activity
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Process
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import com.phlox.tvwebbrowser.activity.IncognitoModeMainActivity
import com.phlox.tvwebbrowser.activity.main.MainActivity
import com.phlox.tvwebbrowser.utils.activemodel.ActiveModelsRepository
import java.net.CookieHandler
import java.net.CookieManager
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import kotlin.system.exitProcess

/**
* Created by PDT on 09.09.2016.
@@ -34,7 +35,8 @@ class TVBro : Application(), Application.ActivityLifecycleCallbacks {
private set

private lateinit var _config: Config

var needToExitProcessAfterMainActivityFinish = false
var needRestartMainActivityAfterExitingProcess = false
override fun onCreate() {
Log.i(TAG, "onCreate")
super.onCreate()
@@ -96,11 +98,15 @@ class TVBro : Application(), Application.ActivityLifecycleCallbacks {

override fun onActivityDestroyed(activity: Activity) {
Log.i(TAG, "onActivityDestroyed: " + activity.javaClass.simpleName)
//we need this because in case of IncognitoModeMainActivity closed by exit button by user
//then incognito mode becomes closed but process are still running and this lead to
//strange problems at next time when we trying to start the incognito mode
if (activity is IncognitoModeMainActivity) {
Process.killProcess(Process.myPid())
if (needToExitProcessAfterMainActivityFinish && activity is MainActivity) {
Log.i(TAG, "onActivityDestroyed: exiting process")
if (needRestartMainActivityAfterExitingProcess) {
Log.i(TAG, "onActivityDestroyed: restarting main activity")
val intent = Intent(this@TVBro, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
}
exitProcess(0)
}
}
}
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import com.phlox.tvwebbrowser.R
import com.phlox.tvwebbrowser.TVBro
import com.phlox.tvwebbrowser.activity.main.MainActivity

//Same as MainActivity but runs in separate process
@@ -21,6 +22,13 @@ class IncognitoModeMainActivity: MainActivity() {
}
}

override fun onDestroy() {
if (isFinishing) {
TVBro.instance.needToExitProcessAfterMainActivityFinish = true
}
super.onDestroy()
}

private fun showIncognitoModeHintDialog() {
AlertDialog.Builder(this)
.setTitle(R.string.incognito_mode)
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ import java.net.URLEncoder
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlin.system.exitProcess


open class MainActivity : AppCompatActivity(), ActionBar.Callback {
@@ -811,7 +812,7 @@ open class MainActivity : AppCompatActivity(), ActionBar.Callback {
intent.putExtras(it)
}
startActivity(intent)
System.exit(0)
exitProcess(0)
}

fun toggleMenu() {
Original file line number Diff line number Diff line change
@@ -119,14 +119,18 @@ class TabsModel : ActiveModel() {
newTab.selected = true
currentTab.value = newTab
var wv = newTab.webEngine.getView()
var needReloadUrl = false
if (wv == null) {
wv = webViewProvider(newTab)
if (wv == null) {
return
}
newTab.restoreWebView()
needReloadUrl = !newTab.restoreWebView()
}
newTab.webEngine.onAttachToWindow(webEngineWindowProviderCallback, webViewParent, fullScreenViewParent)
if (needReloadUrl) {
newTab.webEngine.loadUrl(newTab.url)
}
newTab.webEngine.setNetworkAvailable(Utils.isNetworkConnected(TVBro.instance))
}
}
Original file line number Diff line number Diff line change
@@ -72,7 +72,9 @@ class MainSettingsView @JvmOverloads constructor(
.setTitle(R.string.need_restart)
.setMessage(R.string.need_restart_message)
.setPositiveButton(R.string.exit) { _, _ ->
exitProcess(0)
TVBro.instance.needToExitProcessAfterMainActivityFinish = true
TVBro.instance.needRestartMainActivityAfterExitingProcess = true
activity!!.finish()
}
.setCancelable(false)
.show()
42 changes: 20 additions & 22 deletions app/src/main/java/com/phlox/tvwebbrowser/model/WebTabState.kt
Original file line number Diff line number Diff line change
@@ -162,31 +162,29 @@ data class WebTabState(@PrimaryKey(autoGenerate = true)
thumbnailHash = null
}

fun restoreWebView() {
val restored = kotlin.run {
var state = savedState
val stateFileName = wvStateFileName
if (state != null) {
fun restoreWebView(): Boolean {
var state = savedState
val stateFileName = wvStateFileName
if (state != null) {
webEngine.restoreState(state)
return true
} else if (stateFileName != null) {
if (stateFileName.startsWith(GECKO_SESSION_STATE_HASH_PREFIX) xor (webEngine is GeckoWebEngine)) {
return false
}
try {
val stateBytes = File(getWVStatePath(stateFileName)).readBytes()
state = webEngine.stateFromBytes(stateBytes)
if (state == null) return false
this.savedState = state
webEngine.restoreState(state)
return@run true
} else if (stateFileName != null) {
try {
val stateBytes = File(getWVStatePath(stateFileName)).readBytes()
state = webEngine.stateFromBytes(stateBytes)
if (state == null) return@run false
this.savedState = state
webEngine.restoreState(state)
return@run true
} catch (e: Exception) {
e.printStackTrace()
return@run false
}
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
return@run false
}
if (!restored) {
this.url.takeIf { !TextUtils.isEmpty(url) }?.apply { webEngine.loadUrl(this) }
}
return false
}

fun saveWebViewStateToFile() {
Original file line number Diff line number Diff line change
@@ -82,8 +82,9 @@ open class ObservableValue<T>(default: T) : Subscribable<ValueObserver<T>> {
override val observers = ArrayList<ValueObserver<T>>()

override fun notifyObservers() {
for (observer in observers) {
observer(value)
//observers can be removed during iteration
for (i in observers.size - 1 downTo 0) {
observers[i](value)
}
}

@@ -121,8 +122,8 @@ class EventSource: Subscribable<EventObserver> {

override fun notifyObservers() {
if (!wasEmitted) return
for (observer in observers) {
observer()
for (i in observers.size - 1 downTo 0) {
observers[i]()
}
}

@@ -145,8 +146,8 @@ class ParameterizedEventSource<T>: Subscribable<ParameterizedEventObserver<T>> {

override fun notifyObservers() {
val event = lastEvent ?: return
for (observer in observers) {
observer(event)
for (i in observers.size - 1 downTo 0) {
observers[i](event)
}
}

0 comments on commit 42cae54

Please sign in to comment.