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.content.Intent;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.widget.TextView;
24 import java.lang.reflect.Method;
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;
46 * A context, that is pushed to each fragment.
47 * This context will be persistent during app execution.
49 public class CampusAppContext {
50 private class AppPage {
55 public AppPage(String name, Class<?> fragment) {
57 this.fragment = fragment;
60 public AppPage(String name, Class<?> fragment, int type) {
62 this.fragment = fragment;
63 this.fragementType = type;
66 public class NavigationItem {
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";
77 new NavigationItem() {{
78 navItemId = R.id.nav_vorlesungsplan;
79 navTarget = "Vorlesungsplan";
81 new NavigationItem() {{
82 navItemId = R.id.nav_mensa;
85 new NavigationItem() {{
86 navItemId = R.id.nav_mensa;
89 new NavigationItem() {{
90 navItemId = R.id.nav_news;
93 new NavigationItem() {{
94 navItemId = R.id.nav_settings;
95 navTarget = "Settings";
97 new NavigationItem() {{
98 navItemId = R.id.nav_wifi;
99 navTarget = "WifiSettings";
101 new NavigationItem() {{
102 navItemId = R.id.nav_impressum;
103 navTarget = "Impressum";
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)
125 private static CampusAppContext instance;
126 public static CampusAppContext getInstance() {
127 Log.i("AppContext", "Request new context instance");
131 private Activity oMainActivity;
132 private NavigationManager oNavigationManager;
133 private DatabaseManager oDatabaseManager;
134 private NfcCardListener oNfcCardListener;
135 private Bundle oContextVariables;
136 private CampusAppExceptionHandler oExceptionHandler;
138 public CampusAppContext(CampusApp mainActivity, int fragmentContainerId, int navigationViewId) {
139 final CampusAppContext AppContext = this;
142 oMainActivity = mainActivity;
143 oContextVariables = new Bundle();
144 oNavigationManager = new NavigationManager(this, fragmentContainerId, navigationViewId);
145 oDatabaseManager = new DatabaseManager(this);
146 oNfcCardListener = new NfcCardListener(this);
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);
152 oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
154 public void onNfcReaderStateChanged(boolean state) {
159 public void onNfcReaderReceived(NfcCardData data) {
161 AppContext.onNfcCardDataReceived(data);
166 public Activity getMainActivity() {
167 return oMainActivity;
169 public void setMainActivity(Activity activity) {
170 oMainActivity = activity;
173 public NavigationManager getNavigationManager() {
174 return oNavigationManager;
177 public void setTitle(String title) {
178 // Ändern des Titels (Im App Header angezeigt)
179 PopupFragment popup = oNavigationManager.getDialog();
181 if(popup.getDialog() != null)
182 popup.getDialog().setTitle(title);
184 TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
185 titleView.setText(title);
189 public DatabaseManager getDatabaseManager() {
190 return oDatabaseManager;
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++) {
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) {
206 public void addSearchIndices(SearchIndices[] indices) {
207 oDatabaseManager.addSearchIndices(indices);
210 public NfcCardListener getNfcCardListener() {
211 return oNfcCardListener;
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);
221 oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
222 oContextVariables.putDouble("nfcCardBalance", cardBalance);
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);
230 oNavigationManager.navigatePage("MensaCard", bundle);
233 public Bundle getContextVariables() {
234 return oContextVariables;
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);
243 public String getResString(int id) {
244 String str = oMainActivity.getResources().getString(id);