This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMainFlowActivity.java
More file actions
413 lines (353 loc) · 16.2 KB
/
MainFlowActivity.java
File metadata and controls
413 lines (353 loc) · 16.2 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package com.uwflow.flow_android;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.PatternMatcher;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.facebook.Session;
import com.uwflow.flow_android.adapters.NavDrawerAdapter;
import com.uwflow.flow_android.constant.Constants;
import com.uwflow.flow_android.dao.FlowDatabaseHelper;
import com.uwflow.flow_android.db_object.User;
import com.uwflow.flow_android.entities.NavDrawerItem;
import com.uwflow.flow_android.fragment.AboutFragment;
import com.uwflow.flow_android.fragment.CourseFragment;
import com.uwflow.flow_android.fragment.ExploreFragment;
import com.uwflow.flow_android.fragment.ProfileFragment;
import com.uwflow.flow_android.network.FlowAsyncClient;
import com.uwflow.flow_android.nfc.SharableURL;
import com.uwflow.flow_android.util.FacebookUtilities;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainFlowActivity extends FlowActivity {
private static final String TAG = "MainFlowActivity";
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private LinearLayout mDrawerContainer;
private ActionBarDrawerToggle mDrawerToggle;
private LinearLayout mBottomItemLayout;
private NavDrawerAdapter mNavDrawerAdapter;
private ArrayList<NavDrawerItem> mDrawerItems;
private NfcAdapter mNfcAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flow_main);
mDrawerContainer = (LinearLayout) findViewById(R.id.drawer);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mBottomItemLayout = (LinearLayout) findViewById(R.id.drawer_bottom_item);
boolean isUserLoggedIn = ((FlowApplication)getApplication()).isUserLoggedIn();
mDrawerItems = new ArrayList<NavDrawerItem>();
if (isUserLoggedIn) {
mDrawerItems.add(new NavDrawerItem(
Constants.NAV_DRAWER_PROFILE_INDEX,
getString(R.string.drawer_item_profile),
R.drawable.drawer_profile_icon));
}
mDrawerItems.add(new NavDrawerItem(
Constants.NAV_DRAWER_EXPLORE_INDEX,
getString(R.string.drawer_item_explore),
R.drawable.drawer_explore_icon));
mDrawerItems.add(new NavDrawerItem(
Constants.NAV_DRAWER_ABOUT_INDEX,
getString(R.string.drawer_item_about),
R.drawable.drawer_about_icon));
// Configure bottom menu item
TextView label = (TextView) mBottomItemLayout.findViewById(R.id.drawer_item_name);
ImageView icon = (ImageView) mBottomItemLayout.findViewById(R.id.drawer_item_icon);
if (isUserLoggedIn) {
label.setText("Log out");
icon.setImageResource(R.drawable.drawer_log_out_icon);
mBottomItemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Session.getActiveSession() != null) {
Session.getActiveSession().closeAndClearTokenInformation();
}
Session.setActiveSession(null);
// Remove flow cookies
FlowAsyncClient.clearCookie();
// Remove database entries from all tables on logout
FlowDatabaseHelper.clearDatabase(getHelper().getConnectionSource());
FlowApplication app = (FlowApplication) getApplication();
app.setUserLoggedIn(false);
app.track("Logout");
Intent intent = new Intent(MainFlowActivity.this, LoginActivity.class);
intent.putExtra("finish", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return;
}
});
} else {
label.setText("Log in");
icon.setImageResource(R.drawable.drawer_log_in_icon);
mBottomItemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((FlowApplication) getApplication()).track("Login intent from logged out user");
Intent intent = new Intent(MainFlowActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
mNavDrawerAdapter = new NavDrawerAdapter(mDrawerItems, this);
mDrawerList.setAdapter(mNavDrawerAdapter);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer_am,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
if (savedInstanceState == null) {
Fragment initialFragment;
if (isUserLoggedIn) {
selectItem(Constants.NAV_DRAWER_PROFILE_INDEX, false);
} else {
selectItem(Constants.NAV_DRAWER_EXPLORE_INDEX, false);
}
}
// Check for available NFC Adapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
// TODO: Log that NFC is unavailable on this device?
} else {
// Register NFC push message callback
mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.content_frame);
if (fragment != null && fragment instanceof SharableURL) {
String url = ((SharableURL) fragment).getUrl();
if (url == null) return null;
NdefMessage msg = new NdefMessage(
new NdefRecord[] {
NdefRecord.createUri(url)
}
);
JSONObject properties = new JSONObject();
try {
properties.put("uri", url);
} catch (JSONException e) {
e.printStackTrace();
}
((FlowApplication) getApplication()).track("NFC push event", properties);
return msg;
}
return null;
}
}, this);
}
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onResume() {
super.onResume();
if (mNfcAdapter != null) {
// If system supports NFC...
enableForegroundDispatch(this, mNfcAdapter);
// See if the Activity is being started/resumed due to an NFC event and handle it
handleNfcIntent(getIntent());
}
}
@Override
public void onPause() {
super.onPause();
if (mNfcAdapter != null) {
// If system supports NFC...
mNfcAdapter.disableForegroundDispatch(this);
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
NavDrawerItem drawerItem = (NavDrawerItem) parent.getItemAtPosition(position);
selectItem(drawerItem.getId(), true);
}
}
@Override
public void setTitle(CharSequence title) {
getActionBar().setTitle(title);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_refresh:
Intent intent = new Intent(Constants.BroadcastActionId.UPDATE_CURRENT_FRAGMENT);
LocalBroadcastManager.getInstance(this.getApplicationContext()).sendBroadcast(intent);
break;
case R.id.action_search:
selectItem(Constants.NAV_DRAWER_EXPLORE_INDEX, true);
break;
case R.id.action_view_on_fb:
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.content_frame);
if (fragment != null && fragment instanceof ProfileFragment) {
User user = ((ProfileFragment) fragment).getUser();
if (user != null && user.getFbid() != null) {
FacebookUtilities.viewUserOnFacebook(this, user.getFbid());
((FlowApplication) getApplication()).track("View user on Facebook");
}
}
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleNfcIntent(intent);
}
/** Swaps fragments in the main content view */
private void selectItem(int itemID, boolean addToBackStack) {
Fragment fragment;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_frame);
switch (itemID) {
case(Constants.NAV_DRAWER_PROFILE_INDEX) :
if (currentFragment != null && currentFragment instanceof ProfileFragment) {
if (((ProfileFragment)currentFragment).getProfileID() == null) {
// Do nothing. Signed-in user's profile is already onscreen.
mDrawerLayout.closeDrawer(mDrawerContainer);
return;
}
}
fragment = new ProfileFragment();
break;
case(Constants.NAV_DRAWER_EXPLORE_INDEX) :
if (currentFragment != null && currentFragment instanceof ExploreFragment) {
// Do nothing. Explore fragment is already onscreen.
mDrawerLayout.closeDrawer(mDrawerContainer);
return;
}
fragment = new ExploreFragment();
break;
case(Constants.NAV_DRAWER_ABOUT_INDEX) :
if (currentFragment != null && currentFragment instanceof AboutFragment) {
// Do nothing. About fragment is already onscreen.
mDrawerLayout.closeDrawer(mDrawerContainer);
return;
}
fragment = new AboutFragment();
break;
default :
Log.e(TAG, "Unrecognized drawer item selected.");
return;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment);
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
int selectedPosition = mNavDrawerAdapter.getPositionFromId(itemID);
if (selectedPosition >= 0) {
mDrawerList.setItemChecked(selectedPosition, true);
}
mDrawerLayout.closeDrawer(mDrawerContainer);
}
private void handleNfcIntent(Intent intent) {
if (intent == null) return;
// Check if Activity was opened via NFC to load a specific Fragment
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
String payload = intent.getDataString();
if (payload != null) {
String profileUrlFormat = Constants.BASE_URL + Constants.URL_PROFILE_EXT;
String courseUrlFormat = Constants.BASE_URL + Constants.URL_COURSE_EXT;
if (payload.startsWith(profileUrlFormat)) {
// Open a ProfileFragment with the provided payload profile ID
String profileID = payload.substring(profileUrlFormat.length());
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, ProfileFragment.newInstance(profileID))
.addToBackStack(null)
.commit();
} else if (payload.startsWith(courseUrlFormat)) {
// Open a CourseFragment with the provided payload course ID
String courseID = payload.substring(courseUrlFormat.length());
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, CourseFragment.newInstance(courseID))
.addToBackStack(null)
.commit();
}
JSONObject properties = new JSONObject();
try {
properties.put("uri", payload);
} catch (JSONException e) {
e.printStackTrace();
}
((FlowApplication) getApplication()).track("NFC receive event", properties);
}
}
}
/**
* @param activity The activity that's requesting dispatch
* @param adapter NfcAdapter for the current context
*/
private void enableForegroundDispatch(Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
filters[0].addDataScheme("https");
filters[0].addDataAuthority(Constants.FLOW_DOMAIN, null);
filters[0].addDataPath(".*", PatternMatcher.PATTERN_SIMPLE_GLOB);
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
}