- In the action bar, configure the Home button as the Up button.

- When starting an activity, always use FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK together.
- TrackDetailActivity should handle events for a specific trackId, not
  handling events for the current recording track.
- In TrackListActivity, to support screen rotation, make sure the EULA dialog
  is not shown before creating another one.
- Do not need to bind service in onStart. OnResume will bind the service
  if a recording is happening.
This commit is contained in:
Jimmy Shih
2012-03-30 14:19:29 -07:00
parent 9ff1af221b
commit 6869f003e7
16 changed files with 84 additions and 106 deletions
@@ -8,11 +8,13 @@ import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ScrollView;
import android.widget.TextView;
@@ -60,8 +62,7 @@ public class AggregatedStatsActivity extends Activity implements
this.tracksProvider = MyTracksProviderUtils.Factory.get(this);
// Show the action bar (or nothing at all).
ApiAdapterFactory.getApiAdapter().showActionBar(this);
ApiAdapterFactory.getApiAdapter().configureActionBarHomeAsUp(this);
setContentView(R.layout.stats);
@@ -87,6 +88,16 @@ public class AggregatedStatsActivity extends Activity implements
loadAggregatedStats();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() != android.R.id.home) {
return false;
}
startActivity(new Intent(this, TrackListActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
return true;
}
/**
* 1. Reads tracks from the db
* 2. Merges the trip stats from the tracks
@@ -28,7 +28,6 @@ import com.google.android.apps.mytracks.content.TrackDataListener;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.stats.DoubleBuffer;
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
@@ -118,9 +117,6 @@ public class ChartActivity extends Activity implements TrackDataListener {
// The volume we want to control is the Text-To-Speech volume
setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
// Show the action bar (or nothing at all).
ApiAdapterFactory.getApiAdapter().showActionBar(this);
setContentView(R.layout.mytracks_charts);
ViewGroup layout = (ViewGroup) findViewById(R.id.elevation_chart);
chartView = new ChartView(this);
@@ -141,8 +141,7 @@ public class ImportActivity extends Activity {
public void onClick(DialogInterface dialog, int which) {
if (!importAll && trackId != -1L) {
Intent intent = new Intent(ImportActivity.this, TrackDetailActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(TrackDetailActivity.TRACK_ID, trackId);
startActivity(intent);
}
@@ -24,7 +24,6 @@ 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.stats.TripStatistics;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.GeoRect;
import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.maps.GeoPoint;
@@ -142,9 +141,6 @@ public class MapActivity extends com.google.android.maps.MapActivity
// The volume we want to control is the Text-To-Speech volume
setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
// Show the action bar (or nothing at all).
ApiAdapterFactory.getApiAdapter().showActionBar(this);
// Inflate the layout:
setContentView(R.layout.mytracks_layout);
@@ -248,11 +248,10 @@ public class SearchActivity extends ListActivity {
private Intent createViewDataIntent(Map<String, Object> clickedData) {
Intent intent = new Intent(this, TrackDetailActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(TrackDetailActivity.TRACK_ID, (Long) clickedData.get(TRACK_ID_FIELD));
if (clickedData.containsKey(WAYPOINT_ID_FIELD)) {
intent.putExtra(TrackDetailActivity.WAYPOINT_ID, (Long) clickedData.get(WAYPOINT_ID_FIELD));
} else {
intent.putExtra(TrackDetailActivity.TRACK_ID, (Long) clickedData.get(TRACK_ID_FIELD));
}
return intent;
}
@@ -628,7 +628,7 @@ public class SettingsActivity extends PreferenceActivity {
// Restart the settings activity so all changes are loaded.
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
@@ -23,7 +23,6 @@ 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.ServiceUtils;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
@@ -107,9 +106,6 @@ public class StatsActivity extends Activity implements TrackDataListener {
// The volume we want to control is the Text-To-Speech volume
setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
// Show the action bar (or nothing at all).
ApiAdapterFactory.getApiAdapter().showActionBar(this);
setContentView(R.layout.stats);
ScrollView sv = ((ScrollView) findViewById(R.id.scrolly));
@@ -21,7 +21,6 @@ import static com.google.android.apps.mytracks.Constants.STATS_TAB_TAG;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.TrackDataHub;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.io.file.SaveActivity;
import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat;
@@ -78,7 +77,8 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
private TrackDataHub trackDataHub;
private TrackRecordingServiceConnection trackRecordingServiceConnection;
private NavControls navControls;
private long trackId;
private MenuItem stopRecording;
private MenuItem insertMarker;
private MenuItem play;
@@ -106,10 +106,7 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
return;
}
if (key.equals(getString(R.string.recording_track_key))) {
if (isRecording()) {
trackRecordingServiceConnection.startAndBind();
}
updateMenu();
updateMenu();
}
}
};
@@ -118,16 +115,14 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
ApiAdapterFactory.getApiAdapter().configureActionBarHomeAsUp(this);
sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
trackDataHub = ((MyTracksApplication) getApplication()).getTrackDataHub();
trackRecordingServiceConnection = new TrackRecordingServiceConnection(this, null);
setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
// Show the action bar (or nothing at all).
ApiAdapterFactory.getApiAdapter().showActionBar(this);
final Resources res = getResources();
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec(MAP_TAB_TAG)
@@ -152,43 +147,33 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
navControls.show();
tabHost.addView(layout);
layout.setOnTouchListener(this);
// Get the trackid
Intent intent = getIntent();
trackId = intent.getLongExtra(TRACK_ID, -1L);
if (trackId == -1L) {
startTrackListActivity();
finish();
}
trackDataHub.loadTrack(trackId);
// Get the waypointId
long waypointId = intent.getLongExtra(WAYPOINT_ID, -1L);
if (waypointId != -1L) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
getTabHost().setCurrentTab(0);
mapActivity.showWaypoint(trackId, waypointId);
} else {
Log.e(TAG, "MapActivity is null");
}
}
}
@Override
protected void onStart() {
super.onStart();
trackDataHub.start();
if (isRecording()) {
trackRecordingServiceConnection.startAndBind();
}
Intent intent = getIntent();
long trackId = intent.getLongExtra(TRACK_ID, -1L);
if (trackId != -1L) {
trackDataHub.loadTrack(trackId);
return;
}
long waypointId = intent.getLongExtra(WAYPOINT_ID, -1L);
if (waypointId != -1L) {
Waypoint waypoint = MyTracksProviderUtils.Factory.get(this).getWaypoint(waypointId);
if (waypoint != null) {
trackId = waypoint.getTrackId();
trackDataHub.loadTrack(trackId);
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
getTabHost().setCurrentTab(0);
mapActivity.showWaypoint(trackId, waypointId);
} else {
Log.e(TAG, "MapActivity is null");
}
return;
}
}
// No track id or waypoint id, return to the track list activity.
startTrackListActivity();
}
@Override
@@ -219,7 +204,6 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
R.string.track_detail_delete_confirm_message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
long trackId = trackDataHub.getSelectedTrackId();
MyTracksProviderUtils.Factory.get(TrackDetailActivity.this).deleteTrack(trackId);
startTrackListActivity();
}
@@ -275,8 +259,10 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
public boolean onOptionsItemSelected(MenuItem item) {
MapActivity mapActivity;
Intent intent;
long trackId = trackDataHub.getSelectedTrackId();
switch (item.getItemId()) {
case android.R.id.home:
startTrackListActivity();
return true;
case R.id.menu_stop_recording:
updateMenuItems(false);
stopRecording();
@@ -302,16 +288,16 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
startActivity(intent);
return true;
case R.id.menu_share_gpx:
startSaveActivity(trackId, TrackFileFormat.GPX, true);
startSaveActivity(TrackFileFormat.GPX, true);
return true;
case R.id.menu_share_kml:
startSaveActivity(trackId, TrackFileFormat.KML, true);
startSaveActivity(TrackFileFormat.KML, true);
return true;
case R.id.menu_share_csv:
startSaveActivity(trackId, TrackFileFormat.CSV, true);
startSaveActivity(TrackFileFormat.CSV, true);
return true;
case R.id.menu_share_tcx:
startSaveActivity(trackId, TrackFileFormat.TCX, true);
startSaveActivity(TrackFileFormat.TCX, true);
return true;
case R.id.menu_markers:
intent = new Intent(this, WaypointsList.class)
@@ -324,16 +310,16 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
startActivity(intent);
return true;
case R.id.menu_save_gpx:
startSaveActivity(trackId, TrackFileFormat.GPX, false);
startSaveActivity(TrackFileFormat.GPX, false);
return true;
case R.id.menu_save_kml:
startSaveActivity(trackId, TrackFileFormat.KML, false);
startSaveActivity(TrackFileFormat.KML, false);
return true;
case R.id.menu_save_csv:
startSaveActivity(trackId, TrackFileFormat.CSV, false);
startSaveActivity(TrackFileFormat.CSV, false);
return true;
case R.id.menu_save_tcx:
startSaveActivity(trackId, TrackFileFormat.TCX, false);
startSaveActivity(TrackFileFormat.TCX, false);
return true;
case R.id.menu_edit:
startActivity(new Intent(this, TrackEditActivity.class)
@@ -434,7 +420,8 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
* Updates the menu.
*/
private void updateMenu() {
updateMenuItems(isRecording());
updateMenuItems(
trackId == sharedPreferences.getLong(getString(R.string.recording_track_key), -1L));
}
/**
@@ -470,25 +457,20 @@ public class TrackDetailActivity extends TabActivity implements OnTouchListener
}
/**
* Starts the {@link TrackListActivity} and ends this activity.
* Starts the {@link TrackListActivity}.
*/
private void startTrackListActivity() {
Intent intent = new Intent(this, TrackListActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
startActivity(new Intent(this, TrackListActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
}
/**
* Starts the {@link SaveActivity} to save a track.
*
* @param trackId the track id
* @param trackFileFormat the track file format
* @param shareTrack true to share the track after saving
*/
private void startSaveActivity(
long trackId, TrackFileFormat trackFileFormat, boolean shareTrack) {
private void startSaveActivity(TrackFileFormat trackFileFormat, boolean shareTrack) {
Intent intent = new Intent(this, SaveActivity.class)
.putExtra(SaveActivity.EXTRA_TRACK_ID, trackId)
.putExtra(SaveActivity.EXTRA_TRACK_FILE_FORMAT, (Parcelable) trackFileFormat)
@@ -41,6 +41,7 @@ import android.os.Bundle;
import android.os.Parcelable;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
@@ -170,6 +171,7 @@ public class TrackListActivity extends FragmentActivity {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(TrackListActivity.this, TrackDetailActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(TrackDetailActivity.TRACK_ID, id);
startActivity(intent);
}
@@ -239,15 +241,10 @@ public class TrackListActivity extends FragmentActivity {
});
if (!EulaUtils.getEulaValue(this)) {
new EulaDialogFragment().show(getSupportFragmentManager(), EULA_DIALOG_TAG);
}
}
@Override
protected void onStart() {
super.onStart();
if (isRecording()) {
trackRecordingServiceConnection.startAndBind();
Fragment fragment = getSupportFragmentManager().findFragmentByTag(EULA_DIALOG_TAG);
if (fragment == null) {
new EulaDialogFragment().show(getSupportFragmentManager(), EULA_DIALOG_TAG);
}
}
}
@@ -92,9 +92,8 @@ public class RestoreActivity extends Activity {
public void onAsyncTaskCompleted(boolean success, int messageId) {
removeDialog(DIALOG_PROGRESS_ID);
Toast.makeText(this, messageId, success ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
startActivity(
new Intent(this, TrackListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
startActivity(new Intent(this, TrackListActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
}
/**
@@ -206,8 +206,7 @@ public class SaveActivity extends Activity {
startActivity(Intent.createChooser(intent, getString(R.string.share_track_picker_title)));
} else if (playTrack) {
Intent intent = new Intent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(PlayTrackUtils.TOUR_FEATURE_ID, KmlTrackWriter.TOUR_FEATURE_ID)
.setClassName(PlayTrackUtils.GOOGLE_EARTH_PACKAGE, PlayTrackUtils.GOOGLE_EARTH_CLASS)
.setDataAndType(Uri.fromFile(new File(filePath)), PlayTrackUtils.KML_MIME_TYPE);
@@ -485,6 +485,7 @@ public class TrackRecordingService extends Service {
R.drawable.arrow_320, null /* tickerText */,
System.currentTimeMillis());
Intent intent = new Intent(this, TrackDetailActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(TrackDetailActivity.TRACK_ID, recordingTrackId);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0 /* requestCode */, intent, 0 /* flags */);
@@ -17,11 +17,11 @@
package com.google.android.apps.mytracks.util;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.MenuItem;
import android.view.Window;
import android.widget.SearchView;
/**
@@ -33,8 +33,10 @@ import android.widget.SearchView;
public class Api11Adapter extends Api10Adapter {
@Override
public void showActionBar(Activity activity) {
activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
public void configureActionBarHomeAsUp(Activity activity) {
ActionBar actionBar = activity.getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
@@ -104,7 +104,7 @@ public class Api7Adapter implements ApiAdapter {
}
@Override
public void showActionBar(Activity activity) {
public void configureActionBarHomeAsUp(Activity activity) {
// Action bar not available, just hide the title and let actions be shown
// via the regular menu.
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
@@ -101,15 +101,15 @@ public interface ApiAdapter {
*/
public BluetoothSocket getBluetoothSocket(BluetoothDevice bluetoothDevice) throws IOException;
/**
* Shows the action bar, if the current system supports it.
* If it's not supported, the title bar will be kept hidden.
*
/**
* Configures the action bar with the Home button as an Up button. If the
* system doesn't support action bar, hide the title bar.
* <p>
* Due to changes in API level 11.
*
* @param activity the current activity
*/
public void showActionBar(Activity activity);
public void configureActionBarHomeAsUp(Activity activity);
/**
* Configures the search widget.
@@ -145,7 +145,8 @@ public class TrackWidgetProvider
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.track_widget);
// Make all of the stats open the mytracks activity.
Intent intent = new Intent(context, TrackDetailActivity.class);
Intent intent = new Intent(context, TrackDetailActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
if (track != null) {
intent.putExtra(TrackDetailActivity.TRACK_ID, track.getId());
}