Added own crash handler for debugging
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / CampusApp.java
index f201da69cf985fb61e91848b133bf65aeb68fa16..403c7401b748e12feeb2d2ca8f935f60b7eb6596 100644 (file)
@@ -1,7 +1,23 @@
+/* CampusApp.java
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
 package de.dhbwloe.campusapp;
 
 import android.content.Context;
 import android.content.Intent;
+import android.content.res.TypedArray;
 import android.nfc.NfcAdapter;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
@@ -30,7 +46,6 @@ import java.util.Date;
 import de.dhbwloe.campusapp.nfcreader.NfcCardListener;
 
 public class CampusApp extends FragmentActivity {
-
     private boolean bSearchActive = false;
     private CampusAppContext AppContext = null;
 
@@ -51,23 +66,41 @@ public class CampusApp extends FragmentActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         Log.i("CampusApp", "Event: onCreate");
+
+        if(CampusAppContext.DEBUG) {
+            final Thread.UncaughtExceptionHandler systemExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
+            final CampusAppExceptionHandler oExceptionHandler = new CampusAppExceptionHandler();
+            Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() {
+                @Override
+                public void uncaughtException(Thread thread, Throwable e) {
+                    oExceptionHandler.handleUncaughtException(thread, e);
+                    //systemExceptionHandler.uncaughtException(thread, e);
+                    System.exit(1);
+                }
+            });
+        }
+
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_campus_app);
 
+        // Erstelle neuen AppContext, wenn keiner existiert
         AppContext = CampusAppContext.getInstance();
         if(AppContext == null)
-            AppContext = new CampusAppContext(this, R.id.fragment_container);
+            AppContext = new CampusAppContext(this, R.id.fragment_container, R.id.nav_view);
         else
             AppContext.setMainActivity(this);
 
+        // Wenn die App aus dem Ruhezustand (oder auch Orientation change) reaktiviert wird soll nicht zuerst der Splashscreen angezeigt werden.
         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;
 
+                // restore previous Title
                 AppContext.setTitle(savedInstanceState.getString("activetitle"));
-
             }
 
 
@@ -91,6 +124,7 @@ public class CampusApp extends FragmentActivity {
     @Override
     public void onSaveInstanceState(Bundle savedInstanceState) {
         Log.i("CampusApp", "Event: onSaveInstanceState");
+
         // Save instance state during "restarts" due to orientation changes.
         // We don't want to see the splash screen everytime the orientation changes ;)
         savedInstanceState.putLong("lastrun", (new Date()).getTime() / 1000);
@@ -135,28 +169,11 @@ public class CampusApp extends FragmentActivity {
                 // Handle navigation view item clicks here.
                 int id = item.getItemId();
 
-                switch (id) { // Navigation Items from res/menu/activity_campus_app_drawer.xml
-                    case R.id.nav_dashboard:
-                        AppContext.getNavigationManager().navigatePage("Dashboard");
-                        break;
-                    case R.id.nav_vorlesungsplan:
-                        AppContext.getNavigationManager().navigatePage("Vorlesungsplan");
+                for(CampusAppContext.NavigationItem navitem : AppContext.NAVIGATION_TARGETS) {
+                    if(navitem.navItemId == id) {
+                        AppContext.getNavigationManager().navigatePage(navitem.navTarget);
                         break;
-                    case R.id.nav_mensa:
-                        AppContext.getNavigationManager().navigatePage("Mensa");
-                        break;
-                    case R.id.nav_news:
-                        AppContext.getNavigationManager().navigatePage("News");
-                        break;
-                    case R.id.nav_settings:
-                        Intent settings = new Intent(AppContext.getMainActivity(), SettingsActivity.class);
-                        AppContext.getMainActivity().startActivity(settings);
-                        break;
-                    case R.id.nav_impressum:
-                        AppContext.getNavigationManager().navigatePage("Impressum");
-                        break;
-                    default:
-                        return false;
+                    }
                 }
 
                 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
@@ -172,6 +189,7 @@ public class CampusApp extends FragmentActivity {
         EditText edtSearchInput = (EditText) findViewById(R.id.search_input);
         TextView txtTitle = (TextView) findViewById(R.id.title);
 
+        // EventListener für Suchfunktion
         btnOpenSearch.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
@@ -284,6 +302,13 @@ public class CampusApp extends FragmentActivity {
     /* nfc listener related callbacks */
     @Override
     public void onResume() {
+        if(AppContext == null)
+            AppContext = CampusAppContext.getInstance();
+        if(AppContext == null)
+            AppContext = new CampusAppContext(this, R.id.fragment_container, R.id.nav_view);
+        else
+            AppContext.setMainActivity(this);
+        
         super.onResume();
         Log.i("CampusApp", "onResume event");
         AppContext.getNfcCardListener().resumeForefrontDispatcher();