Cleanup code to enable sync to Google Drive

This commit is contained in:
Jimmy Shih
2014-01-08 13:15:58 -08:00
parent 68685828ca
commit 071e33719e
9 changed files with 78 additions and 103 deletions
@@ -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));
}
}
}
@@ -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
@@ -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) {