fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java

139 lines
5.8 KiB
Java
Raw Normal View History

2017-05-26 17:20:36 +02:00
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastodon Etalab for mastodon.etalab.gouv.fr
*
* 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.
*
* Mastodon Etalab 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 Thomas Schneider; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
import android.Manifest;
2017-06-30 17:09:07 +02:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
2017-05-26 17:20:36 +02:00
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
2017-05-26 17:20:36 +02:00
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.DownloadListener;
2017-05-26 17:20:36 +02:00
import android.webkit.WebView;
import android.widget.FrameLayout;
2017-05-26 17:20:36 +02:00
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.webview.MastalabWebChromeClient;
import fr.gouv.etalab.mastodon.webview.MastalabWebViewClient;
2017-05-26 17:20:36 +02:00
import mastodon.etalab.gouv.fr.mastodon.R;
import static fr.gouv.etalab.mastodon.helper.Helper.EXTERNAL_STORAGE_REQUEST_CODE;
import static fr.gouv.etalab.mastodon.helper.Helper.manageDownloads;
2017-05-26 17:20:36 +02:00
/**
* Created by Thomas on 24/06/2017.
* Webview activity
2017-05-26 17:20:36 +02:00
*/
public class WebviewActivity extends AppCompatActivity {
2017-05-26 17:20:36 +02:00
private String url;
2017-06-24 12:02:29 +02:00
2017-05-26 17:20:36 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
2017-05-26 17:20:36 +02:00
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_LIGHT);
if( theme == Helper.THEME_LIGHT){
setTheme(R.style.AppTheme);
}else {
setTheme(R.style.AppThemeDark);
}
2017-05-26 17:20:36 +02:00
setContentView(R.layout.activity_webview);
Bundle b = getIntent().getExtras();
if(b != null)
url = b.getString("url", null);
if( url == null)
finish();
if( getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2017-05-26 17:20:36 +02:00
WebView webView = Helper.initializeWebview(WebviewActivity.this, R.id.webview);
2017-05-26 17:20:36 +02:00
setTitle("");
FrameLayout webview_container = (FrameLayout) findViewById(R.id.webview_container);
final ViewGroup videoLayout = (ViewGroup) findViewById(R.id.videoLayout); // Your own view, read class comments
2017-05-26 17:20:36 +02:00
MastalabWebChromeClient mastalabWebChromeClient = new MastalabWebChromeClient(WebviewActivity.this, webView, webview_container, videoLayout);
mastalabWebChromeClient.setOnToggledFullscreen(new MastalabWebChromeClient.ToggledFullscreenCallback() {
@Override
public void toggledFullscreen(boolean fullscreen) {
2017-05-26 17:20:36 +02:00
if (fullscreen) {
videoLayout.setVisibility(View.VISIBLE);
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
} else {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
videoLayout.setVisibility(View.GONE);
2017-05-26 17:20:36 +02:00
}
}
});
webView.setWebChromeClient(mastalabWebChromeClient);
webView.setWebViewClient(new MastalabWebViewClient(WebviewActivity.this));
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
if(Build.VERSION.SDK_INT >= 23 ){
if (ContextCompat.checkSelfPermission(WebviewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(WebviewActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(WebviewActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE);
} else {
manageDownloads(WebviewActivity.this, url);
}
}else{
manageDownloads(WebviewActivity.this, url);
}
}
});
webView.loadUrl(url);
2017-05-26 17:20:36 +02:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
2017-05-26 17:20:36 +02:00
}
}
@Override
public void onDestroy(){
super.onDestroy();
2017-05-26 17:20:36 +02:00
}
}