|
|
|
|
@@ -1,13 +1,141 @@
|
|
|
|
|
package de.weseng.wifiweatherstation;
|
|
|
|
|
|
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
|
import android.webkit.WebView;
|
|
|
|
|
import android.webkit.WebViewClient;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
WebView myWebView;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
final ProgressDialog pd = ProgressDialog.show(MainActivity.this, "",
|
|
|
|
|
"Bitte warten, die Ansicht wird geladen...", true);
|
|
|
|
|
|
|
|
|
|
/* apply layout to the current screen */
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
|
|
|
|
/* find WebView element by its id*/
|
|
|
|
|
if (savedInstanceState != null)
|
|
|
|
|
((WebView) findViewById(R.id.webview)).restoreState(savedInstanceState);
|
|
|
|
|
myWebView = findViewById(R.id.webview);
|
|
|
|
|
|
|
|
|
|
/* create new settings for WebView element */
|
|
|
|
|
WebSettings webSettings = myWebView.getSettings();
|
|
|
|
|
webSettings.setJavaScriptEnabled(true);
|
|
|
|
|
webSettings.setLoadWithOverviewMode(true);
|
|
|
|
|
webSettings.setUseWideViewPort(true);
|
|
|
|
|
//webSettings.setSupportZoom(true);
|
|
|
|
|
//webSettings.setBuiltInZoomControls(true);
|
|
|
|
|
|
|
|
|
|
//myWebView.setBackgroundColor(getResources().getColor(R.color.black));
|
|
|
|
|
//myWebView.setBackgroundResource(Color.TRANSPARENT);
|
|
|
|
|
myWebView.setBackgroundColor(Color.TRANSPARENT);
|
|
|
|
|
myWebView.setPadding(0, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
myWebView.setWebViewClient(new WebViewClient() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
|
|
pd.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onPageFinished(WebView view, String url) {
|
|
|
|
|
pd.dismiss();
|
|
|
|
|
String webUrl = myWebView.getUrl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
|
|
/* will open a new web page in webview*/
|
|
|
|
|
view.loadUrl(url);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
|
|
/*stop loading and show error.html page from assets folder*/
|
|
|
|
|
/*see: http://xbsoftware.com/blog/web-android-two-steps/ */
|
|
|
|
|
//view.stopLoading();
|
|
|
|
|
//view.loadUrl(String.format("file:///android_asset/error.html?code=%s&description=%s&url=%s", Uri.encode(String.valueOf(errorCode)), Uri.encode(description), Uri.encode(failingUrl)));
|
|
|
|
|
Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
/* set URL site */
|
|
|
|
|
if (savedInstanceState == null) {
|
|
|
|
|
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
|
|
|
|
|
|
|
if (mWifi.isConnected()) {
|
|
|
|
|
// loadUrl needs: AndroidManifest.xml <application android:usesCleartextTraffic="true"
|
|
|
|
|
if (mWifi.getExtraInfo() != null && mWifi.getExtraInfo().equals("\"FRITZ!Box Fon WLAN 7360\"")) {
|
|
|
|
|
//Toast.makeText(getApplicationContext(), "intern", Toast.LENGTH_LONG).show();
|
|
|
|
|
myWebView.loadUrl("http://192.168.178.5/site/wifi-weather-station/");
|
|
|
|
|
} else {
|
|
|
|
|
//Toast.makeText(getApplicationContext(), "extern", Toast.LENGTH_LONG).show();
|
|
|
|
|
myWebView.loadUrl("http://inetsrv.no-ip.org/site/wifi-weather-station/");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
|
// Check if the key event was the Back button and if there's history
|
|
|
|
|
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
|
|
|
|
|
myWebView.goBack();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// If it wasn't the Back key or there's no web page history, bubble up to the default
|
|
|
|
|
// system behavior (probably exit the activity)
|
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
|
//getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
// Handle action bar item clicks here. The action bar will
|
|
|
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
|
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
|
|
int id = item.getItemId();
|
|
|
|
|
|
|
|
|
|
//noinspection SimplifiableIfStatement
|
|
|
|
|
if (id == R.string.action_settings) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
|
myWebView.saveState(outState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
|
|
myWebView.restoreState(savedInstanceState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|