mobilizon-android-app/app/src/main/java/app/fedilab/mobilizon/helper/Helper.java

92 lines
4.0 KiB
Java

package app.fedilab.mobilizon.helper;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
/* 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 String APP_SHARED_PREF = "app_shared_prefs";
public static String LAST_LOCATION = "last_location";
public static int LOCATION_REFRESH_TIME = 60000;
public static int LOCATION_REFRESH_DISTANCE = 100;
/**
* 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, "test.mobilizon.org");
}
/**
* Initialize the webview
* @param context Context
* @param webView WebView
*/
@SuppressLint("SetJavaScriptEnabled")
public static void initializeWebview(Context context, WebView webView) {
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().setAllowContentAccess(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setGeolocationDatabasePath(context.getFilesDir().getPath() );
webView.getSettings().setGeolocationEnabled(true);
webView.getSettings().setAppCachePath(context.getCacheDir().getPath());
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
}
webView.setBackgroundColor(Color.TRANSPARENT);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
}
}