Remove ChooseMapActivity.

This commit is contained in:
Jimmy Shih
2013-06-04 16:13:17 -07:00
parent f89c6696b3
commit 2e81f981ca
15 changed files with 37 additions and 1059 deletions
@@ -32,7 +32,6 @@ import com.google.android.apps.mytracks.io.file.SaveActivity;
import com.google.android.apps.mytracks.io.file.TrackFileFormat;
import com.google.android.apps.mytracks.io.fusiontables.SendFusionTablesActivity;
import com.google.android.apps.mytracks.io.gdata.maps.MapsConstants;
import com.google.android.apps.mytracks.io.maps.ChooseMapActivity;
import com.google.android.apps.mytracks.io.maps.SendMapsActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
@@ -200,8 +199,7 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
if (packageName != null && className != null) {
sendRequest.setMapsSharePackageName(packageName);
sendRequest.setMapsShareClassName(className);
Intent intent = IntentUtils.newIntent(
this, sendRequest.isMapsExistingMap() ? ChooseMapActivity.class : SendMapsActivity.class)
Intent intent = IntentUtils.newIntent(this, SendMapsActivity.class)
.putExtra(SendRequest.SEND_REQUEST_KEY, sendRequest);
startActivity(intent);
}
@@ -320,7 +318,7 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
getSupportFragmentManager(), ChooseActivityDialogFragment.CHOOSE_ACTIVITY_DIALOG_TAG);
return;
}
next = sendRequest.isMapsExistingMap() ? ChooseMapActivity.class : SendMapsActivity.class;
next = SendMapsActivity.class;
} else if (sendRequest.isSendFusionTables()) {
next = SendFusionTablesActivity.class;
} else if (sendRequest.isSendSpreadsheets()) {
@@ -17,7 +17,6 @@
package com.google.android.apps.mytracks.io.drive;
import com.google.android.apps.mytracks.io.fusiontables.SendFusionTablesActivity;
import com.google.android.apps.mytracks.io.maps.ChooseMapActivity;
import com.google.android.apps.mytracks.io.maps.SendMapsActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask;
@@ -62,7 +61,7 @@ public class SendDriveActivity extends AbstractSendActivity {
return UploadResultActivity.class;
} else {
if (sendRequest.isSendMaps()) {
return sendRequest.isMapsExistingMap() ? ChooseMapActivity.class : SendMapsActivity.class;
return SendMapsActivity.class;
} else if (sendRequest.isSendFusionTables()) {
return SendFusionTablesActivity.class;
} else if (sendRequest.isSendSpreadsheets()) {
@@ -1,248 +0,0 @@
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.io.maps;
import com.google.android.apps.mytracks.io.gdata.maps.MapsMapMetadata;
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.maps.mytracks.R;
import com.google.common.annotations.VisibleForTesting;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* An activity to choose a Google Map.
*
* @author Jimmy Shih
*/
public class ChooseMapActivity extends Activity {
private static final int DIALOG_PROGRESS_ID = 0;
private static final int DIALOG_ERROR_ID = 1;
private SendRequest sendRequest;
private ChooseMapAsyncTask asyncTask;
@VisibleForTesting
ArrayAdapter<ListItem> arrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendRequest = getIntent().getParcelableExtra(SendRequest.SEND_REQUEST_KEY);
setContentView(R.layout.choose_map);
arrayAdapter = new ArrayAdapter<ListItem>(this, R.layout.choose_map_item, new ArrayList<
ListItem>()) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.choose_map_item, parent, false);
}
MapsMapMetadata mapData = getItem(position).getMapData();
TextView title = (TextView) convertView.findViewById(R.id.choose_map_item_title);
title.setText(mapData.getTitle());
TextView description = (TextView) convertView.findViewById(
R.id.choose_map_item_description);
String descriptionText = mapData.getDescription();
if (descriptionText == null || descriptionText.equals("")) {
description.setVisibility(View.GONE);
} else {
description.setVisibility(View.VISIBLE);
description.setText(descriptionText);
}
TextView searchStatus = (TextView) convertView.findViewById(
R.id.choose_map_item_search_status);
searchStatus.setTextColor(mapData.getSearchable() ? Color.RED : Color.GREEN);
searchStatus.setText(mapData.getSearchable() ? R.string.maps_list_public_label
: R.string.maps_list_unlisted_label);
return convertView;
}
};
ListView list = (ListView) findViewById(R.id.choose_map_list_view);
list.setEmptyView(findViewById(R.id.choose_map_empty_view));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startNextActivity(arrayAdapter.getItem(position).getMapId());
}
});
list.setAdapter(arrayAdapter);
Object retained = getLastNonConfigurationInstance();
if (retained instanceof ChooseMapAsyncTask) {
asyncTask = (ChooseMapAsyncTask) retained;
asyncTask.setActivity(this);
} else {
asyncTask = new ChooseMapAsyncTask(this, sendRequest.getAccount());
asyncTask.execute();
}
}
@Override
public Object onRetainNonConfigurationInstance() {
asyncTask.setActivity(null);
return asyncTask;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_PROGRESS_ID:
return DialogUtils.createSpinnerProgressDialog(
this, R.string.maps_list_progress_message, new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
asyncTask.cancel(true);
finish();
}
});
case DIALOG_ERROR_ID:
return new AlertDialog.Builder(this)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.maps_list_error)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish();
}
})
.setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
finish();
}
})
.setTitle(R.string.generic_error_title)
.create();
default:
return null;
}
}
/**
* Invokes when the associated AsyncTask completes.
*
* @param success true if success
* @param mapIds an array of map ids
* @param mapData an array of map data
*/
public void onAsyncTaskCompleted(
boolean success, ArrayList<String> mapIds, ArrayList<MapsMapMetadata> mapData) {
removeProgressDialog();
if (success) {
arrayAdapter.clear();
// To prevent displaying the emptyView message momentarily before the
// arrayAdapter is set, don't set the emptyView message in the xml layout.
// Instead, set it only when needed.
if (mapIds.size() == 0) {
TextView emptyView = (TextView) findViewById(R.id.choose_map_empty_view);
emptyView.setText(R.string.maps_list_no_maps);
} else {
for (int i = 0; i < mapIds.size(); i++) {
arrayAdapter.add(new ListItem(mapIds.get(i), mapData.get(i)));
}
}
} else {
showErrorDialog();
}
}
/**
* Shows the progress dialog.
*/
public void showProgressDialog() {
showDialog(DIALOG_PROGRESS_ID);
}
/**
* Shows the error dialog.
*/
@VisibleForTesting
void showErrorDialog() {
showDialog(DIALOG_ERROR_ID);
}
/**
* Remove the progress dialog.
*/
@VisibleForTesting
void removeProgressDialog() {
removeDialog(DIALOG_PROGRESS_ID);
}
/**
* Starts the next activity, {@link SendMapsActivity}.
*
* @param mapId the chosen map id
*/
private void startNextActivity(String mapId) {
sendRequest.setMapsExistingMapId(mapId);
Intent intent = IntentUtils.newIntent(this, SendMapsActivity.class)
.putExtra(SendRequest.SEND_REQUEST_KEY, sendRequest);
startActivity(intent);
finish();
}
/**
* A class containing {@link ChooseMapActivity} list item.
*
* @author Jimmy Shih
*/
@VisibleForTesting
class ListItem {
private String mapId;
private MapsMapMetadata mapData;
private ListItem(String mapId, MapsMapMetadata mapData) {
this.mapId = mapId;
this.mapData = mapData;
}
/**
* Gets the map id.
*/
public String getMapId() {
return mapId;
}
/**
* Gets the map data.
*/
public MapsMapMetadata getMapData() {
return mapData;
}
}
}
@@ -1,239 +0,0 @@
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.io.maps;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.gdata.GDataClientFactory;
import com.google.android.apps.mytracks.io.gdata.maps.MapFeatureEntry;
import com.google.android.apps.mytracks.io.gdata.maps.MapsClient;
import com.google.android.apps.mytracks.io.gdata.maps.MapsConstants;
import com.google.android.apps.mytracks.io.gdata.maps.MapsGDataConverter;
import com.google.android.apps.mytracks.io.gdata.maps.MapsMapMetadata;
import com.google.android.apps.mytracks.io.gdata.maps.XmlMapsGDataParserFactory;
import com.google.android.common.gdata.AndroidXmlParserFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.wireless.gdata.client.GDataClient;
import com.google.wireless.gdata.client.HttpException;
import com.google.wireless.gdata.parser.GDataParser;
import com.google.wireless.gdata.parser.ParseException;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
/**
* AsyncTask for {@link ChooseMapActivity} to get all the maps from Google Maps.
*
* @author Jimmy Shih
*/
public class ChooseMapAsyncTask extends AsyncTask<Void, Integer, Boolean> {
private static final String TAG = ChooseMapAsyncTask.class.getSimpleName();
private ChooseMapActivity activity;
private final Account account;
private final Context context;
private final GDataClient gDataClient;
private final MapsClient mapsClient;
/**
* True if can retry sending to Google Fusion Tables.
*/
private boolean canRetry;
/**
* True if the AsyncTask has completed.
*/
private boolean completed;
/**
* True if the result is success.
*/
private boolean success;
// The following variables are for per request states
private String authToken;
private ArrayList<String> mapIds;
private ArrayList<MapsMapMetadata> mapData;
public ChooseMapAsyncTask(ChooseMapActivity activity, Account account) {
this.activity = activity;
this.account = account;
this.context = activity.getApplicationContext();
this.gDataClient = GDataClientFactory
.getGDataClient(activity.getApplicationContext());
this.mapsClient = new MapsClient(
GDataClientFactory.getGDataClient(activity.getApplicationContext()),
new XmlMapsGDataParserFactory(new AndroidXmlParserFactory()));
canRetry = true;
completed = false;
success = false;
}
/**
* Sets the activity associated with this AyncTask.
*
* @param activity the activity.
*/
public void setActivity(ChooseMapActivity activity) {
this.activity = activity;
if (completed && activity != null) {
activity.onAsyncTaskCompleted(success, mapIds, mapData);
}
}
@Override
protected void onPreExecute() {
activity.showProgressDialog();
}
@Override
protected Boolean doInBackground(Void... params) {
try {
return getMaps();
} finally {
if (gDataClient != null) {
gDataClient.close();
}
}
}
@Override
protected void onPostExecute(Boolean result) {
success = result;
completed = true;
if (activity != null) {
activity.onAsyncTaskCompleted(success, mapIds, mapData);
}
}
/**
* Gets all the maps from Google Maps.
*
* @return true if success.
*/
@VisibleForTesting
boolean getMaps() {
// Reset the per request states
authToken = null;
mapIds = new ArrayList<String>();
mapData = new ArrayList<MapsMapMetadata>();
try {
authToken = AccountManager.get(context).blockingGetAuthToken(
account, MapsConstants.SERVICE_NAME, false);
} catch (OperationCanceledException e) {
Log.d(TAG, "Unable to get auth token", e);
return retryUpload();
} catch (AuthenticatorException e) {
Log.d(TAG, "Unable to get auth token", e);
return retryUpload();
} catch (IOException e) {
Log.d(TAG, "Unable to get auth token", e);
return retryUpload();
}
if (isCancelled()) {
return false;
}
GDataParser gDataParser = null;
try {
gDataParser = mapsClient.getParserForFeed(
MapFeatureEntry.class, MapsClient.getMapsFeed(), authToken);
gDataParser.init();
while (gDataParser.hasMoreData()) {
MapFeatureEntry entry = (MapFeatureEntry) gDataParser.readNextEntry(null);
mapIds.add(MapsGDataConverter.getMapidForEntry(entry));
mapData.add(MapsGDataConverter.getMapMetadataForEntry(entry));
}
} catch (ParseException e) {
Log.d(TAG, "Unable to get maps", e);
return retryUpload();
} catch (IOException e) {
Log.d(TAG, "Unable to get maps", e);
return retryUpload();
} catch (HttpException e) {
Log.d(TAG, "Unable to get maps", e);
return retryUpload();
} finally {
if (gDataParser != null) {
gDataParser.close();
}
}
return true;
}
/**
* Retries upload. Invalidates the authToken. If can retry, invokes
* {@link ChooseMapAsyncTask#getMaps()}. Returns false if cannot retry.
*/
@VisibleForTesting
boolean retryUpload() {
if (isCancelled()) {
return false;
}
AccountManager.get(context).invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
if (canRetry) {
canRetry = false;
return getMaps();
}
return false;
}
/**
* Gets the complete status of task.
*/
@VisibleForTesting
boolean getCompleted() {
return completed;
}
/**
* Sets the complete status of task.
* @param completed
*/
@VisibleForTesting
void setCompleted(boolean completed) {
this.completed = completed;
}
/**
* Sets the status of canRetry.
* @param canRetry status of canRetry
*/
@VisibleForTesting
void setCanRetry(boolean canRetry) {
this.canRetry = canRetry;
}
/**
* Gets the status of canRetry.
*/
@VisibleForTesting
boolean getCanRetry() {
return canRetry;
}
}
@@ -36,8 +36,7 @@ public class SendMapsActivity extends AbstractSendActivity {
@Override
protected AbstractSendAsyncTask createAsyncTask() {
return new SendMapsAsyncTask(
this, sendRequest.getTrackId(), sendRequest.getAccount(), sendRequest.getMapsExistingMapId());
return new SendMapsAsyncTask(this, sendRequest.getTrackId(), sendRequest.getAccount());
}
@Override
@@ -83,7 +83,6 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
private final long trackId;
private final Account account;
private final String chooseMapId;
private final MyTracksProviderUtils myTracksProviderUtils;
private final Context context;
private final GDataClient gDataClient;
@@ -95,9 +94,8 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
private String mapId;
int currentSegment;
public SendMapsAsyncTask(
SendMapsActivity activity, long trackId, Account account, String chooseMapId) {
this(activity, trackId, account, chooseMapId, MyTracksProviderUtils.Factory.get(
public SendMapsAsyncTask(SendMapsActivity activity, long trackId, Account account) {
this(activity, trackId, account, MyTracksProviderUtils.Factory.get(
activity.getApplicationContext()));
}
@@ -106,11 +104,10 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
*/
@VisibleForTesting
public SendMapsAsyncTask(SendMapsActivity activity, long trackId, Account account,
String chooseMapId, MyTracksProviderUtils myTracksProviderUtils) {
MyTracksProviderUtils myTracksProviderUtils) {
super(activity);
this.trackId = trackId;
this.account = account;
this.chooseMapId = chooseMapId;
this.myTracksProviderUtils = myTracksProviderUtils;
context = activity.getApplicationContext();
@@ -218,30 +215,25 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
return false;
}
if (chooseMapId != null) {
mapId = chooseMapId;
return true;
} else {
boolean defaultMapPublic = PreferencesUtils.getBoolean(context,
R.string.export_google_maps_public_key,
PreferencesUtils.EXPORT_GOOGLE_MAPS_PUBLIC_DEFAULT);
try {
String description = track.getCategory() + "\n" + track.getDescription() + "\n"
+ context.getString(R.string.send_google_by_my_tracks, "", "");
mapId = SendMapsUtils.createNewMap(
track.getName(), description, defaultMapPublic, mapsClient, authToken);
} catch (ParseException e) {
Log.d(TAG, "Unable to create a new map", e);
return false;
} catch (HttpException e) {
Log.d(TAG, "Unable to create a new map", e);
return false;
} catch (IOException e) {
Log.d(TAG, "Unable to create a new map", e);
return false;
}
return mapId != null;
boolean defaultMapPublic = PreferencesUtils.getBoolean(context,
R.string.export_google_maps_public_key, PreferencesUtils.EXPORT_GOOGLE_MAPS_PUBLIC_DEFAULT);
try {
String description = track.getCategory() + "\n" + track.getDescription() + "\n"
+ context.getString(R.string.send_google_by_my_tracks, "", "");
mapId = SendMapsUtils.createNewMap(
track.getName(), description, defaultMapPublic, mapsClient, authToken);
} catch (ParseException e) {
Log.d(TAG, "Unable to create a new map", e);
return false;
} catch (HttpException e) {
Log.d(TAG, "Unable to create a new map", e);
return false;
} catch (IOException e) {
Log.d(TAG, "Unable to create a new map", e);
return false;
}
return mapId != null;
}
/**
@@ -43,8 +43,6 @@ public class SendRequest implements Parcelable {
private boolean mapsShare = false;
private String mapsSharePackageName = null;
private String mapsShareClassName = null;
private boolean mapsExistingMap = false;
private String mapsExistingMapId = null;
private Account account = null;
@@ -146,22 +144,6 @@ public class SendRequest implements Parcelable {
this.mapsShareClassName = mapsShareClassName;
}
public boolean isMapsExistingMap() {
return mapsExistingMap;
}
public void setMapsExistingMap(boolean mapsExistingMap) {
this.mapsExistingMap = mapsExistingMap;
}
public String getMapsExistingMapId() {
return mapsExistingMapId;
}
public void setMapsExistingMapId(String mapsExistingMapId) {
this.mapsExistingMapId = mapsExistingMapId;
}
public Account getAccount() {
return account;
}
@@ -214,8 +196,6 @@ public class SendRequest implements Parcelable {
mapsShare = in.readByte() == 1;
mapsSharePackageName = in.readString();
mapsShareClassName = in.readString();
mapsExistingMap = in.readByte() == 1;
mapsExistingMapId = in.readString();
account = in.readParcelable(null);
driveSuccess = in.readByte() == 1;
mapsSuccess = in.readByte() == 1;
@@ -241,8 +221,6 @@ public class SendRequest implements Parcelable {
out.writeByte((byte) (mapsShare ? 1 : 0));
out.writeString(mapsSharePackageName);
out.writeString(mapsShareClassName);
out.writeByte((byte) (mapsExistingMap ? 1 : 0));
out.writeString(mapsExistingMapId);
out.writeParcelable(account, 0);
out.writeByte((byte) (driveSuccess ? 1 : 0));
out.writeByte((byte) (mapsSuccess ? 1 : 0));