some fixes
[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.MensaWochenplan;
18 import de.dhbwloe.campusapp.fragments.News;
19 import de.dhbwloe.campusapp.fragments.PopupFragment;
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("Impressum", Impressum.class),
61             new AppPage("WebBrowser", WebBrowser.class),
62             new AppPage("WebBrowserPopup", WebBrowser.class, 3)
63     };
64
65     private static CampusAppContext instance;
66     public static CampusAppContext getInstance() {
67         Log.i("AppContext", "Request new context instance");
68         return instance;
69     }
70
71     private Activity oMainActivity;
72     private NavigationManager oNavigationManager;
73     private DatabaseManager oDatabaseManager;
74     private NfcCardListener oNfcCardListener;
75     private Bundle oContextVariables;
76
77     public CampusAppContext(CampusApp mainActivity, int fragmentContainerId) {
78         final CampusAppContext AppContext = this;
79         instance = this;
80         oMainActivity = mainActivity;
81         oContextVariables = new Bundle();
82         oNavigationManager = new NavigationManager(this, fragmentContainerId);
83         oDatabaseManager = new DatabaseManager(this);
84         oNfcCardListener = new NfcCardListener(this);
85
86         for(int i = 0; i < PAGES.length; i++)
87             oNavigationManager.registerPage(PAGES[i].name, PAGES[i].fragment, PAGES[i].fragementType);
88
89         oNfcCardListener.registerNfcCardInterface(new NfcCardInterface() {
90             @Override
91             public void onNfcReaderStateChanged(boolean state) {
92
93             }
94
95             @Override
96             public void onNfcReaderReceived(NfcCardData data) {
97                 if (data != null)
98                     AppContext.onNfcCardDataReceived(data);
99             }
100         });
101     }
102
103     public Activity getMainActivity() {
104         return oMainActivity;
105     }
106     public void setMainActivity(Activity activity) {
107         oMainActivity = activity;
108     }
109
110     public NavigationManager getNavigationManager() {
111         return oNavigationManager;
112     }
113
114     public void setTitle(String title) {
115         PopupFragment popup = oNavigationManager.getDialog();
116         if(popup != null) {
117             popup.getDialog().setTitle(title);
118         } else {
119             TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
120             titleView.setText(title);
121         }
122     }
123
124     public DatabaseManager getDatabaseManager() {
125         return oDatabaseManager;
126     }
127
128     public void addDefaultSearchIndexes() {
129         for(int i = 0; i < PAGES.length; i++) {
130             try {
131                 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
132                 Object result = m.invoke(null);
133                 SearchIndices[] indices = (SearchIndices[]) result;
134                 addSearchIndices(indices);
135             } catch (Exception e) {
136             }
137         }
138     }
139
140     public void addSearchIndices(SearchIndices[] indices) {
141         oDatabaseManager.addSearchIndices(indices);
142     }
143
144     public NfcCardListener getNfcCardListener() {
145         return oNfcCardListener;
146     }
147
148     private void onNfcCardDataReceived(NfcCardData data) {
149         Bundle bundle = new Bundle();
150         double cardBalance = data.getBalance() / 100.0;
151         bundle.putDouble("balance", cardBalance);
152         bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
153
154         oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
155         oContextVariables.putDouble("nfcCardBalance", cardBalance);
156
157         String pagename = oNavigationManager.getCurrentPageName();
158         if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
159             MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
160             fragment.showNfcCardData(bundle);
161         } else
162             oNavigationManager.navigatePage("MensaCard", bundle);
163     }
164
165     public Bundle getContextVariables() {
166         return oContextVariables;
167     }
168
169 }