add settings

add permissions to access ssid, fix error in theme, add settings activity with shared preferences and support German and English
This commit is contained in:
2019-03-02 16:53:39 +01:00
parent 56195eb8f0
commit 026d5a27e1
10 changed files with 303 additions and 30 deletions

View File

@@ -6,8 +6,8 @@ android {
applicationId "de.weseng.wifiweatherstation"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 20190302
versionName "2019.3.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
@@ -25,4 +25,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
}

View File

@@ -3,22 +3,31 @@
package="de.weseng.wifiweatherstation">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- or ACCESS_FINE_LOCATION for SSID -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/DarkTheme"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>

View File

@@ -2,13 +2,18 @@ package de.weseng.wifiweatherstation;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
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;
@@ -17,15 +22,20 @@ import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final String PREFS_NAME = "Settings";
WebView myWebView;
public static String MESSAGE = "de.weseng.wifiweatherstation.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProgressDialog pd = ProgressDialog.show(MainActivity.this, "",
"Bitte warten, die Ansicht wird geladen...", true);
getString(R.string.loading), true);
/* apply layout to the current screen */
setContentView(R.layout.activity_main);
@@ -48,6 +58,11 @@ public class MainActivity extends AppCompatActivity {
myWebView.setBackgroundColor(Color.TRANSPARENT);
myWebView.setPadding(0, 0, 0, 0);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String ssidLocal = "\"" + settings.getString("ssidLocal", getString(R.string.ssid)) + "\"";
MESSAGE = "";
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
@@ -79,19 +94,43 @@ public class MainActivity extends AppCompatActivity {
if (savedInstanceState == null) {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//MESSAGE = MESSAGE.concat("\nmWifi: " + mWifi.toString());
//MESSAGE = MESSAGE.concat("\nmWifi.isConnected(): " + mWifi.isConnected()); // is: true
//MESSAGE = MESSAGE.concat("\nmWifi.getExtraInfo(): " + mWifi.getExtraInfo()); // is: null
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/");
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
//MESSAGE = MESSAGE.concat("\nwifiInfo: " + wifiInfo.toString());
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = findSSIDForWifiInfo(wifiManager, info);
MESSAGE = MESSAGE.concat("\nSSID: " + ssid);
// loadUrl needs: AndroidManifest.xml <application android:usesCleartextTraffic="true"
//if (ssidIntern.contains(ssid)) {
if (ssid.equals(ssidLocal)) {
MESSAGE = MESSAGE.concat("\nconnection: internal");
//Toast.makeText(getApplicationContext(), "intern", Toast.LENGTH_LONG).show();
myWebView.loadUrl(getString(R.string.url_local));
} else {
MESSAGE = MESSAGE.concat("\nconnection: external");
//Toast.makeText(getApplicationContext(), "extern", Toast.LENGTH_LONG).show();
myWebView.loadUrl(getString(R.string.url_global));
}
}
}
}
}
public String findSSIDForWifiInfo(WifiManager wifiManager, WifiInfo wifiInfo) {
List<WifiConfiguration> listOfConfigurations = wifiManager.getConfiguredNetworks();
for (int index = 0; index < listOfConfigurations.size(); index++) {
WifiConfiguration configuration = listOfConfigurations.get(index);
if (configuration.networkId == wifiInfo.getNetworkId()) {
return configuration.SSID;
}
}
return null;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
@@ -107,23 +146,20 @@ public class MainActivity extends AppCompatActivity {
@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);
getMenuInflater().inflate(R.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;
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
settings();
return true;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
@Override
@@ -138,4 +174,12 @@ public class MainActivity extends AppCompatActivity {
myWebView.restoreState(savedInstanceState);
}
/** Called when the user taps the Send button */
public void settings() {
Intent intentSettings = new Intent(this, SettingsActivity.class);
intentSettings.putExtra(MESSAGE, MESSAGE);
startActivity(intentSettings);
}
}

View File

@@ -0,0 +1,127 @@
package de.weseng.wifiweatherstation;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static de.weseng.wifiweatherstation.MainActivity.PREFS_NAME;
public class SettingsActivity extends AppCompatActivity {
public static final String PREFS_NAME = "Settings";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.MESSAGE);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String ssidLocal = settings.getString("ssidLocal", getString(R.string.ssid));
String urlLocal = settings.getString("urlLocal", getString(R.string.url_local));
String urlGlobal = settings.getString("urlGlobal", getString(R.string.url_global));
//message = message.concat("\nSSIDLocal: " + ssidLocal);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
final EditText editTextSsid = findViewById(R.id.editTextSsidLocal);
editTextSsid.setText(ssidLocal);
editTextSsid.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
String value = editTextSsid.getText().toString();
//Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
save("ssidLocal", value);
return true;
}
return false;
}
});
final EditText editTextUrlLocal = findViewById(R.id.editTextUrlLocal);
editTextUrlLocal.setText(urlLocal);
editTextUrlLocal.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
String value = editTextUrlLocal.getText().toString();
//Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
save("urlLocal", value);
return true;
}
return false;
}
});
final EditText editTextUrlGlobal = findViewById(R.id.editTextUrlGlobal);
editTextUrlGlobal.setText(urlGlobal);
editTextUrlGlobal.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
String value = editTextUrlGlobal.getText().toString();
//Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
save("urlGlobal", value);
return true;
}
return false;
}
});
}
@Override
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the
// platform.
save_all();
super.onBackPressed();
return;
}
@Override
protected void onStop(){
super.onStop();
save_all();
}
public void save_all() {
save("ssidLocal", ((EditText)findViewById(R.id.editTextSsidLocal)).getText().toString());
save("urlLocal", ((EditText)findViewById(R.id.editTextUrlLocal)).getText().toString());
save("urlGlobal", ((EditText)findViewById(R.id.editTextUrlGlobal)).getText().toString());
}
public void save(String key, String value) {
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
// Commit the edits!
editor.commit();
}
}

View File

@@ -14,6 +14,6 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"></WebView>
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

View File

@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutSsidLocal"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
app:hintAnimationEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<android.support.design.widget.TextInputEditText
android:id="@+id/editTextSsidLocal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SSID"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutUrlLocal"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:hintAnimationEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayoutSsidLocal">
<android.support.design.widget.TextInputEditText
android:id="@+id/editTextUrlLocal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Local URL"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutUrlGlobal"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:hintAnimationEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayoutUrlLocal">
<android.support.design.widget.TextInputEditText
android:id="@+id/editTextUrlGlobal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Global URL"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings"
android:title="@string/settings"/>
//android:icon="@drawable/ic_new_game"
//android:showAsAction="ifRoom"
</menu>

View File

@@ -0,0 +1,5 @@
<resources>
<string name="app_name">WiFi-Wetter-Station</string>
<string name="settings">Einstellungen</string>
<string name="loading">Bitte warten, die Ansicht wird geladen...</string>
</resources>

View File

@@ -1,4 +1,8 @@
<resources>
<string name="app_name">WiFi Weather Station</string>
<string name="action_settings">Einstellungen</string>
<string name="settings">Settings</string>
<string name="loading">Please wait, the view is loading...</string>
<string name="ssid" translatable="false">NETGEAR26-5G-2</string>
<string name="url_local" translatable="false">http://192.168.1.5/site/wifi-weather-station/</string>
<string name="url_global" translatable="false">http://inetsrv.no-ip.org/site/wifi-weather-station/</string>
</resources>

View File

@@ -1,7 +1,8 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">-->
<style name="DarkTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>