From 883596b388a7429ab0a84bce35eb78303feb3b83 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Tue, 19 Feb 2013 18:41:26 -0800 Subject: [PATCH] In the start up flow, ask the user if he wants to enable Google Drive sync. --- MyTracks/res/values/strings.xml | 14 +++ .../AbstractSendToGoogleActivity.java | 16 +++- .../apps/mytracks/TrackListActivity.java | 28 +++++- .../fragments/EnableSyncDialogFragment.java | 87 +++++++++++++++++++ .../mytracks/io/sendtogoogle/SendRequest.java | 11 +++ .../android/apps/mytracks/util/EulaUtils.java | 11 ++- .../io/sendtogoogle/SendRequestTest.java | 13 ++- 7 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 MyTracks/src/com/google/android/apps/mytracks/fragments/EnableSyncDialogFragment.java diff --git a/MyTracks/res/values/strings.xml b/MyTracks/res/values/strings.xml index faa32126c..5f0a274af 100644 --- a/MyTracks/res/values/strings.xml +++ b/MyTracks/res/values/strings.xml @@ -556,6 +556,20 @@ limitations under the License. Segment + + + Do you want to sync your tracks with Google Drive? This will allow you to view your tracks in + Google Drive, share tracks via Google Drive, and view tracks shared with you. All tracks synced + to Google Drive will be private by default. + + + Sync with Google Drive + + diff --git a/MyTracks/src/com/google/android/apps/mytracks/AbstractSendToGoogleActivity.java b/MyTracks/src/com/google/android/apps/mytracks/AbstractSendToGoogleActivity.java index c5ab138a1..ba5276dac 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/AbstractSendToGoogleActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/AbstractSendToGoogleActivity.java @@ -32,6 +32,7 @@ import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest; import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils; import com.google.android.apps.mytracks.io.sendtogoogle.UploadResultActivity; import com.google.android.apps.mytracks.io.spreadsheets.SendSpreadsheetsActivity; +import com.google.android.apps.mytracks.io.sync.SyncUtils; import com.google.android.apps.mytracks.util.IntentUtils; import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.maps.mytracks.R; @@ -43,6 +44,7 @@ import android.accounts.AccountManagerFuture; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.app.Activity; +import android.content.ContentResolver; import android.content.Intent; import android.os.Bundle; import android.util.Log; @@ -287,7 +289,19 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv private void startNextActivity() { Class next; if (sendRequest.isSendDrive()) { - if (sendRequest.isDriveShare()) { + if (sendRequest.isDriveEnableSync()) { + PreferencesUtils.setBoolean(this, R.string.drive_sync_key, true); + + // Turn off everything + SyncUtils.disableSync(this); + + // Turn on sync + ContentResolver.setMasterSyncAutomatically(true); + + // Enable sync for account + SyncUtils.enableSync(sendRequest.getAccount()); + return; + } else if (sendRequest.isDriveShare()) { AddEmailsDialogFragment.newInstance(sendRequest.getTrackId()) .show(getSupportFragmentManager(), AddEmailsDialogFragment.ADD_EMAILS_DIALOG_TAG); return; diff --git a/MyTracks/src/com/google/android/apps/mytracks/TrackListActivity.java b/MyTracks/src/com/google/android/apps/mytracks/TrackListActivity.java index 9a74f491f..2b461a545 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/TrackListActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/TrackListActivity.java @@ -28,6 +28,8 @@ import com.google.android.apps.mytracks.fragments.ConfirmDialogFragment.ConfirmC import com.google.android.apps.mytracks.fragments.DeleteAllTrackDialogFragment; import com.google.android.apps.mytracks.fragments.DeleteOneTrackDialogFragment; import com.google.android.apps.mytracks.fragments.DeleteOneTrackDialogFragment.DeleteOneTrackCaller; +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.WelcomeDialogFragment; @@ -93,7 +95,7 @@ import java.util.Locale; * @author Leif Hendrik Wilden */ public class TrackListActivity extends AbstractSendToGoogleActivity - implements EulaCaller, WelcomeCaller, ConfirmCaller, DeleteOneTrackCaller { + implements EulaCaller, WelcomeCaller, EnableSyncCaller, ConfirmCaller, DeleteOneTrackCaller { private static final String TAG = TrackListActivity.class.getSimpleName(); private static final String START_GPS_KEY = "start_gps_key"; @@ -720,8 +722,32 @@ public class TrackListActivity extends AbstractSendToGoogleActivity }); if (dialog != null) { dialog.show(); + return; } } + showEnableSync(); + } + + private void showEnableSync() { + if (EulaUtils.getShowEnableSync(this)) { + Fragment fragment = getSupportFragmentManager() + .findFragmentByTag(EnableSyncDialogFragment.ENABLE_SYNC_DIALOG_TAG); + if (fragment == null) { + new EnableSyncDialogFragment().show( + getSupportFragmentManager(), EnableSyncDialogFragment.ENABLE_SYNC_DIALOG_TAG); + } + } + } + + @Override + public void onEnableSyncDone(boolean enable) { + EulaUtils.setShowEnableSync(this); + if (enable) { + SendRequest sendRequest = new SendRequest(-1L); + sendRequest.setSendDrive(true); + sendRequest.setDriveEnableSync(true); + sendToGoogle(sendRequest); + } } /** diff --git a/MyTracks/src/com/google/android/apps/mytracks/fragments/EnableSyncDialogFragment.java b/MyTracks/src/com/google/android/apps/mytracks/fragments/EnableSyncDialogFragment.java new file mode 100644 index 000000000..93c3bd232 --- /dev/null +++ b/MyTracks/src/com/google/android/apps/mytracks/fragments/EnableSyncDialogFragment.java @@ -0,0 +1,87 @@ +/* + * 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.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. + * + * @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 Dialog onCreateDialog(Bundle savedInstanceState) { + fragmentActivity = getActivity(); + return new AlertDialog.Builder(fragmentActivity).setNegativeButton( + R.string.generic_cancel, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + caller.onEnableSyncDone(false); + } + }).setPositiveButton(R.string.generic_ok, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + caller.onEnableSyncDone(true); + } + }).setTitle(R.string.enable_sync_title).setMessage(R.string.enable_sync_message).create(); + } + + @Override + public void onCancel(DialogInterface arg0) { + caller.onEnableSyncDone(false); + } +} diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequest.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequest.java index eb03bb61f..cd4961143 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequest.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequest.java @@ -36,6 +36,7 @@ public class SendRequest implements Parcelable { private boolean sendFusionTables = false; private boolean sendSpreadsheets = false; + private boolean driveEnableSync = false; private boolean driveShare = false; private String driveShareEmails = null; @@ -97,6 +98,14 @@ public class SendRequest implements Parcelable { this.sendSpreadsheets = sendSpreadsheets; } + public boolean isDriveEnableSync() { + return driveEnableSync; + } + + public void setDriveEnableSync(boolean driveEnableSync) { + this.driveEnableSync = driveEnableSync; + } + public boolean isDriveShare() { return driveShare; } @@ -199,6 +208,7 @@ public class SendRequest implements Parcelable { sendMaps = in.readByte() == 1; sendFusionTables = in.readByte() == 1; sendSpreadsheets = in.readByte() == 1; + driveEnableSync = in.readByte() == 1; driveShare = in.readByte() == 1; driveShareEmails = in.readString(); mapsShare = in.readByte() == 1; @@ -225,6 +235,7 @@ public class SendRequest implements Parcelable { out.writeByte((byte) (sendMaps ? 1 : 0)); out.writeByte((byte) (sendFusionTables ? 1 : 0)); out.writeByte((byte) (sendSpreadsheets ? 1 : 0)); + out.writeByte((byte) (driveEnableSync ? 1 : 0)); out.writeByte((byte) (driveShare ? 1 : 0)); out.writeString(driveShareEmails); out.writeByte((byte) (mapsShare ? 1 : 0)); diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/EulaUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/EulaUtils.java index 733f17ba2..9186c5108 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/EulaUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/EulaUtils.java @@ -31,6 +31,7 @@ public class EulaUtils { // Accepting Google mobile terms of service private static final String ACCEPT_EULA_PREFERENCE_KEY = "eula.google_mobile_tos_accepted"; private static final String SHOW_WELCOME_PREFERENCE_KEY = "showWelcome"; + private static final String SHOW_ENABLE_SYNC_PREFERENCE_KEY = "showEnableSync"; private EulaUtils() {} @@ -49,7 +50,15 @@ public class EulaUtils { public static void setShowWelcome(Context context) { setValue(context, SHOW_WELCOME_PREFERENCE_KEY, false); } - + + public static boolean getShowEnableSync(Context context) { + return getValue(context, SHOW_ENABLE_SYNC_PREFERENCE_KEY, true); + } + + public static void setShowEnableSync(Context context) { + setValue(context, SHOW_ENABLE_SYNC_PREFERENCE_KEY, false); + } + private static boolean getValue(Context context, String key, boolean defaultValue) { SharedPreferences sharedPreferences = context.getSharedPreferences( EULA_PREFERENCE_FILE, Context.MODE_PRIVATE); diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequestTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequestTest.java index bdd38df82..7b44bd753 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequestTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/SendRequestTest.java @@ -151,6 +151,7 @@ public class SendRequestTest extends AndroidTestCase { parcel.writeByte((byte) 1); parcel.writeByte((byte) 1); parcel.writeByte((byte) 1); + parcel.writeByte((byte) 1); parcel.writeString(DRIVE_SHARE_EMAILS); parcel.writeByte((byte) 1); parcel.writeString(""); @@ -170,6 +171,7 @@ public class SendRequestTest extends AndroidTestCase { assertTrue(sendRequest.isSendMaps()); assertTrue(sendRequest.isSendFusionTables()); assertTrue(sendRequest.isSendSpreadsheets()); + assertTrue(sendRequest.isDriveEnableSync()); assertTrue(sendRequest.isDriveShare()); assertEquals(DRIVE_SHARE_EMAILS, sendRequest.getDriveShareEmails()); assertTrue(sendRequest.isMapsShare()); @@ -196,6 +198,7 @@ public class SendRequestTest extends AndroidTestCase { parcel.writeByte((byte) 0); parcel.writeByte((byte) 0); parcel.writeByte((byte) 0); + parcel.writeByte((byte) 0); parcel.writeString(null); parcel.writeByte((byte) 0); parcel.writeString(null); @@ -215,6 +218,7 @@ public class SendRequestTest extends AndroidTestCase { assertFalse(sendRequest.isSendMaps()); assertFalse(sendRequest.isSendFusionTables()); assertFalse(sendRequest.isSendSpreadsheets()); + assertFalse(sendRequest.isDriveEnableSync()); assertFalse(sendRequest.isDriveShare()); assertNull(sendRequest.getDriveShareEmails()); assertFalse(sendRequest.isMapsShare()); @@ -243,6 +247,7 @@ public class SendRequestTest extends AndroidTestCase { boolean sendMaps = parcel.readByte() == 1; boolean sendFusionTables = parcel.readByte() == 1; boolean sendSpreadsheets = parcel.readByte() == 1; + boolean driveEnableSync = parcel.readByte() == 1; boolean driveShare = parcel.readByte() == 1; String dirveShareEmails = parcel.readString(); boolean mapsShare = parcel.readByte() == 1; @@ -260,6 +265,7 @@ public class SendRequestTest extends AndroidTestCase { assertFalse(sendMaps); assertFalse(sendFusionTables); assertFalse(sendSpreadsheets); + assertFalse(driveEnableSync); assertFalse(driveShare); assertNull(dirveShareEmails); assertFalse(mapsShare); @@ -283,6 +289,7 @@ public class SendRequestTest extends AndroidTestCase { sendRequest.setSendMaps(true); sendRequest.setSendFusionTables(true); sendRequest.setSendSpreadsheets(true); + sendRequest.setDriveEnableSync(true); sendRequest.setDriveShare(true); sendRequest.setDriveShareEmails(DRIVE_SHARE_EMAILS); sendRequest.setMapsShare(true); @@ -305,7 +312,8 @@ public class SendRequestTest extends AndroidTestCase { boolean sendMaps = parcel.readByte() == 1; boolean sendFusionTables = parcel.readByte() == 1; boolean sendSpreadsheets = parcel.readByte() == 1; - boolean shareDrive = parcel.readByte() == 1; + boolean driveEnableSync = parcel.readByte() == 1; + boolean driveShare = parcel.readByte() == 1; String driveShareEmails = parcel.readString(); boolean mapsShare = parcel.readByte() == 1; String mapsSharePackageName = parcel.readString(); @@ -322,7 +330,8 @@ public class SendRequestTest extends AndroidTestCase { assertTrue(sendMaps); assertTrue(sendFusionTables); assertTrue(sendSpreadsheets); - assertTrue(shareDrive); + assertTrue(driveEnableSync); + assertTrue(driveShare); assertEquals(DRIVE_SHARE_EMAILS, driveShareEmails); assertTrue(mapsShare); assertEquals(MAPS_SHARE_PACKAGE_NAME, mapsSharePackageName);