Create a ServicePickerActivity to allow users

to pick the Google services to send a track to.

Benefits of the refactor:
- ServicePickerActivity can easily handle screen rotation
- Reduce complexity in SendActivity
This commit is contained in:
Jimmy Shih
2012-01-10 15:16:24 -08:00
parent c013db29bc
commit 9feb097d11
6 changed files with 324 additions and 324 deletions
+2
View File
@@ -246,6 +246,8 @@ limitations under the License.
android:label="@string/welcome_title"
android:exported="false" />
<activity android:name="com.google.android.apps.mytracks.io.sendtogoogle.ServicePickerActivity"/>
<service android:name="com.google.android.apps.mytracks.services.TrackRecordingService"
android:label="@string/my_tracks_recording_service"
android:icon="@drawable/arrow_320"
@@ -25,8 +25,8 @@ import com.google.android.apps.mytracks.content.TrackDataListener;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.io.file.SaveActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.SendActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.SendType;
import com.google.android.apps.mytracks.io.sendtogoogle.ServicePickerActivity;
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerFactory;
import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.util.ApiFeatures;
@@ -42,6 +42,7 @@ import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.Settings;
import android.util.Log;
import android.view.ContextMenu;
@@ -399,15 +400,25 @@ public class MapActivity extends com.google.android.maps.MapActivity
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Intent intent;
long trackId = dataHub.getSelectedTrackId();
switch (item.getItemId()) {
case Constants.MENU_SEND_TO_GOOGLE:
SendActivity.sendToGoogle(this, dataHub.getSelectedTrackId(), null);
intent = new Intent(this, ServicePickerActivity.class);
intent.putExtra(ServicePickerActivity.TRACK_ID, trackId);
startActivity(intent);
return true;
case Constants.MENU_SHARE_MAP:
SendActivity.sendToGoogle(this, dataHub.getSelectedTrackId(), SendType.MAPS);
intent = new Intent(this, ServicePickerActivity.class);
intent.putExtra(ServicePickerActivity.TRACK_ID, trackId);
intent.putExtra(ServicePickerActivity.SEND_TYPE, (Parcelable) SendType.MAPS);
startActivity(intent);
return true;
case Constants.MENU_SHARE_FUSION_TABLE:
SendActivity.sendToGoogle(this, dataHub.getSelectedTrackId(), SendType.FUSION_TABLES);
intent = new Intent(this, ServicePickerActivity.class);
intent.putExtra(ServicePickerActivity.TRACK_ID, trackId);
intent.putExtra(ServicePickerActivity.SEND_TYPE, (Parcelable) SendType.FUSION_TABLES);
startActivity(intent);
return true;
case Constants.MENU_SAVE_GPX_FILE:
case Constants.MENU_SAVE_KML_FILE:
@@ -417,19 +428,18 @@ public class MapActivity extends com.google.android.maps.MapActivity
case Constants.MENU_SHARE_KML_FILE:
case Constants.MENU_SHARE_CSV_FILE:
case Constants.MENU_SHARE_TCX_FILE:
SaveActivity.handleExportTrackAction(this, dataHub.getSelectedTrackId(),
Constants.getActionFromMenuId(item.getItemId()));
SaveActivity.handleExportTrackAction(
this, trackId, Constants.getActionFromMenuId(item.getItemId()));
return true;
case Constants.MENU_EDIT: {
Intent intent = new Intent(this, TrackDetail.class);
intent.putExtra(TrackDetail.TRACK_ID, dataHub.getSelectedTrackId());
intent = new Intent(this, TrackDetail.class);
intent.putExtra(TrackDetail.TRACK_ID, trackId);
startActivity(intent);
return true;
}
case Constants.MENU_DELETE: {
Uri uri = ContentUris.withAppendedId(
TracksColumns.CONTENT_URI, dataHub.getSelectedTrackId());
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
Uri uri = ContentUris.withAppendedId(TracksColumns.CONTENT_URI, trackId);
intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
return true;
}
@@ -19,8 +19,8 @@ import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.io.file.SaveActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.SendActivity;
import com.google.android.apps.mytracks.io.sendtogoogle.SendType;
import com.google.android.apps.mytracks.io.sendtogoogle.ServicePickerActivity;
import com.google.android.apps.mytracks.services.ServiceUtils;
import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection;
import com.google.android.apps.mytracks.util.StringUtils;
@@ -36,6 +36,7 @@ import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
@@ -158,13 +159,14 @@ public class TrackList extends ListActivity
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case Constants.MENU_SHOW: {
onListItemClick(null, null, 0, trackId);
return true;
}
case Constants.MENU_EDIT: {
Intent intent = new Intent(this, TrackDetail.class);
intent = new Intent(this, TrackDetail.class);
intent.putExtra(TrackDetail.TRACK_ID, trackId);
startActivity(intent);
return true;
@@ -173,13 +175,21 @@ public class TrackList extends ListActivity
case Constants.MENU_WRITE_TO_SD_CARD:
return false;
case Constants.MENU_SEND_TO_GOOGLE:
SendActivity.sendToGoogle(this, trackId, null);
intent = new Intent(this, ServicePickerActivity.class);
intent.putExtra(ServicePickerActivity.TRACK_ID, trackId);
startActivity(intent);
return true;
case Constants.MENU_SHARE_MAP:
SendActivity.sendToGoogle(this, trackId, SendType.MAPS);
intent = new Intent(this, ServicePickerActivity.class);
intent.putExtra(ServicePickerActivity.TRACK_ID, trackId);
intent.putExtra(ServicePickerActivity.SEND_TYPE, (Parcelable) SendType.MAPS);
startActivity(intent);
return true;
case Constants.MENU_SHARE_FUSION_TABLE:
SendActivity.sendToGoogle(this, trackId, SendType.FUSION_TABLES);
intent = new Intent(this, ServicePickerActivity.class);
intent.putExtra(ServicePickerActivity.TRACK_ID, trackId);
intent.putExtra(ServicePickerActivity.SEND_TYPE, (Parcelable) SendType.FUSION_TABLES);
startActivity(intent);
return true;
case Constants.MENU_SAVE_GPX_FILE:
case Constants.MENU_SAVE_KML_FILE:
@@ -193,7 +203,7 @@ public class TrackList extends ListActivity
Constants.getActionFromMenuId(item.getItemId()));
return true;
case Constants.MENU_DELETE: {
Intent intent = new Intent(Intent.ACTION_DELETE);
intent = new Intent(Intent.ACTION_DELETE);
Uri uri = ContentUris.withAppendedId(TracksColumns.CONTENT_URI, trackId);
intent.setDataAndType(uri, TracksColumns.CONTENT_ITEMTYPE);
startActivity(intent);
@@ -24,7 +24,6 @@ import com.google.android.apps.mytracks.MapsList;
import com.google.android.apps.mytracks.ProgressIndicator;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.io.AuthManager;
import com.google.android.apps.mytracks.io.AuthManager.AuthCallback;
import com.google.android.apps.mytracks.io.AuthManagerFactory;
@@ -35,23 +34,19 @@ import com.google.android.apps.mytracks.io.SendToMaps;
import com.google.android.apps.mytracks.io.maps.MapsConstants;
import com.google.android.apps.mytracks.io.maps.MapsFacade;
import com.google.android.apps.mytracks.util.SystemUtils;
import com.google.android.apps.mytracks.util.UriUtils;
import com.google.android.maps.mytracks.R;
import android.accounts.Account;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import java.util.ArrayList;
@@ -64,6 +59,14 @@ import java.util.List;
* @author Rodrigo Damazio
*/
public class SendActivity extends Activity implements ProgressIndicator {
public static final String TRACK_ID = "trackId";
public static final String SHARE_URL = "shareUrl";
public static final String SEND_MAPS = "sendMaps";
public static final String SEND_FUSION_TABLES = "sendFusionTables";
public static final String SEND_DOCS = "sendDocs";
public static final String CREATE_MAP = "createMap";
// Keys for saved state variables.
private static final String STATE_SEND_TO_MAPS = "mapsSend";
private static final String STATE_SEND_TO_FUSION_TABLES = "fusionSend";
@@ -76,11 +79,9 @@ public class SendActivity extends Activity implements ProgressIndicator {
private static final String STATE_ACCOUNT_NAME = "accountName";
private static final String STATE_TABLE_ID = "tableId";
private static final String STATE_MAP_ID = "mapId";
private static final String SEND_TYPE = "sendType";
/** States for the state machine that defines the upload process. */
private enum SendState {
SEND_OPTIONS,
START,
AUTHENTICATE_MAPS,
PICK_MAP,
@@ -100,10 +101,9 @@ public class SendActivity extends Activity implements ProgressIndicator {
NOT_READY
}
private static final int SEND_DIALOG = 1;
private static final int PROGRESS_DIALOG = 2;
private static final int PROGRESS_DIALOG = 1;
/* @VisibleForTesting */
static final int DONE_DIALOG = 3;
static final int DONE_DIALOG = 2;
// UI
private ProgressDialog progressDialog;
@@ -121,8 +121,8 @@ public class SendActivity extends Activity implements ProgressIndicator {
private String lastAccountType;
// Send request information.
private SendType sendType;
private long sendTrackId;
private boolean shareRequest;
private boolean sendToMaps;
private boolean sendToMapsNewMap;
private boolean sendToFusionTables;
@@ -174,7 +174,7 @@ public class SendActivity extends Activity implements ProgressIndicator {
}
// Only consider the intent if we're not restoring from a previous state.
if (currentState == SendState.SEND_OPTIONS) {
if (currentState == SendState.START) {
if (!handleIntent()) {
finish();
return;
@@ -185,29 +185,27 @@ public class SendActivity extends Activity implements ProgressIndicator {
Log.w(TAG, "Starting at state " + currentState);
executeStateMachine(currentState);
}
private boolean handleIntent() {
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Uri data = intent.getData();
if (!Intent.ACTION_SEND.equals(action) ||
!TracksColumns.CONTENT_ITEMTYPE.equals(type) ||
!UriUtils.matchesContentUri(data, TracksColumns.CONTENT_URI)) {
Log.e(TAG, "Got bad send intent: " + intent);
sendTrackId = intent.getLongExtra(TRACK_ID, -1L);
if (sendTrackId == -1L) {
return false;
}
sendTrackId = ContentUris.parseId(data);
sendType = intent.getParcelableExtra(SEND_TYPE);
sendToMaps = intent.getBooleanExtra(SEND_MAPS, false);
sendToFusionTables = intent.getBooleanExtra(SEND_FUSION_TABLES, false);
sendToDocs = intent.getBooleanExtra(SEND_DOCS, false);
if (!sendToMaps && !sendToFusionTables && !sendToDocs) {
return false;
}
sendToMapsNewMap = intent.getBooleanExtra(CREATE_MAP, true);
return true;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SEND_DIALOG:
return createSendDialog();
case PROGRESS_DIALOG:
return createProgressDialog();
case DONE_DIALOG:
@@ -216,42 +214,6 @@ public class SendActivity extends Activity implements ProgressIndicator {
return null;
}
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case SEND_DIALOG:
SendDialog sendDialog = (SendDialog) dialog;
sendDialog.setSendType(sendType);
return;
default:
return;
}
}
private Dialog createSendDialog() {
final SendDialog sendDialog = new SendDialog(this);
sendDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which != DialogInterface.BUTTON_POSITIVE) {
finish();
return;
}
dialog.dismiss();
sendToMaps = sendDialog.getSendToMaps();
sendToMapsNewMap = sendDialog.getCreateNewMap();
sendToFusionTables = sendDialog.getSendToFusionTables();
sendToDocs = sendDialog.getSendToDocs();
executeStateMachine(SendState.START);
}
});
sendDialog.setOnCancelListener(finishOnCancelListener);
return sendDialog;
}
private void restoreInstanceState(Bundle savedInstanceState) {
currentState = SendState.values()[savedInstanceState.getInt(STATE_STATE)];
@@ -321,8 +283,6 @@ public class SendActivity extends Activity implements ProgressIndicator {
private SendState executeState(SendState state) {
switch (state) {
case SEND_OPTIONS:
return showSendOptions();
case START:
return startSend();
case AUTHENTICATE_MAPS:
@@ -359,12 +319,6 @@ public class SendActivity extends Activity implements ProgressIndicator {
}
}
private SendState showSendOptions() {
showDialog(SEND_DIALOG);
return SendState.NOT_READY;
}
/**
* Initiates the process to send tracks to google.
* This is called once the user has selected sending options via the
@@ -577,8 +531,6 @@ public class SendActivity extends Activity implements ProgressIndicator {
public void run() {
Log.d(TAG, "Sending to Google done.");
dismissDialog(PROGRESS_DIALOG);
dismissDialog(SEND_DIALOG);
// Ensure a new done dialog is created each time.
// This is required because the send results must be available at the
// time the dialog is created.
@@ -633,9 +585,9 @@ public class SendActivity extends Activity implements ProgressIndicator {
};
}
DialogInterface.OnClickListener onOkListener = (canShare && sendType != null)
DialogInterface.OnClickListener onOkListener = (canShare && shareRequest)
? doShareListener : finishListener;
DialogInterface.OnClickListener onShareListener = (canShare && sendType == null)
DialogInterface.OnClickListener onShareListener = (canShare && !shareRequest)
? doShareListener : null;
return ResultDialogFactory.makeDialog(
@@ -645,7 +597,6 @@ public class SendActivity extends Activity implements ProgressIndicator {
private SendState onAllDone() {
Log.d(TAG, "All sending done.");
removeDialog(PROGRESS_DIALOG);
removeDialog(SEND_DIALOG);
removeDialog(DONE_DIALOG);
progressDialog = null;
finish();
@@ -859,7 +810,7 @@ public class SendActivity extends Activity implements ProgressIndicator {
* Resets status information for sending to Maps/Fusion Tables/Docs.
*/
private void resetState() {
currentState = SendState.SEND_OPTIONS;
currentState = SendState.START;
sendToMapsMapId = null;
sendToMapsSuccess = true;
sendToFusionTablesSuccess = true;
@@ -905,14 +856,4 @@ public class SendActivity extends Activity implements ProgressIndicator {
public void clearProgressMessage() {
progressDialog.setMessage("");
}
public static void sendToGoogle(Context ctx, long trackId, SendType sendType) {
Uri uri = ContentUris.withAppendedId(TracksColumns.CONTENT_URI, trackId);
Intent intent = new Intent(ctx, SendActivity.class);
intent.setAction(Intent.ACTION_SEND);
intent.setDataAndType(uri, TracksColumns.CONTENT_ITEMTYPE);
intent.putExtra(SEND_TYPE, (Parcelable) sendType);
ctx.startActivity(intent);
}
}
@@ -1,223 +0,0 @@
/*
* Copyright 2008 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.sendtogoogle;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.ApiFeatures;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TableRow;
/**
* A dialog where the user can choose where to send a track to, i.e., Google
* Maps, Google Fusion Tables, and Google Docs.
*
* @author Leif Hendrik Wilden
*/
public class SendDialog extends Dialog {
private boolean mapsEnabled;
private boolean fusionTablesEnabled;
private boolean docsEnabled;
private TableRow mapsTableRow;
private TableRow fusionTablesTableRow;
private TableRow docsTableRow;
private CheckBox mapsCheckBox;
private CheckBox fusionTablesCheckBox;
private CheckBox docsCheckBox;
private TableRow mapsOptionTableRow;
private RadioButton newMapRadioButton;
private RadioButton existingMapRadioButton;
private Button send;
private OnClickListener clickListener;
public SendDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mytracks_send_to_google);
mapsTableRow = (TableRow) findViewById(R.id.send_google_maps_row);
fusionTablesTableRow = (TableRow) findViewById(R.id.send_google_fusion_tables_row);
docsTableRow = (TableRow) findViewById(R.id.send_google_docs_row);
OnCheckedChangeListener checkBoxListener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton button, boolean checked) {
updateDisplay();
}
};
mapsCheckBox = (CheckBox) findViewById(R.id.send_google_maps);
mapsCheckBox.setOnCheckedChangeListener(checkBoxListener);
fusionTablesCheckBox = (CheckBox) findViewById(R.id.send_google_fusion_tables);
fusionTablesCheckBox.setOnCheckedChangeListener(checkBoxListener);
docsCheckBox = (CheckBox) findViewById(R.id.send_google_docs);
docsCheckBox.setOnCheckedChangeListener(checkBoxListener);
mapsOptionTableRow = (TableRow) findViewById(R.id.send_google_maps_option_row);
newMapRadioButton = (RadioButton) findViewById(R.id.send_google_new_map);
existingMapRadioButton = (RadioButton) findViewById(R.id.send_google_existing_map);
Button cancel = (Button) findViewById(R.id.send_google_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (clickListener != null) {
clickListener.onClick(SendDialog.this, BUTTON_NEGATIVE);
}
dismiss();
}
});
send = (Button) findViewById(R.id.send_google_send_now);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (clickListener != null) {
clickListener.onClick(SendDialog.this, BUTTON_POSITIVE);
}
dismiss();
}
});
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences prefs = getContext().getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
if (prefs != null) {
Editor editor = prefs.edit();
if (editor != null) {
editor.putBoolean(
getContext().getString(R.string.pick_existing_map_key), !getCreateNewMap());
if (mapsEnabled && fusionTablesEnabled && docsEnabled) {
editor.putBoolean(
getContext().getString(R.string.send_to_maps_key), getSendToMaps());
editor.putBoolean(
getContext().getString(R.string.send_to_fusion_tables_key), getSendToFusionTables());
editor.putBoolean(getContext().getString(R.string.send_to_docs_key), getSendToDocs());
}
ApiFeatures.getInstance().getApiAdapter().applyPreferenceChanges(editor);
}
}
}
/**
* Sets the sendType. If sendType is null, allows sending to all the Google
* services.
*
* @param sendType sendType
*/
public void setSendType(SendType sendType) {
updateState();
/*
* Note that sendType can be null, so cannot use a switch statement based on
* the sendType.
*/
if (sendType == SendType.MAPS) {
mapsEnabled = true;
fusionTablesEnabled = false;
docsEnabled = false;
mapsCheckBox.setChecked(true);
} else if (sendType == SendType.FUSION_TABLES) {
mapsEnabled = false;
fusionTablesEnabled = true;
docsEnabled = false;
fusionTablesCheckBox.setChecked(true);
} else {
mapsEnabled = true;
fusionTablesEnabled = true;
docsEnabled = true;
}
mapsTableRow.setVisibility(mapsEnabled ? View.VISIBLE : View.GONE);
fusionTablesTableRow.setVisibility(fusionTablesEnabled ? View.VISIBLE : View.GONE);
docsTableRow.setVisibility(docsEnabled ? View.VISIBLE : View.GONE);
updateDisplay();
}
/**
* Updates the UI state from the shared preferences.
*/
private void updateState() {
SharedPreferences prefs = getContext().getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
if (prefs != null) {
boolean pickExistingMap = prefs.getBoolean(
getContext().getString(R.string.pick_existing_map_key), false);
newMapRadioButton.setChecked(!pickExistingMap);
existingMapRadioButton.setChecked(pickExistingMap);
mapsCheckBox.setChecked(
prefs.getBoolean(getContext().getString(R.string.send_to_maps_key), true));
fusionTablesCheckBox.setChecked(
prefs.getBoolean(getContext().getString(R.string.send_to_fusion_tables_key), true));
docsCheckBox.setChecked(
prefs.getBoolean(getContext().getString(R.string.send_to_docs_key), true));
}
}
/**
* Updates the UI display based on the current state.
*/
private void updateDisplay() {
send.setEnabled(getSendToMaps() || getSendToFusionTables() || getSendToDocs());
mapsOptionTableRow.setVisibility(getSendToMaps() ? View.VISIBLE : View.GONE);
}
public void setOnClickListener(OnClickListener clickListener) {
this.clickListener = clickListener;
}
public boolean getCreateNewMap() {
return newMapRadioButton.isChecked();
}
public boolean getSendToMaps() {
return mapsEnabled && mapsCheckBox.isChecked();
}
public boolean getSendToFusionTables() {
return fusionTablesEnabled && fusionTablesCheckBox.isChecked();
}
public boolean getSendToDocs() {
return docsEnabled && docsCheckBox.isChecked();
}
}
@@ -0,0 +1,260 @@
/*
* 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.sendtogoogle;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.ApiFeatures;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TableRow;
/**
* A picker to select the Google services to send a track to.
*
* @author jshih@google.com (Jimmy Shih)
*/
public class ServicePickerActivity extends Activity {
/**
* The track id.
*/
public static final String TRACK_ID = "trackId";
/**
* The send type. Null to send to all Google services.
*/
public static final String SEND_TYPE = "sendType";
private static final int SERVICE_PICKER_DIALOG = 1;
private long trackId;
private SendType sendType;
private TableRow mapsTableRow;
private TableRow fusionTablesTableRow;
private TableRow docsTableRow;
private CheckBox mapsCheckBox;
private CheckBox fusionTablesCheckBox;
private CheckBox docsCheckBox;
private TableRow mapsOptionTableRow;
private RadioButton newMapRadioButton;
private RadioButton existingMapRadioButton;
private Button cancel;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
trackId = intent.getLongExtra(TRACK_ID, -1L);
sendType = intent.getParcelableExtra(SEND_TYPE);
showDialog(SERVICE_PICKER_DIALOG);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SERVICE_PICKER_DIALOG:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mytracks_send_to_google);
dialog.setCancelable(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface d) {
finish();
}
});
mapsTableRow = (TableRow) dialog.findViewById(R.id.send_google_maps_row);
fusionTablesTableRow = (TableRow) dialog.findViewById(R.id.send_google_fusion_tables_row);
docsTableRow = (TableRow) dialog.findViewById(R.id.send_google_docs_row);
mapsCheckBox = (CheckBox) dialog.findViewById(R.id.send_google_maps);
fusionTablesCheckBox = (CheckBox) dialog.findViewById(R.id.send_google_fusion_tables);
docsCheckBox = (CheckBox) dialog.findViewById(R.id.send_google_docs);
mapsOptionTableRow = (TableRow) dialog.findViewById(R.id.send_google_maps_option_row);
newMapRadioButton = (RadioButton) dialog.findViewById(R.id.send_google_new_map);
existingMapRadioButton = (RadioButton) dialog.findViewById(R.id.send_google_existing_map);
// Setup checkboxes
OnCheckedChangeListener checkBoxListener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton button, boolean checked) {
updateStateBySelection();
}
};
mapsCheckBox.setOnCheckedChangeListener(checkBoxListener);
fusionTablesCheckBox.setOnCheckedChangeListener(checkBoxListener);
docsCheckBox.setOnCheckedChangeListener(checkBoxListener);
// Setup buttons
cancel = (Button) dialog.findViewById(R.id.send_google_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
send = (Button) dialog.findViewById(R.id.send_google_send_now);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
saveState();
Intent intent = new Intent(ServicePickerActivity.this, SendActivity.class);
intent.putExtra(SendActivity.TRACK_ID, trackId);
intent.putExtra(SendActivity.SHARE_URL, sendType != null);
intent.putExtra(SendActivity.SEND_MAPS, sendMaps());
intent.putExtra(SendActivity.SEND_FUSION_TABLES, sendFusionTables());
intent.putExtra(SendActivity.SEND_DOCS, sendDocs());
intent.putExtra(SendActivity.CREATE_MAP, !existingMapRadioButton.isChecked());
startActivity(intent);
finish();
}
});
// Setup initial state
initState();
// Update state based on sendType
updateStateBySendType();
// Update state based on current user selection
updateStateBySelection();
return dialog;
default:
return null;
}
}
/**
* Initializes the UI state based on the shared preferences.
*/
private void initState() {
SharedPreferences prefs = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
boolean pickExistingMap = prefs.getBoolean(getString(R.string.pick_existing_map_key), false);
newMapRadioButton.setChecked(!pickExistingMap);
existingMapRadioButton.setChecked(pickExistingMap);
mapsCheckBox.setChecked(prefs.getBoolean(getString(R.string.send_to_maps_key), true));
fusionTablesCheckBox.setChecked(
prefs.getBoolean(getString(R.string.send_to_fusion_tables_key), true));
docsCheckBox.setChecked(prefs.getBoolean(getString(R.string.send_to_docs_key), true));
}
/**
* Updates the UI state based on the sendType.
*/
private void updateStateBySendType() {
if (sendType == SendType.MAPS) {
mapsCheckBox.setChecked(true);
} else if (sendType == SendType.FUSION_TABLES) {
fusionTablesCheckBox.setChecked(true);
} else if (sendType == SendType.DOCS) {
docsCheckBox.setChecked(true);
} else {
// sendType == null
}
mapsTableRow.setVisibility(showMaps() ? View.VISIBLE : View.GONE);
fusionTablesTableRow.setVisibility(showFusionTables() ? View.VISIBLE : View.GONE);
docsTableRow.setVisibility(showDocs() ? View.VISIBLE : View.GONE);
}
/**
* Updates the UI state based on the current selection.
*/
private void updateStateBySelection() {
mapsOptionTableRow.setVisibility(sendMaps() ? View.VISIBLE : View.GONE);
send.setEnabled(sendMaps() || sendFusionTables() || sendDocs());
}
/**
* Saves the UI state to the shared preferences.
*/
private void saveState() {
SharedPreferences prefs = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putBoolean(
getString(R.string.pick_existing_map_key), existingMapRadioButton.isChecked());
if (sendType == null) {
editor.putBoolean(getString(R.string.send_to_maps_key), sendMaps());
editor.putBoolean(getString(R.string.send_to_fusion_tables_key), sendFusionTables());
editor.putBoolean(getString(R.string.send_to_docs_key), sendDocs());
}
ApiFeatures.getInstance().getApiAdapter().applyPreferenceChanges(editor);
}
/**
* Returns true to show the Google Maps option.
*/
private boolean showMaps() {
return sendType == null || sendType == SendType.MAPS;
}
/**
* Returns true to show the Google Fusion Tables option.
*/
private boolean showFusionTables() {
return sendType == null || sendType == SendType.FUSION_TABLES;
}
/**
* Returns true to show the Google Docs option.
*/
private boolean showDocs() {
return sendType == null || sendType == SendType.DOCS;
}
/**
* Returns true to send to Google Maps.
*/
private boolean sendMaps() {
return showMaps() && mapsCheckBox.isChecked();
}
/**
* Returns true to send to Google Fusion Tables.
*/
private boolean sendFusionTables() {
return showFusionTables() && fusionTablesCheckBox.isChecked();
}
/**
* Returns true to send to Google Docs.
*/
private boolean sendDocs() {
return showDocs() && docsCheckBox.isChecked();
}
}