mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
create MyTracksApplication to keep global state like TrackDataHub
This commit is contained in:
@@ -21,6 +21,7 @@ limitations under the License.
|
||||
android:versionName="1.1.9">
|
||||
<application android:icon="@drawable/arrow_icon"
|
||||
android:label="@string/app_name"
|
||||
android:name="com.google.android.apps.mytracks.MyTracksApplication"
|
||||
android:backupAgent="com.google.android.apps.mytracks.io.backup.MyTracksBackupAgent">
|
||||
<uses-library android:name="com.google.android.maps" />
|
||||
|
||||
|
||||
@@ -20,11 +20,11 @@ import static com.google.android.apps.mytracks.Constants.TAG;
|
||||
import com.google.android.apps.mytracks.ChartView.Mode;
|
||||
import com.google.android.apps.mytracks.content.MyTracksLocation;
|
||||
import com.google.android.apps.mytracks.content.Sensor;
|
||||
import com.google.android.apps.mytracks.content.TrackDataHub;
|
||||
import com.google.android.apps.mytracks.content.TrackDataListener;
|
||||
import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
|
||||
import com.google.android.apps.mytracks.content.TrackDataHub.ListenerDataType;
|
||||
import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.content.TrackDataHub;
|
||||
import com.google.android.apps.mytracks.content.TrackDataHub.ListenerDataType;
|
||||
import com.google.android.apps.mytracks.content.TrackDataListener;
|
||||
import com.google.android.apps.mytracks.content.Waypoint;
|
||||
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerFactory;
|
||||
import com.google.android.apps.mytracks.stats.DoubleBuffer;
|
||||
@@ -148,7 +148,7 @@ public class ChartActivity extends Activity implements TrackDataListener {
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
dataHub = TrackDataHub.getStartedInstance();
|
||||
dataHub = ((MyTracksApplication) getApplication()).getTrackDataHub();
|
||||
dataHub.registerTrackDataListener(this, EnumSet.of(
|
||||
ListenerDataType.SELECTED_TRACK_CHANGED,
|
||||
ListenerDataType.TRACK_UPDATES,
|
||||
|
||||
@@ -185,7 +185,7 @@ public class MapActivity extends com.google.android.maps.MapActivity
|
||||
Log.d(TAG, "MapActivity.onResume");
|
||||
super.onResume();
|
||||
|
||||
dataHub = TrackDataHub.getStartedInstance();
|
||||
dataHub = ((MyTracksApplication) getApplication()).getTrackDataHub();
|
||||
dataHub.registerTrackDataListener(this, EnumSet.of(
|
||||
ListenerDataType.SELECTED_TRACK_CHANGED,
|
||||
ListenerDataType.POINT_UPDATES,
|
||||
|
||||
@@ -146,7 +146,7 @@ public class MyTracks extends TabActivity implements OnTouchListener {
|
||||
|
||||
providerUtils = MyTracksProviderUtils.Factory.get(this);
|
||||
preferences = getSharedPreferences(Constants.SETTINGS_NAME, 0);
|
||||
dataHub = TrackDataHub.newInstance(this);
|
||||
dataHub = ((MyTracksApplication) getApplication()).getTrackDataHub();
|
||||
menuManager = new MenuManager(this);
|
||||
serviceConnection = new TrackRecordingServiceConnection(this, serviceBindCallback);
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
|
||||
package com.google.android.apps.mytracks;
|
||||
|
||||
import com.google.android.apps.mytracks.content.TrackDataHub;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
/**
|
||||
* MyTracksApplication for keeping global state.
|
||||
*
|
||||
* @author jshih@google.com (Jimmy Shih)
|
||||
*
|
||||
*/
|
||||
public class MyTracksApplication extends Application {
|
||||
private TrackDataHub trackDataHub;
|
||||
|
||||
/**
|
||||
* Gets the application's TrackDataHub.
|
||||
*
|
||||
*/
|
||||
public TrackDataHub getTrackDataHub() {
|
||||
if (trackDataHub == null) {
|
||||
trackDataHub = TrackDataHub.newInstance(getApplicationContext());
|
||||
}
|
||||
return trackDataHub;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTerminate() {
|
||||
trackDataHub = null;
|
||||
super.onTerminate();
|
||||
}
|
||||
}
|
||||
@@ -137,7 +137,7 @@ public class StatsActivity extends Activity implements TrackDataListener {
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
dataHub = TrackDataHub.getStartedInstance();
|
||||
dataHub = ((MyTracksApplication) getApplication()).getTrackDataHub();
|
||||
dataHub.registerTrackDataListener(this, EnumSet.of(
|
||||
ListenerDataType.SELECTED_TRACK_CHANGED,
|
||||
ListenerDataType.TRACK_UPDATES,
|
||||
|
||||
@@ -195,15 +195,12 @@ public class TrackDataHub {
|
||||
private int lastSamplingFrequency;
|
||||
private DoubleBufferedLocationFactory locationFactory;
|
||||
|
||||
private static TrackDataHub startedInstance;
|
||||
|
||||
private boolean started = false;
|
||||
|
||||
/**
|
||||
* Builds a new {@link TrackDataHub} instance.
|
||||
*/
|
||||
public synchronized static TrackDataHub newInstance(Context context) {
|
||||
// Ensure our singleton is never bound to an activity, to avoid memory leaks.
|
||||
context = context.getApplicationContext();
|
||||
|
||||
SharedPreferences preferences = context.getSharedPreferences(Constants.SETTINGS_NAME, 0);
|
||||
MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(context);
|
||||
return new TrackDataHub(context,
|
||||
@@ -240,11 +237,11 @@ public class TrackDataHub {
|
||||
*/
|
||||
public void start() {
|
||||
Log.i(TAG, "TrackDataHub.start");
|
||||
if (startedInstance != null) {
|
||||
if (isStarted()) {
|
||||
Log.w(TAG, "Already started, ignoring");
|
||||
return;
|
||||
}
|
||||
startedInstance = this;
|
||||
started = true;
|
||||
|
||||
listenerHandlerThread = new HandlerThread("trackDataContentThread");
|
||||
listenerHandlerThread.start();
|
||||
@@ -266,20 +263,6 @@ public class TrackDataHub {
|
||||
return new DataSourcesWrapperImpl(context, preferences);
|
||||
}
|
||||
|
||||
/**
|
||||
* If there's an instance for which {@link start} has been called, returns it.
|
||||
*
|
||||
* @return the started instance
|
||||
* @throws IllegalStateException if there isn't a started instance
|
||||
*/
|
||||
public static TrackDataHub getStartedInstance() {
|
||||
if (startedInstance == null) {
|
||||
throw new IllegalStateException("Data hub not started");
|
||||
}
|
||||
|
||||
return startedInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops listening to data sources and reporting the data to external
|
||||
* listeners.
|
||||
@@ -290,12 +273,12 @@ public class TrackDataHub {
|
||||
Log.w(TAG, "Not started, ignoring");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Unregister internal listeners even if there are external listeners registered.
|
||||
dataSourceManager.unregisterAllListeners();
|
||||
listenerHandlerThread.getLooper().quit();
|
||||
|
||||
startedInstance = null;
|
||||
started = false;
|
||||
|
||||
dataSources = null;
|
||||
dataSourceManager = null;
|
||||
@@ -304,7 +287,7 @@ public class TrackDataHub {
|
||||
}
|
||||
|
||||
private boolean isStarted() {
|
||||
return startedInstance != null;
|
||||
return started;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -45,12 +45,6 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
|
||||
super(MyTracks.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
clearSelectedAndRecordingTracks();
|
||||
|
||||
Reference in New Issue
Block a user