2017-06-24 11:38:18 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2017-06-24 11:38:18 +02:00
|
|
|
*
|
|
|
|
* 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
|
2017-06-24 11:38:18 +02:00
|
|
|
* 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,
|
2017-06-24 11:38:18 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.activities;
|
2017-06-24 11:38:18 +02:00
|
|
|
|
|
|
|
|
2018-11-03 12:06:44 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2017-06-24 11:38:18 +02:00
|
|
|
import android.content.Context;
|
2017-07-29 09:23:46 +02:00
|
|
|
import android.content.Intent;
|
2017-06-24 11:38:18 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2018-11-03 12:06:44 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-06-24 11:38:18 +02:00
|
|
|
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;
|
2017-06-24 11:38:18 +02:00
|
|
|
import android.widget.ProgressBar;
|
2018-11-03 12:06:44 +01:00
|
|
|
import android.widget.TextView;
|
2017-07-29 09:23:46 +02:00
|
|
|
import android.widget.Toast;
|
2017-06-24 11:38:18 +02:00
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2017-11-18 12:22:41 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.HttpsConnection;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
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;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
|
2018-11-03 12:06:44 +01:00
|
|
|
|
2017-06-24 11:38:18 +02: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 {
|
2017-06-24 11:38:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
private WebView webView;
|
|
|
|
private AlertDialog alert;
|
|
|
|
private String clientId, clientSecret;
|
|
|
|
private String instance;
|
2019-01-02 19:39:40 +01:00
|
|
|
private UpdateAccountInfoAsyncTask.SOCIAL social;
|
2017-06-24 11:38:18 +02:00
|
|
|
|
2017-07-29 09:23:46 +02:00
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2017-06-24 11:38:18 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
2017-06-30 17:09:07 +02:00
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
2017-07-04 21:15:11 +02:00
|
|
|
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
2018-05-11 11:28:05 +02:00
|
|
|
switch (theme){
|
|
|
|
case Helper.THEME_LIGHT:
|
|
|
|
setTheme(R.style.AppTheme);
|
|
|
|
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
|
|
|
|
2017-06-24 12:51:38 +02:00
|
|
|
setContentView(R.layout.activity_webview_connect);
|
2017-06-24 11:38:18 +02:00
|
|
|
Bundle b = getIntent().getExtras();
|
2019-01-02 19:39:40 +01:00
|
|
|
if(b != null) {
|
2017-06-24 11:38:18 +02:00
|
|
|
instance = b.getString("instance");
|
2019-01-02 19:39:40 +01:00
|
|
|
social = (UpdateAccountInfoAsyncTask.SOCIAL) b.getSerializable("social");
|
|
|
|
}
|
2017-06-24 11:38:18 +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();
|
|
|
|
if( actionBar != null ) {
|
|
|
|
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
assert inflater != null;
|
|
|
|
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.simple_bar, null);
|
|
|
|
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);
|
2019-05-18 11:10:30 +02:00
|
|
|
if (theme == Helper.THEME_LIGHT){
|
2018-11-03 12:06:44 +01:00
|
|
|
Toolbar toolbar = actionBar.getCustomView().findViewById(R.id.toolbar);
|
|
|
|
Helper.colorizeToolbar(toolbar, R.color.black, WebviewConnectActivity.this);
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 15:34:53 +02:00
|
|
|
webView = findViewById(R.id.webviewConnect);
|
2019-02-08 17:33:08 +01:00
|
|
|
webView.getSettings().setJavaScriptEnabled(true);
|
2017-06-24 11:38:18 +02:00
|
|
|
clearCookies(getApplicationContext());
|
2017-10-27 15:34:53 +02:00
|
|
|
final ProgressBar pbar = findViewById(R.id.progress_bar);
|
2017-06-24 11:38:18 +02:00
|
|
|
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);
|
|
|
|
if( proxyEnabled ){
|
|
|
|
String host = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1");
|
|
|
|
int port = sharedpreferences.getInt(Helper.SET_PROXY_PORT, 8118);
|
|
|
|
ProxyHelper.setProxy(getApplicationContext(), webView,host, port,WebviewConnectActivity.class.getName());
|
|
|
|
}
|
2017-06-24 11:38:18 +02:00
|
|
|
|
|
|
|
webView.setWebViewClient(new WebViewClient() {
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
@Override
|
|
|
|
public boolean shouldOverrideUrlLoading(WebView view, String url){
|
|
|
|
super.shouldOverrideUrlLoading(view,url);
|
|
|
|
if( url.contains(Helper.REDIRECT_CONTENT_WEB)){
|
|
|
|
String val[] = url.split("code=");
|
2017-07-29 09:23:46 +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();
|
2017-07-29 09:23:46 +02:00
|
|
|
Intent myIntent = new Intent(WebviewConnectActivity.this, LoginActivity.class);
|
|
|
|
startActivity(myIntent);
|
|
|
|
finish();
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-24 11:38:18 +02:00
|
|
|
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);
|
|
|
|
parameters.put(Helper.REDIRECT_URI,Helper.REDIRECT_CONTENT_WEB);
|
|
|
|
parameters.put("grant_type", "authorization_code");
|
|
|
|
parameters.put("code",code);
|
|
|
|
new Thread(new Runnable(){
|
2017-06-24 11:38:18 +02:00
|
|
|
@Override
|
2017-11-18 12:22:41 +01:00
|
|
|
public void run() {
|
2017-06-24 11:38:18 +02:00
|
|
|
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;
|
|
|
|
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);
|
2017-12-02 14:54:25 +01:00
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
} catch (Exception ignored) {}
|
2017-11-19 12:35:33 +01:00
|
|
|
}}).start();
|
2017-06-24 11:38:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2019-05-24 15:13:28 +02:00
|
|
|
webView.loadUrl(LoginActivity.redirectUserToAuthorizeAndLogin(getApplicationContext(), social, clientId, instance));
|
2017-06-24 11:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|