99a1d17f94bb7d019604e162ccd655b7f06bda68
[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 final NavigationItem[] NAVIGATION_TARGETS = new NavigationItem[] {
71             new NavigationItem() {{
72                 navItemId = R.id.nav_dashboard;
73                 navTarget = "Dashboard";
74             }},
75             new NavigationItem() {{
76                 navItemId = R.id.nav_vorlesungsplan;
77                 navTarget = "Vorlesungsplan";
78             }},
79             new NavigationItem() {{
80                 navItemId = R.id.nav_mensa;
81                 navTarget = "Mensa";
82             }},
83             new NavigationItem() {{
84                 navItemId = R.id.nav_mensa;
85                 navTarget = "Mensa";
86             }},
87             new NavigationItem() {{
88                 navItemId = R.id.nav_news;
89                 navTarget = "News";
90             }},
91             new NavigationItem() {{
92                 navItemId = R.id.nav_settings;
93                 navTarget = "Settings";
94             }},
95             new NavigationItem() {{
96                 navItemId = R.id.nav_wifi;
97                 navTarget = "WifiSettings";
98             }},
99             new NavigationItem() {{
100                 navItemId = R.id.nav_impressum;
101                 navTarget = "Impressum";
102             }},
103
104     };
105
106     public static final String APPVERSION = "0.1.3 (beta)";
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
135     public CampusAppContext(CampusApp mainActivity, int fragmentContainerId, int navigationViewId) {
136         final CampusAppContext AppContext = this;
137         instance = this;
138         oMainActivity = mainActivity;
139         oContextVariables = new Bundle();
140         oNavigationManager = new NavigationManager(this, fragmentContainerId, navigationViewId);
141         oDatabaseManager = new DatabaseManager(this);
142         oNfcCardListener = new NfcCardListener(this);
143
144         // Alle Fragmente "registrieren"
145         for(int i = 0; i < PAGES.length; i++)
146             oNavigationManager.registerPage(PAGES[i].name, PAGES[i].fragment, PAGES[i].fragementType);
147
148         oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
149             @Override
150             public void onNfcReaderStateChanged(boolean state) {
151
152             }
153
154             @Override
155             public void onNfcReaderReceived(NfcCardData data) {
156                 if (data != null)
157                     AppContext.onNfcCardDataReceived(data);
158             }
159         });
160     }
161
162     public Activity getMainActivity() {
163         return oMainActivity;
164     }
165     public void setMainActivity(Activity activity) {
166         oMainActivity = activity;
167     }
168
169     public NavigationManager getNavigationManager() {
170         return oNavigationManager;
171     }
172
173     public void setTitle(String title) {
174         // Ändern des Titels (Im App Header angezeigt)
175         PopupFragment popup = oNavigationManager.getDialog();
176         if(popup != null) {
177             if(popup.getDialog() != null)
178                 popup.getDialog().setTitle(title);
179         } else {
180             TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
181             titleView.setText(title);
182         }
183     }
184
185     public DatabaseManager getDatabaseManager() {
186         return oDatabaseManager;
187     }
188
189     public void addDefaultSearchIndexes() {
190         // Alle Search Indices der eigetragenen Seiten beziehen und in der Datenbank speichern
191         for(int i = 0; i < PAGES.length; i++) {
192             try {
193                 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
194                 Object result = m.invoke(null);
195                 SearchIndices[] indices = (SearchIndices[]) result;
196                 addSearchIndices(indices);
197             } catch (Exception e) {
198             }
199         }
200     }
201
202     public void addSearchIndices(SearchIndices[] indices) {
203         oDatabaseManager.addSearchIndices(indices);
204     }
205
206     public NfcCardListener getNfcCardListener() {
207         return oNfcCardListener;
208     }
209
210     private void onNfcCardDataReceived(NfcCardData data) {
211         // NFC Daten erhalten
212         Bundle bundle = new Bundle();
213         double cardBalance = data.getBalance() / 100.0;
214         bundle.putDouble("balance", cardBalance);
215         bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
216
217         oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
218         oContextVariables.putDouble("nfcCardBalance", cardBalance);
219
220         String pagename = oNavigationManager.getCurrentPageName();
221         // Update angezeigtes Guthaben lediglich, wenn das Fragment bereits angezeigt wird
222         if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
223             MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
224             fragment.showNfcCardData(bundle);
225         } else
226             oNavigationManager.navigatePage("MensaCard", bundle);
227     }
228
229     public Bundle getContextVariables() {
230         return oContextVariables;
231     }
232
233     public String getResString(int id, Object... arguments) {
234         // printf like resource strings
235         String format = getResString(id);
236         String res = String.format(format, arguments);
237         return res;
238     }
239     public String getResString(int id) {
240         String str = oMainActivity.getResources().getString(id);
241         return str;
242     }
243
244 }