forked from criticalmaps/criticalmaps-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
509 lines (429 loc) · 20.7 KB
/
Main.java
File metadata and controls
509 lines (429 loc) · 20.7 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package de.stephanlindauer.criticalmaps;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Toast;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.navigation.NavigationView;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import javax.inject.Inject;
import de.stephanlindauer.criticalmaps.databinding.ActivityMainBinding;
import de.stephanlindauer.criticalmaps.handler.ApplicationCloseHandler;
import de.stephanlindauer.criticalmaps.handler.PermissionCheckHandler;
import de.stephanlindauer.criticalmaps.handler.ProcessCameraResultHandler;
import de.stephanlindauer.criticalmaps.handler.StartCameraHandler;
import de.stephanlindauer.criticalmaps.helper.clientinfo.BuildInfo;
import de.stephanlindauer.criticalmaps.helper.clientinfo.DeviceInformation;
import de.stephanlindauer.criticalmaps.managers.LocationUpdateManager;
import de.stephanlindauer.criticalmaps.prefs.SharedPrefsKeys;
import de.stephanlindauer.criticalmaps.provider.FragmentProvider;
import de.stephanlindauer.criticalmaps.service.ServerSyncService;
import de.stephanlindauer.criticalmaps.utils.DrawerClosingDrawerLayoutListener;
import de.stephanlindauer.criticalmaps.utils.ImageUtils;
import de.stephanlindauer.criticalmaps.utils.IntentUtil;
import de.stephanlindauer.criticalmaps.vo.RequestCodes;
import info.metadude.android.typedpreferences.BooleanPreference;
import timber.log.Timber;
public class Main extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private final static String KEY_NAV_ID = "main_navid";
private final static String KEY_SAVED_FRAGMENT_STATES = "main_savedfragmentstate";
private final static String KEY_NEW_CAMERA_OUTPUT_FILE = "main_newcameraoutputfile";
@Inject
public PermissionCheckHandler permissionCheckHandler;
@Inject
LocationUpdateManager locationUpdateManager;
@Inject
SharedPreferences sharedPreferences;
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener =
(sharedPreferences, key) -> {
switch (key) {
case SharedPrefsKeys.SHOW_ON_LOCKSCREEN:
setShowOnLockscreen();
break;
case SharedPrefsKeys.KEEP_SCREEN_ON:
setKeepScreenOn();
break;
case SharedPrefsKeys.PRIVACY_POLICY_ACCEPTED:
if (!LocationUpdateManager.checkPermission()) {
locationUpdateManager.requestPermission();
}
break;
}
};
private ActivityMainBinding binding;
private Uri newCameraOutputFile;
private int currentNavId;
private SparseArray<Fragment.SavedState> savedFragmentStates = new SparseArray<>();
private SwitchCompat observerModeSwitch;
private BooleanPreference privacyPolicyAcceptedPreference;
@Override
public void onCreate(Bundle bundle) {
setTheme(R.style.AppTheme); // has to be before super!
super.onCreate(bundle);
App.components().inject(this);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
binding.drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
// inset the toolbar down by the status bar height
ViewCompat.setOnApplyWindowInsetsListener(binding.toolbar, (v, insets) -> {
ViewGroup.MarginLayoutParams lpToolbar =
(ViewGroup.MarginLayoutParams) binding.toolbar.getLayoutParams();
lpToolbar.topMargin += insets.getSystemWindowInsetTop();
binding.toolbar.setLayoutParams(lpToolbar);
// clear this listener so insets aren't re-applied
ViewCompat.setOnApplyWindowInsetsListener(binding.toolbar, null);
return insets;
});
// inset header in nav drawer down by the status bar height
View navHeader = binding.drawerNavigation.getHeaderView(0);
ViewCompat.setOnApplyWindowInsetsListener(navHeader, (v, insets) -> {
v.setPaddingRelative(
v.getPaddingStart(), v.getPaddingTop() + insets.getSystemWindowInsetTop(),
v.getPaddingEnd(), v.getPaddingBottom());
// clear this listener so insets aren't re-applied
ViewCompat.setOnApplyWindowInsetsListener(navHeader, null);
return insets;
});
}
// This is a little hacky and might break with a materialcomponents lib update
RecyclerView navigationMenuView = findViewById(R.id.design_navigation_view);
navigationMenuView.setNestedScrollingEnabled(false);
setShowOnLockscreen();
setKeepScreenOn();
}
@Override
protected void onStart() {
super.onStart();
permissionCheckHandler.attachActivity(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
privacyPolicyAcceptedPreference = new BooleanPreference(sharedPreferences, SharedPrefsKeys.PRIVACY_POLICY_ACCEPTED);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
binding.drawerNavigation.setNavigationItemSelectedListener(this);
setSupportActionBar(binding.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, binding.drawerLayout, binding.toolbar, R.string.open_drawer, R.string.close_drawer);
binding.drawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
binding.drawerLayout.addDrawerListener(new DrawerClosingDrawerLayoutListener() {
@Override
public void onDrawerClosed(@NonNull View drawerView) {
super.onDrawerClosed(drawerView);
navigateTo(binding.drawerNavigation.getCheckedItem().getItemId());
}
});
observerModeSwitch = binding.drawerNavigation.getMenu().findItem(R.id.navigation_observer_mode)
.getActionView().findViewById(R.id.navigation_observer_mode_switch);
observerModeSwitch.setChecked(new BooleanPreference(
sharedPreferences, SharedPrefsKeys.OBSERVER_MODE_ACTIVE).get());
observerModeSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) -> handleObserverModeSwitchCheckedChanged(isChecked));
binding.understandButton.setOnClickListener(view -> {
binding.introductionView.setVisibility(View.GONE);
privacyPolicyAcceptedPreference.set(true);
});
if (savedInstanceState != null) {
SparseArray<Fragment.SavedState> restoredStates =
savedInstanceState.getSparseParcelableArray(KEY_SAVED_FRAGMENT_STATES);
if (restoredStates != null) {
savedFragmentStates = restoredStates;
}
newCameraOutputFile = savedInstanceState.getParcelable(KEY_NEW_CAMERA_OUTPUT_FILE);
currentNavId = savedInstanceState.getInt(KEY_NAV_ID);
if (currentNavId != R.id.navigation_map) {
// set toolbar title
//noinspection ConstantConditions
getSupportActionBar().setTitle(binding.drawerNavigation.getCheckedItem().getTitle());
// set toolbar margins
ViewGroup.MarginLayoutParams toolbarParams =
(ViewGroup.MarginLayoutParams) binding.toolbar.getLayoutParams();
int marginPixels =
getResources().getDimensionPixelSize(R.dimen.map_toolbar_margins);
toolbarParams.topMargin -= marginPixels;
toolbarParams.rightMargin -= marginPixels;
toolbarParams.leftMargin -= marginPixels;
binding.toolbar.setLayoutParams(toolbarParams);
// set toolbar background
((GradientDrawable) binding.toolbar.getBackground()).setCornerRadius(0F);
// set statusbar color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(
ContextCompat.getColor(this, R.color.main_statusbarcolor_others));
}
}
} else {
navigateTo(R.id.navigation_map);
}
locationUpdateManager.initialize();
final boolean isPrivacyPolicyAccepted = privacyPolicyAcceptedPreference.get();
if (!isPrivacyPolicyAccepted) {
binding.introductionText.setMovementMethod(LinkMovementMethod.getInstance());
binding.introductionText.setText(Html.fromHtml(getString(R.string.introduction_gps)));
binding.introductionView.setVisibility(View.VISIBLE);
}
}
@Override
protected void onResume() {
super.onResume();
Timber.d("onResume() called");
final boolean isPrivacyPolicyAccepted = privacyPolicyAcceptedPreference.get();
if (isPrivacyPolicyAccepted) {
initiateServiceStartIfPermitted();
}
}
@Override
protected void onStop() {
permissionCheckHandler.detachActivity();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(
sharedPreferenceChangeListener);
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_toolbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_close) {
handleCloseRequested();
} else if (itemId == R.id.take_picture) {
new StartCameraHandler(this).execute();
} else if (itemId == R.id.settings_feedback) {
startFeedbackIntent();
} else if (itemId == R.id.settings_datenschutz) {
startDatenschutzIntent();
} else if (itemId == R.id.rate_the_app) {
startRateTheApp();
} else {
return super.onOptionsItemSelected(item);
}
return true;
}
@Override
public void onBackPressed() {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawers();
} else if (currentNavId != R.id.navigation_map) {
navigateTo(R.id.navigation_map);
} else {
super.onBackPressed();
}
}
private void handleCloseRequested() {
new ApplicationCloseHandler(this).execute();
}
private void startFeedbackIntent() {
Intent intent = new Intent(Intent.ACTION_SEND)
.setType("text/email")
.putExtra(Intent.EXTRA_EMAIL, new String[]{getString(R.string.contact_email)})
.putExtra(Intent.EXTRA_SUBJECT, "feedback critical maps")
.putExtra(Intent.EXTRA_TEXT, DeviceInformation.getString() + BuildInfo.getString());
startActivity(Intent.createChooser(intent, "Send Feedback:"));
}
private void startDatenschutzIntent() {
IntentUtil.startFromURL(this,
"http://criticalmaps.net/info#Datenschutzerklärung");
}
private void startRateTheApp() {
IntentUtil.startFromURL(this,
"https://play.google.com/store/apps/details?id=de.stephanlindauer.criticalmaps");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
Timber.d("requestCode: %d, resultCode: %d", requestCode, resultCode);
Toast.makeText(this, R.string.something_went_wrong, Toast.LENGTH_LONG).show();
return;
}
if (requestCode == RequestCodes.CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
File movedFile =
ImageUtils.movePhotoToFilesDir(new File(newCameraOutputFile.getPath()));
newCameraOutputFile = null;
new ProcessCameraResultHandler(this, movedFile).execute();
}
}
public void setNewCameraOutputFile(Uri newCameraOutputFile) {
this.newCameraOutputFile = newCameraOutputFile;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("shouldClose") && intent.getBooleanExtra("shouldClose", false)) {
new ApplicationCloseHandler(this).execute();
}
}
private void setShowOnLockscreen() {
if (new BooleanPreference(sharedPreferences, SharedPrefsKeys.SHOW_ON_LOCKSCREEN).get()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
}
private void setKeepScreenOn() {
if (new BooleanPreference(sharedPreferences, SharedPrefsKeys.KEEP_SCREEN_ON).get()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
@Override
protected void onSaveInstanceState(@NotNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_NAV_ID, currentNavId);
outState.putSparseParcelableArray(KEY_SAVED_FRAGMENT_STATES, savedFragmentStates);
if (newCameraOutputFile != null) {
outState.putParcelable(KEY_NEW_CAMERA_OUTPUT_FILE, newCameraOutputFile);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getGroupId() == R.id.navigation_group) {
item.setChecked(true);
binding.drawerLayout.closeDrawer(GravityCompat.START);
//noinspection ConstantConditions
getSupportActionBar().setTitle(item.getTitle());
return true;
}
if (item.getItemId() == R.id.navigation_observer_mode) {
observerModeSwitch.setChecked(!observerModeSwitch.isChecked());
return true;
}
return false;
}
private void navigateTo(@IdRes int navId) {
if (currentNavId == navId) {
return; // no need for action
}
// save state of current fragment
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_frame);
if (currentFragment != null) {
Fragment.SavedState state =
getSupportFragmentManager().saveFragmentInstanceState(currentFragment);
savedFragmentStates.put(currentNavId, state);
}
final Fragment nextFragment = FragmentProvider.getFragmentForNavId(navId);
// restore saved state of new fragment if it was shown before; otherwise passing null is ok
nextFragment.setInitialSavedState(savedFragmentStates.get(navId));
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, nextFragment).commit();
// animate toolbar and statusbar color
if (currentNavId == R.id.navigation_map) {
// from map to other fragment
animateToolbar(200, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fadeInStatusBarColor(200, false);
}
} else if (navId == R.id.navigation_map && currentNavId != 0) {
// from other fragment to map AND not app start
animateToolbar(500, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fadeInStatusBarColor(500, true);
}
}
currentNavId = navId;
}
private void animateToolbar(int durationMillis, boolean toMap) {
ViewGroup.MarginLayoutParams toolbarParamsChanging =
(ViewGroup.MarginLayoutParams) binding.toolbar.getLayoutParams();
ViewGroup.MarginLayoutParams toolbarParamsStart =
new ViewGroup.MarginLayoutParams(toolbarParamsChanging);
int marginPixels = getResources().getDimensionPixelSize(R.dimen.map_toolbar_margins);
if (toMap) {
marginPixels *= -1;
}
ValueAnimator marginAnimator = ValueAnimator.ofInt(0, marginPixels);
marginAnimator.setDuration(durationMillis);
marginAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
marginAnimator.addUpdateListener(animation -> {
int animatedValue = (int) animation.getAnimatedValue();
toolbarParamsChanging.topMargin = toolbarParamsStart.topMargin - animatedValue;
toolbarParamsChanging.rightMargin = toolbarParamsStart.rightMargin - animatedValue;
toolbarParamsChanging.leftMargin = toolbarParamsStart.leftMargin - animatedValue;
binding.toolbar.setLayoutParams(toolbarParamsChanging);
});
GradientDrawable toolbarBackground = (GradientDrawable) binding.toolbar.getBackground();
float radiusMap = getResources().getDimension(R.dimen.map_toolbar_corner_radius);
float radiusFrom = toMap ? 0 : radiusMap;
float radiusTo = toMap ? radiusMap : 0;
ValueAnimator radiusAnimator = ValueAnimator.ofFloat(radiusFrom, radiusTo);
radiusAnimator.setDuration(durationMillis);
marginAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
radiusAnimator.addUpdateListener(animation -> {
float value = (float) animation.getAnimatedValue();
toolbarBackground.setCornerRadius(value);
});
marginAnimator.start();
radiusAnimator.start();
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private void fadeInStatusBarColor(int duration, boolean toMap) {
int colorMap = ContextCompat.getColor(this, R.color.main_statusbarcolor_map);
int colorOthers = ContextCompat.getColor(this, R.color.main_statusbarcolor_others);
int colorFrom = toMap ? colorOthers : colorMap;
int colorTo = toMap ? colorMap : colorOthers;
ValueAnimator valueAnimator =
ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
valueAnimator.setDuration(duration);
valueAnimator.addUpdateListener(animation -> {
int color = (int) animation.getAnimatedValue();
getWindow().setStatusBarColor(color);
});
valueAnimator.start();
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (!permissionCheckHandler.handlePermissionRequestCallback(requestCode, grantResults)) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void handleObserverModeSwitchCheckedChanged(boolean isChecked) {
new BooleanPreference(
sharedPreferences, SharedPrefsKeys.OBSERVER_MODE_ACTIVE).set(isChecked);
}
private void initiateServiceStartIfPermitted() {
if (LocationUpdateManager.checkPermission()) {
if (!ServerSyncService.isCurrentlyRunning()) {
Timber.d("Location and notification permissions granted. Attempting to start ServerSyncService.");
ServerSyncService.startService();
} else {
Timber.d("Location and notification permission granted, but service is already running.");
}
} else {
Timber.d("Location and notification permission NOT granted. ServerSyncService will not be started.");
}
}
}