fixed title for popups
[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             if(popup.getDialog() != null)
118                 popup.getDialog().setTitle(title);
119         } else {
120             TextView titleView = (TextView)oMainActivity.findViewById(R.id.title);
121             titleView.setText(title);
122         }
123     }
124
125     public DatabaseManager getDatabaseManager() {
126         return oDatabaseManager;
127     }
128
129     public void addDefaultSearchIndexes() {
130         for(int i = 0; i < PAGES.length; i++) {
131             try {
132                 Method m = PAGES[i].fragment.getMethod("GetSearchIndices");
133                 Object result = m.invoke(null);
134                 SearchIndices[] indices = (SearchIndices[]) result;
135                 addSearchIndices(indices);
136             } catch (Exception e) {
137             }
138         }
139     }
140
141     public void addSearchIndices(SearchIndices[] indices) {
142         oDatabaseManager.addSearchIndices(indices);
143     }
144
145     public NfcCardListener getNfcCardListener() {
146         return oNfcCardListener;
147     }
148
149     private void onNfcCardDataReceived(NfcCardData data) {
150         Bundle bundle = new Bundle();
151         double cardBalance = data.getBalance() / 100.0;
152         bundle.putDouble("balance", cardBalance);
153         bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
154
155         oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
156         oContextVariables.putDouble("nfcCardBalance", cardBalance);
157
158         String pagename = oNavigationManager.getCurrentPageName();
159         if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
160             MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
161             fragment.showNfcCardData(bundle);
162         } else
163             oNavigationManager.navigatePage("MensaCard", bundle);
164     }
165
166     public Bundle getContextVariables() {
167         return oContextVariables;
168     }
169
170 }