Created
July 26, 2020 17:30
-
-
Save androidovshchik/c70c46172159f2e6cbc88abfe0f38130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pro.biochemica | |
import android.content.Intent | |
import android.graphics.Bitmap | |
import android.os.Bundle | |
import android.view.ViewGroup | |
import android.webkit.CookieManager | |
import com.google.firebase.iid.FirebaseInstanceId | |
import im.delight.android.webview.AdvancedWebView | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.tasks.await | |
import org.jetbrains.anko.UI | |
import org.jetbrains.anko.frameLayout | |
import org.jetbrains.anko.matchParent | |
import pro.biochemica.extension.advancedWebView | |
import pro.biochemica.extension.isLollipopPlus | |
class MainActivity : BaseActivity(), AdvancedWebView.Listener { | |
private lateinit var webView: AdvancedWebView | |
private lateinit var webClient: WebClient | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
webClient = WebClient() | |
setContentView(UI { | |
frameLayout { | |
layoutParams = ViewGroup.LayoutParams(matchParent, matchParent) | |
webView = advancedWebView { | |
settings.apply { | |
domStorageEnabled = true | |
databaseEnabled = true | |
setAppCacheEnabled(true) | |
setAppCachePath(cacheDir.path) | |
} | |
webViewClient = webClient | |
setCookiesEnabled(true) | |
setThirdPartyCookiesEnabled(true) | |
}.lparams(matchParent, matchParent) | |
} | |
}.view) | |
webView.setListener(this, this) | |
launch { | |
try { | |
webClient.token = FirebaseInstanceId.getInstance().instanceId.await().token | |
} catch (e: Throwable) { | |
e.printStackTrace() | |
} finally { | |
webView.loadUrl("https://biochemica.pro") | |
} | |
} | |
} | |
override fun onResume() { | |
super.onResume() | |
webView.onResume() | |
} | |
override fun onPageStarted(url: String, favicon: Bitmap?) {} | |
override fun onPageFinished(url: String) {} | |
override fun onPageError(errorCode: Int, description: String?, failingUrl: String) { | |
} | |
override fun onDownloadRequested( | |
url: String, | |
suggestedFilename: String?, | |
mimeType: String?, | |
contentLength: Long, | |
contentDisposition: String?, | |
userAgent: String? | |
) { | |
} | |
override fun onExternalPageRequest(url: String) {} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) { | |
webView.onActivityResult(requestCode, resultCode, intent) | |
super.onActivityResult(requestCode, resultCode, intent) | |
} | |
override fun onBackPressed() { | |
if (!webView.onBackPressed()) { | |
return | |
} | |
super.onBackPressed() | |
} | |
override fun onPause() { | |
webView.onPause() | |
super.onPause() | |
} | |
override fun onStop() { | |
if (isLollipopPlus()) { | |
CookieManager.getInstance().flush() | |
} | |
super.onStop() | |
} | |
override fun onDestroy() { | |
webView.onDestroy() | |
super.onDestroy() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment