storymaker / app / src / org / storymaker / app / BaseActivity.java @ 5debefcb
History | View | Annotate | Download (17.1 KB)
1 |
package org.storymaker.app; |
---|---|
2 |
|
3 |
import android.Manifest; |
4 |
import android.app.Activity; |
5 |
import android.app.Notification; |
6 |
import android.content.Context; |
7 |
import android.content.Intent; |
8 |
import android.content.SharedPreferences; |
9 |
import android.content.pm.PackageManager; |
10 |
import android.content.res.AssetManager; |
11 |
import android.content.res.Configuration; |
12 |
import android.graphics.Bitmap; |
13 |
import android.graphics.BitmapFactory; |
14 |
import android.graphics.PixelFormat; |
15 |
import android.os.Bundle; |
16 |
import android.preference.PreferenceManager; |
17 |
import android.support.v4.app.ActionBarDrawerToggle; |
18 |
import android.support.v4.app.ActivityCompat; |
19 |
import android.support.v4.app.FragmentActivity; |
20 |
import android.support.v4.app.NotificationCompat; |
21 |
import android.support.v4.widget.DrawerLayout; |
22 |
import android.view.MenuItem; |
23 |
import android.view.View; |
24 |
import android.view.View.OnClickListener; |
25 |
import android.view.ViewGroup; |
26 |
import android.view.WindowManager; |
27 |
import android.widget.Button; |
28 |
import android.widget.ImageButton; |
29 |
import android.widget.ImageView; |
30 |
import android.widget.RelativeLayout; |
31 |
import android.widget.TextView; |
32 |
import android.widget.Toast; |
33 |
|
34 |
import net.hockeyapp.android.FeedbackManager; |
35 |
|
36 |
import org.storymaker.app.server.ServerManager; |
37 |
|
38 |
import java.io.IOException; |
39 |
import java.io.InputStream; |
40 |
import java.util.ArrayList; |
41 |
|
42 |
import info.guardianproject.cacheword.CacheWordHandler; |
43 |
import info.guardianproject.cacheword.ICacheWordSubscriber; |
44 |
import scal.io.liger.StorageHelper; |
45 |
import timber.log.Timber; |
46 |
|
47 |
//import com.google.analytics.tracking.android.EasyTracker;
|
48 |
// NEW/CACHEWORD
|
49 |
|
50 |
public class BaseActivity extends FragmentActivity implements ICacheWordSubscriber { |
51 |
|
52 |
// NEW/CACHEWORD
|
53 |
protected CacheWordHandler mCacheWordHandler;
|
54 |
public static final String CACHEWORD_UNSET = "unset"; |
55 |
public static final String CACHEWORD_FIRST_LOCK = "first_lock"; |
56 |
public static final String CACHEWORD_SET = "set"; |
57 |
public static final String CACHEWORD_TIMEOUT = "300"; |
58 |
|
59 |
public boolean setPin = false; |
60 |
|
61 |
protected ActionBarDrawerToggle mDrawerToggle;
|
62 |
protected DrawerLayout mDrawerLayout;
|
63 |
protected ViewGroup mDrawerContainer;
|
64 |
protected boolean mDrawerOpen; |
65 |
|
66 |
@Override
|
67 |
public void onStart() { |
68 |
super.onStart();
|
69 |
} |
70 |
|
71 |
@Override
|
72 |
public void onStop() { |
73 |
super.onStop();
|
74 |
} |
75 |
|
76 |
// NEW/CACHEWORD
|
77 |
@Override
|
78 |
protected void onPause() { |
79 |
super.onPause();
|
80 |
mCacheWordHandler.disconnectFromService(); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
protected void onResume() { |
85 |
super.onResume();
|
86 |
|
87 |
// only display notification if the user has set a pin
|
88 |
SharedPreferences sp = getSharedPreferences("appPrefs", MODE_PRIVATE);
|
89 |
String cachewordStatus = sp.getString("cacheword_status", "default"); |
90 |
if (cachewordStatus.equals(CACHEWORD_SET)) {
|
91 |
Timber.d("pin set, so display notification (base)");
|
92 |
mCacheWordHandler.setNotification(buildNotification(this));
|
93 |
} else {
|
94 |
Timber.d("no pin set, so no notification (base)");
|
95 |
} |
96 |
|
97 |
mCacheWordHandler.connectToService(); |
98 |
updateSlidingMenuWithUserState(); |
99 |
} |
100 |
|
101 |
private Notification buildNotification(Context c) { |
102 |
Timber.d("buildNotification (base)");
|
103 |
|
104 |
NotificationCompat.Builder b = new NotificationCompat.Builder(c);
|
105 |
b.setSmallIcon(R.drawable.ic_menu_key); |
106 |
b.setContentTitle(c.getText(R.string.cacheword_notification_cached_title)); |
107 |
b.setContentText(c.getText(R.string.cacheword_notification_cached_message)); |
108 |
b.setTicker(c.getText(R.string.cacheword_notification_cached)); |
109 |
b.setWhen(System.currentTimeMillis());
|
110 |
b.setOngoing(true);
|
111 |
b.setContentIntent(CacheWordHandler.getPasswordLockPendingIntent(c)); |
112 |
return b.getNotification();
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public void onCacheWordUninitialized() { |
117 |
// if we're uninitialized, default behavior should be to stop
|
118 |
Timber.d("cacheword uninitialized, activity will not continue");
|
119 |
finish(); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public void onCacheWordLocked() { |
124 |
// if we're locked, default behavior should be to stop
|
125 |
Timber.d("cacheword locked, activity will not continue");
|
126 |
finish(); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void onCacheWordOpened() { |
131 |
// mount vfs file (if a pin has been set) - mounting here seems to be required for loading stories from the home screen
|
132 |
|
133 |
SharedPreferences sp = getSharedPreferences("appPrefs", MODE_PRIVATE);
|
134 |
String cachewordStatus = sp.getString("cacheword_status", "default"); |
135 |
if (cachewordStatus.equals(CACHEWORD_SET)) {
|
136 |
if (mCacheWordHandler.isLocked()) {
|
137 |
Timber.d("onCacheWordOpened(storymaker) - pin set but cacheword locked, cannot mount vfs");
|
138 |
} else {
|
139 |
Timber.d("onCacheWordOpened(storymaker) - pin set and cacheword unlocked, mounting vfs");
|
140 |
StorageHelper.mountStorage(this, null, mCacheWordHandler.getEncryptionKey()); |
141 |
} |
142 |
} else {
|
143 |
Timber.d("onCacheWordOpened(storymaker) - no pin set, cannot mount vfs");
|
144 |
} |
145 |
|
146 |
// unsure how this resolves first-run crash issue but i'm unable to reproduce the error with this check here
|
147 |
if (mCacheWordHandler.isLocked()) {
|
148 |
Timber.d("onCacheWordOpened(storymaker) - we're in on-opened method but we're still locked somehow");
|
149 |
} else {
|
150 |
// if we're opened, check db and update menu status
|
151 |
Timber.d("onCacheWordOpened(storymaker) - cacheword unlocked, updating menu");
|
152 |
updateSlidingMenuWithUserState(); |
153 |
} |
154 |
} |
155 |
|
156 |
public void setupDrawerLayout() { |
157 |
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); |
158 |
mDrawerContainer = (ViewGroup) findViewById(R.id.left_drawer); |
159 |
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, |
160 |
R.drawable.ic_drawer_white, R.string.open_drawer, R.string.close_drawer) { |
161 |
|
162 |
/** Called when a drawer has settled in a completely closed state. */
|
163 |
public void onDrawerClosed(View view) { |
164 |
super.onDrawerClosed(view);
|
165 |
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
|
166 |
mDrawerOpen = false;
|
167 |
} |
168 |
|
169 |
/** Called when a drawer has settled in a completely open state. */
|
170 |
public void onDrawerOpened(View drawerView) { |
171 |
super.onDrawerOpened(drawerView);
|
172 |
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
|
173 |
mDrawerOpen = true;
|
174 |
} |
175 |
}; |
176 |
|
177 |
mDrawerLayout.setDrawerListener(mDrawerToggle); |
178 |
|
179 |
final Activity activity = this; |
180 |
|
181 |
RelativeLayout llDrawerLogin = (RelativeLayout) findViewById(R.id.llLogin); |
182 |
|
183 |
ImageButton btnDrawerQuickCaptureVideo = (ImageButton) findViewById(R.id.btnDrawerQuickCaptureVideo); |
184 |
ImageButton btnDrawerQuickCapturePhoto = (ImageButton) findViewById(R.id.btnDrawerQuickCapturePhoto); |
185 |
ImageButton btnDrawerQuickCaptureAudio = (ImageButton) findViewById(R.id.btnDrawerQuickCaptureAudio); |
186 |
|
187 |
Button btnDrawerHome = (Button) findViewById(R.id.btnDrawerHome); |
188 |
Button btnDrawerCatalog = (Button) findViewById(R.id.btnDrawerCatalog); |
189 |
// Button btnDrawerProjects = (Button) findViewById(R.id.btnDrawerProjects);
|
190 |
//Button btnDrawerAccount = (Button) findViewById(R.id.btnDrawerAccount);
|
191 |
Button btnDrawerAccounts = (Button) findViewById(R.id.btnDrawerAccounts); |
192 |
Button btnDrawerExports = (Button) findViewById(R.id.btnDrawerExports); |
193 |
Button btnDrawerUploadManager = (Button) findViewById(R.id.btnDrawerUploadManager); |
194 |
Button btnDrawerSettings = (Button) findViewById(R.id.btnDrawerSettings); |
195 |
Button btnDrawerFeedback = (Button) findViewById(R.id.btnDrawerFeedback); |
196 |
TextView textViewVersion = (TextView) findViewById(R.id.textViewVersion); |
197 |
|
198 |
// NEW/CACHEWORD
|
199 |
Button btnDrawerLock = (Button) findViewById(R.id.btnDrawerLock); |
200 |
|
201 |
// disable button if the user has set a pin
|
202 |
SharedPreferences sp = getSharedPreferences("appPrefs", MODE_PRIVATE);
|
203 |
String cachewordStatus = sp.getString("cacheword_status", "default"); |
204 |
if (cachewordStatus.equals(CACHEWORD_SET)) {
|
205 |
Timber.d("pin set, so remove button");
|
206 |
btnDrawerLock.setVisibility(View.GONE);
|
207 |
} else {
|
208 |
Timber.d("no pin set, so show button");
|
209 |
} |
210 |
|
211 |
String pkg = getPackageName();
|
212 |
try {
|
213 |
String versionName = getPackageManager().getPackageInfo(pkg, 0).versionName; |
214 |
int versionCode = getPackageManager().getPackageInfo(pkg, 0).versionCode; |
215 |
textViewVersion.setText("v" + versionName + " build " + versionCode); |
216 |
} catch (PackageManager.NameNotFoundException e) {
|
217 |
Timber.e(e, "NameNotFoundException?");
|
218 |
} |
219 |
|
220 |
updateSlidingMenuWithUserState(); |
221 |
|
222 |
// Set a random profile background
|
223 |
ImageView imageViewProfileBg = (ImageView) findViewById(R.id.imageViewProfileBg); |
224 |
int profileBg = (int) (Math.random() * 2); |
225 |
switch (profileBg) {
|
226 |
case 0: |
227 |
imageViewProfileBg.setImageResource(R.drawable.profile_bg1); |
228 |
break;
|
229 |
case 1: |
230 |
imageViewProfileBg.setImageResource(R.drawable.profile_bg2); |
231 |
break;
|
232 |
case 2: |
233 |
imageViewProfileBg.setImageResource(R.drawable.profile_bg3); |
234 |
break;
|
235 |
} |
236 |
|
237 |
llDrawerLogin.setOnClickListener(new OnClickListener() {
|
238 |
@Override
|
239 |
public void onClick(View v) { |
240 |
|
241 |
mDrawerLayout.closeDrawers(); |
242 |
|
243 |
Intent i = new Intent(activity, ConnectAccountActivity.class);
|
244 |
activity.startActivity(i); |
245 |
} |
246 |
}); |
247 |
|
248 |
btnDrawerHome.setOnClickListener(new OnClickListener() {
|
249 |
@Override
|
250 |
public void onClick(View v) { |
251 |
|
252 |
mDrawerLayout.closeDrawers(); |
253 |
|
254 |
Intent i = new Intent(activity, HomeActivity.class);
|
255 |
activity.startActivity(i); |
256 |
} |
257 |
}); |
258 |
|
259 |
btnDrawerCatalog.setOnClickListener(new OnClickListener() {
|
260 |
@Override
|
261 |
public void onClick(View v) { |
262 |
mDrawerLayout.closeDrawers(); |
263 |
|
264 |
Intent i = new Intent(activity, CatalogActivity.class);
|
265 |
activity.startActivity(i); |
266 |
} |
267 |
}); |
268 |
|
269 |
btnDrawerExports.setOnClickListener(new OnClickListener() {
|
270 |
@Override
|
271 |
public void onClick(View v) { |
272 |
|
273 |
mDrawerLayout.closeDrawers(); |
274 |
Intent i = new Intent(activity, ProjectsActivity.class);
|
275 |
activity.startActivity(i); |
276 |
} |
277 |
}); |
278 |
|
279 |
btnDrawerAccounts.setOnClickListener(new OnClickListener() {
|
280 |
@Override
|
281 |
public void onClick(View v) { |
282 |
mDrawerLayout.closeDrawers(); |
283 |
|
284 |
Intent i = new Intent(activity, AccountsActivity.class);
|
285 |
activity.startActivity(i); |
286 |
} |
287 |
}); |
288 |
|
289 |
btnDrawerUploadManager.setOnClickListener(new OnClickListener() {
|
290 |
@Override
|
291 |
public void onClick(View v) { |
292 |
mDrawerLayout.closeDrawers(); |
293 |
Toast.makeText(getApplicationContext(), "Not yet implemented", Toast.LENGTH_LONG).show();
|
294 |
// Intent i = new Intent(activity, AccountsActivity.class);
|
295 |
// activity.startActivity(i);
|
296 |
} |
297 |
}); |
298 |
|
299 |
btnDrawerSettings.setOnClickListener(new OnClickListener() {
|
300 |
@Override
|
301 |
public void onClick(View v) { |
302 |
mDrawerLayout.closeDrawers(); |
303 |
|
304 |
Intent i = new Intent(activity, SimplePreferences.class);
|
305 |
activity.startActivity(i); |
306 |
} |
307 |
}); |
308 |
|
309 |
btnDrawerFeedback.setOnClickListener(new OnClickListener() {
|
310 |
@Override
|
311 |
public void onClick(View v) { |
312 |
mDrawerLayout.closeDrawers(); |
313 |
|
314 |
FeedbackManager.register(activity, AppConstants.HOCKEY_APP_ID, null);
|
315 |
FeedbackManager.showFeedbackActivity(activity); |
316 |
} |
317 |
}); |
318 |
|
319 |
// NEW/CACHEWORD
|
320 |
btnDrawerLock.setOnClickListener(new OnClickListener() {
|
321 |
@Override
|
322 |
public void onClick(View v) { |
323 |
// if there has been no first lock, set status so user will be prompted to create a pin
|
324 |
SharedPreferences sp = getSharedPreferences("appPrefs", MODE_PRIVATE);
|
325 |
String cachewordStatus = sp.getString("cacheword_status", "default"); |
326 |
if (cachewordStatus.equals(CACHEWORD_UNSET)) {
|
327 |
|
328 |
// set flag so user will be prompted to create a pin
|
329 |
setPin = true;
|
330 |
Timber.d("set cacheword first lock flag");
|
331 |
} |
332 |
mCacheWordHandler.lock(); |
333 |
} |
334 |
}); |
335 |
} |
336 |
|
337 |
/**
|
338 |
* Alter the Profile badge of the SlidingMenu with the current user state
|
339 |
*
|
340 |
* e.g: Show username if logged in, prompt to sign up or sign in if not.
|
341 |
*/
|
342 |
private void updateSlidingMenuWithUserState() { |
343 |
ServerManager serverManager = StoryMakerApp.getServerManager(); |
344 |
TextView textViewSignIn = (TextView) findViewById(R.id.textViewSignIn); |
345 |
TextView textViewJoinStorymaker = (TextView) findViewById(R.id.textViewJoinStorymaker); |
346 |
|
347 |
if (mCacheWordHandler.isLocked()) {
|
348 |
|
349 |
// prevent credential check attempt if database is locked
|
350 |
Timber.d("cacheword locked, skipping menu credential check");
|
351 |
textViewSignIn.setText(R.string.sign_in); |
352 |
textViewJoinStorymaker.setVisibility(View.VISIBLE);
|
353 |
|
354 |
} else if (serverManager.hasCreds()) { |
355 |
// The Storymaker user is logged in. Replace Sign/Up language with username
|
356 |
textViewSignIn.setText(serverManager.getUserName()); |
357 |
textViewJoinStorymaker.setVisibility(View.GONE);
|
358 |
} else {
|
359 |
textViewSignIn.setText(R.string.sign_in); |
360 |
textViewJoinStorymaker.setVisibility(View.VISIBLE);
|
361 |
} |
362 |
} |
363 |
|
364 |
@Override
|
365 |
public void onCreate(Bundle savedInstanceState) { |
366 |
super.onCreate(savedInstanceState);
|
367 |
|
368 |
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); |
369 |
int timeout = Integer.parseInt(settings.getString("pcachewordtimeout", CACHEWORD_TIMEOUT)); |
370 |
mCacheWordHandler = new CacheWordHandler(this, timeout); |
371 |
|
372 |
if(!Eula.isAccepted(this)) { |
373 |
Intent firstStartIntent = new Intent(this, FirstStartActivity.class); |
374 |
firstStartIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); |
375 |
startActivity(firstStartIntent); |
376 |
} |
377 |
|
378 |
setContentView(R.layout.activity_base); |
379 |
|
380 |
getActionBar().setHomeButtonEnabled(true);
|
381 |
} |
382 |
|
383 |
@Override
|
384 |
protected void onPostCreate(Bundle savedInstanceState) { |
385 |
super.onPostCreate(savedInstanceState);
|
386 |
// Sync the toggle state after onRestoreInstanceState has occurred.
|
387 |
mDrawerToggle.syncState(); |
388 |
} |
389 |
|
390 |
@Override
|
391 |
public void onConfigurationChanged(Configuration newConfig) { |
392 |
super.onConfigurationChanged(newConfig);
|
393 |
mDrawerToggle.onConfigurationChanged(newConfig); |
394 |
} |
395 |
|
396 |
@Override
|
397 |
public boolean onOptionsItemSelected(MenuItem item) { |
398 |
if (mDrawerToggle.onOptionsItemSelected(item)) {
|
399 |
return true; |
400 |
} |
401 |
return super.onOptionsItemSelected(item); |
402 |
} |
403 |
|
404 |
public void setContentView(int resId) { |
405 |
if (!getClass().getSimpleName().equals("BaseActivity")) { |
406 |
|
407 |
super.setContentView(R.layout.activity_base);
|
408 |
if (resId != R.layout.activity_base) {
|
409 |
setContentViewWithinDrawerLayout(resId); |
410 |
setupDrawerLayout(); |
411 |
} |
412 |
|
413 |
} else {
|
414 |
super.setContentView(resId);
|
415 |
} |
416 |
} |
417 |
|
418 |
/**
|
419 |
* Inflate the given layout into this Activity's DrawerLayout
|
420 |
*
|
421 |
* @param resId the layout resource to inflate
|
422 |
*/
|
423 |
public void setContentViewWithinDrawerLayout(int resId) { |
424 |
ViewGroup content = (ViewGroup) findViewById(R.id.content_frame); |
425 |
getLayoutInflater().inflate(resId, content, true);
|
426 |
} |
427 |
|
428 |
public void toggleDrawer() { |
429 |
if (mDrawerLayout == null) return; |
430 |
|
431 |
if (mDrawerOpen) mDrawerLayout.closeDrawer(mDrawerContainer);
|
432 |
else mDrawerLayout.openDrawer(mDrawerContainer);
|
433 |
} |
434 |
|
435 |
// force permissions we require
|
436 |
protected void checkAndEnforcePermissions() { |
437 |
ArrayList<String> perms = new ArrayList<>(); |
438 |
|
439 |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { |
440 |
perms.add(Manifest.permission.READ_EXTERNAL_STORAGE);
|
441 |
} |
442 |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { |
443 |
perms.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
444 |
} |
445 |
if (perms.size() > 0) { |
446 |
ActivityCompat.requestPermissions(this, perms.toArray(new String[0]), Constants.PERMS_REQ_ALL); |
447 |
} |
448 |
} |
449 |
} |