Init
This commit is contained in:
commit
f8f4b25050
29 changed files with 917 additions and 0 deletions
|
@ -0,0 +1,13 @@
|
|||
package me.datenknoten.tueroeffner;
|
||||
|
||||
import android.app.Application;
|
||||
import android.test.ApplicationTestCase;
|
||||
|
||||
/**
|
||||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
|
||||
*/
|
||||
public class ApplicationTest extends ApplicationTestCase<Application> {
|
||||
public ApplicationTest() {
|
||||
super(Application.class);
|
||||
}
|
||||
}
|
25
app/src/main/AndroidManifest.xml
Normal file
25
app/src/main/AndroidManifest.xml
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="me.datenknoten.tueroeffner" >
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:label="@string/app_name" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
|
@ -0,0 +1,44 @@
|
|||
package me.datenknoten.tueroeffner;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* Created by tim on 24.01.15.
|
||||
*/
|
||||
public class DerivBroadcastReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
final String action = intent.getAction();
|
||||
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
|
||||
NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||
if (nwInfo.getExtraInfo().equals(MainActivity.networkSSID) && nwInfo.isConnected()) {
|
||||
View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
|
||||
Button button = (Button) rootView.findViewById(R.id.button2);
|
||||
button.setEnabled(true);
|
||||
button = (Button) rootView.findViewById(R.id.button3);
|
||||
button.setEnabled(true);
|
||||
button = (Button) rootView.findViewById(R.id.button);
|
||||
button.setEnabled(true);
|
||||
Toast.makeText(context, context.getString(R.string.wlan_connected), Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
|
||||
Button button = (Button) rootView.findViewById(R.id.button2);
|
||||
button.setEnabled(false);
|
||||
button = (Button) rootView.findViewById(R.id.button3);
|
||||
button.setEnabled(false);
|
||||
button = (Button) rootView.findViewById(R.id.button);
|
||||
button.setEnabled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
199
app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java
Normal file
199
app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java
Normal file
|
@ -0,0 +1,199 @@
|
|||
package me.datenknoten.tueroeffner;
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.os.Build;
|
||||
import android.widget.Button;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Switch;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
|
||||
final static String networkSSID = "\"KrautSpace\"";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
if (savedInstanceState == null) {
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add(R.id.container, new PlaceholderFragment())
|
||||
.commit();
|
||||
}
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ;
|
||||
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
||||
registerReceiver(new DerivBroadcastReceiver(),intentFilter);
|
||||
}
|
||||
|
||||
|
||||
@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.id.action_settings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* A placeholder fragment containing a simple view.
|
||||
*/
|
||||
public static class PlaceholderFragment extends Fragment {
|
||||
|
||||
public PlaceholderFragment() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
|
||||
|
||||
final SharedPreferences door_pref = getActivity().getSharedPreferences("tueroeffner",Context.MODE_PRIVATE);
|
||||
String door_key = door_pref.getString(getString(R.string.door_key),"");
|
||||
final EditText key_editor = (EditText) rootView.findViewById(R.id.txtPass);
|
||||
key_editor.setText(door_key);
|
||||
|
||||
key_editor.addTextChangedListener(new TextWatcher() {
|
||||
|
||||
public void afterTextChanged(Editable s) {
|
||||
|
||||
// you can call or do what you want with your EditText here
|
||||
SharedPreferences.Editor editor = door_pref.edit();
|
||||
editor.putString(getString(R.string.door_key),key_editor.getText().toString());
|
||||
editor.apply();
|
||||
|
||||
}
|
||||
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
});
|
||||
|
||||
Switch s = (Switch) rootView.findViewById(R.id.switchWLAN);
|
||||
|
||||
if (s != null) {
|
||||
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
Context context = rootView.getContext();
|
||||
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
|
||||
if (isChecked) {
|
||||
WifiConfiguration conf = new WifiConfiguration();
|
||||
conf.SSID = networkSSID;
|
||||
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
|
||||
int retval = wifiManager.addNetwork(conf);
|
||||
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
|
||||
|
||||
|
||||
for( WifiConfiguration i : list ) {
|
||||
if(i.SSID != null && i.SSID.equals(networkSSID)) {
|
||||
wifiManager.enableNetwork(i.networkId, false);
|
||||
} else {
|
||||
wifiManager.disableNetwork(i.networkId);
|
||||
}
|
||||
}
|
||||
wifiManager.disconnect();
|
||||
wifiManager.reconnect();
|
||||
Toast.makeText(buttonView.getContext(), getString(R.string.wlan_activated), Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
|
||||
for( WifiConfiguration i : list ) {
|
||||
if(i.SSID != null && i.SSID.equals(networkSSID)) {
|
||||
wifiManager.removeNetwork(i.networkId);
|
||||
} else {
|
||||
wifiManager.enableNetwork(i.networkId,false);
|
||||
}
|
||||
}
|
||||
wifiManager.disconnect();
|
||||
wifiManager.reconnect();
|
||||
Toast.makeText(buttonView.getContext(), getString(R.string.wlan_deactivated), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return rootView;
|
||||
}
|
||||
}
|
||||
|
||||
public void buttonOpenOuterDoor(View v) {
|
||||
executeCommand("outdoor_buzz");
|
||||
Toast.makeText(v.getContext(), getString(R.string.buzzer_success), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void executeCommand(String cmd) {
|
||||
EditText key_editor = (EditText) findViewById(R.id.txtPass);
|
||||
|
||||
try {
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpGet request = new HttpGet();
|
||||
URI uri = new URI("https://tuer.hackspace-jena.de/cgi-bin/kraut.space?secret="+key_editor.getText()+"&cmd="+cmd);
|
||||
request.setURI(uri);
|
||||
HttpResponse response = client.execute(request);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClientProtocolException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void buttonOpenInnerDoor(View v) {
|
||||
executeCommand("indoor_unlock");
|
||||
Toast.makeText(v.getContext(), getString(R.string.door_unlock), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public void buttonUnluckInnerDoor(View v) {
|
||||
executeCommand("indoor_open");
|
||||
Toast.makeText(v.getContext(), getString(R.string.door_open), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
BIN
app/src/main/res/drawable-hdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/drawable-hdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 9.2 KiB |
BIN
app/src/main/res/drawable-mdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/drawable-mdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.1 KiB |
BIN
app/src/main/res/drawable-xhdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/drawable-xhdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
BIN
app/src/main/res/drawable-xxhdpi/ic_launcher.png
Normal file
BIN
app/src/main/res/drawable-xxhdpi/ic_launcher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
4
app/src/main/res/layout/activity_main.xml
Normal file
4
app/src/main/res/layout/activity_main.xml
Normal file
|
@ -0,0 +1,4 @@
|
|||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent"
|
||||
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />
|
62
app/src/main/res/layout/fragment_main.xml
Normal file
62
app/src/main/res/layout/fragment_main.xml
Normal file
|
@ -0,0 +1,62 @@
|
|||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
tools:context=".MainActivity$PlaceholderFragment">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignParentStart="true">
|
||||
|
||||
<Switch
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/wlan_switcher"
|
||||
android:id="@+id/switchWLAN"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:checked="false" />
|
||||
|
||||
<EditText
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/txtPass"
|
||||
android:inputType="text" />
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/button_outer_door"
|
||||
android:id="@+id/button3"
|
||||
android:onClick="buttonOpenOuterDoor"
|
||||
android:clickable="false"
|
||||
android:enabled="false" />
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/button_inner_door"
|
||||
android:id="@+id/button2"
|
||||
android:onClick="buttonOpenInnerDoor"
|
||||
android:enabled="false" />
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/button"
|
||||
android:text="@string/button_inner_door_open"
|
||||
android:enabled="false" />
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
6
app/src/main/res/menu/menu_main.xml
Normal file
6
app/src/main/res/menu/menu_main.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<menu 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" tools:context=".MainActivity">
|
||||
<item android:id="@+id/action_settings" android:title="@string/action_settings"
|
||||
android:orderInCategory="100" app:showAsAction="never" />
|
||||
</menu>
|
6
app/src/main/res/values-w820dp/dimens.xml
Normal file
6
app/src/main/res/values-w820dp/dimens.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<resources>
|
||||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
|
||||
(such as screen margins) for screens with more than 820dp of available width. This
|
||||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
|
||||
<dimen name="activity_horizontal_margin">64dp</dimen>
|
||||
</resources>
|
5
app/src/main/res/values/dimens.xml
Normal file
5
app/src/main/res/values/dimens.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<resources>
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
</resources>
|
23
app/src/main/res/values/strings.xml
Normal file
23
app/src/main/res/values/strings.xml
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<string name="app_name">Türöffner</string>
|
||||
<string name="hello_world">Hello world!</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="unlock_door">Sesam öffne dich</string>
|
||||
<string name="button_wlan">✘ WLAN</string>
|
||||
<string name="button_outer_door">Buzzer</string>
|
||||
<string name="button_inner_door">Aufschliesen</string>
|
||||
<string name="button_wlan_connected">✓ WLAN</string>
|
||||
<string name="could_not_connect">Konnte nicht zum WLAN „Krautspace“ verbinden.</string>
|
||||
<string name="door_key">DoorKey</string>
|
||||
<string name="wlan_switcher">Tür WLAN aktivieren</string>
|
||||
<string name="button_inner_door_open">Öffnen</string>
|
||||
<string name="wlan_activated">WLAN „KrautSpace“ aktiviert.</string>
|
||||
<string name="wlan_connected">WLAN „KrautSpace“ verbunden.</string>
|
||||
<string name="wlan_deactivated">WLAN „KrautSpace“ deaktiviert.</string>
|
||||
<string name="buzzer_success">Buzzer betätigt</string>
|
||||
<string name="door_unlock">Tür entsperrt</string>
|
||||
<string name="door_open">Tür geöffnet</string>
|
||||
|
||||
</resources>
|
8
app/src/main/res/values/styles.xml
Normal file
8
app/src/main/res/values/styles.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<resources>
|
||||
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
Reference in a new issue