Added own crash handler for debugging
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / CampusAppContext.java
1 /* CampusAppContext.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp;
17
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.widget.TextView;
23
24 import java.lang.reflect.Method;
25
26 import de.dhbwloe.campusapp.database.DatabaseManager;
27 import de.dhbwloe.campusapp.fragments.AppSearch;
28 import de.dhbwloe.campusapp.fragments.Dashboard;
29 import de.dhbwloe.campusapp.fragments.FirstRun;
30 import de.dhbwloe.campusapp.fragments.Impressum;
31 import de.dhbwloe.campusapp.fragments.Mensa;
32 import de.dhbwloe.campusapp.fragments.MensaCard;
33 import de.dhbwloe.campusapp.fragments.News;
34 import de.dhbwloe.campusapp.fragments.PopupFragment;
35 import de.dhbwloe.campusapp.fragments.Settings;
36 import de.dhbwloe.campusapp.fragments.SplashScreen;
37 import de.dhbwloe.campusapp.fragments.Vorlesungsplan;
38 import de.dhbwloe.campusapp.fragments.WebBrowser;
39 import de.dhbwloe.campusapp.fragments.WifiSettings;
40 import de.dhbwloe.campusapp.nfcreader.NfcCardInterface;
41 import de.dhbwloe.campusapp.nfcreader.NfcCardListener;
42 import de.dhbwloe.campusapp.database.NfcCardData;
43 import de.dhbwloe.campusapp.search.SearchIndices;
44
45 /** CampusAppContext
46  * A context, that is pushed to each fragment.
47  * This context will be persistent during app execution.
48  */
49 public class CampusAppContext {
50     private class AppPage {
51         String name;
52         Class<?> fragment;
53         int fragementType;
54
55         public AppPage(String name, Class<?> fragment) {
56             this.name = name;
57             this.fragment = fragment;
58         }
59
60         public AppPage(String name, Class<?> fragment, int type) {
61             this.name = name;
62             this.fragment = fragment;
63             this.fragementType = type;
64         }
65     }
66     public class NavigationItem {
67         int navItemId;
68         String navTarget;
69     };
70
71     public final static boolean DEBUG = true;
72     public final NavigationItem[] NAVIGATION_TARGETS = new NavigationItem[] {
73             new NavigationItem() {{
74                 navItemId = R.id.nav_dashboard;
75                 navTarget = "Dashboard";
76             }},
77             new NavigationItem() {{
78                 navItemId = R.id.nav_vorlesungsplan;
79                 navTarget = "Vorlesungsplan";
80             }},
81             new NavigationItem() {{
82                 navItemId = R.id.nav_mensa;
83                 navTarget = "Mensa";
84             }},
85             new NavigationItem() {{
86                 navItemId = R.id.nav_mensa;
87                 navTarget = "Mensa";
88             }},
89             new NavigationItem() {{
90                 navItemId = R.id.nav_news;
91                 navTarget = "News";
92             }},
93             new NavigationItem() {{
94                 navItemId = R.id.nav_settings;
95                 navTarget = "Settings";
96             }},
97             new NavigationItem() {{
98                 navItemId = R.id.nav_wifi;
99                 navTarget = "WifiSettings";
100             }},
101             new NavigationItem() {{
102                 navItemId = R.id.nav_impressum;
103                 navTarget = "Impressum";
104             }},
105
106     };
107
108     public static final String APPVERSION = "0.1.3 (beta)";
109     private final AppPage[] PAGES = { // Hier müssen alle Fragmente, die auf der Activity angezeigt werden sollen eingetragen werden.
110             new AppPage("SplashScreen", SplashScreen.class),
111             new AppPage("Dashboard", Dashboard.class),
112             new AppPage("AppSearch", AppSearch.class),
113             new AppPage("Vorlesungsplan", Vorlesungsplan.class),
114             new AppPage("Mensa", Mensa.class),
115             new AppPage("MensaCard", MensaCard.class, 3),
116             new AppPage("News", News.class),
117             new AppPage("WifiSettings", WifiSettings.class),
118             new AppPage("FirstRun", FirstRun.class),
119             new AppPage("Settings", Settings.class),
120             new AppPage("Impressum", Impressum.class),
121             new AppPage("WebBrowser", WebBrowser.class),
122             new AppPage("WebBrowserPopup", WebBrowser.class, 3)
123     };
124
125     private static CampusAppContext instance;
126     public static CampusAppContext getInstance() {
127         Log.i("AppContext", "Request new context instance");
128         return instance;
129     }
130
131     private Activity oMainActivity;
132     private NavigationManager oNavigationManager;
133     private DatabaseManager oDatabaseManager;
134     private NfcCardListener oNfcCardListener;
135     private Bundle oContextVariables;
136     private CampusAppExceptionHandler oExceptionHandler;
137
138     public CampusAppContext(CampusApp mainActivity, int fragmentContainerId, int navigationViewId) {
139         final CampusAppContext AppContext = this;
140         instance = this;
141
142         oMainActivity = mainActivity;
143         oContextVariables = new Bundle();
144         oNavigationManager = new NavigationManager(this, fragmentContainerId, navigationViewId);
145         oDatabaseManager = new DatabaseManager(this);
146         oNfcCardListener = new NfcCardListener(this);
147
148         // Alle Fragmente "registrieren"
149         for(int i = 0; i < PAGES.length; i++)
150             oNavigationManager.registerPage(PAGES[i].name, PAGES[i].fragment, PAGES[i].fragementType);
151
152         oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
153             @Override
154             public void onNfcReaderStateChanged(boolean state) {
155
156             }
157
158             @Override
159             public void onNfcReaderReceived(NfcCardData data) {
160                 if (data != null)
161                     AppContext.onNfcCardDataReceived(data);
162             }
163         });
164     }
165
166     public Activity getMainActivity() {
167         return oMainActivity;
168     }
169     public void setMainActivity(Activity activity) {
170         oMainActivity = activity;
171     }
172
173     public NavigationManager getNavigationManager() {
174         return oNavigationManager;
175     }
176
177     public void setTitle(String title) {
178         // Ändern des Titels (Im App Header angezeigt)
179         PopupFragment popup = oNavigationManager.getDialog();
180         if(popup != null) {
181             if(popup.getDialog() != null)
182                 popup.getDialog().setTitle(title);
183         } else {
184             TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
185             titleView.setText(title);
186         }
187     }
188
189     public DatabaseManager getDatabaseManager() {
190         return oDatabaseManager;
191     }
192
193     public void addDefaultSearchIndexes() {
194         // Alle Search Indices der eigetragenen Seiten beziehen und in der Datenbank speichern
195         for(int i = 0; i < PAGES.length; i++) {
196             try {
197                 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
198                 Object result = m.invoke(null);
199                 SearchIndices[] indices = (SearchIndices[]) result;
200                 addSearchIndices(indices);
201             } catch (Exception e) {
202             }
203         }
204     }
205
206     public void addSearchIndices(SearchIndices[] indices) {
207         oDatabaseManager.addSearchIndices(indices);
208     }
209
210     public NfcCardListener getNfcCardListener() {
211         return oNfcCardListener;
212     }
213
214     private void onNfcCardDataReceived(NfcCardData data) {
215         // NFC Daten erhalten
216         Bundle bundle = new Bundle();
217         double cardBalance = data.getBalance() / 100.0;
218         bundle.putDouble("balance", cardBalance);
219         bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
220
221         oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
222         oContextVariables.putDouble("nfcCardBalance", cardBalance);
223
224         String pagename = oNavigationManager.getCurrentPageName();
225         // Update angezeigtes Guthaben lediglich, wenn das Fragment bereits angezeigt wird
226         if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
227             MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
228             fragment.showNfcCardData(bundle);
229         } else
230             oNavigationManager.navigatePage("MensaCard", bundle);
231     }
232
233     public Bundle getContextVariables() {
234         return oContextVariables;
235     }
236
237     public String getResString(int id, Object... arguments) {
238         // printf like resource strings
239         String format = getResString(id);
240         String res = String.format(format, arguments);
241         return res;
242     }
243     public String getResString(int id) {
244         String str = oMainActivity.getResources().getString(id);
245         return str;
246     }
247
248 }