174 lines
6.7 KiB
Java
174 lines
6.7 KiB
Java
package app.fedilab.fedilabtube;
|
|
/* Copyright 2020 Thomas Schneider
|
|
*
|
|
* This file is a part of TubeLab
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
* Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.os.AsyncTask;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.webkit.CookieManager;
|
|
import android.webkit.CookieSyncManager;
|
|
import android.webkit.WebChromeClient;
|
|
import android.webkit.WebView;
|
|
import android.webkit.WebViewClient;
|
|
import android.widget.ProgressBar;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.net.URL;
|
|
import java.util.HashMap;
|
|
import java.util.regex.Matcher;
|
|
|
|
import app.fedilab.fedilabtube.asynctasks.UpdateAccountInfoAsyncTask;
|
|
import app.fedilab.fedilabtube.client.HttpsConnection;
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
|
import app.fedilab.fedilabtube.webview.CustomWebview;
|
|
|
|
|
|
public class WebviewConnectActivity extends AppCompatActivity {
|
|
|
|
|
|
private CustomWebview webView;
|
|
private AlertDialog alert;
|
|
private String clientId, clientSecret;
|
|
private String url;
|
|
|
|
@SuppressWarnings("deprecation")
|
|
public static void clearCookies(Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
|
|
CookieManager.getInstance().removeAllCookies(null);
|
|
CookieManager.getInstance().flush();
|
|
} else {
|
|
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
|
|
cookieSyncMngr.startSync();
|
|
CookieManager cookieManager = CookieManager.getInstance();
|
|
cookieManager.removeAllCookie();
|
|
cookieManager.removeSessionCookie();
|
|
cookieSyncMngr.stopSync();
|
|
cookieSyncMngr.sync();
|
|
}
|
|
}
|
|
|
|
@SuppressLint("SetJavaScriptEnabled")
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
setContentView(R.layout.activity_webview_connect);
|
|
Bundle b = getIntent().getExtras();
|
|
if (b != null) {
|
|
url = b.getString("url");
|
|
}
|
|
if (url == null)
|
|
finish();
|
|
|
|
clientId = sharedpreferences.getString(Helper.CLIENT_ID, null);
|
|
clientSecret = sharedpreferences.getString(Helper.CLIENT_SECRET, null);
|
|
|
|
webView = findViewById(R.id.webviewConnect);
|
|
clearCookies(WebviewConnectActivity.this);
|
|
webView.getSettings().setJavaScriptEnabled(true);
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
|
|
} else {
|
|
CookieManager.getInstance().setAcceptCookie(true);
|
|
}
|
|
|
|
|
|
final ProgressBar pbar = findViewById(R.id.progress_bar);
|
|
webView.setWebChromeClient(new WebChromeClient() {
|
|
@Override
|
|
public void onProgressChanged(WebView view, int progress) {
|
|
if (progress < 100 && pbar.getVisibility() == ProgressBar.GONE) {
|
|
pbar.setVisibility(ProgressBar.VISIBLE);
|
|
}
|
|
pbar.setProgress(progress);
|
|
if (progress == 100) {
|
|
pbar.setVisibility(ProgressBar.GONE);
|
|
}
|
|
}
|
|
});
|
|
|
|
webView.setWebViewClient(new WebViewClient() {
|
|
|
|
|
|
@Override
|
|
public void onPageFinished(WebView view, String url) {
|
|
Matcher matcher = Helper.redirectPattern.matcher(url);
|
|
if (matcher.find()) {
|
|
String externalAuthToken = matcher.group(1);
|
|
String username = matcher.group(2);
|
|
new Thread(() -> {
|
|
try {
|
|
String newUrl = "https://" + new URL(url).getHost() + "/api/v1/users/token";
|
|
HashMap<String, String> params = new HashMap<>();
|
|
clientId = sharedpreferences.getString(Helper.CLIENT_ID, "null");
|
|
clientSecret = sharedpreferences.getString(Helper.CLIENT_SECRET, "null");
|
|
params.put("client_id", clientId);
|
|
params.put("client_secret", clientSecret);
|
|
params.put("username", username);
|
|
params.put("grant_type", "password");
|
|
params.put("externalAuthToken", externalAuthToken);
|
|
String response = new HttpsConnection(WebviewConnectActivity.this).post(newUrl, 30, params, null);
|
|
JSONObject jsonObject = new JSONObject(response);
|
|
String token = jsonObject.getString("access_token");
|
|
String refresh_token = jsonObject.getString("refresh_token");
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
|
|
editor.putString(Helper.PREF_INSTANCE, new URL(url).getHost());
|
|
editor.apply();
|
|
new UpdateAccountInfoAsyncTask(WebviewConnectActivity.this, token, clientId, clientSecret, refresh_token, new URL(url).getHost()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
finish();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}).start();
|
|
}
|
|
super.onPageFinished(view, url);
|
|
}
|
|
|
|
|
|
});
|
|
webView.loadUrl(url);
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onBackPressed() {
|
|
if (webView != null && webView.canGoBack()) {
|
|
webView.goBack();
|
|
} else {
|
|
super.onBackPressed();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
if (alert != null) {
|
|
alert.dismiss();
|
|
alert = null;
|
|
}
|
|
if (webView != null) {
|
|
webView.destroy();
|
|
}
|
|
}
|
|
} |