-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathHomeScreenActivity.java
More file actions
301 lines (249 loc) · 9.02 KB
/
HomeScreenActivity.java
File metadata and controls
301 lines (249 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package edu.mit.mitmobile2;
import java.util.Arrays;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.mit.mitmobile2.MobileWebApi.DefaultErrorListener;
import edu.mit.mitmobile2.MobileWebApi.JSONObjectResponseListener;
import edu.mit.mitmobile2.MobileWebApi.ServerResponseException;
import edu.mit.mitmobile2.about.AboutActivity;
import edu.mit.mitmobile2.alerts.NotificationsHelper;
import edu.mit.mitmobile2.dining.DiningModule;
import edu.mit.mitmobile2.emergency.EmergencyModule;
import edu.mit.mitmobile2.events.EventsModule;
import edu.mit.mitmobile2.facilities.FacilitiesModule;
import edu.mit.mitmobile2.id.OpenIDConnectModule;
import edu.mit.mitmobile2.libraries.LibrariesModule;
import edu.mit.mitmobile2.links.LinksModule;
import edu.mit.mitmobile2.maps.MapsModule;
import edu.mit.mitmobile2.news.NewsModule;
import edu.mit.mitmobile2.people.PeopleModule;
import edu.mit.mitmobile2.qrreader.QRReaderModule;
import edu.mit.mitmobile2.settings.MITSettingsActivity;
import edu.mit.mitmobile2.shuttles.ShuttlesModule;
import edu.mit.mitmobile2.tour.TourModule;
public class HomeScreenActivity extends Activity implements OnSharedPreferenceChangeListener {
private static final int ABOUT_MENU_ID = 0;
private static final int SETTINGS_MENU_ID = 1;
Context ctx;
private GridView mSpringBoard;
private RemoteImageView mBannerView;
private Banner mBanner;
public static final String TAG = "MITNewsWidgetActivity";
private Global app;
private SharedPreferences prefs;
private SharedPreferences mHomePreferences;
private final static String PHOTO_URL_KEY="photo_url";
private final static String ACTION_URL_KEY="action_url";
private final static String HEIGHT_KEY="height_url";
private final static String WIDTH_KEY="width_url";
/****************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate()");
// get App
app = (Global)this.getApplication();
Log.d(TAG,"app = " + app);
// get preferences
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
ctx = this;
createView();
NotificationsHelper.setupAlarmData(getApplicationContext());
mHomePreferences = this.getSharedPreferences("home_prefs", MODE_PRIVATE);
String url = getIntent().getDataString();
if (url != null && url.startsWith("mitmobile://")) {
CommonActions.doAction(this, url);
finish();
}
}
@Override
public void onWindowFocusChanged (boolean hasFocus) {
if (hasFocus && mBanner == null) {
getBannerData();
}
}
/****************************************************/
void createView() {
setContentView(R.layout.home);
Module[] modules = new Module[] {
new NewsModule(),
new ShuttlesModule(),
new MapsModule(),
new EventsModule(),
new PeopleModule(),
new TourModule(),
new EmergencyModule(),
new LibrariesModule(),
new FacilitiesModule(),
new DiningModule(),
new QRReaderModule(),
new LinksModule(),
new OpenIDConnectModule()
};
mSpringBoard = (GridView) findViewById(R.id.homeSpringBoardGV);
SimpleArrayAdapter<Module> springBoardAdapter = new SimpleArrayAdapter<Module>(
this, Arrays.asList(modules), R.layout.springboard_item) {
@Override
public void updateView(Module item, View view) {
Resources resources = mContext.getResources();
ImageView iconView = (ImageView) view.findViewById(R.id.springBoardImage);
iconView.setImageDrawable(resources.getDrawable(item.getHomeIconResourceId()));
TextView titleView = (TextView) view.findViewById(R.id.springBoardTitle);
titleView.setText(item.getShortName());
}
};
springBoardAdapter.setOnItemClickListener(mSpringBoard,
new SimpleArrayAdapter.OnItemClickListener<Module>() {
@Override
public void onItemSelected(Module item) {
Intent intent = new Intent(ctx, item.getModuleHomeActivity());
startActivity(intent);
}
}
);
mSpringBoard.setAdapter(springBoardAdapter);
mBannerView = (RemoteImageView) findViewById(R.id.homeSpringBoardBanner);
}
private Handler uiHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.arg1 == MobileWebApi.SUCCESS) {
updateBanner();
} else {
String photoUrl = mHomePreferences.getString(PHOTO_URL_KEY, "");
if (photoUrl.length() > 0) {
Banner banner = new Banner();
banner.photoUrl = photoUrl;
banner.height = mHomePreferences.getInt(HEIGHT_KEY, 0);
banner.width = mHomePreferences.getInt(WIDTH_KEY, 0);
String actionUrl = mHomePreferences.getString(ACTION_URL_KEY, "");
if (actionUrl.length() > 0) {
banner.actionUrl = actionUrl;
}
mBanner = banner;
updateBanner();
}
}
}
};
private void updateBanner() {
if (mBanner != null) {
float width = mSpringBoard.getWidth();
int height = (int) (((float)mBanner.height) / ((float)mBanner.width) * width);
mBannerView.setURL(mBanner.photoUrl);
mBannerView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
if (mBanner.actionUrl != null) {
mBannerView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CommonActions.doAction(HomeScreenActivity.this, mBanner.actionUrl);
}
});
}
mBannerView.setVisibility(View.VISIBLE);
}
}
private void getBannerData() {
MobileWebApi webApi = new MobileWebApi(false, false, "Banner", this, uiHandler);
HashMap<String, String> params = new HashMap<String, String>();
params.put("module", "features");
params.put("command", "banner");
webApi.requestJSONObject(params,
new JSONObjectResponseListener(new DefaultErrorListener(uiHandler), null) {
@Override
public void onResponse(JSONObject object) throws ServerResponseException, JSONException {
if(object.getBoolean("showBanner")) {
Banner banner = new Banner();
banner.photoUrl = object.getString("photo-url");
banner.height = object.getJSONObject("dimensions").getInt("height");
banner.width = object.getJSONObject("dimensions").getInt("width");
banner.actionUrl = object.getString("url");
mBanner = banner;
// save banner data in persistently
Editor editor = mHomePreferences.edit();
editor.putString(PHOTO_URL_KEY, banner.photoUrl);
editor.putInt(HEIGHT_KEY, banner.height);
editor.putInt(WIDTH_KEY, banner.width);
editor.putString(ACTION_URL_KEY, banner.actionUrl);
editor.commit();
} else {
Editor editor = mHomePreferences.edit();
editor.putString(PHOTO_URL_KEY, null);
editor.commit();
}
MobileWebApi.sendSuccessMessage(uiHandler);
}
}
);
}
private static class Banner {
int height;
int width;
String photoUrl;
String actionUrl;
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(0, SETTINGS_MENU_ID, 0, "Settings")
.setIcon(R.drawable.menu_settings);
menu.add(0, ABOUT_MENU_ID , 0, "About")
.setIcon(R.drawable.menu_about);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case ABOUT_MENU_ID:
intent = new Intent(ctx, AboutActivity.class);
startActivity(intent);
return true;
case SETTINGS_MENU_ID:
intent = new Intent(ctx, MITSettingsActivity.class);
startActivity(intent);
return true;
}
return false;
}
public static void goHome(Context context) {
Intent i = new Intent(context, HomeScreenActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
@Override
public synchronized void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Log.d(TAG, "Preference changed: " + key);
Context mContext = this;
Handler uiHandler = new Handler();
if (key.equalsIgnoreCase(Global.MIT_MOBILE_SERVER_KEY)) {
Global.setMobileWebDomain(prefs.getString(Global.MIT_MOBILE_SERVER_KEY, null));
// Update the version map any time the Mobile server is changed
Global.getVersionInfo(mContext, uiHandler);
Toast.makeText(this, "Mobile Web Domain set to " + Global.getMobileWebDomain(), Toast.LENGTH_SHORT).show();
}
}
}