fb2defde821b468c36da9d93235643ecc47cbd15
[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.os.Bundle;
20 import android.util.Log;
21 import android.widget.TextView;
22
23 import java.lang.reflect.Method;
24
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;
43
44 /** CampusAppContext
45  * A context, that is pushed to each fragment.
46  * This context will be persistent during app execution.
47  */
48 public class CampusAppContext {
49     private class AppPage {
50         String name;
51         Class<?> fragment;
52         int fragementType;
53
54         public AppPage(String name, Class<?> fragment) {
55             this.name = name;
56             this.fragment = fragment;
57         }
58
59         public AppPage(String name, Class<?> fragment, int type) {
60             this.name = name;
61             this.fragment = fragment;
62             this.fragementType = type;
63         }
64     }
65     public class NavigationItem {
66         int navItemId;
67         String navTarget;
68     };
69
70     public static final String APPVERSION = "0.1.4 (beta)";
71     public static final 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     private final AppPage[] PAGES = { // Hier müssen alle Fragmente, die auf der Activity angezeigt werden sollen eingetragen werden.
108             new AppPage("SplashScreen", SplashScreen.class),
109             new AppPage("Dashboard", Dashboard.class),
110             new AppPage("AppSearch", AppSearch.class),
111             new AppPage("Vorlesungsplan", Vorlesungsplan.class),
112             new AppPage("Mensa", Mensa.class),
113             new AppPage("MensaCard", MensaCard.class, 3),
114             new AppPage("News", News.class),
115             new AppPage("WifiSettings", WifiSettings.class),
116             new AppPage("FirstRun", FirstRun.class),
117             new AppPage("Settings", Settings.class),
118             new AppPage("Impressum", Impressum.class),
119             new AppPage("WebBrowser", WebBrowser.class),
120             new AppPage("WebBrowserPopup", WebBrowser.class, 3)
121     };
122
123     private static CampusAppContext instance;
124     public static CampusAppContext getInstance() {
125         Log.i("AppContext", "Request new context instance");
126         return instance;
127     }
128
129     private Activity oMainActivity;
130     private NavigationManager oNavigationManager;
131     private DatabaseManager oDatabaseManager;
132     private NfcCardListener oNfcCardListener;
133     private Bundle oContextVariables;
134     private CampusAppCrashHandler oExceptionHandler;
135
136     public CampusAppContext(CampusApp mainActivity, int fragmentContainerId, int navigationViewId) {
137         final CampusAppContext AppContext = this;
138         instance = this;
139
140         oMainActivity = mainActivity;
141         oContextVariables = new Bundle();
142         oNavigationManager = new NavigationManager(this, fragmentContainerId, navigationViewId);
143         oDatabaseManager = new DatabaseManager(this);
144         oNfcCardListener = new NfcCardListener(this);
145
146         // Alle Fragmente "registrieren"
147         for(int i = 0; i < PAGES.length; i++)
148             oNavigationManager.registerPage(PAGES[i].name, PAGES[i].fragment, PAGES[i].fragementType);
149
150         oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
151             @Override
152             public void onNfcReaderStateChanged(boolean state) {
153
154             }
155
156             @Override
157             public void onNfcReaderReceived(NfcCardData data) {
158                 if (data != null)
159                     AppContext.onNfcCardDataReceived(data);
160             }
161         });
162     }
163
164     public Activity getMainActivity() {
165         return oMainActivity;
166     }
167     public void setMainActivity(Activity activity) {
168         oMainActivity = activity;
169     }
170
171     public NavigationManager getNavigationManager() {
172         return oNavigationManager;
173     }
174
175     public void setTitle(String title) {
176         // Ändern des Titels (Im App Header angezeigt)
177         PopupFragment popup = oNavigationManager.getDialog();
178         if(popup != null) {
179             if(popup.getDialog() != null)
180                 popup.getDialog().setTitle(title);
181         } else {
182             TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
183             titleView.setText(title);
184         }
185     }
186
187     public DatabaseManager getDatabaseManager() {
188         return oDatabaseManager;
189     }
190
191     public void addDefaultSearchIndexes() {
192         // Alle Search Indices der eigetragenen Seiten beziehen und in der Datenbank speichern
193         for(int i = 0; i < PAGES.length; i++) {
194             try {
195                 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
196                 Object result = m.invoke(null);
197                 SearchIndices[] indices = (SearchIndices[]) result;
198                 addSearchIndices(indices);
199             } catch (Exception e) {
200             }
201         }
202     }
203
204     public void addSearchIndices(SearchIndices[] indices) {
205         oDatabaseManager.addSearchIndices(indices);
206     }
207
208     public NfcCardListener getNfcCardListener() {
209         return oNfcCardListener;
210     }
211
212     private void onNfcCardDataReceived(NfcCardData data) {
213         // NFC Daten erhalten
214         Bundle bundle = new Bundle();
215         double cardBalance = data.getBalance() / 100.0;
216         bundle.putDouble("balance", cardBalance);
217         bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
218
219         oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
220         oContextVariables.putDouble("nfcCardBalance", cardBalance);
221
222         String pagename = oNavigationManager.getCurrentPageName();
223         // Update angezeigtes Guthaben lediglich, wenn das Fragment bereits angezeigt wird
224         if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
225             MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
226             fragment.showNfcCardData(bundle);
227         } else
228             oNavigationManager.navigatePage("MensaCard", bundle);
229     }
230
231     public Bundle getContextVariables() {
232         return oContextVariables;
233     }
234
235     public String getResString(int id, Object... arguments) {
236         // printf like resource strings
237         String format = getResString(id);
238         String res = String.format(format, arguments);
239         return res;
240     }
241     public String getResString(int id) {
242         String str = oMainActivity.getResources().getString(id);
243         return str;
244     }
245
246 }