trkpt/app/src/main/java/org/y20k/trackbook/MainActivity.java

533 lines
19 KiB
Java
Raw Normal View History

2016-08-29 12:50:41 +00:00
/**
* MainActivity.java
* Implements the app's main activity
* The main activity sets up the main view end inflates a menu bar menu
*
* This file is part of
* TRACKBOOK - Movement Recorder for Android
*
* Copyright (c) 2016 - Y20K.org
* Licensed under the MIT-License
* http://opensource.org/licenses/MIT
*
2016-08-29 12:50:41 +00:00
* Trackbook uses osmdroid - OpenStreetMap-Tools for Android
* https://github.com/osmdroid/osmdroid
*/
package org.y20k.trackbook;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.DialogFragment;
import android.content.BroadcastReceiver;
import android.content.Context;
2016-08-29 12:50:41 +00:00
import android.content.Intent;
import android.content.IntentFilter;
2016-08-29 12:50:41 +00:00
import android.content.pm.PackageManager;
import android.location.Location;
2016-08-29 12:50:41 +00:00
import android.os.Build;
import android.os.Bundle;
2016-09-12 12:31:37 +00:00
import android.support.annotation.NonNull;
2016-08-29 12:50:41 +00:00
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
2016-08-29 12:50:41 +00:00
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
2016-08-29 12:50:41 +00:00
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.y20k.trackbook.helpers.DialogClearFragment;
2016-09-12 12:31:37 +00:00
import org.y20k.trackbook.helpers.LogHelper;
2016-09-07 13:48:12 +00:00
import org.y20k.trackbook.helpers.NotificationHelper;
2016-08-29 12:50:41 +00:00
import org.y20k.trackbook.helpers.TrackbookKeys;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* MainActivity class
*/
public class MainActivity extends AppCompatActivity implements TrackbookKeys, DialogClearFragment.DialogClearListener {
2016-08-29 12:50:41 +00:00
/* Define log tag */
private static final String LOG_TAG = MainActivity.class.getSimpleName();
2016-08-29 12:50:41 +00:00
/* Main class variables */
2016-09-07 10:30:46 +00:00
private boolean mTrackerServiceRunning;
2016-08-29 12:50:41 +00:00
private boolean mPermissionsGranted;
private List<String> mMissingPermissions;
private FloatingActionButton mFloatingActionButton;
private MainActivityMapFragment mMainActivityMapFragment;
private BroadcastReceiver mTrackingStoppedReceiver;
2016-08-29 12:50:41 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set state of tracking
2016-09-07 10:30:46 +00:00
mTrackerServiceRunning = false;
if (savedInstanceState != null) {
2016-09-07 10:30:46 +00:00
mTrackerServiceRunning = savedInstanceState.getBoolean(INSTANCE_TRACKING_STATE, false);
}
2016-08-29 12:50:41 +00:00
// check permissions on Android 6 and higher
mPermissionsGranted = false;
2016-08-29 12:50:41 +00:00
if (Build.VERSION.SDK_INT >= 23) {
// check permissions
mMissingPermissions = checkPermissions();
mPermissionsGranted = mMissingPermissions.size() == 0;
} else {
mPermissionsGranted = true;
}
// set user agent to prevent getting banned from the osm servers
org.osmdroid.tileprovider.constants.OpenStreetMapTileProviderConstants.setUserAgentValue(BuildConfig.APPLICATION_ID);
// set up main layout
setupLayout();
// setupTestLayout();
2016-08-29 12:50:41 +00:00
// register broadcast receiver for stopped tracking
mTrackingStoppedReceiver = createTrackingStoppedReceiver();
IntentFilter trackingStoppedIntentFilter = new IntentFilter(ACTION_TRACKING_STOPPED);
LocalBroadcastManager.getInstance(this).registerReceiver(mTrackingStoppedReceiver, trackingStoppedIntentFilter);
2016-08-29 12:50:41 +00:00
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// inflate action bar options menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle action bar options menu selection
switch (item.getItemId()) {
// CASE ABOUT
case R.id.action_bar_about:
2016-09-07 10:30:46 +00:00
// get title
String aboutTitle = getString(R.string.header_about);
// put title and content into intent and start activity
Intent aboutIntent = new Intent(this, InfosheetActivity.class);
aboutIntent.putExtra(EXTRA_INFOSHEET_TITLE, aboutTitle);
aboutIntent.putExtra(EXTRA_INFOSHEET_CONTENT, INFOSHEET_CONTENT_ABOUT);
startActivity(aboutIntent);
2016-08-29 12:50:41 +00:00
return true;
// CASE DEFAULT
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
2016-09-07 10:30:46 +00:00
outState.putBoolean(INSTANCE_TRACKING_STATE, mTrackerServiceRunning);
super.onSaveInstanceState(outState);
}
@Override
protected void onResume() {
super.onResume();
2016-09-12 12:31:37 +00:00
// handle new intents - onNewIntent does not seem to work
handleIncomingIntent();
2016-09-07 13:48:12 +00:00
// if not in onboarding mode: set state of FloatingActionButton
if (mFloatingActionButton != null) {
setFloatingActionButtonState();
}
}
@Override
public void onDestroy() {
super.onDestroy();
LogHelper.v(LOG_TAG, "onDestroy called.");
// disable broadcast receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(mTrackingStoppedReceiver);
}
2016-08-29 12:50:41 +00:00
@Override
2016-09-12 12:31:37 +00:00
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
2016-08-29 12:50:41 +00:00
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// check for ACCESS_FINE_LOCATION and WRITE_EXTERNAL_STORAGE
Boolean location = perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
Boolean storage = perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (location && storage) {
// permissions granted - notify user
Toast.makeText(this, R.string.toast_message_permissions_granted, Toast.LENGTH_SHORT).show();
mPermissionsGranted = true;
// switch to main map layout
setupLayout();
} else {
// permissions denied - notify user
Toast.makeText(this, R.string.toast_message_unable_to_start_app, Toast.LENGTH_SHORT).show();
mPermissionsGranted = false;
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@Override
public void onDialogClearPositiveClick(DialogFragment dialog) {
// DialogClear: User chose CLEAR.
LogHelper.v(LOG_TAG, "User chose CLEAR");
// clear the map
mMainActivityMapFragment.clearMap();
// dismiss notification
NotificationHelper.stop();
}
@Override
public void onDialogClearNegativeClick(DialogFragment dialog) {
// DialogClear: User chose CANCEL.
LogHelper.v(LOG_TAG, "User chose CANCEL.");
}
2016-08-29 12:50:41 +00:00
/* Set up main layout */
private void setupLayout() {
if (mPermissionsGranted) {
// point to the main map layout
setContentView(R.layout.activity_main);
// show action bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// get reference to fragment
mMainActivityMapFragment = (MainActivityMapFragment)getSupportFragmentManager().findFragmentById(R.id.content_main);
2016-08-29 12:50:41 +00:00
// show the record button and attach listener
mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2016-09-12 12:31:37 +00:00
// onClick: start / stop tracking
2016-08-29 12:50:41 +00:00
handleFloatingActionButtonClick(view);
}
});
2016-09-07 13:48:12 +00:00
mFloatingActionButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
2016-09-12 12:31:37 +00:00
// onLongClick: clear map
if (mTrackerServiceRunning || mMainActivityMapFragment == null) {
2016-09-07 13:48:12 +00:00
return false;
} else {
// show clear dialog
DialogFragment dialog = new DialogClearFragment();
dialog.show(getFragmentManager(), "DialogClearFragment");
2016-09-07 13:48:12 +00:00
return true;
}
}
});
2016-08-29 12:50:41 +00:00
} else {
// point to the on main onboarding layout
setContentView(R.layout.activity_main_onboarding);
2016-08-29 12:50:41 +00:00
// show the okay button and attach listener
Button okButton = (Button) findViewById(R.id.button_okay);
okButton.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
if (mMissingPermissions != null && !mMissingPermissions.isEmpty()) {
// request permissions
String[] params = mMissingPermissions.toArray(new String[mMissingPermissions.size()]);
requestPermissions(params, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
});
}
}
/* TEST: Set up main layout */
private void setupTestLayout() {
if (mPermissionsGranted) {
// point to the main map layout
setContentView(R.layout.activity_main_test);
// show action bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* BEGIN NEW STUFF */
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
2016-09-20 11:51:17 +00:00
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
2016-09-20 11:51:17 +00:00
ViewPager viewPager = (ViewPager) findViewById(R.id.container);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
2016-09-20 11:51:17 +00:00
tabLayout.setupWithViewPager(viewPager);
/* END NEW STUFF */
// get reference to fragment
mMainActivityMapFragment = (MainActivityMapFragment)getSupportFragmentManager().findFragmentById(R.id.content_main);
// show the record button and attach listener
mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// onClick: start / stop tracking
handleFloatingActionButtonClick(view);
}
});
mFloatingActionButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
// onLongClick: clear map
if (mTrackerServiceRunning || mMainActivityMapFragment == null) {
return false;
} else {
return true;
}
}
});
} else {
// point to the on main onboarding layout
setContentView(R.layout.activity_main_onboarding);
// show the okay button and attach listener
Button okButton = (Button) findViewById(R.id.button_okay);
okButton.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
if (mMissingPermissions != null && !mMissingPermissions.isEmpty()) {
// request permissions
String[] params = mMissingPermissions.toArray(new String[mMissingPermissions.size()]);
requestPermissions(params, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
});
}
}
2016-08-29 12:50:41 +00:00
/* Handles tap on the record button */
private void handleFloatingActionButtonClick(View view) {
2016-09-07 10:30:46 +00:00
if (mTrackerServiceRunning) {
// show snackbar
2016-08-29 12:50:41 +00:00
Snackbar.make(view, R.string.snackbar_message_tracking_stopped, Snackbar.LENGTH_SHORT).setAction("Action", null).show();
// change state
// --> is handled by broadcast receiver
2016-08-29 12:50:41 +00:00
// stop tracker service
Intent intent = new Intent(this, TrackerService.class);
intent.setAction(ACTION_STOP);
startService(intent);
} else {
// show snackbar
2016-08-29 12:50:41 +00:00
Snackbar.make(view, R.string.snackbar_message_tracking_started, Snackbar.LENGTH_SHORT).setAction("Action", null).show();
// change state
2016-09-07 10:30:46 +00:00
mTrackerServiceRunning = true;
setFloatingActionButtonState();
// get last location from MainActivity Fragment
Location lastLocation = mMainActivityMapFragment.getCurrentBestLocation();
2016-08-29 12:50:41 +00:00
if (lastLocation != null) {
// start tracker service
Intent intent = new Intent(this, TrackerService.class);
intent.setAction(ACTION_START);
intent.putExtra(EXTRA_LAST_LOCATION, lastLocation);
startService(intent);
} else {
Toast.makeText(this, getString(R.string.toast_message_location_services_not_ready), Toast.LENGTH_LONG).show();
// change state back
mTrackerServiceRunning = false;
setFloatingActionButtonState();
}
2016-08-29 12:50:41 +00:00
}
// update tracking state in MainActivityMapFragment
mMainActivityMapFragment.setTrackingState(mTrackerServiceRunning);
2016-08-29 12:50:41 +00:00
}
/* Set state of FloatingActionButton */
private void setFloatingActionButtonState() {
2016-09-07 10:30:46 +00:00
if (mTrackerServiceRunning) {
mFloatingActionButton.setImageResource(R.drawable.ic_fiber_manual_record_red_24dp);
} else {
mFloatingActionButton.setImageResource(R.drawable.ic_fiber_manual_record_white_24dp);
}
}
2016-09-12 12:31:37 +00:00
/* Handles new incoming intents */
private void handleIncomingIntent() {
Intent intent = getIntent();
String intentAction = intent.getAction();
2016-09-07 13:48:12 +00:00
2016-09-12 12:31:37 +00:00
switch (intentAction) {
case ACTION_SHOW_MAP:
if (intent.hasExtra(EXTRA_TRACKING_STATE) && mMainActivityMapFragment != null) {
2016-09-12 12:31:37 +00:00
mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false);
mMainActivityMapFragment.setTrackingState(mTrackerServiceRunning);
2016-09-12 12:31:37 +00:00
// prevent multiple reactions to intent
intent.setAction(ACTION_DEFAULT);
} else if (intent.hasExtra(EXTRA_CLEAR_MAP) && mMainActivityMapFragment != null) {
// show clear dialog
DialogFragment dialog = new DialogClearFragment();
dialog.show(getFragmentManager(), "DialogClearFragment");
2016-09-12 12:31:37 +00:00
// prevent multiple reactions to intent
intent.setAction(ACTION_DEFAULT);
}
break;
default:
// log
LogHelper.v(LOG_TAG, "Intent received. Doing nothing. Type of ACTION: " + intentAction);
break;
}
2016-09-07 13:48:12 +00:00
}
2016-08-29 12:50:41 +00:00
/* Check which permissions have been granted */
private List<String> checkPermissions() {
List<String> permissions = new ArrayList<>();
// check for location permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// add missing permission
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
// check for storage permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// add missing permission
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
return permissions;
}
/* Creates receiver for stopped tracking */
private BroadcastReceiver createTrackingStoppedReceiver() {
return new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// change state
2016-09-07 10:30:46 +00:00
mTrackerServiceRunning = false;
setFloatingActionButtonState();
// pass tracking state to MainActivityMapFragment
mMainActivityMapFragment.setTrackingState(false);
}
};
}
/**
* Inner class: SectionsPagerAdapter that returns a fragment corresponding to one of the tabs.
* see also: https://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
switch (position) {
case 0:
// if (mMainActivityMapFragment == null) {
// mMainActivityMapFragment = new MainActivityMapFragment();
// }
// return mMainActivityMapFragment;
return new MainActivityMapFragment();
case 1:
// if (mMainActivityMapFragment == null) {
// mMainActivityMapFragment = new MainActivityMapFragment();
// }
// return mMainActivityMapFragment;
return new MainActivityTrackFragment();
}
return null;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.tab_map);
case 1:
return getString(R.string.tab_last_track);
}
return null;
}
}
/**
* End of inner class
*/
2016-08-29 12:50:41 +00:00
}