112 lines
4.8 KiB
Java
112 lines
4.8 KiB
Java
package app.fedilab.mobilizon.helper;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.graphics.Color;
|
|
import android.util.Base64;
|
|
import android.webkit.CookieManager;
|
|
import android.webkit.WebSettings;
|
|
import android.webkit.WebStorage;
|
|
import android.webkit.WebView;
|
|
|
|
import java.io.InputStream;
|
|
|
|
/* Copyright 2020 Thomas Schneider
|
|
*
|
|
* This file is a part of Mobilizon app
|
|
*
|
|
* 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.
|
|
*
|
|
* Mobilizon app 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 Mobilizon app; if not,
|
|
* see <http://www.gnu.org/licenses>. */
|
|
public class Helper {
|
|
|
|
public static final String TAG = "mobilizon_app";
|
|
public static final String APP_PREFS = "app_prefs";
|
|
public static final String PREF_INSTANCE = "instance";
|
|
public static final String PREF_VERSION = "version_";
|
|
/**
|
|
* Returns the preferred instance
|
|
*
|
|
* @param context Context
|
|
* @return String domain instance
|
|
*/
|
|
public static String getLiveInstance(Context context) {
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
return sharedpreferences.getString(Helper.PREF_INSTANCE, "mobilizon.fr");
|
|
}
|
|
|
|
/**
|
|
* Initialize the webview
|
|
*
|
|
* @param context Context
|
|
* @param webView WebView
|
|
*/
|
|
@SuppressLint("SetJavaScriptEnabled")
|
|
public static void initializeWebview(Context context, WebView webView, String instance) {
|
|
|
|
webView.getSettings().setJavaScriptEnabled(true);
|
|
webView.getSettings().setUseWideViewPort(true);
|
|
webView.getSettings().setLoadWithOverviewMode(true);
|
|
webView.getSettings().setSupportZoom(true);
|
|
webView.getSettings().setDisplayZoomControls(false);
|
|
webView.getSettings().setBuiltInZoomControls(true);
|
|
webView.getSettings().setLoadsImagesAutomatically(true);
|
|
webView.getSettings().setSupportMultipleWindows(false);
|
|
webView.getSettings().setGeolocationDatabasePath(context.getFilesDir().getPath()+"/" + instance);
|
|
webView.getSettings().setGeolocationEnabled(true);
|
|
webView.getSettings().setAppCachePath(context.getCacheDir().getPath());
|
|
webView.getSettings().setAppCacheEnabled(true);
|
|
webView.getSettings().setDatabaseEnabled(true);
|
|
webView.getSettings().setAllowFileAccess(true);
|
|
webView.getSettings().setAllowContentAccess(true);
|
|
webView.getSettings().setAllowFileAccessFromFileURLs(true);
|
|
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
|
|
webView.getSettings().setDomStorageEnabled(true);
|
|
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
|
|
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
|
|
CookieManager cookieManager = CookieManager.getInstance();
|
|
cookieManager.setAcceptThirdPartyCookies(webView, true);
|
|
webView.setBackgroundColor(Color.TRANSPARENT);
|
|
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
|
|
webView.requestFocus();
|
|
}
|
|
|
|
|
|
@SuppressWarnings({"ResultOfMethodCallIgnored"})
|
|
public static void injectCSS(Activity activity, WebView view, String cssFile) {
|
|
try {
|
|
InputStream inputStream = activity.getAssets().open(cssFile);
|
|
byte[] buffer = new byte[inputStream.available()];
|
|
inputStream.read(buffer);
|
|
inputStream.close();
|
|
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
|
|
view.loadUrl("javascript:(function() {" +
|
|
"var parent = document.getElementsByTagName('head').item(0);" +
|
|
"var style = document.createElement('style');" +
|
|
"style.type = 'text/css';" +
|
|
// Tell the browser to BASE64-decode the string into your script !!!
|
|
"style.innerHTML = window.atob('" + encoded + "');" +
|
|
"parent.appendChild(style)" +
|
|
"})()");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void clearCookies() {
|
|
WebStorage.getInstance().deleteAllData();
|
|
CookieManager.getInstance().removeAllCookies(null);
|
|
CookieManager.getInstance().flush();
|
|
}
|
|
|
|
}
|