forked from upstream-mirrors/OpenTracks
Cleanup code to enable sync to Google Drive
This commit is contained in:
@@ -2127,14 +2127,18 @@ limitations under the License.
|
||||
All files in the %1$s Google Drive ‘%2$s’ folder will be imported as new tracks. If
|
||||
not desired, move the files to a different folder beforehand.
|
||||
</string>
|
||||
<string name="sync_drive_confirm_title"
|
||||
translation_description="The dialog title to confirm sync to Google Drive.">
|
||||
Sync with Google Drive?
|
||||
</string>
|
||||
<string name="sync_drive_enable_message"
|
||||
translation_description="The dialog message to enable sync to Google Drive.">
|
||||
Do you want to sync your tracks to Google Drive? This will allow you to view your tracks in
|
||||
Google Drive and view the tracks shared with you. All tracks synced to Google Drive will be
|
||||
private by default.
|
||||
</string>
|
||||
<string name="sync_drive_title"
|
||||
translation_description="The dialog title to sync to Google Drive.">
|
||||
<string name="sync_drive_enable_title"
|
||||
translation_description="The dialog title after the track is saved.">
|
||||
Track Saved
|
||||
</string>
|
||||
|
||||
|
||||
@@ -16,12 +16,8 @@
|
||||
|
||||
package com.google.android.apps.mytracks;
|
||||
|
||||
import com.google.android.apps.mytracks.fragments.ChooseAccountDialogFragment;
|
||||
import com.google.android.apps.mytracks.fragments.ChooseAccountDialogFragment.ChooseAccountCaller;
|
||||
import com.google.android.apps.mytracks.fragments.ConfirmDeleteDialogFragment;
|
||||
import com.google.android.apps.mytracks.fragments.ConfirmDeleteDialogFragment.ConfirmDeleteCaller;
|
||||
import com.google.android.apps.mytracks.fragments.ConfirmSyncDialogFragment;
|
||||
import com.google.android.apps.mytracks.fragments.ConfirmSyncDialogFragment.ConfirmSyncCaller;
|
||||
import com.google.android.apps.mytracks.fragments.ExportDialogFragment.ExportType;
|
||||
import com.google.android.apps.mytracks.fragments.InstallEarthDialogFragment;
|
||||
import com.google.android.apps.mytracks.fragments.ShareTrackDialogFragment;
|
||||
@@ -69,14 +65,15 @@ import java.io.IOException;
|
||||
* {@link SearchListActivity}:
|
||||
* <p>
|
||||
* - share track <br>
|
||||
* - delete track <br>
|
||||
* - play track <br>
|
||||
* - export track to Google services<br>
|
||||
* - enable sync to Google Drive <br>
|
||||
* - delete tracks <br>
|
||||
* - play tracks
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActivity implements
|
||||
ChooseAccountCaller, ConfirmSyncCaller, CheckPermissionCaller, ShareTrackCaller,
|
||||
ConfirmDeleteCaller {
|
||||
public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActivity
|
||||
implements CheckPermissionCaller, ShareTrackCaller, ConfirmDeleteCaller {
|
||||
|
||||
private static final String TAG = AbstractMyTracksActivity.class.getSimpleName();
|
||||
private static final String SEND_REQUEST_KEY = "send_request_key";
|
||||
@@ -170,7 +167,7 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
|
||||
sendRequest.setDriveSharePublic(makePublic);
|
||||
sendRequest.setDriveShareEmails(emails);
|
||||
sendRequest.setAccount(account);
|
||||
onConfirmSyncDone(true);
|
||||
checkPermissions();
|
||||
}
|
||||
|
||||
protected void exportTrackToGoogle(long trackId, ExportType exportType, Account account) {
|
||||
@@ -195,60 +192,47 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
|
||||
}
|
||||
AnalyticsUtils.sendPageViews(this, pageView);
|
||||
sendRequest.setAccount(account);
|
||||
onConfirmSyncDone(true);
|
||||
checkPermissions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request to Google.
|
||||
*
|
||||
* @param request the request
|
||||
* Enables Google Drive sync.
|
||||
*/
|
||||
protected void sendToGoogle(SendRequest request) {
|
||||
sendRequest = request;
|
||||
new ChooseAccountDialogFragment().show(
|
||||
getSupportFragmentManager(), ChooseAccountDialogFragment.CHOOSE_ACCOUNT_DIALOG_TAG);
|
||||
protected void enableSync(Account account) {
|
||||
sendRequest = new SendRequest(-1L);
|
||||
sendRequest.setSendDrive(true);
|
||||
sendRequest.setDriveSync(true);
|
||||
sendRequest.setAccount(account);
|
||||
checkPermissions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChooseAccountDone(String account) {
|
||||
String googleAccount = PreferencesUtils.getString(
|
||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
if (googleAccount == null || googleAccount.equals(PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT)) {
|
||||
return;
|
||||
}
|
||||
sendRequest.setAccount(new Account(googleAccount, Constants.ACCOUNT_TYPE));
|
||||
|
||||
if (sendRequest.isSendDrive() && sendRequest.isDriveSync()
|
||||
&& sendRequest.isDriveSyncConfirm()) {
|
||||
new ConfirmSyncDialogFragment().show(
|
||||
getSupportFragmentManager(), ConfirmSyncDialogFragment.CONFIRM_SYNC_DIALOG_TAG);
|
||||
/**
|
||||
* Checks permissions to needed Google services.
|
||||
*/
|
||||
private void checkPermissions() {
|
||||
// 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) {
|
||||
startCheckPermission(SendToGoogleUtils.DRIVE_SCOPE);
|
||||
} else {
|
||||
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) {
|
||||
startCheckPermission(SendToGoogleUtils.DRIVE_SCOPE);
|
||||
} else {
|
||||
onDrivePermissionSuccess();
|
||||
}
|
||||
onDrivePermissionSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts checking permission for a Google service.
|
||||
*
|
||||
* @param scope the service scope
|
||||
*/
|
||||
private void startCheckPermission(String scope) {
|
||||
asyncTask = new CheckPermissionAsyncTask(this, sendRequest.getAccount().name, scope);
|
||||
asyncTask.execute();
|
||||
|
||||
@@ -19,6 +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.ChooseAccountDialogFragment;
|
||||
import com.google.android.apps.mytracks.fragments.ChooseAccountDialogFragment.ChooseAccountCaller;
|
||||
import com.google.android.apps.mytracks.fragments.ConfirmSyncDialogFragment;
|
||||
import com.google.android.apps.mytracks.fragments.ConfirmSyncDialogFragment.ConfirmSyncCaller;
|
||||
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;
|
||||
@@ -28,7 +32,6 @@ import com.google.android.apps.mytracks.fragments.PlayMultipleDialogFragment.Pla
|
||||
import com.google.android.apps.mytracks.io.file.TrackFileFormat;
|
||||
import com.google.android.apps.mytracks.io.file.exporter.SaveActivity;
|
||||
import com.google.android.apps.mytracks.io.file.importer.ImportActivity;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
|
||||
import com.google.android.apps.mytracks.io.sync.SyncUtils;
|
||||
import com.google.android.apps.mytracks.services.ITrackRecordingService;
|
||||
import com.google.android.apps.mytracks.services.MyTracksLocationManager;
|
||||
@@ -51,6 +54,7 @@ import com.google.android.gms.common.GooglePlayServicesUtil;
|
||||
import com.google.android.maps.mytracks.BuildConfig;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.app.Dialog;
|
||||
import android.app.SearchManager;
|
||||
import android.content.Context;
|
||||
@@ -90,7 +94,7 @@ import java.util.Locale;
|
||||
* @author Leif Hendrik Wilden
|
||||
*/
|
||||
public class TrackListActivity extends AbstractSendToGoogleActivity
|
||||
implements EulaCaller, FileTypeCaller, PlayMultipleCaller {
|
||||
implements EulaCaller, FileTypeCaller, PlayMultipleCaller, ChooseAccountCaller, ConfirmSyncCaller {
|
||||
|
||||
private static final String TAG = TrackListActivity.class.getSimpleName();
|
||||
private static final String[] PROJECTION = new String[] { TracksColumns._ID, TracksColumns.NAME,
|
||||
@@ -539,13 +543,8 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
|
||||
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);
|
||||
new ChooseAccountDialogFragment().show(
|
||||
getSupportFragmentManager(), ChooseAccountDialogFragment.CHOOSE_ACCOUNT_DIALOG_TAG);
|
||||
}
|
||||
return true;
|
||||
case R.id.track_list_aggregated_statistics:
|
||||
@@ -795,4 +794,24 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
|
||||
public void onPlayMultipleDone(long[] trackIds) {
|
||||
playTracks(trackIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChooseAccountDone(String account) {
|
||||
PreferencesUtils.setString(this, R.string.google_account_key, account);
|
||||
if (PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT.equals(account)) {
|
||||
return;
|
||||
} else {
|
||||
new ConfirmSyncDialogFragment().show(
|
||||
getSupportFragmentManager(), ConfirmSyncDialogFragment.CONFIRM_SYNC_DIALOG_TAG);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfirmSyncDone(boolean enable) {
|
||||
if (enable) {
|
||||
String googleAccount = PreferencesUtils.getString(
|
||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
enableSync(new Account(googleAccount, Constants.ACCOUNT_TYPE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-13
@@ -24,7 +24,6 @@ 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.FragmentActivity;
|
||||
|
||||
/**
|
||||
@@ -62,17 +61,6 @@ public class ConfirmSyncDialogFragment extends AbstractMyTracksDialogFragment {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (PreferencesUtils.getBoolean(
|
||||
getActivity(), R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT)) {
|
||||
dismiss();
|
||||
caller.onConfirmSyncDone(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Dialog createDialog() {
|
||||
FragmentActivity fragmentActivity = getActivity();
|
||||
@@ -91,7 +79,7 @@ public class ConfirmSyncDialogFragment extends AbstractMyTracksDialogFragment {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
caller.onConfirmSyncDone(true);
|
||||
}
|
||||
}).setTitle(R.string.sync_drive_title).create();
|
||||
}).setTitle(R.string.sync_drive_confirm_title).create();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-2
@@ -25,7 +25,7 @@ import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
|
||||
/**
|
||||
* A DialogFrament to enable sync to Google Drive.
|
||||
* A DialogFrament to prompt user to sync to Google Drive after saving first track.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
@@ -72,7 +72,7 @@ public class EnableSyncDialogFragment extends AbstractMyTracksDialogFragment {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
caller.onEnableSyncDone(true);
|
||||
}
|
||||
}).setTitle(R.string.sync_drive_title).create();
|
||||
}).setTitle(R.string.sync_drive_enable_title).create();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -37,7 +37,6 @@ public class SendRequest implements Parcelable {
|
||||
private boolean sendSpreadsheets = false;
|
||||
|
||||
private boolean driveSync = false; // to enable Drive sync
|
||||
private boolean driveSyncConfirm = false;
|
||||
private boolean driveSharePublic = false; // for driveShare, share as public
|
||||
private String driveShareEmails = null; // for driveShare, emails to share
|
||||
|
||||
@@ -102,14 +101,6 @@ public class SendRequest implements Parcelable {
|
||||
this.driveSync = driveSync;
|
||||
}
|
||||
|
||||
public boolean isDriveSyncConfirm() {
|
||||
return driveSyncConfirm;
|
||||
}
|
||||
|
||||
public void setDriveSyncConfirm(boolean driveSyncConfirm) {
|
||||
this.driveSyncConfirm = driveSyncConfirm;
|
||||
}
|
||||
|
||||
public boolean isDriveSharePublic() {
|
||||
return driveSharePublic;
|
||||
}
|
||||
@@ -181,7 +172,6 @@ public class SendRequest implements Parcelable {
|
||||
sendFusionTables = in.readByte() == 1;
|
||||
sendSpreadsheets = in.readByte() == 1;
|
||||
driveSync = in.readByte() == 1;
|
||||
driveSyncConfirm = in.readByte() == 1;
|
||||
driveSharePublic = in.readByte() == 1;
|
||||
driveShareEmails = in.readString();
|
||||
account = in.readParcelable(null);
|
||||
@@ -205,7 +195,6 @@ 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) (driveSharePublic ? 1 : 0));
|
||||
out.writeString(driveShareEmails);
|
||||
out.writeParcelable(account, 0);
|
||||
|
||||
@@ -162,7 +162,7 @@ public class SettingsActivity extends AbstractSettingsActivity implements CheckP
|
||||
});
|
||||
break;
|
||||
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
|
||||
dialog = DialogUtils.createConfirmationDialog(this, R.string.sync_drive_title,
|
||||
dialog = DialogUtils.createConfirmationDialog(this, R.string.sync_drive_confirm_title,
|
||||
getString(R.string.sync_drive_confirm_message), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface d, int button) {
|
||||
|
||||
+1
-10
@@ -128,7 +128,6 @@ 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);
|
||||
Account account = new Account(ACCOUNTNAME, ACCOUNTYPE);
|
||||
parcel.writeParcelable(account, 0);
|
||||
@@ -145,7 +144,6 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
assertTrue(sendRequest.isSendFusionTables());
|
||||
assertTrue(sendRequest.isSendSpreadsheets());
|
||||
assertTrue(sendRequest.isDriveSync());
|
||||
assertTrue(sendRequest.isDriveSyncConfirm());
|
||||
assertTrue(sendRequest.isDriveSharePublic());
|
||||
assertEquals(DRIVE_SHARE_EMAILS, sendRequest.getDriveShareEmails());
|
||||
assertEquals(account, sendRequest.getAccount());
|
||||
@@ -169,7 +167,6 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
parcel.writeByte((byte) 0);
|
||||
parcel.writeByte((byte) 0);
|
||||
parcel.writeByte((byte) 0);
|
||||
parcel.writeByte((byte) 0);
|
||||
parcel.writeString(null);
|
||||
Account account = new Account(ACCOUNTNAME, ACCOUNTYPE);
|
||||
parcel.writeParcelable(account, 0);
|
||||
@@ -186,8 +183,7 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
assertFalse(sendRequest.isSendFusionTables());
|
||||
assertFalse(sendRequest.isSendSpreadsheets());
|
||||
assertFalse(sendRequest.isDriveSync());
|
||||
assertFalse(sendRequest.isDriveSyncConfirm());
|
||||
assertFalse(sendRequest.isDriveSharePublic());
|
||||
assertFalse(sendRequest.isDriveSharePublic());
|
||||
assertNull(sendRequest.getDriveShareEmails());
|
||||
assertEquals(account, sendRequest.getAccount());
|
||||
assertFalse(sendRequest.isDriveSuccess());
|
||||
@@ -212,7 +208,6 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
boolean sendFusionTables = parcel.readByte() == 1;
|
||||
boolean sendSpreadsheets = parcel.readByte() == 1;
|
||||
boolean driveSync = parcel.readByte() == 1;
|
||||
boolean driveSyncConfirm = parcel.readByte() == 1;
|
||||
boolean driveSharePublic = parcel.readByte() == 1;
|
||||
String dirveShareEmails = parcel.readString();
|
||||
Parcelable account = parcel.readParcelable(null);
|
||||
@@ -227,7 +222,6 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
assertFalse(sendFusionTables);
|
||||
assertFalse(sendSpreadsheets);
|
||||
assertFalse(driveSync);
|
||||
assertFalse(driveSyncConfirm);
|
||||
assertFalse(driveSharePublic);
|
||||
assertNull(dirveShareEmails);
|
||||
assertNull(account);
|
||||
@@ -248,7 +242,6 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
sendRequest.setSendFusionTables(true);
|
||||
sendRequest.setSendSpreadsheets(true);
|
||||
sendRequest.setDriveSync(true);
|
||||
sendRequest.setDriveSyncConfirm(true);
|
||||
sendRequest.setDriveSharePublic(true);
|
||||
sendRequest.setDriveShareEmails(DRIVE_SHARE_EMAILS);
|
||||
Account accountNew = new Account(ACCOUNTNAME + "2", ACCOUNTYPE + "2");
|
||||
@@ -268,7 +261,6 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
boolean sendFusionTables = parcel.readByte() == 1;
|
||||
boolean sendSpreadsheets = parcel.readByte() == 1;
|
||||
boolean driveSync = parcel.readByte() == 1;
|
||||
boolean driveSyncConfirm = parcel.readByte() == 1;
|
||||
boolean driveSharePublic = parcel.readByte() == 1;
|
||||
String driveShareEmails = parcel.readString();
|
||||
Parcelable account = parcel.readParcelable(null);
|
||||
@@ -283,7 +275,6 @@ public class SendRequestTest extends AndroidTestCase {
|
||||
assertTrue(sendFusionTables);
|
||||
assertTrue(sendSpreadsheets);
|
||||
assertTrue(driveSync);
|
||||
assertTrue(driveSyncConfirm);
|
||||
assertTrue(driveSharePublic);
|
||||
assertEquals(DRIVE_SHARE_EMAILS, driveShareEmails);
|
||||
assertEquals(accountNew, account);
|
||||
|
||||
+1
-1
@@ -343,7 +343,7 @@ public class EndToEndTestUtils {
|
||||
if ((getButtonOnScreen(activityMytracks.getString(R.string.eula_accept), false, false) != null)) {
|
||||
getButtonOnScreen(activityMytracks.getString(R.string.eula_accept), true, true);
|
||||
}
|
||||
if (SOLO.waitForText(activityMytracks.getString(R.string.sync_drive_title))) {
|
||||
if (SOLO.waitForText(activityMytracks.getString(R.string.sync_drive_confirm_title))) {
|
||||
// Click for Sync with Google Drive(Don't enable sync as default).
|
||||
getButtonOnScreen(activityMytracks.getString(R.string.generic_no), true, true);
|
||||
instrumentation.waitForIdleSync();
|
||||
|
||||
Reference in New Issue
Block a user