Add menu option to Sync to Google Drive.

This commit is contained in:
Jimmy Shih
2013-06-24 14:09:25 -07:00
parent 176b2b05d3
commit 2d4cabf4ef
13 changed files with 250 additions and 129 deletions
@@ -22,6 +22,8 @@ import com.google.android.apps.mytracks.fragments.ChooseAccountDialogFragment;
import com.google.android.apps.mytracks.fragments.ChooseAccountDialogFragment.ChooseAccountCaller;
import com.google.android.apps.mytracks.fragments.ConfirmDialogFragment;
import com.google.android.apps.mytracks.fragments.ConfirmDialogFragment.ConfirmCaller;
import com.google.android.apps.mytracks.fragments.ConfirmSyncDialogFragment;
import com.google.android.apps.mytracks.fragments.ConfirmSyncDialogFragment.ConfirmSyncCaller;
import com.google.android.apps.mytracks.fragments.InstallEarthDialogFragment;
import com.google.android.apps.mytracks.fragments.ShareTrackDialogFragment;
import com.google.android.apps.mytracks.fragments.ShareTrackDialogFragment.ShareTrackCaller;
@@ -65,7 +67,7 @@ import java.io.IOException;
* @author Jimmy Shih
*/
public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActivity implements
ChooseAccountCaller, CheckPermissionCaller, ShareTrackCaller, ConfirmCaller {
ChooseAccountCaller, ConfirmSyncCaller, CheckPermissionCaller, ShareTrackCaller, ConfirmCaller {
private static final String TAG = AbstractMyTracksActivity.class.getSimpleName();
private static final String SEND_REQUEST_KEY = "send_request_key";
@@ -132,27 +134,39 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
}
sendRequest.setAccount(new Account(googleAccount, Constants.ACCOUNT_TYPE));
// Check Drive permission
boolean needDrivePermission = sendRequest.isSendDrive();
if (!needDrivePermission && sendRequest.isSendFusionTables()) {
needDrivePermission = PreferencesUtils.getBoolean(this,
R.string.export_google_fusion_tables_public_key,
PreferencesUtils.EXPORT_GOOGLE_FUSION_TABLES_PUBLIC_DEFAULT);
}
if (!needDrivePermission) {
needDrivePermission = sendRequest.isSendSpreadsheets();
}
if (needDrivePermission) {
Fragment fragment = CheckPermissionFragment.newInstance(
sendRequest.getAccount().name, SendToGoogleUtils.DRIVE_SCOPE);
getSupportFragmentManager()
.beginTransaction().add(fragment, CheckPermissionFragment.CHECK_PERMISSION_TAG).commit();
if (sendRequest.isSendDrive() && sendRequest.isDriveSync() && sendRequest.isDriveSyncConfirm()) {
new ConfirmSyncDialogFragment().show(
getSupportFragmentManager(), ConfirmSyncDialogFragment.CONFIRM_SYNC_DIALOG_TAG);
} else {
onDrivePermissionSuccess();
onConfirmSyncDone(true);
}
}
@Override
public void onConfirmSyncDone(boolean enable) {
if (enable) {
// Check Drive permission
boolean needDrivePermission = sendRequest.isSendDrive();
if (!needDrivePermission && sendRequest.isSendFusionTables()) {
needDrivePermission = PreferencesUtils.getBoolean(this,
R.string.export_google_fusion_tables_public_key,
PreferencesUtils.EXPORT_GOOGLE_FUSION_TABLES_PUBLIC_DEFAULT);
}
if (!needDrivePermission) {
needDrivePermission = sendRequest.isSendSpreadsheets();
}
if (needDrivePermission) {
Fragment fragment = CheckPermissionFragment.newInstance(
sendRequest.getAccount().name, SendToGoogleUtils.DRIVE_SCOPE);
getSupportFragmentManager().beginTransaction()
.add(fragment, CheckPermissionFragment.CHECK_PERMISSION_TAG).commit();
} else {
onDrivePermissionSuccess();
}
}
}
@Override
public void onCheckPermissionDone(String scope, boolean success, Intent intent) {
if (success) {
@@ -19,10 +19,10 @@ package com.google.android.apps.mytracks;
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.fragments.ConfirmSyncDialogFragment;
import com.google.android.apps.mytracks.fragments.ConfirmSyncDialogFragment.ConfirmSyncCaller;
import com.google.android.apps.mytracks.fragments.DeleteTrackDialogFragment;
import com.google.android.apps.mytracks.fragments.DeleteTrackDialogFragment.DeleteTrackCaller;
import com.google.android.apps.mytracks.fragments.EnableSyncDialogFragment;
import com.google.android.apps.mytracks.fragments.EnableSyncDialogFragment.EnableSyncCaller;
import com.google.android.apps.mytracks.fragments.EulaDialogFragment;
import com.google.android.apps.mytracks.fragments.EulaDialogFragment.EulaCaller;
import com.google.android.apps.mytracks.fragments.FileTypeDialogFragment;
@@ -93,7 +93,7 @@ import java.util.Locale;
* @author Leif Hendrik Wilden
*/
public class TrackListActivity extends AbstractSendToGoogleActivity
implements EulaCaller, ConfirmSyncCaller, DeleteTrackCaller, FileTypeCaller {
implements EulaCaller, EnableSyncCaller, DeleteTrackCaller, FileTypeCaller {
private static final String TAG = TrackListActivity.class.getSimpleName();
private static final int GOOGLE_PLAY_SERVICES_REQUEST_CODE = 0;
@@ -293,10 +293,10 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
// Menu items
private MenuItem searchMenuItem;
private MenuItem startGpsMenuItem;
private MenuItem importAllMenuItem;
private MenuItem exportAllMenuItem;
private MenuItem deleteAllMenuItem;
private MenuItem refreshMenuItem;
private MenuItem exportAllMenuItem;
private MenuItem importAllMenuItem;
private MenuItem deleteAllMenuItem;
private MenuItem feedbackMenuItem;
private boolean startGps = false; // true to start gps
@@ -469,10 +469,10 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
searchMenuItem = menu.findItem(R.id.track_list_search);
startGpsMenuItem = menu.findItem(R.id.track_list_start_gps);
importAllMenuItem = menu.findItem(R.id.track_list_import_all);
exportAllMenuItem = menu.findItem(R.id.track_list_export_all);
deleteAllMenuItem = menu.findItem(R.id.track_list_delete_all);
refreshMenuItem = menu.findItem(R.id.track_list_refresh);
exportAllMenuItem = menu.findItem(R.id.track_list_export_all);
importAllMenuItem = menu.findItem(R.id.track_list_import_all);
deleteAllMenuItem = menu.findItem(R.id.track_list_delete_all);
feedbackMenuItem = menu.findItem(R.id.track_list_feedback);
feedbackMenuItem.setVisible(ApiAdapterFactory.getApiAdapter().isGoogleFeedbackAvailable());
@@ -533,27 +533,37 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
}
myTracksLocationManager.close();
return true;
case R.id.track_list_import_all:
FileTypeDialogFragment.newInstance(R.id.track_list_import_all,
R.string.import_selection_title, R.string.import_selection_option, 2)
.show(getSupportFragmentManager(), FileTypeDialogFragment.FILE_TYPE_DIALOG_TAG);
case R.id.track_list_refresh:
if (driveSync) {
SyncUtils.syncNow(this);
} else {
PreferencesUtils.setString(
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
SendRequest sendRequest = new SendRequest(-1L);
sendRequest.setSendDrive(true);
sendRequest.setDriveSync(true);
sendRequest.setDriveSyncConfirm(true);
sendToGoogle(sendRequest);
}
return true;
case R.id.track_list_aggregated_statistics:
intent = IntentUtils.newIntent(this, AggregatedStatsActivity.class);
startActivity(intent);
return true;
case R.id.track_list_export_all:
FileTypeDialogFragment.newInstance(R.id.track_list_export_all,
R.string.export_all_title, R.string.export_all_option, 4)
.show(getSupportFragmentManager(), FileTypeDialogFragment.FILE_TYPE_DIALOG_TAG);
return true;
case R.id.track_list_import_all:
FileTypeDialogFragment.newInstance(R.id.track_list_import_all,
R.string.import_selection_title, R.string.import_selection_option, 2)
.show(getSupportFragmentManager(), FileTypeDialogFragment.FILE_TYPE_DIALOG_TAG);
return true;
case R.id.track_list_delete_all:
DeleteTrackDialogFragment.newInstance(true, new long[] {})
.show(getSupportFragmentManager(), DeleteTrackDialogFragment.DELETE_TRACK_DIALOG_TAG);
return true;
case R.id.track_list_aggregated_statistics:
intent = IntentUtils.newIntent(this, AggregatedStatsActivity.class);
startActivity(intent);
return true;
case R.id.track_list_refresh:
SyncUtils.syncNow(this);
return true;
case R.id.track_list_settings:
intent = IntentUtils.newIntent(this, SettingsActivity.class);
startActivity(intent);
@@ -693,17 +703,16 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
private void showEnableSync() {
if (EulaUtils.getShowEnableSync(this)) {
Fragment fragment = getSupportFragmentManager()
.findFragmentByTag(ConfirmSyncDialogFragment.CONFIRM_SYNC_DIALOG_TAG);
.findFragmentByTag(EnableSyncDialogFragment.ENABLE_SYNC_DIALOG_TAG);
if (fragment == null) {
ConfirmSyncDialogFragment.newInstance(
R.string.sync_enable_title, getString(R.string.sync_enable_message))
.show(getSupportFragmentManager(), ConfirmSyncDialogFragment.CONFIRM_SYNC_DIALOG_TAG);
new EnableSyncDialogFragment().show(
getSupportFragmentManager(), EnableSyncDialogFragment.ENABLE_SYNC_DIALOG_TAG);
}
}
}
@Override
public void onConfirmSyncDone(boolean enable) {
public void onEnableSyncDone(boolean enable) {
EulaUtils.setShowEnableSync(this);
if (enable) {
SendRequest sendRequest = new SendRequest(-1L);
@@ -728,18 +737,18 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
isGpsStarted ? R.drawable.menu_stop_gps : R.drawable.menu_start_gps);
}
}
if (importAllMenuItem != null) {
importAllMenuItem.setVisible(!isRecording);
if (refreshMenuItem != null) {
refreshMenuItem.setTitle(driveSync ? R.string.menu_refresh : R.string.menu_sync_drive);
}
if (exportAllMenuItem != null) {
exportAllMenuItem.setVisible(!isRecording);
}
if (importAllMenuItem != null) {
importAllMenuItem.setVisible(!isRecording);
}
if (deleteAllMenuItem != null) {
deleteAllMenuItem.setVisible(!isRecording);
}
if (refreshMenuItem != null) {
refreshMenuItem.setVisible(driveSync);
}
}
/**
@@ -50,22 +50,9 @@ public class ConfirmSyncDialogFragment extends DialogFragment {
public static final String CONFIRM_SYNC_DIALOG_TAG = "confirmSyncDialog";
private static final String KEY_TITLE_ID = "titleId";
private static final String KEY_MESSAGE = "message";
private ConfirmSyncCaller caller;
private FragmentActivity fragmentActivity;
public static ConfirmSyncDialogFragment newInstance(int titleId, String message) {
Bundle bundle = new Bundle();
bundle.putInt(KEY_TITLE_ID, titleId);
bundle.putString(KEY_MESSAGE, message);
ConfirmSyncDialogFragment confirmSyncDialogFragment = new ConfirmSyncDialogFragment();
confirmSyncDialogFragment.setArguments(bundle);
return confirmSyncDialogFragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
@@ -91,8 +78,10 @@ public class ConfirmSyncDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int titleId = getArguments().getInt(KEY_TITLE_ID);
String message = getArguments().getString(KEY_MESSAGE);
String googleAccount = PreferencesUtils.getString(
fragmentActivity, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
String message = getString(
R.string.sync_drive_confirm_message, googleAccount, getString(R.string.my_tracks_app_name));
return new AlertDialog.Builder(fragmentActivity).setMessage(message)
.setNegativeButton(R.string.generic_no, new OnClickListener() {
@Override
@@ -104,7 +93,7 @@ public class ConfirmSyncDialogFragment extends DialogFragment {
public void onClick(DialogInterface dialog, int which) {
caller.onConfirmSyncDone(true);
}
}).setTitle(titleId).create();
}).setTitle(R.string.sync_drive_title).create();
}
@Override
@@ -0,0 +1,99 @@
/*
* Copyright 2013 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.fragments;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
/**
* A DialogFrament to enable sync to Google Drive.
*
* @author Jimmy Shih
*/
public class EnableSyncDialogFragment extends DialogFragment {
/**
* Interface for caller of this dialog fragment.
*
* @author Jimmy Shih
*/
public interface EnableSyncCaller {
/**
* Called when enable sync is done.
*/
public void onEnableSyncDone(boolean enable);
}
public static final String ENABLE_SYNC_DIALOG_TAG = "enableSyncDialog";
private EnableSyncCaller caller;
private FragmentActivity fragmentActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
caller = (EnableSyncCaller) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
activity.toString() + " must implement " + EnableSyncCaller.class.getSimpleName());
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentActivity = getActivity();
if (PreferencesUtils.getBoolean(
fragmentActivity, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT)) {
dismiss();
caller.onEnableSyncDone(false);
return;
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(fragmentActivity).setMessage(R.string.sync_drive_enable_message)
.setNegativeButton(R.string.generic_no, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
caller.onEnableSyncDone(false);
}
}).setPositiveButton(R.string.generic_yes, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
caller.onEnableSyncDone(true);
}
}).setTitle(R.string.sync_drive_title).create();
}
@Override
public void onCancel(DialogInterface arg0) {
caller.onEnableSyncDone(false);
}
}
@@ -36,7 +36,8 @@ public class SendRequest implements Parcelable {
private boolean sendFusionTables = false;
private boolean sendSpreadsheets = false;
private boolean driveSync = false; // to enable Drive sync
private boolean driveSync = false; // to enable Drive sync
private boolean driveSyncConfirm = false;
private boolean driveShare = false; // to enable Drive share
private String driveShareEmails = null; // for driveShare, emails to share
private boolean driveSharePublic = false; // for driveShare, share as public
@@ -102,6 +103,14 @@ public class SendRequest implements Parcelable {
this.driveSync = driveSync;
}
public boolean isDriveSyncConfirm() {
return driveSyncConfirm;
}
public void setDriveSyncConfirm(boolean driveSyncConfirm) {
this.driveSyncConfirm = driveSyncConfirm;
}
public boolean isDriveShare() {
return driveShare;
}
@@ -181,6 +190,7 @@ public class SendRequest implements Parcelable {
sendFusionTables = in.readByte() == 1;
sendSpreadsheets = in.readByte() == 1;
driveSync = in.readByte() == 1;
driveSyncConfirm = in.readByte() == 1;
driveShare = in.readByte() == 1;
driveShareEmails = in.readString();
driveSharePublic = in.readByte() == 1;
@@ -205,6 +215,7 @@ public class SendRequest implements Parcelable {
out.writeByte((byte) (sendFusionTables ? 1 : 0));
out.writeByte((byte) (sendSpreadsheets ? 1 : 0));
out.writeByte((byte) (driveSync ? 1 : 0));
out.writeByte((byte) (driveSyncConfirm ? 1 : 0));
out.writeByte((byte) (driveShare ? 1 : 0));
out.writeString(driveShareEmails);
out.writeByte((byte) (driveSharePublic ? 1 : 0));
@@ -144,14 +144,12 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
this, R.string.settings_google_account_confirm_message, null);
break;
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
dialog = DialogUtils.createConfirmationDialog(this,
R.string.settings_google_drive_sync_confirm_message,
new DialogInterface.OnClickListener() {
dialog = DialogUtils.createConfirmationDialog(this, R.string.sync_drive_title,
R.string.sync_drive_confirm_message, null, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int button) {
final String googleAccount = PreferencesUtils.getString(
GoogleSettingsActivity.this, R.string.google_account_key,
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
final String googleAccount = PreferencesUtils.getString(GoogleSettingsActivity.this,
R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
/*
* This class, a PreferenceActivity, needs to support api level
* 8+, thus cannot use CheckPermissionFragment because a
@@ -181,7 +179,7 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
});
thread.start();
}
});
}, null);
break;
default:
dialog = null;
@@ -237,8 +235,7 @@ public class GoogleSettingsActivity extends AbstractSettingsActivity {
});
break;
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
alertDialog.setMessage(getString(
R.string.settings_google_drive_sync_confirm_message, googleAccount,
alertDialog.setMessage(getString(R.string.sync_drive_confirm_message, googleAccount,
getString(R.string.my_tracks_app_name)));
break;
default:
@@ -36,44 +36,44 @@ public class DialogUtils {
/**
* Creates a confirmation dialog.
*
*
* @param context the context
* @param messageId the message id
* @param okListener the listener when OK is clicked
*/
public static Dialog createConfirmationDialog(
Context context, int messageId, DialogInterface.OnClickListener okListener) {
return createConfirmationDialog(context, messageId, null, okListener, null);
return createConfirmationDialog(
context, R.string.generic_confirm_title, messageId, null, okListener, null);
}
/**
* Creates a confirmation dialog.
*
* @param context the context
* @param titleId the titleId
* @param messageId the messageId
* @param view the view
* @param okListener the listener when OK is clicked
* @param cancelListener the listener when cancel is clicked
*/
public static Dialog createConfirmationDialog(
Context context, int messageId, View view, DialogInterface.OnClickListener okListener,
public static Dialog createConfirmationDialog(Context context, int titleId, int messageId,
View view, DialogInterface.OnClickListener okListener,
DialogInterface.OnClickListener cancelListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCancelable(true)
AlertDialog.Builder builder = new AlertDialog.Builder(context).setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_alert)
.setNegativeButton(R.string.generic_no, cancelListener)
.setPositiveButton(R.string.generic_yes, okListener)
.setTitle(R.string.generic_confirm_title);
.setPositiveButton(R.string.generic_yes, okListener).setTitle(titleId);
if (messageId != -1) {
builder.setMessage(messageId);
}
if (view != null) {
builder.setView(view);
}
return builder.create();
}
/**
* Creates a spinner progress dialog.
*