import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import org.imaginativeworld.oopsnointernet.ConnectionCallback;
import org.imaginativeworld.oopsnointernet.NoInternetDialog;
import org.imaginativeworld.oopsnointernet.NoInternetSnackbar;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private WebView webView;
private SwipeRefreshLayout mySwipeRefreshLayout;
// Dialog No internet connexion
NoInternetDialog noInternetDialog;
// Dialog No internet connexion Snackbar
NoInternetSnackbar noInternetSnackbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
String url ="https://www.brosburger.fr/menu";
webView =(WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
mySwipeRefreshLayout.setRefreshing(false);
}
}
);
//PuB Admob
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//chargement de la page
final ProgressDialog progressBar = new ProgressDialog(MainActivity.this);
progressBar.setMessage("Chargement de la Page...");
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (!progressBar.isShowing()) {
progressBar.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (progressBar.isShowing()) {
progressBar.dismiss();
webView.loadUrl("about:blank");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.app_name);
builder.setIcon(R.drawable.logo);
builder.setMessage("Aucune Connexion Internet Veuillez Redémarrer l'Application")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
});
}
//Dialog No internet connexion
@Override
protected void onResume() {
super.onResume();
// No Internet Dialog
NoInternetDialog.Builder builder1 = new NoInternetDialog.Builder(this);
builder1.setConnectionCallback(new ConnectionCallback() { // Optional
@Override
public void hasActiveConnection(boolean hasActiveConnection) {
// ...
}
});
builder1.setCancelable(false); // Optional
builder1.setNoInternetConnectionTitle("Aucune Connexion Internet"); // Optional
builder1.setNoInternetConnectionMessage("Vérifiez votre connexion puis réessayez"); // Optional
builder1.setShowInternetOnButtons(true); // Optional
builder1.setPleaseTurnOnText("Activer"); // Optional
builder1.setWifiOnButtonText("Wifi"); // Optional
builder1.setMobileDataOnButtonText("Données mobiles"); // Optional
builder1.setOnAirplaneModeTitle("Aucune Connexion Internet"); // Optional
builder1.setOnAirplaneModeMessage("Vous avez activé le mode avion"); // Optional
builder1.setPleaseTurnOffText("Veuillez le désactiver"); // Optional
builder1.setAirplaneModeOffButtonText("Mode Avion"); // Optional
builder1.setShowAirplaneModeOffButtons(true); // Optional
noInternetDialog = builder1.build();
// No Internet Snackbar
NoInternetSnackbar.Builder builder2 = new NoInternetSnackbar.Builder(this, (ViewGroup) findViewById(android.R.id.content));
builder2.setConnectionCallback(new ConnectionCallback() { // Optional
@Override
public void hasActiveConnection(boolean hasActiveConnection) {
// ...
}
});
builder2.setIndefinite(true); // Optional
builder2.setNoInternetConnectionMessage("Aucune Connexion Internet"); // Optional
builder2.setOnAirplaneModeMessage("Vous avez activé le mode avion!"); // Optional
builder2.setSnackbarActionText("Paramètres");
builder2.setShowActionToDismiss(false);
builder2.setSnackbarDismissActionText("OK");
noInternetSnackbar = builder2.build();
}
@Override
protected void onPause() {
super.onPause();
// No Internet Dialog
if (noInternetDialog != null) {
noInternetDialog.destroy();
}
// No Internet Snackbar
if (noInternetSnackbar != null) {
noInternetSnackbar.destroy();
}
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final WebView simpleWebView = (WebView) findViewById(R.id.webView);
if ((keyCode == KeyEvent.KEYCODE_BACK) && simpleWebView.canGoBack()) {
//if Back key pressed and webview can navigate to previous page
simpleWebView.goBack();
// go back to previous page
return true;
}
return super.onKeyDown(keyCode, event);
}
}