beta 0.1.3
[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
66     public static final String APPVERSION = "0.1.3 (beta)";
67     private final AppPage[] PAGES = { // Hier müssen alle Fragmente, die auf der Activity angezeigt werden sollen eingetragen werden.
68             new AppPage("SplashScreen", SplashScreen.class),
69             new AppPage("Dashboard", Dashboard.class),
70             new AppPage("AppSearch", AppSearch.class),
71             new AppPage("Vorlesungsplan", Vorlesungsplan.class),
72             new AppPage("Mensa", Mensa.class),
73             new AppPage("MensaCard", MensaCard.class, 3),
74             new AppPage("News", News.class),
75             new AppPage("WifiSettings", WifiSettings.class),
76             new AppPage("FirstRun", FirstRun.class),
77             new AppPage("Settings", Settings.class),
78             new AppPage("Impressum", Impressum.class),
79             new AppPage("WebBrowser", WebBrowser.class),
80             new AppPage("WebBrowserPopup", WebBrowser.class, 3)
81     };
82
83     private static CampusAppContext instance;
84     public static CampusAppContext getInstance() {
85         Log.i("AppContext", "Request new context instance");
86         return instance;
87     }
88
89     private Activity oMainActivity;
90     private NavigationManager oNavigationManager;
91     private DatabaseManager oDatabaseManager;
92     private NfcCardListener oNfcCardListener;
93     private Bundle oContextVariables;
94
95     public CampusAppContext(CampusApp mainActivity, int fragmentContainerId) {
96         final CampusAppContext AppContext = this;
97         instance = this;
98         oMainActivity = mainActivity;
99         oContextVariables = new Bundle();
100         oNavigationManager = new NavigationManager(this, fragmentContainerId);
101         oDatabaseManager = new DatabaseManager(this);
102         oNfcCardListener = new NfcCardListener(this);
103
104         // Alle Fragmente "registrieren"
105         for(int i = 0; i < PAGES.length; i++)
106             oNavigationManager.registerPage(PAGES[i].name, PAGES[i].fragment, PAGES[i].fragementType);
107
108         oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
109             @Override
110             public void onNfcReaderStateChanged(boolean state) {
111
112             }
113
114             @Override
115             public void onNfcReaderReceived(NfcCardData data) {
116                 if (data != null)
117                     AppContext.onNfcCardDataReceived(data);
118             }
119         });
120     }
121
122     public Activity getMainActivity() {
123         return oMainActivity;
124     }
125     public void setMainActivity(Activity activity) {
126         oMainActivity = activity;
127     }
128
129     public NavigationManager getNavigationManager() {
130         return oNavigationManager;
131     }
132
133     public void setTitle(String title) {
134         // Ändern des Titels (Im App Header angezeigt)
135         PopupFragment popup = oNavigationManager.getDialog();
136         if(popup != null) {
137             if(popup.getDialog() != null)
138                 popup.getDialog().setTitle(title);
139         } else {
140             TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
141             titleView.setText(title);
142         }
143     }
144
145     public DatabaseManager getDatabaseManager() {
146         return oDatabaseManager;
147     }
148
149     public void addDefaultSearchIndexes() {
150         // Alle Search Indices der eigetragenen Seiten beziehen und in der Datenbank speichern
151         for(int i = 0; i < PAGES.length; i++) {
152             try {
153                 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
154                 Object result = m.invoke(null);
155                 SearchIndices[] indices = (SearchIndices[]) result;
156                 addSearchIndices(indices);
157             } catch (Exception e) {
158             }
159         }
160     }
161
162     public void addSearchIndices(SearchIndices[] indices) {
163         oDatabaseManager.addSearchIndices(indices);
164     }
165
166     public NfcCardListener getNfcCardListener() {
167         return oNfcCardListener;
168     }
169
170     private void onNfcCardDataReceived(NfcCardData data) {
171         // NFC Daten erhalten
172         Bundle bundle = new Bundle();
173         double cardBalance = data.getBalance() / 100.0;
174         bundle.putDouble("balance", cardBalance);
175         bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
176
177         oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
178         oContextVariables.putDouble("nfcCardBalance", cardBalance);
179
180         String pagename = oNavigationManager.getCurrentPageName();
181         // Update angezeigtes Guthaben lediglich, wenn das Fragment bereits angezeigt wird
182         if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
183             MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
184             fragment.showNfcCardData(bundle);
185         } else
186             oNavigationManager.navigatePage("MensaCard", bundle);
187     }
188
189     public Bundle getContextVariables() {
190         return oContextVariables;
191     }
192
193     public String getResString(int id, Object... arguments) {
194         // printf like resource strings
195         String format = getResString(id);
196         String res = String.format(format, arguments);
197         return res;
198     }
199     public String getResString(int id) {
200         String str = oMainActivity.getResources().getString(id);
201         return str;
202     }
203
204 }