From: pk910 Date: Thu, 4 Feb 2016 10:56:06 +0000 (+0100) Subject: added launcher icon, fixed crash due to AppContext = null (damn Android GC), made... X-Git-Url: http://git.pk910.de/?p=DHBWCampusApp.git;a=commitdiff_plain;h=b5169aeadcc32f9c7388447e5bf9096936837072;hp=60b04973e203241d319e2d8ee6c150d641b02a65 added launcher icon, fixed crash due to AppContext = null (damn Android GC), made WiFi configuration more flexible --- diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 89c3c04..fe7a3d8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,7 +19,7 @@ android:theme="@style/AppTheme"> diff --git a/app/src/main/java/de/dhbwloe/campusapp/CampusApp.java b/app/src/main/java/de/dhbwloe/campusapp/CampusApp.java index f201da6..15ae6dc 100644 --- a/app/src/main/java/de/dhbwloe/campusapp/CampusApp.java +++ b/app/src/main/java/de/dhbwloe/campusapp/CampusApp.java @@ -63,11 +63,12 @@ public class CampusApp extends FragmentActivity { boolean instantRestore = false; if(savedInstanceState != null) { long lastrun = savedInstanceState.getLong("lastrun"); - if(((new Date()).getTime()/1000) - lastrun < 30) { + lastrun = ((new Date()).getTime()/1000) - lastrun; + Log.i("CampusApp", "Restored from Idle state! Idled: "+lastrun+" secs"); + if(lastrun < 30) { instantRestore = true; AppContext.setTitle(savedInstanceState.getString("activetitle")); - } @@ -103,6 +104,8 @@ public class CampusApp extends FragmentActivity { // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); + + AppContext = null; // simulate GC } public void prepareMainUi() { @@ -284,6 +287,11 @@ public class CampusApp extends FragmentActivity { /* nfc listener related callbacks */ @Override public void onResume() { + if(AppContext == null) + AppContext = new CampusAppContext(this, R.id.fragment_container); + else + AppContext.setMainActivity(this); + super.onResume(); Log.i("CampusApp", "onResume event"); AppContext.getNfcCardListener().resumeForefrontDispatcher(); diff --git a/app/src/main/java/de/dhbwloe/campusapp/database/DatabaseManager.java b/app/src/main/java/de/dhbwloe/campusapp/database/DatabaseManager.java index 326b914..ccf9a04 100644 --- a/app/src/main/java/de/dhbwloe/campusapp/database/DatabaseManager.java +++ b/app/src/main/java/de/dhbwloe/campusapp/database/DatabaseManager.java @@ -43,8 +43,15 @@ public class DatabaseManager { AppContext = context; } - public void initializeDatabase() { + private void openDatabase() { + if(database != null) + return; + database = AppContext.getMainActivity().openOrCreateDatabase(DATABASE_NAME, Activity.MODE_PRIVATE, null); + } + + public void initializeDatabase() { + openDatabase(); database.execSQL("CREATE TABLE IF NOT EXISTS Version(Version INT);"); Cursor resultSet = database.rawQuery("Select * from Version", null); @@ -162,6 +169,7 @@ public class DatabaseManager { } public void addSearchIndices(SearchIndices[] indices) { + openDatabase(); for(int i = 0; i < indices.length; i++) { String[] whereArgs = new String[] { indices[i].getKeyName() @@ -195,6 +203,7 @@ public class DatabaseManager { } public SearchIndices[] performSearchRequest(String query, int maxResults) { + openDatabase(); String[] whereArgs = new String[] { "%" + query + "%" }; @@ -228,6 +237,7 @@ public class DatabaseManager { } public void setRuntimeCache(String name, String value) { + openDatabase(); long now = (new Date()).getTime() / 1000; String[] whereArgs = new String[] { name @@ -261,6 +271,7 @@ public class DatabaseManager { } public String getRuntimeCache(String name) { + openDatabase(); String value = null; String[] whereArgs = new String[] { name @@ -274,6 +285,7 @@ public class DatabaseManager { } public void addNfcCardData(NfcCardData nfcCardData) { + openDatabase(); String[] whereArgs = new String[] { Integer.toString(nfcCardData.getUniqueId()), Long.toString(nfcCardData.getLastUpdate()) @@ -306,6 +318,7 @@ public class DatabaseManager { } public NfcCardData[] getNfcCardData(int lastLimit) { + openDatabase(); String value = null; Cursor resultSet; if(lastLimit > 0) { @@ -329,6 +342,7 @@ public class DatabaseManager { } public NfcCardData getLatestNfcCardData(int cardId) { + openDatabase(); String value = null; String[] whereArgs = { Integer.toString(cardId) @@ -343,66 +357,77 @@ public class DatabaseManager { } public void updateCourseCalendar(CourseEvent event) { + openDatabase(); if(vorlesungsplanDBHelper == null) vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database); vorlesungsplanDBHelper.updateCourseCalendar(event); } public CourseEvent[] getCourseCalendarEvents(String coursename, long timeFrom, long timeTo) { + openDatabase(); if(vorlesungsplanDBHelper == null) vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database); return vorlesungsplanDBHelper.getCourseCalendarEvents(coursename, timeFrom, timeTo); } public CourseGroup getCourseGroup(int courseGroupId) { + openDatabase(); if(vorlesungsplanDBHelper == null) vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database); return vorlesungsplanDBHelper.getCourseGroup(courseGroupId); } public CourseGroup getCourseGroup(String coursename, String groupname) { + openDatabase(); if(vorlesungsplanDBHelper == null) vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database); return vorlesungsplanDBHelper.getCourseGroup(coursename, groupname); } public CourseGroup addCourseGroup(String coursename, String groupname) { + openDatabase(); if(vorlesungsplanDBHelper == null) vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database); return vorlesungsplanDBHelper.addCourseGroup(coursename, groupname); } public void updateMensaTagesplan(MensaTagesplan plan) { + openDatabase(); if(mensaplanDBHelper == null) mensaplanDBHelper = new MensaplanDatabaseHelper(AppContext, database); mensaplanDBHelper.updateMensaTagesplan(plan); } public MensaTagesplan[] getMensaTagesplan(long timeFrom, long timeTo) { + openDatabase(); if(mensaplanDBHelper == null) mensaplanDBHelper = new MensaplanDatabaseHelper(AppContext, database); return mensaplanDBHelper.getMensaTagesplan(timeFrom, timeTo); } public long[] getDaysWithPlanData(long timeFrom, long timeTo) { + openDatabase(); if(mensaplanDBHelper == null) mensaplanDBHelper = new MensaplanDatabaseHelper(AppContext, database); return mensaplanDBHelper.getDaysWithPlanData(timeFrom, timeTo); } public long[] getWeeksWithPlanData(long timeFrom, long timeTo) { + openDatabase(); if(mensaplanDBHelper == null) mensaplanDBHelper = new MensaplanDatabaseHelper(AppContext, database); return mensaplanDBHelper.getWeeksWithPlanData(timeFrom, timeTo); } public void updateNewsItem(NewsItem news) { + openDatabase(); if(newsDBHelper == null) newsDBHelper = new NewsDatabaseHelper(AppContext, database); newsDBHelper.updateNewsItem(news); } public NewsItem[] getNewsItems(String source, long timeFrom, long timeTo) { + openDatabase(); if(newsDBHelper == null) newsDBHelper = new NewsDatabaseHelper(AppContext, database); return newsDBHelper.getNewsItems(source, timeFrom, timeTo); diff --git a/app/src/main/java/de/dhbwloe/campusapp/fragments/WifiSettings.java b/app/src/main/java/de/dhbwloe/campusapp/fragments/WifiSettings.java index d57671a..c172ed5 100644 --- a/app/src/main/java/de/dhbwloe/campusapp/fragments/WifiSettings.java +++ b/app/src/main/java/de/dhbwloe/campusapp/fragments/WifiSettings.java @@ -8,13 +8,20 @@ import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.util.Base64; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.StringReader; import java.lang.reflect.Field; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; import de.dhbwloe.campusapp.CampusAppFragment; import de.dhbwloe.campusapp.R; @@ -37,7 +44,150 @@ public class WifiSettings extends CampusAppFragment { }; } + private enum WifiNetworkAuthenticationTypes { + WIFI_AUTHTYPE_NONE, + WIFI_AUTHTYPE_WEP, + WIFI_AUTHTYPE_WPA, + WIFI_AUTHTYPE_WPA_ENTERPRISE, + }; + private class WifiNetworkSettingsSet { + String name; + String ssid; + WifiNetworkAuthenticationTypes authType; + int[] authAlgorithms; + int eapMethod; + int phase2Method; + + String caCertStr; + + String username; + String password; + + public WifiNetworkSettingsSet(String name, String ssid, WifiNetworkAuthenticationTypes authType) { + this.name = name; + this.ssid = ssid; + this.authType = authType; + } + + public WifiNetworkSettingsSet setWpaEnterprise(int[] authAlgorithms, int eapMethod, int phase2Method, String username) { + this.authAlgorithms = authAlgorithms; + this.eapMethod = eapMethod; + this.phase2Method = phase2Method; + this.username = username; + return this; + } + + public WifiNetworkSettingsSet setPassword(String password) { + this.password = password; + return this; + } + + public WifiNetworkSettingsSet setCACertificate(String certificate) { + this.caCertStr = certificate; + return this; + } + + public void setAuthData(String username, String password) { + this.username = username; + this.password = password; + } + + public void setAuthData(String password) { + this.password = password; + } + + public X509Certificate generateCertificate() throws CertificateException, IOException { + byte [] decoded = Base64.decode(caCertStr.replaceAll("-----BEGIN CERTIFICATE-----", "").replaceAll("-----END CERTIFICATE-----", ""), Base64.DEFAULT); + return (X509Certificate)CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(decoded)); + } + } + private View view; + private WifiNetworkSettingsSet[] wifiPresets; + private WifiManager wifimanager; + + private void loadDhbwWifiDefinitions() { + int numOfNetworks = 2; + int i = 0; + wifiPresets = new WifiNetworkSettingsSet[numOfNetworks]; + + wifiPresets[i++] = (new WifiNetworkSettingsSet("dhbw-secure", "dhbw-secure", WifiNetworkAuthenticationTypes.WIFI_AUTHTYPE_WPA_ENTERPRISE)). + setWpaEnterprise( + new int[] {WifiConfiguration.KeyMgmt.WPA_EAP, WifiConfiguration.KeyMgmt.IEEE8021X}, + WifiEnterpriseConfig.Eap.PEAP, + WifiEnterpriseConfig.Phase2.MSCHAPV2, + null + ).setCACertificate("-----BEGIN CERTIFICATE-----\n" + + "MIIDnzCCAoegAwIBAgIBJjANBgkqhkiG9w0BAQUFADBxMQswCQYDVQQGEwJERTEc\n" + + "MBoGA1UEChMTRGV1dHNjaGUgVGVsZWtvbSBBRzEfMB0GA1UECxMWVC1UZWxlU2Vj\n" + + "IFRydXN0IENlbnRlcjEjMCEGA1UEAxMaRGV1dHNjaGUgVGVsZWtvbSBSb290IENB\n" + + "IDIwHhcNOTkwNzA5MTIxMTAwWhcNMTkwNzA5MjM1OTAwWjBxMQswCQYDVQQGEwJE\n" + + "RTEcMBoGA1UEChMTRGV1d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CERTIFICATE-----"); + + wifiPresets[i++] = (new WifiNetworkSettingsSet("dhbw-wlan", "dhbw-wlan", WifiNetworkAuthenticationTypes.WIFI_AUTHTYPE_NONE)); + } + + + @TargetApi(18) + private void connectToWiFi(WifiNetworkSettingsSet settings) { + WifiConfiguration wifiConfig = new WifiConfiguration(); + wifiConfig.SSID = settings.ssid; + for(int i = 0; i < settings.authAlgorithms.length; i++) + wifiConfig.allowedKeyManagement.set(settings.authAlgorithms[i]); + + switch(settings.authType) { + case WIFI_AUTHTYPE_NONE: + break; + case WIFI_AUTHTYPE_WEP: + // not supported by our app + break; + case WIFI_AUTHTYPE_WPA: + + break; + case WIFI_AUTHTYPE_WPA_ENTERPRISE: + WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig(); + enterpriseConfig.setIdentity(settings.username); + enterpriseConfig.setPassword(settings.password); + enterpriseConfig.setEapMethod(settings.eapMethod); + enterpriseConfig.setPhase2Method(settings.phase2Method); + try { + enterpriseConfig.setCaCertificate(settings.generateCertificate()); + } catch (CertificateException e) { + } catch (IOException e) { + } + wifiConfig.enterpriseConfig = enterpriseConfig; + break; + } + int networkId = wifimanager.addNetwork(wifiConfig); + wifimanager.enableNetwork(networkId, true); + } + + + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + loadDhbwWifiDefinitions(); + wifimanager = (WifiManager)AppContext.getMainActivity().getSystemService(AppContext.getMainActivity().WIFI_SERVICE); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -49,7 +199,7 @@ public class WifiSettings extends CampusAppFragment { connectBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Button connectBtn = (Button)v; + Button connectBtn = (Button) v; connectBtn.setEnabled(false); EditText usernameEdt = (EditText) view.findViewById(R.id.wifiUsername); @@ -58,12 +208,14 @@ public class WifiSettings extends CampusAppFragment { String username = usernameEdt.getText().toString(); String password = passwordEdt.getText().toString(); - if(Build.VERSION.SDK_INT < 18) { + if (Build.VERSION.SDK_INT < 18) { // connectToDHWiFi not supported! // do something else? + } else if (username.length() > 0 && password.length() > 0) { + WifiNetworkSettingsSet settings = wifiPresets[0]; + settings.setAuthData(username, password); + connectToWiFi(settings); } - else if(username.length() > 0 && password.length() > 0) - connectToDHWiFi(username, password); connectBtn.setEnabled(true); } @@ -72,26 +224,4 @@ public class WifiSettings extends CampusAppFragment { return view; } - @TargetApi(18) - private void connectToDHWiFi(String username, String password) { - WifiConfiguration wifiConfig = new WifiConfiguration(); - wifiConfig.SSID = "dhbw-secure"; - wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); - wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); - - WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig(); - enterpriseConfig.setIdentity(username); - enterpriseConfig.setPassword(password); - enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP); - enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.MSCHAPV2); - wifiConfig.enterpriseConfig = enterpriseConfig; - - WifiManager wfm = (WifiManager)AppContext.getMainActivity().getSystemService(AppContext.getMainActivity().WIFI_SERVICE); - - int networkId = wfm.addNetwork(wifiConfig); - wfm.enableNetwork(networkId, true); - } - - - } diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png index cde69bc..e6ff0e4 100644 Binary files a/app/src/main/res/mipmap-hdpi/ic_launcher.png and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png index c133a0c..36fc5e2 100644 Binary files a/app/src/main/res/mipmap-mdpi/ic_launcher.png and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png index bfa42f0..a699539 100644 Binary files a/app/src/main/res/mipmap-xhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png index 324e72c..90be762 100644 Binary files a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png index aee44e1..58fcaf8 100644 Binary files a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0bcc8f1..8469d21 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,6 @@ DHBW Campus App + Campus App Open navigation drawer Close navigation drawer