forked from upstream-mirrors/OpenTracks
In StatsFragment, use the handler thread to update the total time
instead of creating a new thread.
This commit is contained in:
@@ -28,9 +28,9 @@ import com.google.android.maps.mytracks.R;
|
|||||||
|
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentActivity;
|
import android.support.v4.app.FragmentActivity;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@@ -47,10 +47,10 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
|
|
||||||
public static final String STATS_FRAGMENT_TAG = "statsFragment";
|
public static final String STATS_FRAGMENT_TAG = "statsFragment";
|
||||||
|
|
||||||
private static final String TAG = StatsFragment.class.getSimpleName();
|
private static final int ONE_SECOND = 1000;
|
||||||
|
|
||||||
private TrackDataHub trackDataHub;
|
private TrackDataHub trackDataHub;
|
||||||
private UiUpdateThread uiUpdateThread;
|
private Handler handler;
|
||||||
|
|
||||||
private Location lastLocation = null;
|
private Location lastLocation = null;
|
||||||
private TripStatistics lastTripStatistics = null;
|
private TripStatistics lastTripStatistics = null;
|
||||||
@@ -58,34 +58,16 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
// A runnable to update the total time field.
|
// A runnable to update the total time field.
|
||||||
private final Runnable updateTotalTime = new Runnable() {
|
private final Runnable updateTotalTime = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (isResumed() && isSelectedTrackRecording() && !isSelectedTrackPaused()
|
if (isResumed() && isSelectedTrackRecording()) {
|
||||||
&& lastTripStatistics != null) {
|
if (!isSelectedTrackPaused() && lastTripStatistics != null) {
|
||||||
StatsUtils.setTotalTimeValue(getActivity(), System.currentTimeMillis()
|
StatsUtils.setTotalTimeValue(getActivity(), System.currentTimeMillis()
|
||||||
- lastTripStatistics.getStopTime() + lastTripStatistics.getTotalTime());
|
- lastTripStatistics.getStopTime() + lastTripStatistics.getTotalTime());
|
||||||
|
}
|
||||||
|
handler.postDelayed(this, ONE_SECOND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* A thread that updates the total time field every second.
|
|
||||||
*/
|
|
||||||
private class UiUpdateThread extends Thread {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Log.d(TAG, "UI update thread started");
|
|
||||||
while (isResumed() && isSelectedTrackRecording()) {
|
|
||||||
getActivity().runOnUiThread(updateTotalTime);
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000L);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Log.d(TAG, "UI update thread caught exception", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Log.d(TAG, "UI update thread finished");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(
|
public View onCreateView(
|
||||||
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
@@ -95,6 +77,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
handler = new Handler();
|
||||||
updateUi(getActivity());
|
updateUi(getActivity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,10 +91,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
pauseTrackDataHub();
|
pauseTrackDataHub();
|
||||||
if (uiUpdateThread != null) {
|
handler.removeCallbacks(updateTotalTime);
|
||||||
uiUpdateThread.interrupt();
|
|
||||||
uiUpdateThread = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -159,13 +139,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onSelectedTrackChanged(Track track) {
|
public void onSelectedTrackChanged(Track track) {
|
||||||
if (isResumed()) {
|
if (isResumed()) {
|
||||||
if (uiUpdateThread != null) {
|
handler.removeCallbacks(updateTotalTime);
|
||||||
uiUpdateThread.interrupt();
|
|
||||||
uiUpdateThread = null;
|
|
||||||
}
|
|
||||||
if (isSelectedTrackRecording()) {
|
if (isSelectedTrackRecording()) {
|
||||||
uiUpdateThread = new UiUpdateThread();
|
handler.post(updateTotalTime);
|
||||||
uiUpdateThread.start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user