1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

Convert Login to kotlin

This commit is contained in:
Ammar Githam 2021-05-28 09:00:16 +09:00
parent a49221775e
commit c9f1e2eff4

View File

@ -1,141 +1,119 @@
package awais.instagrabber.activities; package awais.instagrabber.activities
import android.annotation.SuppressLint; import android.annotation.SuppressLint
import android.content.Intent; import android.content.Intent
import android.graphics.Bitmap; import android.graphics.Bitmap
import android.os.Build; import android.os.Build
import android.os.Bundle; import android.os.Bundle
import android.view.LayoutInflater; import android.view.LayoutInflater
import android.view.View; import android.view.View
import android.webkit.CookieManager; import android.webkit.*
import android.webkit.CookieSyncManager; import android.widget.Toast
import android.webkit.WebChromeClient; import awais.instagrabber.R
import android.webkit.WebSettings; import awais.instagrabber.databinding.ActivityLoginBinding
import android.webkit.WebView; import awais.instagrabber.utils.Constants
import android.webkit.WebViewClient; import awais.instagrabber.utils.getCookie
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.annotation.Nullable; class Login : BaseLanguageActivity(), View.OnClickListener {
private var webViewUrl: String? = null
private var ready = false
private lateinit var loginBinding: ActivityLoginBinding
import awais.instagrabber.R; private val webChromeClient = WebChromeClient()
import awais.instagrabber.databinding.ActivityLoginBinding; private val webViewClient: WebViewClient = object : WebViewClient() {
import awais.instagrabber.utils.Constants; override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
import awais.instagrabber.utils.CookieUtils; webViewUrl = url
import awais.instagrabber.utils.TextUtils;
public final class Login extends BaseLanguageActivity implements View.OnClickListener {
private final WebViewClient webViewClient = new WebViewClient() {
@Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
webViewUrl = url;
} }
@Override override fun onPageFinished(view: WebView, url: String) {
public void onPageFinished(final WebView view, final String url) { webViewUrl = url
webViewUrl = url; val mainCookie = getCookie(url)
final String mainCookie = CookieUtils.getCookie(url); if (mainCookie.isNullOrBlank() || !mainCookie.contains("; ds_user_id=")) {
if (TextUtils.isEmpty(mainCookie) || !mainCookie.contains("; ds_user_id=")) { ready = true
ready = true; return
return;
} }
if (mainCookie.contains("; ds_user_id=") && ready) { if (mainCookie.contains("; ds_user_id=") && ready) {
returnCookieResult(mainCookie); returnCookieResult(mainCookie)
} }
} }
};
private void returnCookieResult(final String mainCookie) {
final Intent intent = new Intent();
intent.putExtra("cookie", mainCookie);
setResult(Constants.LOGIN_RESULT_CODE, intent);
finish();
} }
private final WebChromeClient webChromeClient = new WebChromeClient(); private fun returnCookieResult(mainCookie: String?) {
private String webViewUrl; val intent = Intent()
private boolean ready = false; intent.putExtra("cookie", mainCookie)
private ActivityLoginBinding loginBinding; setResult(Constants.LOGIN_RESULT_CODE, intent)
finish()
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginBinding = ActivityLoginBinding.inflate(LayoutInflater.from(getApplicationContext()));
setContentView(loginBinding.getRoot());
initWebView();
loginBinding.cookies.setOnClickListener(this);
loginBinding.refresh.setOnClickListener(this);
} }
@Override override fun onCreate(savedInstanceState: Bundle?) {
public void onClick(final View v) { super.onCreate(savedInstanceState)
if (v == loginBinding.refresh) { loginBinding = ActivityLoginBinding.inflate(LayoutInflater.from(applicationContext))
loginBinding.webView.loadUrl("https://instagram.com/"); setContentView(loginBinding.root)
return; initWebView()
loginBinding.cookies.setOnClickListener(this)
loginBinding.refresh.setOnClickListener(this)
} }
if (v == loginBinding.cookies) {
final String mainCookie = CookieUtils.getCookie(webViewUrl); override fun onClick(v: View) {
if (TextUtils.isEmpty(mainCookie) || !mainCookie.contains("; ds_user_id=")) { if (v === loginBinding.refresh) {
Toast.makeText(this, R.string.login_error_loading_cookies, Toast.LENGTH_SHORT).show(); loginBinding.webView.loadUrl("https://instagram.com/")
return; return
} }
returnCookieResult(mainCookie); if (v === loginBinding.cookies) {
val mainCookie = getCookie(webViewUrl)
if (mainCookie.isNullOrBlank() || !mainCookie.contains("; ds_user_id=")) {
Toast.makeText(this, R.string.login_error_loading_cookies, Toast.LENGTH_SHORT).show()
return
}
returnCookieResult(mainCookie)
} }
} }
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
private void initWebView() { private fun initWebView() {
if (loginBinding != null) { loginBinding.webView.webChromeClient = webChromeClient
loginBinding.webView.setWebChromeClient(webChromeClient); loginBinding.webView.webViewClient = webViewClient
loginBinding.webView.setWebViewClient(webViewClient); val webSettings = loginBinding.webView.settings
final WebSettings webSettings = loginBinding.webView.getSettings(); webSettings.userAgentString =
if (webSettings != null) { "Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Mobile Safari/537.36"
webSettings.setUserAgentString("Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.105 Mobile Safari/537.36"); webSettings.javaScriptEnabled = true
webSettings.setJavaScriptEnabled(true); webSettings.domStorageEnabled = true
webSettings.setDomStorageEnabled(true); webSettings.setSupportZoom(true)
webSettings.setSupportZoom(true); webSettings.builtInZoomControls = true
webSettings.setBuiltInZoomControls(true); webSettings.displayZoomControls = false
webSettings.setDisplayZoomControls(false); webSettings.loadWithOverviewMode = true
webSettings.setLoadWithOverviewMode(true); webSettings.useWideViewPort = true
webSettings.setUseWideViewPort(true); webSettings.allowFileAccessFromFileURLs = true
webSettings.setAllowFileAccessFromFileURLs(true); webSettings.allowUniversalAccessFromFileURLs = true
webSettings.setAllowUniversalAccessFromFileURLs(true); webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null); CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush(); CookieManager.getInstance().flush()
} else { } else {
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(getApplicationContext()); val cookieSyncMngr = CookieSyncManager.createInstance(applicationContext)
cookieSyncMngr.startSync(); cookieSyncMngr.startSync()
CookieManager cookieManager = CookieManager.getInstance(); val cookieManager = CookieManager.getInstance()
cookieManager.removeAllCookie(); cookieManager.removeAllCookie()
cookieManager.removeSessionCookie(); cookieManager.removeSessionCookie()
cookieSyncMngr.stopSync(); cookieSyncMngr.stopSync()
cookieSyncMngr.sync(); cookieSyncMngr.sync()
}
loginBinding.webView.loadUrl("https://instagram.com/");
} }
loginBinding.webView.loadUrl("https://instagram.com/")
} }
@Override override fun onPause() {
protected void onPause() { loginBinding.webView.onPause()
if (loginBinding != null) loginBinding.webView.onPause(); super.onPause()
super.onPause();
} }
@Override override fun onResume() {
protected void onResume() { super.onResume()
super.onResume(); loginBinding.webView.onResume()
if (loginBinding != null) loginBinding.webView.onResume();
} }
@Override override fun onDestroy() {
protected void onDestroy() { loginBinding.webView.destroy()
if (loginBinding != null) loginBinding.webView.destroy(); super.onDestroy()
super.onDestroy();
} }
} }