fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/WebviewConnectActivity.java

236 lines
10 KiB
Java
Raw Normal View History

/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab 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.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2019-11-13 12:54:01 +01:00
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
2018-11-03 12:06:44 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
2018-11-03 12:06:44 +01:00
import android.widget.ImageView;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
import android.widget.ProgressBar;
2018-11-03 12:06:44 +01:00
import android.widget.TextView;
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import org.json.JSONException;
import org.json.JSONObject;
2017-11-18 12:22:41 +01:00
import java.util.HashMap;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.HttpsConnection;
import app.fedilab.android.helper.Helper;
2019-10-14 18:54:04 +02:00
import app.fedilab.android.webview.CustomWebview;
2019-05-24 15:13:28 +02:00
import app.fedilab.android.webview.ProxyHelper;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2018-11-03 12:06:44 +01:00
/**
* Created by Thomas on 24/04/2017.
* Webview to connect accounts
*/
2017-12-12 18:17:01 +01:00
public class WebviewConnectActivity extends BaseActivity {
2019-10-14 18:54:04 +02:00
private CustomWebview webView;
private AlertDialog alert;
private String clientId, clientSecret;
private String instance;
2019-01-02 19:39:40 +01:00
private UpdateAccountInfoAsyncTask.SOCIAL social;
2019-11-15 16:32:25 +01:00
@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();
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-06-30 17:09:07 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2018-05-11 11:28:05 +02:00
case Helper.THEME_LIGHT:
2019-11-09 15:47:38 +01:00
setTheme(R.style.AppTheme_Fedilab);
2018-05-11 11:28:05 +02:00
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
2017-06-30 17:09:07 +02:00
}
2018-05-11 11:28:05 +02:00
setContentView(R.layout.activity_webview_connect);
Bundle b = getIntent().getExtras();
2019-09-06 17:55:14 +02:00
if (b != null) {
instance = b.getString("instance");
2019-01-02 19:39:40 +01:00
social = (UpdateAccountInfoAsyncTask.SOCIAL) b.getSerializable("social");
}
2019-09-06 17:55:14 +02:00
if (instance == null)
finish();
clientId = sharedpreferences.getString(Helper.CLIENT_ID, null);
clientSecret = sharedpreferences.getString(Helper.CLIENT_SECRET, null);
2018-11-03 12:06:44 +01:00
ActionBar actionBar = getSupportActionBar();
2019-09-06 17:55:14 +02:00
if (actionBar != null) {
2019-11-13 12:54:01 +01:00
actionBar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(WebviewConnectActivity.this, R.color.cyanea_primary)));
2018-11-03 12:06:44 +01:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
2019-09-06 17:55:14 +02:00
View view = inflater.inflate(R.layout.simple_bar, new LinearLayout(getApplicationContext()), false);
2018-11-03 12:06:44 +01:00
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
TextView toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
toolbar_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
toolbar_title.setText(R.string.add_account);
}
2017-10-27 15:34:53 +02:00
webView = findViewById(R.id.webviewConnect);
clearCookies(getApplicationContext());
2019-07-16 16:09:12 +02:00
webView.getSettings().setJavaScriptEnabled(true);
if (android.os.Build.VERSION.SDK_INT >= 21) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
} else {
CookieManager.getInstance().setAcceptCookie(true);
}
2017-10-27 15:34:53 +02:00
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);
}
}
});
2019-05-24 15:13:28 +02:00
boolean proxyEnabled = sharedpreferences.getBoolean(Helper.SET_PROXY_ENABLED, false);
2019-09-06 17:55:14 +02:00
if (proxyEnabled) {
2019-05-24 15:13:28 +02:00
String host = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1");
int port = sharedpreferences.getInt(Helper.SET_PROXY_PORT, 8118);
2019-09-06 17:55:14 +02:00
ProxyHelper.setProxy(getApplicationContext(), webView, host, port, WebviewConnectActivity.class.getName());
2019-05-24 15:13:28 +02:00
}
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
2019-09-06 17:55:14 +02:00
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (url.contains(Helper.REDIRECT_CONTENT_WEB)) {
2019-11-15 16:32:25 +01:00
String[] val = url.split("code=");
2019-09-06 17:55:14 +02:00
if (val.length < 2) {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(), getString(R.string.toast_code_error), Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(WebviewConnectActivity.this, LoginActivity.class);
startActivity(myIntent);
finish();
return false;
}
String code = val[1];
2017-11-18 12:22:41 +01:00
final String action = "/oauth/token";
final HashMap<String, String> parameters = new HashMap<>();
parameters.put(Helper.CLIENT_ID, clientId);
parameters.put(Helper.CLIENT_SECRET, clientSecret);
2019-09-06 17:55:14 +02:00
parameters.put(Helper.REDIRECT_URI, Helper.REDIRECT_CONTENT_WEB);
2017-11-18 12:22:41 +01:00
parameters.put("grant_type", "authorization_code");
2019-09-06 17:55:14 +02:00
parameters.put("code", code);
new Thread(new Runnable() {
@Override
2017-11-18 12:22:41 +01:00
public void run() {
try {
2019-05-24 15:13:28 +02:00
final String response = new HttpsConnection(WebviewConnectActivity.this, instance).post(Helper.instanceWithProtocol(getApplicationContext(), instance) + action, 30, parameters, null);
2017-11-18 12:22:41 +01:00
JSONObject resobj;
try {
resobj = new JSONObject(response);
String token = resobj.get("access_token").toString();
2019-01-05 13:00:15 +01:00
String refresh_token = null;
2019-09-06 17:55:14 +02:00
if (resobj.has("refresh_token"))
2019-01-12 09:26:18 +01:00
refresh_token = resobj.get("refresh_token").toString();
2017-11-18 12:22:41 +01:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
editor.apply();
//Update the account with the token;
2019-01-05 13:00:15 +01:00
new UpdateAccountInfoAsyncTask(WebviewConnectActivity.this, token, clientId, clientSecret, refresh_token, instance, social).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2019-09-06 17:55:14 +02:00
} catch (JSONException ignored) {
}
} catch (Exception ignored) {
}
}
}).start();
return true;
}
return false;
}
});
2019-05-24 15:13:28 +02:00
webView.loadUrl(LoginActivity.redirectUserToAuthorizeAndLogin(getApplicationContext(), social, clientId, instance));
}
@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;
}
}
}