mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-17 00:29:50 -04:00
Feat[mslogin]: handle WebView state saving
This commit is contained in:
parent
62dd60d96c
commit
16c7c44313
@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@ -13,6 +14,7 @@ import android.webkit.WebView;
|
|||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
import net.kdt.pojavlaunch.R;
|
import net.kdt.pojavlaunch.R;
|
||||||
@ -24,30 +26,57 @@ public class MicrosoftLoginFragment extends Fragment {
|
|||||||
public static final String TAG = "MICROSOFT_LOGIN_FRAGMENT";
|
public static final String TAG = "MICROSOFT_LOGIN_FRAGMENT";
|
||||||
private WebView mWebview;
|
private WebView mWebview;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
mWebview = (WebView) inflater.inflate(R.layout.fragment_microsoft_login, container, false);
|
mWebview = (WebView) inflater.inflate(R.layout.fragment_microsoft_login, container, false);
|
||||||
CookieManager.getInstance().removeAllCookies(this::onCookiesRemoved);
|
setWebViewSettings();
|
||||||
|
if(savedInstanceState == null) startNewSession();
|
||||||
|
else restoreWebViewState(savedInstanceState);
|
||||||
return mWebview;
|
return mWebview;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("SetJavaScriptEnabled") // required for Microsoft log-in
|
// WebView.restoreState() does not restore the WebSettings or the client, so set them there
|
||||||
public void onCookiesRemoved(Boolean b) {
|
// separately. Note that general state should not be altered here (aka no loading pages, no manipulating back/front lists),
|
||||||
|
// to avoid "undesirable side-effects"
|
||||||
|
@SuppressLint("SetJavaScriptEnabled")
|
||||||
|
private void setWebViewSettings() {
|
||||||
WebSettings settings = mWebview.getSettings();
|
WebSettings settings = mWebview.getSettings();
|
||||||
|
|
||||||
settings.setJavaScriptEnabled(true);
|
settings.setJavaScriptEnabled(true);
|
||||||
mWebview.clearHistory();
|
|
||||||
mWebview.clearCache(true);
|
|
||||||
mWebview.clearFormData();
|
|
||||||
mWebview.clearHistory();
|
|
||||||
mWebview.setWebViewClient(new WebViewTrackClient());
|
mWebview.setWebViewClient(new WebViewTrackClient());
|
||||||
|
}
|
||||||
|
|
||||||
mWebview.loadUrl("https://login.live.com/oauth20_authorize.srf" +
|
private void startNewSession() {
|
||||||
"?client_id=00000000402b5328" +
|
CookieManager.getInstance().removeAllCookies((b)->{
|
||||||
"&response_type=code" +
|
mWebview.clearHistory();
|
||||||
"&scope=service%3A%3Auser.auth.xboxlive.com%3A%3AMBI_SSL" +
|
mWebview.clearCache(true);
|
||||||
"&redirect_url=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf");
|
mWebview.clearFormData();
|
||||||
|
mWebview.clearHistory();
|
||||||
|
mWebview.loadUrl("https://login.live.com/oauth20_authorize.srf" +
|
||||||
|
"?client_id=00000000402b5328" +
|
||||||
|
"&response_type=code" +
|
||||||
|
"&scope=service%3A%3Auser.auth.xboxlive.com%3A%3AMBI_SSL" +
|
||||||
|
"&redirect_url=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restoreWebViewState(Bundle savedInstanceState) {
|
||||||
|
Log.i("MSAuthFragment","Restoring state...");
|
||||||
|
if(mWebview.restoreState(savedInstanceState) == null) {
|
||||||
|
Log.w("MSAuthFragment", "Failed to restore state, starting afresh");
|
||||||
|
// if, for some reason, we failed to restore our session,
|
||||||
|
// just start afresh
|
||||||
|
startNewSession();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||||
|
// Since the value cannot be null, just creaqte a "blank" client. This is done to not let Android
|
||||||
|
// kill us if something happens after onSaveInstanceState
|
||||||
|
mWebview.setWebViewClient(new WebViewClient());
|
||||||
|
mWebview.saveState(outState);
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
// if something happens after this, well, too bad
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Expose webview actions to others */
|
/* Expose webview actions to others */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user