alpha 0.0.2
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / CampusAppContext.java
1 package de.dhbwloe.campusapp;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6 import android.widget.TextView;
7
8 import java.lang.reflect.Method;
9
10 import de.dhbwloe.campusapp.database.DatabaseManager;
11 import de.dhbwloe.campusapp.fragments.AppSearch;
12 import de.dhbwloe.campusapp.fragments.Dashboard;
13 import de.dhbwloe.campusapp.fragments.FirstRun;
14 import de.dhbwloe.campusapp.fragments.Impressum;
15 import de.dhbwloe.campusapp.fragments.Mensa;
16 import de.dhbwloe.campusapp.fragments.MensaCard;
17 import de.dhbwloe.campusapp.fragments.News;
18 import de.dhbwloe.campusapp.fragments.PopupFragment;
19 import de.dhbwloe.campusapp.fragments.Settings;
20 import de.dhbwloe.campusapp.fragments.SplashScreen;
21 import de.dhbwloe.campusapp.fragments.Vorlesungsplan;
22 import de.dhbwloe.campusapp.fragments.WebBrowser;
23 import de.dhbwloe.campusapp.fragments.WifiSettings;
24 import de.dhbwloe.campusapp.nfcreader.NfcCardInterface;
25 import de.dhbwloe.campusapp.nfcreader.NfcCardListener;
26 import de.dhbwloe.campusapp.database.NfcCardData;
27 import de.dhbwloe.campusapp.search.SearchIndices;
28
29 /** CampusAppContext
30  * A context, that is pushed to each fragment.
31  * This context will be persistent during app execution.
32  */
33 public class CampusAppContext {
34     private class AppPage {
35         String name;
36         Class<?> fragment;
37         int fragementType;
38
39         public AppPage(String name, Class<?> fragment) {
40             this.name = name;
41             this.fragment = fragment;
42         }
43
44         public AppPage(String name, Class<?> fragment, int type) {
45             this.name = name;
46             this.fragment = fragment;
47             this.fragementType = type;
48         }
49     }
50     private final AppPage[] PAGES = {
51             new AppPage("SplashScreen", SplashScreen.class),
52             new AppPage("Dashboard", Dashboard.class),
53             new AppPage("AppSearch", AppSearch.class),
54             new AppPage("Vorlesungsplan", Vorlesungsplan.class),
55             new AppPage("Mensa", Mensa.class),
56             new AppPage("MensaCard", MensaCard.class, 3),
57             new AppPage("News", News.class),
58             new AppPage("WifiSettings", WifiSettings.class),
59             new AppPage("FirstRun", FirstRun.class),
60             new AppPage("Settings", Settings.class),
61             new AppPage("Impressum", Impressum.class),
62             new AppPage("WebBrowser", WebBrowser.class),
63             new AppPage("WebBrowserPopup", WebBrowser.class, 3)
64     };
65
66     private static CampusAppContext instance;
67     public static CampusAppContext getInstance() {
68         Log.i("AppContext", "Request new context instance");
69         return instance;
70     }
71
72     private Activity oMainActivity;
73     private NavigationManager oNavigationManager;
74     private DatabaseManager oDatabaseManager;
75     private NfcCardListener oNfcCardListener;
76     private Bundle oContextVariables;
77
78     public CampusAppContext(CampusApp mainActivity, int fragmentContainerId) {
79         final CampusAppContext AppContext = this;
80         instance = this;
81         oMainActivity = mainActivity;
82         oContextVariables = new Bundle();
83         oNavigationManager = new NavigationManager(this, fragmentContainerId);
84         oDatabaseManager = new DatabaseManager(this);
85         oNfcCardListener = new NfcCardListener(this);
86
87         for(int i = 0; i < PAGES.length; i++)
88             oNavigationManager.registerPage(PAGES[i].name, PAGES[i].fragment, PAGES[i].fragementType);
89
90         oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
91             @Override
92             public void onNfcReaderStateChanged(boolean state) {
93
94             }
95
96             @Override
97             public void onNfcReaderReceived(NfcCardData data) {
98                 if (data != null)
99                     AppContext.onNfcCardDataReceived(data);
100             }
101         });
102     }
103
104     public Activity getMainActivity() {
105         return oMainActivity;
106     }
107     public void setMainActivity(Activity activity) {
108         oMainActivity = activity;
109     }
110
111     public NavigationManager getNavigationManager() {
112         return oNavigationManager;
113     }
114
115     public void setTitle(String title) {
116         PopupFragment popup = oNavigationManager.getDialog();
117         if(popup != null) {
118             if(popup.getDialog() != null)
119                 popup.getDialog().setTitle(title);
120         } else {
121             TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
122             titleView.setText(title);
123         }
124     }
125
126     public DatabaseManager getDatabaseManager() {
127         return oDatabaseManager;
128     }
129
130     public void addDefaultSearchIndexes() {
131         for(int i = 0; i < PAGES.length; i++) {
132             try {
133                 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
134                 Object result = m.invoke(null);
135                 SearchIndices[] indices = (SearchIndices[]) result;
136                 addSearchIndices(indices);
137             } catch (Exception e) {
138             }
139         }
140     }
141
142     public void addSearchIndices(SearchIndices[] indices) {
143         oDatabaseManager.addSearchIndices(indices);
144     }
145
146     public NfcCardListener getNfcCardListener() {
147         return oNfcCardListener;
148     }
149
150     private void onNfcCardDataReceived(NfcCardData data) {
151         Bundle bundle = new Bundle();
152         double cardBalance = data.getBalance() / 100.0;
153         bundle.putDouble("balance", cardBalance);
154         bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
155
156         oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
157         oContextVariables.putDouble("nfcCardBalance", cardBalance);
158
159         String pagename = oNavigationManager.getCurrentPageName();
160         if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
161             MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
162             fragment.showNfcCardData(bundle);
163         } else
164             oNavigationManager.navigatePage("MensaCard", bundle);
165     }
166
167     public Bundle getContextVariables() {
168         return oContextVariables;
169     }
170
171     public String getResString(int id, Object... arguments) {
172         String format = getResString(id);
173         String res = String.format(format, arguments);
174         return res;
175     }
176     public String getResString(int id) {
177         String str = oMainActivity.getResources().getString(id);
178         return str;
179     }
180
181 }