added launcher icon, fixed crash due to AppContext = null (damn Android GC), made...
authorpk910 <philipp@pk910.de>
Thu, 4 Feb 2016 10:56:06 +0000 (11:56 +0100)
committerpk910 <philipp@pk910.de>
Thu, 4 Feb 2016 10:56:06 +0000 (11:56 +0100)
app/src/main/AndroidManifest.xml
app/src/main/java/de/dhbwloe/campusapp/CampusApp.java
app/src/main/java/de/dhbwloe/campusapp/database/DatabaseManager.java
app/src/main/java/de/dhbwloe/campusapp/fragments/WifiSettings.java
app/src/main/res/mipmap-hdpi/ic_launcher.png
app/src/main/res/mipmap-mdpi/ic_launcher.png
app/src/main/res/mipmap-xhdpi/ic_launcher.png
app/src/main/res/mipmap-xxhdpi/ic_launcher.png
app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
app/src/main/res/values/strings.xml

index 89c3c048b60981ec84203a213d5687cd13b02ee6..fe7a3d806832eab18bfc41eb77c51dd90f52df08 100644 (file)
@@ -19,7 +19,7 @@
         android:theme="@style/AppTheme">
         <activity
             android:name=".CampusApp"
-            android:label="@string/app_name"
+            android:label="@string/app_name_launcher"
             android:theme="@style/AppTheme.NoActionBar">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
index f201da69cf985fb61e91848b133bf65aeb68fa16..15ae6dcbf70953fa34154e87808d9418082ae6da 100644 (file)
@@ -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();
index 326b914be070117ff8548e7413812ae4734e12f6..ccf9a0424297fc8bdd1485304941bd06e2d9f3ea 100644 (file)
@@ -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);
index d57671a83f759cb3caceb454c4df1a612a35b84c..c172ed5f241f0e10da180a002c94031218eef751 100644 (file)
@@ -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" +
+                "RTEcMBoGA1UEChMTRGV1dHNjaGUgVGVsZWtvbSBBRzEfMB0GA1UECxMWVC1UZWxl\n" +
+                "U2VjIFRydXN0IENlbnRlcjEjMCEGA1UEAxMaRGV1dHNjaGUgVGVsZWtvbSBSb290\n" +
+                "IENBIDIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCrC6M14IspFLEU\n" +
+                "ha88EOQ5bzVdSq7d6mGNlUn0b2SjGmBmpKlAIoTZ1KXleJMOaAGtuU1cOs7TuKhC\n" +
+                "QN/Po7qCWWqSG6wcmtoIKyUn+WkjR/Hg6yx6m/UTAtB+NHzCnjwAWav12gz1Mjwr\n" +
+                "rFDa1sPeg5TKqAyZMg4ISFZbavva4VhYAUlfckE8FQYBjl2tqriTtM2e66foai1S\n" +
+                "NNs671x1Udrb8zH57nGYMsRUFUQM+ZtV7a3fGAigo4aKSe5TBY8ZTNXeWHmb0moc\n" +
+                "QqvF1afPaA+W5OFhmHZhyJF81j4A4pFQh+GdCuatl9Idxjp9y7zaAzTVjlsB9WoH\n" +
+                "txa2bkp/AgMBAAGjQjBAMB0GA1UdDgQWBBQxw3kbuvVT1xfgiXotF2wKsyudMzAP\n" +
+                "BgNVHRMECDAGAQH/AgEFMA4GA1UdDwEB/wQEAwIBBjANBgkqhkiG9w0BAQUFAAOC\n" +
+                "AQEAlGRZrTlk5ynrE/5aw4sTV8gEJPB0d8Bg42f76Ymmg7+Wgnxu1MM9756Abrsp\n" +
+                "tJh6sTtU6zkXR34ajgv8HzFZMQSyzhfzLMdiNlXiItiJVbSYSKpk+tYcNthEeFpa\n" +
+                "IzpXl/V6ME+un2pMSyuOoAPjPuCp1NJ70rOo4nI8rZ7/gFnkm0W09juwzTkZmDLl\n" +
+                "6iFhkOQxIY40sfcvNUqFENrnijchvllj4PKFiDFT1FQUhXB59C4Gdyd1Lx+4ivn+\n" +
+                "xbrYNuSD7Odlt79jWvNGr4GUN9RBjNYj1h7P9WgbRGOiWrqnNVmh5XAFmw4jV5mU\n" +
+                "Cm26OWMohpLzGITY+9HPBVZkVw==\n" +
+                "-----END 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);
-    }
-
-
-
 }
index cde69bcccec65160d92116f20ffce4fce0b5245c..e6ff0e42b73fc80def5227ed93024868542be2e2 100644 (file)
Binary files a/app/src/main/res/mipmap-hdpi/ic_launcher.png and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ
index c133a0cbd379f5af6dbf1a899a0293ca5eccfad0..36fc5e228425c5a99f34a34425ba4c1125697c2c 100644 (file)
Binary files a/app/src/main/res/mipmap-mdpi/ic_launcher.png and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ
index bfa42f0e7b91d006d22352c9ff2f134e504e3c1d..a6995394a78d6f955b7c3889fdfa076e62c1ceb3 100644 (file)
Binary files a/app/src/main/res/mipmap-xhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ
index 324e72cdd7480cb983fa1bcc7ce686e51ef87fe7..90be762dea51142420f882cefc7d8ba81f1c1d5a 100644 (file)
Binary files a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ
index aee44e138434630332d88b1680f33c4b24c70ab3..58fcaf839f9a53fa4ea65a754b4a9faa81a6892e 100644 (file)
Binary files a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ
index 0bcc8f1d5e25bdc10efa1adace73a5eb18f62949..8469d21b03c639588058d7cb7c22facd72aae3cb 100644 (file)
@@ -1,5 +1,6 @@
 <resources>
     <string name="app_name">DHBW Campus App</string>
+    <string name="app_name_launcher">Campus App</string>
 
     <string name="navigation_drawer_open">Open navigation drawer</string>
     <string name="navigation_drawer_close">Close navigation drawer</string>