Code cleanup: Cleanup method order in AbstractSendToGoogleActivity.

This commit is contained in:
Jimmy Shih
2013-06-24 14:37:31 -07:00
parent 2d4cabf4ef
commit d8e0d4ab73
2 changed files with 131 additions and 141 deletions
@@ -20,8 +20,8 @@ import com.google.android.apps.mytracks.fragments.CheckPermissionFragment;
import com.google.android.apps.mytracks.fragments.CheckPermissionFragment.CheckPermissionCaller;
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.ConfirmPlayDialogFragment;
import com.google.android.apps.mytracks.fragments.ConfirmPlayDialogFragment.ConfirmPlayCaller;
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;
@@ -67,19 +67,13 @@ import java.io.IOException;
* @author Jimmy Shih
*/
public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActivity implements
ChooseAccountCaller, ConfirmSyncCaller, CheckPermissionCaller, ShareTrackCaller, ConfirmCaller {
ChooseAccountCaller, ConfirmSyncCaller, CheckPermissionCaller, ShareTrackCaller, ConfirmPlayCaller {
private static final String TAG = AbstractMyTracksActivity.class.getSimpleName();
private static final String SEND_REQUEST_KEY = "send_request_key";
private SendRequest sendRequest;
public void sendToGoogle(SendRequest request) {
sendRequest = request;
new ChooseAccountDialogFragment().show(
getSupportFragmentManager(), ChooseAccountDialogFragment.CHOOSE_ACCOUNT_DIALOG_TAG);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -94,6 +88,100 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
outState.putParcelable(SEND_REQUEST_KEY, sendRequest);
}
/**
* Share a track.
*
* @param trackId the track id
*/
protected void shareTrack(long trackId) {
AnalyticsUtils.sendPageViews(this, "/action/share_drive");
SendRequest newRequest;
newRequest = new SendRequest(trackId);
newRequest.setSendDrive(true);
newRequest.setDriveShare(true);
sendToGoogle(newRequest);
}
/**
* Sends a request to Google.
*
* @param request the request
*/
protected void sendToGoogle(SendRequest request) {
sendRequest = request;
new ChooseAccountDialogFragment().show(
getSupportFragmentManager(), ChooseAccountDialogFragment.CHOOSE_ACCOUNT_DIALOG_TAG);
}
@Override
public void onChooseAccountDone() {
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);
} 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) {
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) {
if (scope.equals(SendToGoogleUtils.DRIVE_SCOPE)) {
onDrivePermissionSuccess();
} else if (scope.equals(SendToGoogleUtils.FUSION_TABLES_SCOPE)) {
onFusionTablesSuccess();
} else {
onSpreadsheetsPermissionSuccess();
}
} else {
if (intent != null) {
int requestCode;
if (scope.equals(SendToGoogleUtils.DRIVE_SCOPE)) {
requestCode = SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE;
} else if (scope.equals(SendToGoogleUtils.FUSION_TABLES_SCOPE)) {
requestCode = SendToGoogleUtils.FUSION_TABLES_PERMISSION_REQUEST_CODE;
} else {
requestCode = SendToGoogleUtils.SPREADSHEET_PERMISSION_REQUEST_CODE;
}
startActivityForResult(intent, requestCode);
} else {
onPermissionFailure();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
@@ -126,83 +214,6 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
}
}
public void onChooseAccountDone() {
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);
} 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) {
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) {
if (scope.equals(SendToGoogleUtils.DRIVE_SCOPE)) {
onDrivePermissionSuccess();
} else if (scope.equals(SendToGoogleUtils.FUSION_TABLES_SCOPE)) {
onFusionTablesSuccess();
} else {
onSpreadsheetsPermissionSuccess();
}
} else {
if (intent != null) {
int requestCode;
if (scope.equals(SendToGoogleUtils.DRIVE_SCOPE)) {
requestCode = SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE;
} else if (scope.equals(SendToGoogleUtils.FUSION_TABLES_SCOPE)) {
requestCode = SendToGoogleUtils.FUSION_TABLES_PERMISSION_REQUEST_CODE;
} else {
requestCode = SendToGoogleUtils.SPREADSHEET_PERMISSION_REQUEST_CODE;
}
startActivityForResult(intent, requestCode);
} else {
onPermissionFailure();
}
}
}
@Override
public void onShareTrackDone(String emails, boolean makePublic) {
sendRequest.setDriveShareEmails(emails);
sendRequest.setDriveSharePublic(makePublic);
Intent intent = IntentUtils.newIntent(this, SendDriveActivity.class)
.putExtra(SendRequest.SEND_REQUEST_KEY, sendRequest);
startActivity(intent);
}
private void onDrivePermissionSuccess() {
// Check Maps permission
if (sendRequest.isSendMaps()) {
@@ -327,6 +338,15 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
Toast.makeText(this, R.string.send_google_no_account_permission, Toast.LENGTH_LONG).show();
}
@Override
public void onShareTrackDone(String emails, boolean makePublic) {
sendRequest.setDriveShareEmails(emails);
sendRequest.setDriveSharePublic(makePublic);
Intent intent = IntentUtils.newIntent(this, SendDriveActivity.class)
.putExtra(SendRequest.SEND_REQUEST_KEY, sendRequest);
startActivity(intent);
}
/**
* Confirm playing tracks in Google Earth.
*
@@ -334,42 +354,26 @@ public abstract class AbstractSendToGoogleActivity extends AbstractMyTracksActiv
*/
protected void confirmPlay(long[] trackIds) {
if (GoogleEarthUtils.isEarthInstalled(this)) {
ConfirmDialogFragment.newInstance(R.string.confirm_play_earth_key,
PreferencesUtils.CONFIRM_PLAY_EARTH_DEFAULT,
getString(R.string.track_detail_play_confirm_message), trackIds)
.show(getSupportFragmentManager(), ConfirmDialogFragment.CONFIRM_DIALOG_TAG);
if (PreferencesUtils.getBoolean(
this, R.string.confirm_play_earth_key, PreferencesUtils.CONFIRM_PLAY_EARTH_DEFAULT)) {
ConfirmPlayDialogFragment.newInstance(trackIds)
.show(getSupportFragmentManager(), ConfirmPlayDialogFragment.CONFIRM_PLAY_DIALOG_TAG);
} else {
onConfirmPlayDone(trackIds);
}
} else {
new InstallEarthDialogFragment().show(
getSupportFragmentManager(), InstallEarthDialogFragment.INSTALL_EARTH_DIALOG_TAG);
}
}
/**
* Confirm sharing a track.
*
* @param trackId the track id
*/
protected void shareTrack(long trackId) {
AnalyticsUtils.sendPageViews(this, "/action/share_drive");
SendRequest newRequest;
newRequest = new SendRequest(trackId);
newRequest.setSendDrive(true);
newRequest.setDriveShare(true);
sendToGoogle(newRequest);
}
@Override
public void onConfirmDone(int confirmId, long[] trackIds) {
switch (confirmId) {
case R.string.confirm_play_earth_key:
AnalyticsUtils.sendPageViews(this, "/action/play");
public void onConfirmPlayDone(long[] trackIds) {
AnalyticsUtils.sendPageViews(this, "/action/play");
Intent intent = IntentUtils.newIntent(this, SaveActivity.class)
.putExtra(SaveActivity.EXTRA_TRACK_IDS, trackIds)
.putExtra(SaveActivity.EXTRA_TRACK_FILE_FORMAT, (Parcelable) TrackFileFormat.KML)
.putExtra(SaveActivity.EXTRA_PLAY_TRACK, true);
startActivity(intent);
break;
default:
}
startActivity(intent);
}
}
@@ -31,89 +31,75 @@ import android.widget.CheckBox;
import android.widget.TextView;
/**
* A DialogFragment to confirm an action.
* A DialogFragment to confirm playing a track.
*
* @author Jimmy Shih
*/
public class ConfirmDialogFragment extends DialogFragment {
public class ConfirmPlayDialogFragment extends DialogFragment {
/**
* Interface for caller of this dialog fragment.
*
* @author Jimmy Shih
*/
public interface ConfirmCaller {
public interface ConfirmPlayCaller {
/**
* Called when confirm is done.
* Called when confirm play is done.
*/
public void onConfirmDone(int confirmId, long[] trackIds);
public void onConfirmPlayDone(long[] trackIds);
}
public static final String CONFIRM_DIALOG_TAG = "confirmDialog";
public static final String CONFIRM_PLAY_DIALOG_TAG = "confirmPlayDialog";
private static final String KEY_CONFIRM_ID = "confirmId";
private static final String KEY_DEFAULT_VALUE = "defaultValue";
private static final String KEY_MESSAGE = "message";
private static final String KEY_TRACK_IDS = "trackIds";
private CheckBox checkBox;
public static ConfirmDialogFragment newInstance(
int confirmId, boolean defaultValue, CharSequence message, long[] trackIds) {
public static ConfirmPlayDialogFragment newInstance(long[] trackIds) {
Bundle bundle = new Bundle();
bundle.putInt(KEY_CONFIRM_ID, confirmId);
bundle.putBoolean(KEY_DEFAULT_VALUE, defaultValue);
bundle.putCharSequence(KEY_MESSAGE, message);
bundle.putLongArray(KEY_TRACK_IDS, trackIds);
ConfirmDialogFragment confirmDialogFragment = new ConfirmDialogFragment();
ConfirmPlayDialogFragment confirmDialogFragment = new ConfirmPlayDialogFragment();
confirmDialogFragment.setArguments(bundle);
return confirmDialogFragment;
}
private ConfirmCaller caller;
private ConfirmPlayCaller caller;
private FragmentActivity fragmentActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
caller = (ConfirmCaller) activity;
caller = (ConfirmPlayCaller) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
activity.toString() + " must implement " + ConfirmCaller.class.getSimpleName());
activity.toString() + " must implement " + ConfirmPlayCaller.class.getSimpleName());
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int confirmId = getArguments().getInt(KEY_CONFIRM_ID);
boolean defaultValue = getArguments().getBoolean(KEY_DEFAULT_VALUE);
fragmentActivity = getActivity();
if (!PreferencesUtils.getBoolean(fragmentActivity, confirmId, defaultValue)) {
long[] trackIds = getArguments().getLongArray(KEY_TRACK_IDS);
dismiss();
caller.onConfirmDone(confirmId, trackIds);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = fragmentActivity.getLayoutInflater().inflate(R.layout.confirm_dialog, null);
TextView textView = (TextView) view.findViewById(R.id.confirm_dialog_message);
textView.setText(getArguments().getCharSequence(KEY_MESSAGE));
textView.setText(R.string.track_detail_play_confirm_message);
checkBox = (CheckBox) view.findViewById(R.id.confirm_dialog_check_box);
return new AlertDialog.Builder(fragmentActivity).setNegativeButton(R.string.generic_no, null)
.setPositiveButton(R.string.generic_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int confirmId = getArguments().getInt(KEY_CONFIRM_ID);
long[] trackIds = getArguments().getLongArray(KEY_TRACK_IDS);
PreferencesUtils.setBoolean(fragmentActivity, confirmId, !checkBox.isChecked());
caller.onConfirmDone(confirmId, trackIds);
PreferencesUtils.setBoolean(
fragmentActivity, R.string.confirm_play_earth_key, !checkBox.isChecked());
caller.onConfirmPlayDone(trackIds);
}
}).setTitle(R.string.generic_confirm_title).setView(view).create();
}