tracking button state gets restored after rotation

This commit is contained in:
y20k 2016-08-30 16:49:54 +02:00
parent 07b5716e69
commit 7b793fc9dd
3 changed files with 97 additions and 48 deletions

View file

@ -62,10 +62,14 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set state of tracking
mTracking = false;
mPermissionsGranted = false;
if (savedInstanceState != null) {
mTracking = savedInstanceState.getBoolean(INSTANCE_TRACKING_STARTED, false);
}
// check permissions on Android 6 and higher
mPermissionsGranted = false;
if (Build.VERSION.SDK_INT >= 23) {
// check permissions
mMissingPermissions = checkPermissions();
@ -109,6 +113,22 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(INSTANCE_TRACKING_STARTED, mTracking);
super.onSaveInstanceState(outState);
}
@Override
protected void onResume() {
super.onResume();
// set state of FloatingActionButton
setFloatingActionButtonState();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
@ -193,9 +213,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
mFloatingActionButton.setImageResource(R.drawable.ic_fiber_manual_record_white_24dp);
mTracking = false;
// re-start preliminary tracking
// startFindingLocation();
// stop tracker service
Intent intent = new Intent(this, TrackerService.class);
intent.setAction(ACTION_STOP);
@ -221,6 +238,16 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
}
/* Set state of FloatingActionButton */
private void setFloatingActionButtonState() {
if (mTracking) {
mFloatingActionButton.setImageResource(R.drawable.ic_fiber_manual_record_red_24dp);
} else {
mFloatingActionButton.setImageResource(R.drawable.ic_fiber_manual_record_white_24dp);
}
}
/* Check which permissions have been granted */
private List<String> checkPermissions() {
List<String> permissions = new ArrayList<>();

View file

@ -52,11 +52,12 @@ public class TrackerService extends Service implements TrackbookKeys {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// acquire reference to Location Manager
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// checking for empty intent
if (intent == null) {
Log.v(LOG_TAG, "Null-Intent received. Stopping self.");
// remove notification
stopForeground(true);
stopSelf();
}
@ -67,25 +68,10 @@ public class TrackerService extends Service implements TrackbookKeys {
// create a new track
mTrack = new Track(getApplicationContext());
// acquire reference to Location Manager
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// listeners that responds to location updates
mGPSListener = createLocationListener();
mNetworkListener = createLocationListener();
// register location listeners and request updates
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGPSListener);
} catch (SecurityException e) {
e.printStackTrace();
}
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mNetworkListener);
} catch (SecurityException e) {
e.printStackTrace();
}
// create gps and network location listeners
createListeners();
// set a timer to prevent endless tracking
mTimer = new CountDownTimer(CONSTANT_MAXIMAL_DURATION, CONSTANT_TRACKING_INTERVAL) {
@Override
public void onTick(long l) {
@ -102,18 +88,10 @@ public class TrackerService extends Service implements TrackbookKeys {
// ACTION STOP
else if (intent.getAction().equals(ACTION_STOP)) {
// remove listeners
try {
mLocationManager.removeUpdates(mGPSListener);
} catch (SecurityException e) {
e.printStackTrace();
}
try {
mLocationManager.removeUpdates(mNetworkListener);
} catch (SecurityException e) {
e.printStackTrace();
}
Log.v(LOG_TAG, "Service received command: STOP");
// remove listeners
removeListeners();
}
// START_STICKY is used for services that are explicitly started and stopped as needed
@ -130,24 +108,15 @@ public class TrackerService extends Service implements TrackbookKeys {
@Override
public void onDestroy() {
super.onDestroy();
Log.v(LOG_TAG, "onDestroy called.");
// remove listeners
try {
mLocationManager.removeUpdates(mGPSListener);
} catch (SecurityException e) {
e.printStackTrace();
}
try {
mLocationManager.removeUpdates(mNetworkListener);
} catch (SecurityException e) {
e.printStackTrace();
}
Log.v(LOG_TAG, "onDestroy called.");
removeListeners();
// cancel notification
stopForeground(true);
super.onDestroy();
}
@ -184,4 +153,56 @@ public class TrackerService extends Service implements TrackbookKeys {
}
};
}
/* Creates gps and network location listeners */
private void createListeners() {
Log.v(LOG_TAG, "Setting up location listeners.");
// listeners that responds to location updates
mGPSListener = createLocationListener();
mNetworkListener = createLocationListener();
// register location listeners and request updates
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGPSListener);
} catch (SecurityException e) {
e.printStackTrace();
}
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mNetworkListener);
} catch (SecurityException e) {
e.printStackTrace();
}
}
/* Removes gps and network location listeners */
private void removeListeners() {
Log.v(LOG_TAG, "Removing location listeners.");
// remove gps listener
if (mGPSListener != null) {
Log.v(LOG_TAG, "Removing GPS location listener.");
try {
mLocationManager.removeUpdates(mGPSListener);
} catch (SecurityException e) {
// catches permission problems
e.printStackTrace();
}
}
// remove network listener
if (mNetworkListener != null) {
Log.v(LOG_TAG, "Removing network location listener.");
try {
mLocationManager.removeUpdates(mNetworkListener);
} catch (SecurityException e) {
// catches permission problems
e.printStackTrace();
}
}
}
}

View file

@ -49,6 +49,7 @@ public interface TrackbookKeys {
public static final String INSTANCE_LONGITUDE = "longitude";
public static final String INSTANCE_ZOOM_LEVEL = "zoomLevel";
public static final String INSTANCE_CURRENT_LOCATION = "currentLocation";
public static final String INSTANCE_TRACKING_STARTED = "trackingStarted";
/* RESULTS */