2016-10-13 16:56:31 +02:00
|
|
|
/*
|
|
|
|
This file is part of the Diaspora for Android.
|
|
|
|
|
|
|
|
Diaspora for Android 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.
|
|
|
|
|
|
|
|
Diaspora for Android 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 the Diaspora for Android.
|
|
|
|
|
|
|
|
If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-10-26 16:23:14 +02:00
|
|
|
package com.github.dfa.diaspora_android.web;
|
2016-09-21 21:15:34 +02:00
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
import android.support.v7.app.AlertDialog;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2016-09-21 21:15:34 +02:00
|
|
|
import android.view.View;
|
2016-10-13 16:56:31 +02:00
|
|
|
import android.view.ViewGroup;
|
2016-09-21 21:15:34 +02:00
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
|
|
|
|
import com.github.dfa.diaspora_android.App;
|
|
|
|
import com.github.dfa.diaspora_android.R;
|
|
|
|
import com.github.dfa.diaspora_android.activity.MainActivity;
|
2016-10-26 16:23:14 +02:00
|
|
|
import com.github.dfa.diaspora_android.ui.theme.ThemeHelper;
|
|
|
|
import com.github.dfa.diaspora_android.ui.theme.ThemedFragment;
|
2016-09-21 21:15:34 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.AppLog;
|
2016-10-26 16:23:14 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.AppSettings;
|
2016-09-21 21:15:34 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
2016-10-13 16:56:31 +02:00
|
|
|
* Fragment with a webView and a ProgressBar.
|
|
|
|
* This Fragment retains its instance.
|
|
|
|
* Created by vanitas on 26.09.16.
|
2016-09-21 21:15:34 +02:00
|
|
|
*/
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
public class BrowserFragment extends ThemedFragment {
|
|
|
|
public static final String TAG = "com.github.dfa.diaspora_android.BrowserFragment";
|
2016-09-21 21:15:34 +02:00
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
protected View rootLayout;
|
2016-09-21 21:15:34 +02:00
|
|
|
protected ContextMenuWebView webView;
|
|
|
|
protected ProgressBar progressBar;
|
|
|
|
protected AppSettings appSettings;
|
2016-10-13 16:56:31 +02:00
|
|
|
protected CustomWebViewClient webViewClient;
|
|
|
|
protected WebSettings webSettings;
|
2016-09-21 21:15:34 +02:00
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
protected String pendingUrl;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
AppLog.d(this, "onCreateView()");
|
2016-10-14 01:26:56 +02:00
|
|
|
if (rootLayout == null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
rootLayout = inflater.inflate(R.layout.browser__fragment, container, false);
|
|
|
|
}
|
|
|
|
return rootLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-21 21:15:34 +02:00
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
2016-10-13 16:56:31 +02:00
|
|
|
AppLog.d(this, "onViewCreated()");
|
2016-09-21 21:15:34 +02:00
|
|
|
super.onViewCreated(view, savedInstanceState);
|
2016-10-13 16:56:31 +02:00
|
|
|
|
2016-10-14 01:26:56 +02:00
|
|
|
if (this.appSettings == null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
this.appSettings = ((App) getActivity().getApplication()).getSettings();
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:26:56 +02:00
|
|
|
if (this.webView == null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
this.webView = (ContextMenuWebView) view.findViewById(R.id.webView);
|
2016-10-25 17:37:22 +02:00
|
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
BrowserFragment.this.applyWebViewSettings();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
ProxyHandler.getInstance().addWebView(webView);
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:26:56 +02:00
|
|
|
if (this.progressBar == null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
this.progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:26:56 +02:00
|
|
|
if (pendingUrl != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
loadUrl(pendingUrl);
|
|
|
|
pendingUrl = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
webView.setParentActivity(getActivity());
|
|
|
|
|
|
|
|
this.setRetainInstance(true);
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
super.onDestroyView();
|
2016-09-21 21:15:34 +02:00
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
if (getRetainInstance() && rootLayout.getParent() instanceof ViewGroup) {
|
|
|
|
((ViewGroup) rootLayout.getParent()).removeView(rootLayout);
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
}
|
2016-09-21 21:15:34 +02:00
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
private void applyWebViewSettings() {
|
|
|
|
this.webSettings = webView.getSettings();
|
2016-09-21 21:15:34 +02:00
|
|
|
webSettings.setAllowFileAccess(false);
|
|
|
|
webSettings.setUseWideViewPort(true);
|
|
|
|
webSettings.setLoadWithOverviewMode(true);
|
2016-11-04 03:08:59 +01:00
|
|
|
webSettings.setUserAgentString("Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
|
2016-09-21 21:15:34 +02:00
|
|
|
webSettings.setDomStorageEnabled(true);
|
|
|
|
webSettings.setMinimumFontSize(appSettings.getMinimumFontSize());
|
|
|
|
webSettings.setLoadsImagesAutomatically(appSettings.isLoadImages());
|
|
|
|
webSettings.setAppCacheEnabled(true);
|
|
|
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= 21) {
|
|
|
|
WebView.enableSlowWholeDocumentDraw();
|
|
|
|
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.registerForContextMenu(webView);
|
|
|
|
//webView.setParentActivity(this);
|
|
|
|
webView.setOverScrollMode(WebView.OVER_SCROLL_ALWAYS);
|
|
|
|
|
|
|
|
this.webViewClient = new CustomWebViewClient((App) getActivity().getApplication(), webView);
|
|
|
|
webView.setWebViewClient(webViewClient);
|
2016-10-13 16:56:31 +02:00
|
|
|
webView.setWebChromeClient(new ProgressBarWebChromeClient(webView, progressBar));
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2016-10-14 01:26:56 +02:00
|
|
|
if (webView != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
webSettings.setMinimumFontSize(appSettings.getMinimumFontSize());
|
|
|
|
webSettings.setLoadsImagesAutomatically(appSettings.isLoadImages());
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
|
|
|
protected boolean makeScreenshotOfWebView(boolean hasToShareScreenshot) {
|
|
|
|
AppLog.i(this, "StreamFragment.makeScreenshotOfWebView()");
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= 23) {
|
|
|
|
int hasWRITE_EXTERNAL_STORAGE = getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
|
|
|
if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
|
|
|
new AlertDialog.Builder(getContext())
|
|
|
|
.setMessage(R.string.permissions_screenshot)
|
|
|
|
.setNegativeButton(android.R.string.no, null)
|
|
|
|
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= 23)
|
|
|
|
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
|
|
|
MainActivity.REQUEST_CODE_ASK_PERMISSIONS);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
|
|
|
MainActivity.REQUEST_CODE_ASK_PERMISSIONS);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Date dateNow = new Date();
|
|
|
|
DateFormat dateFormat = new SimpleDateFormat("yy_MM_dd--HH_mm_ss", Locale.getDefault());
|
|
|
|
File fileSaveDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Diaspora");
|
|
|
|
|
|
|
|
String fileSaveName = hasToShareScreenshot ? ".DfA_share.jpg" : String.format("DfA_%s.jpg", dateFormat.format(dateNow));
|
|
|
|
if (!fileSaveDirectory.exists()) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (!fileSaveDirectory.mkdirs()) {
|
|
|
|
AppLog.w(this, "Could not mkdir " + fileSaveDirectory.getAbsolutePath());
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasToShareScreenshot) {
|
|
|
|
Snackbar.make(webView, getString(R.string.share__toast_screenshot) + " " + fileSaveName, Snackbar.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap bitmap;
|
|
|
|
webView.setDrawingCacheEnabled(true);
|
|
|
|
bitmap = Bitmap.createBitmap(webView.getDrawingCache());
|
|
|
|
webView.setDrawingCacheEnabled(false);
|
|
|
|
|
|
|
|
OutputStream bitmapWriter = null;
|
|
|
|
try {
|
|
|
|
bitmapWriter = new FileOutputStream(new File(fileSaveDirectory, fileSaveName));
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bitmapWriter);
|
|
|
|
bitmapWriter.flush();
|
|
|
|
bitmap.recycle();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
if (bitmapWriter != null) {
|
|
|
|
try {
|
|
|
|
bitmapWriter.close();
|
|
|
|
} catch (IOException _ignSaveored) {/* Nothing */}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only show share intent when Action Share Screenshot was selected
|
|
|
|
if (hasToShareScreenshot) {
|
|
|
|
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
|
|
|
|
sharingIntent.setType("image/jpeg");
|
|
|
|
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, webView.getTitle());
|
|
|
|
sharingIntent.putExtra(Intent.EXTRA_TEXT, webView.getUrl());
|
|
|
|
Uri bmpUri = Uri.fromFile(new File(fileSaveDirectory, fileSaveName));
|
|
|
|
sharingIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
|
|
|
|
startActivity(Intent.createChooser(sharingIntent, getString(R.string.action_share_dotdotdot)));
|
|
|
|
} else {
|
|
|
|
// Broadcast that this file is indexable
|
|
|
|
File file = new File(fileSaveDirectory, fileSaveName);
|
|
|
|
Uri uri = Uri.fromFile(file);
|
|
|
|
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
|
|
|
|
getActivity().sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-22 00:29:32 +02:00
|
|
|
@Override
|
2016-10-13 16:56:31 +02:00
|
|
|
public String getFragmentTag() {
|
|
|
|
return TAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
/* Nothing to do here */
|
|
|
|
}
|
|
|
|
|
2016-09-22 00:29:32 +02:00
|
|
|
public boolean onBackPressed() {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (webView.canGoBack()) {
|
2016-10-25 17:37:22 +02:00
|
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
webView.goBack();
|
|
|
|
}
|
|
|
|
});
|
2016-09-22 00:29:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:37:22 +02:00
|
|
|
public void loadUrl(final String url) {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (getWebView() != null) {
|
|
|
|
AppLog.v(this, "loadUrl(): load " + url);
|
2016-10-25 17:37:22 +02:00
|
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
getWebView().loadUrlNew(url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
} else {
|
2016-10-14 01:26:56 +02:00
|
|
|
AppLog.v(this, "loadUrl(): WebView null: Set pending url to " + url);
|
2016-10-13 16:56:31 +02:00
|
|
|
pendingUrl = url;
|
|
|
|
}
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getUrl() {
|
2016-10-14 01:26:56 +02:00
|
|
|
if (getWebView() != null) {
|
2016-10-13 16:56:31 +02:00
|
|
|
return getWebView().getUrl();
|
|
|
|
} else {
|
|
|
|
return pendingUrl;
|
|
|
|
}
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void reloadUrl() {
|
2016-09-22 00:29:32 +02:00
|
|
|
AppLog.v(this, "reloadUrl()");
|
2016-10-14 01:26:56 +02:00
|
|
|
if (getWebView() != null) {
|
2016-10-25 17:37:22 +02:00
|
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
getWebView().reload();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-13 16:56:31 +02:00
|
|
|
}
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ContextMenuWebView getWebView() {
|
|
|
|
return this.webView;
|
|
|
|
}
|
2016-10-13 16:56:31 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void applyColorToViews() {
|
|
|
|
ThemeHelper.updateProgressBarColor(progressBar);
|
|
|
|
}
|
2016-09-21 21:15:34 +02:00
|
|
|
}
|