1 /* CampusAppContext.java
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.
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.
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/>.
16 package de.dhbwloe.campusapp;
18 import android.app.Activity;
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.widget.TextView;
23 import java.lang.reflect.Method;
25 import de.dhbwloe.campusapp.database.DatabaseManager;
26 import de.dhbwloe.campusapp.fragments.AppSearch;
27 import de.dhbwloe.campusapp.fragments.Dashboard;
28 import de.dhbwloe.campusapp.fragments.FirstRun;
29 import de.dhbwloe.campusapp.fragments.Impressum;
30 import de.dhbwloe.campusapp.fragments.Mensa;
31 import de.dhbwloe.campusapp.fragments.MensaCard;
32 import de.dhbwloe.campusapp.fragments.News;
33 import de.dhbwloe.campusapp.fragments.PopupFragment;
34 import de.dhbwloe.campusapp.fragments.Settings;
35 import de.dhbwloe.campusapp.fragments.SplashScreen;
36 import de.dhbwloe.campusapp.fragments.Vorlesungsplan;
37 import de.dhbwloe.campusapp.fragments.WebBrowser;
38 import de.dhbwloe.campusapp.fragments.WifiSettings;
39 import de.dhbwloe.campusapp.nfcreader.NfcCardInterface;
40 import de.dhbwloe.campusapp.nfcreader.NfcCardListener;
41 import de.dhbwloe.campusapp.database.NfcCardData;
42 import de.dhbwloe.campusapp.search.SearchIndices;
45 * A context, that is pushed to each fragment.
46 * This context will be persistent during app execution.
48 public class CampusAppContext {
49 private class AppPage {
54 public AppPage(String name, Class<?> fragment) {
56 this.fragment = fragment;
59 public AppPage(String name, Class<?> fragment, int type) {
61 this.fragment = fragment;
62 this.fragementType = type;
65 public class NavigationItem {
70 public final static boolean DEBUG = true;
71 public final NavigationItem[] NAVIGATION_TARGETS = new NavigationItem[] {
72 new NavigationItem() {{
73 navItemId = R.id.nav_dashboard;
74 navTarget = "Dashboard";
76 new NavigationItem() {{
77 navItemId = R.id.nav_vorlesungsplan;
78 navTarget = "Vorlesungsplan";
80 new NavigationItem() {{
81 navItemId = R.id.nav_mensa;
84 new NavigationItem() {{
85 navItemId = R.id.nav_mensa;
88 new NavigationItem() {{
89 navItemId = R.id.nav_news;
92 new NavigationItem() {{
93 navItemId = R.id.nav_settings;
94 navTarget = "Settings";
96 new NavigationItem() {{
97 navItemId = R.id.nav_wifi;
98 navTarget = "WifiSettings";
100 new NavigationItem() {{
101 navItemId = R.id.nav_impressum;
102 navTarget = "Impressum";
107 public static final String APPVERSION = "0.1.4 (beta)";
108 private final AppPage[] PAGES = { // Hier müssen alle Fragmente, die auf der Activity angezeigt werden sollen eingetragen werden.
109 new AppPage("SplashScreen", SplashScreen.class),
110 new AppPage("Dashboard", Dashboard.class),
111 new AppPage("AppSearch", AppSearch.class),
112 new AppPage("Vorlesungsplan", Vorlesungsplan.class),
113 new AppPage("Mensa", Mensa.class),
114 new AppPage("MensaCard", MensaCard.class, 3),
115 new AppPage("News", News.class),
116 new AppPage("WifiSettings", WifiSettings.class),
117 new AppPage("FirstRun", FirstRun.class),
118 new AppPage("Settings", Settings.class),
119 new AppPage("Impressum", Impressum.class),
120 new AppPage("WebBrowser", WebBrowser.class),
121 new AppPage("WebBrowserPopup", WebBrowser.class, 3)
124 private static CampusAppContext instance;
125 public static CampusAppContext getInstance() {
126 Log.i("AppContext", "Request new context instance");
130 private Activity oMainActivity;
131 private NavigationManager oNavigationManager;
132 private DatabaseManager oDatabaseManager;
133 private NfcCardListener oNfcCardListener;
134 private Bundle oContextVariables;
135 private CampusAppCrashHandler oExceptionHandler;
137 public CampusAppContext(CampusApp mainActivity, int fragmentContainerId, int navigationViewId) {
138 final CampusAppContext AppContext = this;
141 oMainActivity = mainActivity;
142 oContextVariables = new Bundle();
143 oNavigationManager = new NavigationManager(this, fragmentContainerId, navigationViewId);
144 oDatabaseManager = new DatabaseManager(this);
145 oNfcCardListener = new NfcCardListener(this);
147 // Alle Fragmente "registrieren"
148 for(int i = 0; i < PAGES.length; i++)
149 oNavigationManager.registerPage(PAGES[i].name, PAGES[i].fragment, PAGES[i].fragementType);
151 oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
153 public void onNfcReaderStateChanged(boolean state) {
158 public void onNfcReaderReceived(NfcCardData data) {
160 AppContext.onNfcCardDataReceived(data);
165 public Activity getMainActivity() {
166 return oMainActivity;
168 public void setMainActivity(Activity activity) {
169 oMainActivity = activity;
172 public NavigationManager getNavigationManager() {
173 return oNavigationManager;
176 public void setTitle(String title) {
177 // Ändern des Titels (Im App Header angezeigt)
178 PopupFragment popup = oNavigationManager.getDialog();
180 if(popup.getDialog() != null)
181 popup.getDialog().setTitle(title);
183 TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
184 titleView.setText(title);
188 public DatabaseManager getDatabaseManager() {
189 return oDatabaseManager;
192 public void addDefaultSearchIndexes() {
193 // Alle Search Indices der eigetragenen Seiten beziehen und in der Datenbank speichern
194 for(int i = 0; i < PAGES.length; i++) {
196 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
197 Object result = m.invoke(null);
198 SearchIndices[] indices = (SearchIndices[]) result;
199 addSearchIndices(indices);
200 } catch (Exception e) {
205 public void addSearchIndices(SearchIndices[] indices) {
206 oDatabaseManager.addSearchIndices(indices);
209 public NfcCardListener getNfcCardListener() {
210 return oNfcCardListener;
213 private void onNfcCardDataReceived(NfcCardData data) {
214 // NFC Daten erhalten
215 Bundle bundle = new Bundle();
216 double cardBalance = data.getBalance() / 100.0;
217 bundle.putDouble("balance", cardBalance);
218 bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
220 oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
221 oContextVariables.putDouble("nfcCardBalance", cardBalance);
223 String pagename = oNavigationManager.getCurrentPageName();
224 // Update angezeigtes Guthaben lediglich, wenn das Fragment bereits angezeigt wird
225 if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
226 MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
227 fragment.showNfcCardData(bundle);
229 oNavigationManager.navigatePage("MensaCard", bundle);
232 public Bundle getContextVariables() {
233 return oContextVariables;
236 public String getResString(int id, Object... arguments) {
237 // printf like resource strings
238 String format = getResString(id);
239 String res = String.format(format, arguments);
242 public String getResString(int id) {
243 String str = oMainActivity.getResources().getString(id);