Use the caller pattern for all DialogFragment.

This commit is contained in:
Jimmy Shih
2013-02-19 17:09:35 -08:00
parent 63319b78f9
commit ae1740cacd
14 changed files with 217 additions and 131 deletions
@@ -17,6 +17,9 @@
package com.google.android.apps.mytracks; package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.fragments.AboutDialogFragment; import com.google.android.apps.mytracks.fragments.AboutDialogFragment;
import com.google.android.apps.mytracks.fragments.AboutDialogFragment.AboutCaller;
import com.google.android.apps.mytracks.fragments.EulaDialogFragment;
import com.google.android.apps.mytracks.fragments.EulaDialogFragment.EulaCaller;
import com.google.android.apps.mytracks.util.StringUtils; import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -30,7 +33,7 @@ import android.widget.TextView;
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class HelpActivity extends AbstractMyTracksActivity { public class HelpActivity extends AbstractMyTracksActivity implements AboutCaller, EulaCaller {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -62,4 +65,15 @@ public class HelpActivity extends AbstractMyTracksActivity {
protected int getLayoutResId() { protected int getLayoutResId() {
return R.layout.help; return R.layout.help;
} }
@Override
public void onAboutLicense() {
EulaDialogFragment.newInstance(true)
.show(getSupportFragmentManager(), EulaDialogFragment.EULA_DIALOG_TAG);
}
@Override
public void onEulaDone() {
// Do nothing
}
} }
@@ -29,7 +29,9 @@ import com.google.android.apps.mytracks.fragments.DeleteAllTrackDialogFragment;
import com.google.android.apps.mytracks.fragments.DeleteOneTrackDialogFragment; import com.google.android.apps.mytracks.fragments.DeleteOneTrackDialogFragment;
import com.google.android.apps.mytracks.fragments.DeleteOneTrackDialogFragment.DeleteOneTrackCaller; import com.google.android.apps.mytracks.fragments.DeleteOneTrackDialogFragment.DeleteOneTrackCaller;
import com.google.android.apps.mytracks.fragments.EulaDialogFragment; 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; import com.google.android.apps.mytracks.fragments.WelcomeDialogFragment;
import com.google.android.apps.mytracks.fragments.WelcomeDialogFragment.WelcomeCaller;
import com.google.android.apps.mytracks.io.file.ImportActivity; import com.google.android.apps.mytracks.io.file.ImportActivity;
import com.google.android.apps.mytracks.io.file.SaveActivity; import com.google.android.apps.mytracks.io.file.SaveActivity;
import com.google.android.apps.mytracks.io.file.TrackFileFormat; import com.google.android.apps.mytracks.io.file.TrackFileFormat;
@@ -91,7 +93,7 @@ import java.util.Locale;
* @author Leif Hendrik Wilden * @author Leif Hendrik Wilden
*/ */
public class TrackListActivity extends AbstractSendToGoogleActivity public class TrackListActivity extends AbstractSendToGoogleActivity
implements ConfirmCaller, DeleteOneTrackCaller { implements EulaCaller, WelcomeCaller, ConfirmCaller, DeleteOneTrackCaller {
private static final String TAG = TrackListActivity.class.getSimpleName(); private static final String TAG = TrackListActivity.class.getSimpleName();
private static final String START_GPS_KEY = "start_gps_key"; private static final String START_GPS_KEY = "start_gps_key";
@@ -690,6 +692,21 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
} }
} }
@Override
public void onEulaDone() {
if (EulaUtils.getAcceptEula(this)) {
showStartupDialogs();
return;
}
finish();
}
@Override
public void onWelcomeDone() {
EulaUtils.setShowWelcome(this);
showStartupDialogs();
}
private void checkGooglePlayServices() { private void checkGooglePlayServices() {
int code = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); int code = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (code != ConnectionResult.SUCCESS) { if (code != ConnectionResult.SUCCESS) {
@@ -19,6 +19,7 @@ package com.google.android.apps.mytracks.fragments;
import com.google.android.apps.mytracks.util.SystemUtils; import com.google.android.apps.mytracks.util.SystemUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -35,27 +36,48 @@ import android.widget.TextView;
*/ */
public class AboutDialogFragment extends DialogFragment { public class AboutDialogFragment extends DialogFragment {
/**
* Interface for caller of this dialog fragment.
*
* @author Jimmy Shih
*/
public interface AboutCaller {
/**
* Called when about license is invoked.
*/
public void onAboutLicense();
}
public static final String ABOUT_DIALOG_TAG = "aboutDialog"; public static final String ABOUT_DIALOG_TAG = "aboutDialog";
private FragmentActivity activity; private AboutCaller caller;
private FragmentActivity fragmentActivity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
caller = (AboutCaller) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
activity.toString() + " must implement " + AboutCaller.class.getSimpleName());
}
}
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = getActivity(); fragmentActivity = getActivity();
View view = activity.getLayoutInflater().inflate(R.layout.about, null); View view = fragmentActivity.getLayoutInflater().inflate(R.layout.about, null);
TextView aboutVersion = (TextView) view.findViewById(R.id.about_version); TextView aboutVersion = (TextView) view.findViewById(R.id.about_version);
aboutVersion.setText(SystemUtils.getMyTracksVersion(activity)); aboutVersion.setText(SystemUtils.getMyTracksVersion(fragmentActivity));
return new AlertDialog.Builder(activity) return new AlertDialog.Builder(fragmentActivity).setNegativeButton(
.setNegativeButton(R.string.about_license, new DialogInterface.OnClickListener() { R.string.about_license, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
EulaDialogFragment.newInstance(true).show( caller.onAboutLicense();
activity.getSupportFragmentManager(), EulaDialogFragment.EULA_DIALOG_TAG);
} }
}) }).setPositiveButton(R.string.generic_ok, null).setTitle(R.string.help_about).setView(view)
.setPositiveButton(R.string.generic_ok, null)
.setTitle(R.string.help_about)
.setView(view)
.create(); .create();
} }
} }
@@ -68,8 +68,9 @@ public class AddEmailsDialogFragment extends DialogFragment {
return addPeopleDialogFragment; return addPeopleDialogFragment;
} }
private MultiAutoCompleteTextView multiAutoCompleteTextView;
private AddEmailsCaller caller; private AddEmailsCaller caller;
private FragmentActivity fragmentActivity;
private MultiAutoCompleteTextView multiAutoCompleteTextView;
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
@@ -84,13 +85,13 @@ public class AddEmailsDialogFragment extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
FragmentActivity activity = getActivity(); fragmentActivity = getActivity();
View view = activity.getLayoutInflater().inflate(R.layout.add_emails, null); View view = fragmentActivity.getLayoutInflater().inflate(R.layout.add_emails, null);
multiAutoCompleteTextView = (MultiAutoCompleteTextView) view.findViewById(R.id.add_emails); multiAutoCompleteTextView = (MultiAutoCompleteTextView) view.findViewById(R.id.add_emails);
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.add_emails_item, SimpleCursorAdapter adapter = new SimpleCursorAdapter(fragmentActivity,
getCursor(getActivity(), null), new String[] { R.layout.add_emails_item, getCursor(fragmentActivity, null), new String[] {
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Email.DATA }, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Email.DATA },
new int[] { android.R.id.text1, android.R.id.text2 }, 0); new int[] { android.R.id.text1, android.R.id.text2 }, 0);
adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
@@ -103,12 +104,13 @@ public class AddEmailsDialogFragment extends DialogFragment {
adapter.setFilterQueryProvider(new FilterQueryProvider() { adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override @Override
public Cursor runQuery(CharSequence constraint) { public Cursor runQuery(CharSequence constraint) {
return getCursor(getActivity(), constraint); return getCursor(fragmentActivity, constraint);
} }
}); });
multiAutoCompleteTextView.setAdapter(adapter); multiAutoCompleteTextView.setAdapter(adapter);
return new AlertDialog.Builder(activity).setNegativeButton(R.string.generic_cancel, null) return new AlertDialog.Builder(fragmentActivity).setNegativeButton(
R.string.generic_cancel, null)
.setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
@@ -29,6 +29,7 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
/** /**
* A DialogFragment to choose an account. * A DialogFragment to choose an account.
@@ -53,6 +54,7 @@ public class ChooseAccountDialogFragment extends DialogFragment {
public static final String CHOOSE_ACCOUNT_DIALOG_TAG = "chooseAccount"; public static final String CHOOSE_ACCOUNT_DIALOG_TAG = "chooseAccount";
private ChooseAccountCaller caller; private ChooseAccountCaller caller;
private FragmentActivity fragmentActivity;
private Account[] accounts; private Account[] accounts;
@Override @Override
@@ -69,17 +71,18 @@ public class ChooseAccountDialogFragment extends DialogFragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
accounts = AccountManager.get(getActivity()).getAccountsByType(Constants.ACCOUNT_TYPE); fragmentActivity = getActivity();
accounts = AccountManager.get(fragmentActivity).getAccountsByType(Constants.ACCOUNT_TYPE);
if (accounts.length == 1) { if (accounts.length == 1) {
PreferencesUtils.setString(getActivity(), R.string.google_account_key, accounts[0].name); PreferencesUtils.setString(fragmentActivity, R.string.google_account_key, accounts[0].name);
dismiss(); dismiss();
caller.onChooseAccountDone(); caller.onChooseAccountDone();
return; return;
} }
String googleAccount = PreferencesUtils.getString( String googleAccount = PreferencesUtils.getString(
getActivity(), R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT); fragmentActivity, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
for (int i = 0; i < accounts.length; i++) { for (int i = 0; i < accounts.length; i++) {
if (accounts[i].name.equals(googleAccount)) { if (accounts[i].name.equals(googleAccount)) {
dismiss(); dismiss();
@@ -92,7 +95,7 @@ public class ChooseAccountDialogFragment extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
if (accounts.length == 0) { if (accounts.length == 0) {
return new AlertDialog.Builder(getActivity()).setMessage( return new AlertDialog.Builder(fragmentActivity).setMessage(
R.string.send_google_no_account_message).setTitle(R.string.send_google_no_account_title) R.string.send_google_no_account_message).setTitle(R.string.send_google_no_account_title)
.setPositiveButton(R.string.generic_ok, null).create(); .setPositiveButton(R.string.generic_ok, null).create();
} }
@@ -100,13 +103,14 @@ public class ChooseAccountDialogFragment extends DialogFragment {
for (int i = 0; i < accounts.length; i++) { for (int i = 0; i < accounts.length; i++) {
choices[i] = accounts[i].name; choices[i] = accounts[i].name;
} }
return new AlertDialog.Builder(getActivity()).setNegativeButton(R.string.generic_cancel, null) return new AlertDialog.Builder(fragmentActivity).setNegativeButton(
R.string.generic_cancel, null)
.setPositiveButton(R.string.generic_ok, new OnClickListener() { .setPositiveButton(R.string.generic_ok, new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition(); int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
PreferencesUtils.setString( PreferencesUtils.setString(
getActivity(), R.string.google_account_key, accounts[position].name); fragmentActivity, R.string.google_account_key, accounts[position].name);
caller.onChooseAccountDone(); caller.onChooseAccountDone();
} }
}).setSingleChoiceItems(choices, 0, null) }).setSingleChoiceItems(choices, 0, null)
@@ -69,7 +69,7 @@ public class ChooseUploadServiceDialogFragment extends DialogFragment {
} }
private ChooseUploadServiceCaller caller; private ChooseUploadServiceCaller caller;
private FragmentActivity activity; private FragmentActivity fragmentActivity;
private boolean hideDrive; private boolean hideDrive;
private CheckBox driveCheckBox; private CheckBox driveCheckBox;
@@ -93,10 +93,10 @@ public class ChooseUploadServiceDialogFragment extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = getActivity(); fragmentActivity = getActivity();
hideDrive = getArguments().getBoolean(KEY_HIDE_DRIVE); hideDrive = getArguments().getBoolean(KEY_HIDE_DRIVE);
View view = activity.getLayoutInflater().inflate(R.layout.choose_upload_service, null); View view = fragmentActivity.getLayoutInflater().inflate(R.layout.choose_upload_service, null);
view.findViewById(R.id.choose_upload_service_drive_options) view.findViewById(R.id.choose_upload_service_drive_options)
.setVisibility(hideDrive ? View.GONE : View.VISIBLE); .setVisibility(hideDrive ? View.GONE : View.VISIBLE);
@@ -107,15 +107,15 @@ public class ChooseUploadServiceDialogFragment extends DialogFragment {
spreadsheetsCheckBox = (CheckBox) view.findViewById(R.id.choose_upload_service_spreadsheets); spreadsheetsCheckBox = (CheckBox) view.findViewById(R.id.choose_upload_service_spreadsheets);
driveCheckBox.setChecked(PreferencesUtils.getBoolean( driveCheckBox.setChecked(PreferencesUtils.getBoolean(
activity, R.string.send_to_drive_key, PreferencesUtils.SEND_TO_DRIVE_DEFAULT)); fragmentActivity, R.string.send_to_drive_key, PreferencesUtils.SEND_TO_DRIVE_DEFAULT));
mapsCheckBox.setChecked(PreferencesUtils.getBoolean( mapsCheckBox.setChecked(PreferencesUtils.getBoolean(
activity, R.string.send_to_maps_key, PreferencesUtils.SEND_TO_MAPS_DEFAULT)); fragmentActivity, R.string.send_to_maps_key, PreferencesUtils.SEND_TO_MAPS_DEFAULT));
fusionTablesCheckBox.setChecked(PreferencesUtils.getBoolean( fusionTablesCheckBox.setChecked(PreferencesUtils.getBoolean(
activity, R.string.send_to_fusion_tables_key, fragmentActivity, R.string.send_to_fusion_tables_key,
PreferencesUtils.SEND_TO_FUSION_TABLES_DEFAULT)); PreferencesUtils.SEND_TO_FUSION_TABLES_DEFAULT));
spreadsheetsCheckBox.setChecked(PreferencesUtils.getBoolean( spreadsheetsCheckBox.setChecked(PreferencesUtils.getBoolean(
activity, R.string.send_to_spreadsheets_key, fragmentActivity, R.string.send_to_spreadsheets_key,
PreferencesUtils.SEND_TO_SPREADSHEETS_DEFAULT)); PreferencesUtils.SEND_TO_SPREADSHEETS_DEFAULT));
mapsCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { mapsCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@@ -129,7 +129,7 @@ public class ChooseUploadServiceDialogFragment extends DialogFragment {
RadioButton newMapRadioButton = (RadioButton) view.findViewById( RadioButton newMapRadioButton = (RadioButton) view.findViewById(
R.id.choose_upload_service_new_map); R.id.choose_upload_service_new_map);
boolean defaultMapPublic = PreferencesUtils.getBoolean( boolean defaultMapPublic = PreferencesUtils.getBoolean(
activity, R.string.default_map_public_key, PreferencesUtils.DEFAULT_MAP_PUBLIC_DEFAULT); fragmentActivity, R.string.default_map_public_key, PreferencesUtils.DEFAULT_MAP_PUBLIC_DEFAULT);
newMapRadioButton.setText(defaultMapPublic ? R.string.send_google_new_public_map newMapRadioButton.setText(defaultMapPublic ? R.string.send_google_new_public_map
: R.string.send_google_new_unlisted_map); : R.string.send_google_new_unlisted_map);
@@ -138,7 +138,7 @@ public class ChooseUploadServiceDialogFragment extends DialogFragment {
updateMapsOption(); updateMapsOption();
if (PreferencesUtils.getBoolean( if (PreferencesUtils.getBoolean(
activity, R.string.pick_existing_map_key, PreferencesUtils.PICK_EXISTING_MAP_DEFAULT)) { fragmentActivity, R.string.pick_existing_map_key, PreferencesUtils.PICK_EXISTING_MAP_DEFAULT)) {
existingMapRadioButton.setChecked(true); existingMapRadioButton.setChecked(true);
} else { } else {
newMapRadioButton.setChecked(true); newMapRadioButton.setChecked(true);
@@ -146,33 +146,33 @@ public class ChooseUploadServiceDialogFragment extends DialogFragment {
// Update fusion table // Update fusion table
boolean defaultTablePublic = PreferencesUtils.getBoolean( boolean defaultTablePublic = PreferencesUtils.getBoolean(
activity, R.string.default_table_public_key, PreferencesUtils.DEFAULT_TABLE_PUBLIC_DEFAULT); fragmentActivity, R.string.default_table_public_key, PreferencesUtils.DEFAULT_TABLE_PUBLIC_DEFAULT);
String text = getString(R.string.send_google_fusion_tables) + "\n" String text = getString(R.string.send_google_fusion_tables) + "\n"
+ getString(defaultTablePublic ? R.string.send_google_fusion_tables_public + getString(defaultTablePublic ? R.string.send_google_fusion_tables_public
: R.string.send_google_fusion_tables_private); : R.string.send_google_fusion_tables_private);
fusionTablesCheckBox.setText(text); fusionTablesCheckBox.setText(text);
return new AlertDialog.Builder(activity).setNegativeButton(R.string.generic_cancel, null) return new AlertDialog.Builder(fragmentActivity).setNegativeButton(R.string.generic_cancel, null)
.setPositiveButton(R.string.send_google_send_now, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.send_google_send_now, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
PreferencesUtils.setBoolean( PreferencesUtils.setBoolean(
activity, R.string.send_to_drive_key, driveCheckBox.isChecked()); fragmentActivity, R.string.send_to_drive_key, driveCheckBox.isChecked());
PreferencesUtils.setBoolean( PreferencesUtils.setBoolean(
activity, R.string.pick_existing_map_key, existingMapRadioButton.isChecked()); fragmentActivity, R.string.pick_existing_map_key, existingMapRadioButton.isChecked());
PreferencesUtils.setBoolean( PreferencesUtils.setBoolean(
activity, R.string.send_to_maps_key, mapsCheckBox.isChecked()); fragmentActivity, R.string.send_to_maps_key, mapsCheckBox.isChecked());
PreferencesUtils.setBoolean( PreferencesUtils.setBoolean(
activity, R.string.send_to_fusion_tables_key, fusionTablesCheckBox.isChecked()); fragmentActivity, R.string.send_to_fusion_tables_key, fusionTablesCheckBox.isChecked());
PreferencesUtils.setBoolean( PreferencesUtils.setBoolean(
activity, R.string.send_to_spreadsheets_key, spreadsheetsCheckBox.isChecked()); fragmentActivity, R.string.send_to_spreadsheets_key, spreadsheetsCheckBox.isChecked());
if (sendDrive() || mapsCheckBox.isChecked() || fusionTablesCheckBox.isChecked() if (sendDrive() || mapsCheckBox.isChecked() || fusionTablesCheckBox.isChecked()
|| spreadsheetsCheckBox.isChecked()) { || spreadsheetsCheckBox.isChecked()) {
caller.onChooseUploadServiceDone(sendDrive(), mapsCheckBox.isChecked(), caller.onChooseUploadServiceDone(sendDrive(), mapsCheckBox.isChecked(),
fusionTablesCheckBox.isChecked(), spreadsheetsCheckBox.isChecked(), fusionTablesCheckBox.isChecked(), spreadsheetsCheckBox.isChecked(),
existingMapRadioButton.isChecked()); existingMapRadioButton.isChecked());
} else { } else {
Toast.makeText(activity, R.string.send_google_no_service_selected, Toast.LENGTH_LONG) Toast.makeText(fragmentActivity, R.string.send_google_no_service_selected, Toast.LENGTH_LONG)
.show(); .show();
} }
} }
@@ -25,6 +25,7 @@ import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View; import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.TextView; import android.widget.TextView;
@@ -72,6 +73,7 @@ public class ConfirmDialogFragment extends DialogFragment {
} }
private ConfirmCaller caller; private ConfirmCaller caller;
private FragmentActivity fragmentActivity;
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
@@ -89,7 +91,8 @@ public class ConfirmDialogFragment extends DialogFragment {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
int confirmId = getArguments().getInt(KEY_CONFIRM_ID); int confirmId = getArguments().getInt(KEY_CONFIRM_ID);
boolean defaultValue = getArguments().getBoolean(KEY_DEFAULT_VALUE); boolean defaultValue = getArguments().getBoolean(KEY_DEFAULT_VALUE);
if (!PreferencesUtils.getBoolean(getActivity(), confirmId, defaultValue)) { fragmentActivity = getActivity();
if (!PreferencesUtils.getBoolean(fragmentActivity, confirmId, defaultValue)) {
long trackId = getArguments().getLong(KEY_TRACK_ID); long trackId = getArguments().getLong(KEY_TRACK_ID);
dismiss(); dismiss();
caller.onConfirmDone(confirmId, trackId); caller.onConfirmDone(confirmId, trackId);
@@ -98,18 +101,19 @@ public class ConfirmDialogFragment extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.confirm_dialog, null); View view = fragmentActivity.getLayoutInflater().inflate(R.layout.confirm_dialog, null);
TextView textView = (TextView) view.findViewById(R.id.confirm_dialog_message); TextView textView = (TextView) view.findViewById(R.id.confirm_dialog_message);
textView.setText(getArguments().getCharSequence(KEY_MESSAGE)); textView.setText(getArguments().getCharSequence(KEY_MESSAGE));
checkBox = (CheckBox) view.findViewById(R.id.confirm_dialog_check_box); checkBox = (CheckBox) view.findViewById(R.id.confirm_dialog_check_box);
return new AlertDialog.Builder(getActivity()).setNegativeButton(android.R.string.cancel, null) return new AlertDialog.Builder(fragmentActivity).setNegativeButton(
android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
int confirmId = getArguments().getInt(KEY_CONFIRM_ID); int confirmId = getArguments().getInt(KEY_CONFIRM_ID);
long trackId = getArguments().getLong(KEY_TRACK_ID); long trackId = getArguments().getLong(KEY_TRACK_ID);
PreferencesUtils.setBoolean(getActivity(), confirmId, !checkBox.isChecked()); PreferencesUtils.setBoolean(fragmentActivity, confirmId, !checkBox.isChecked());
caller.onConfirmDone(confirmId, trackId); caller.onConfirmDone(confirmId, trackId);
} }
}).setTitle(R.string.generic_confirm_title).setView(view).create(); }).setTitle(R.string.generic_confirm_title).setView(view).create();
@@ -37,12 +37,12 @@ public class DeleteAllTrackDialogFragment extends DialogFragment {
public static final String DELETE_ALL_TRACK_DIALOG_TAG = "deleteAllTrackDialog"; public static final String DELETE_ALL_TRACK_DIALOG_TAG = "deleteAllTrackDialog";
private FragmentActivity activity; private FragmentActivity fragmentActivity;
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = getActivity(); fragmentActivity = getActivity();
return DialogUtils.createConfirmationDialog(activity, return DialogUtils.createConfirmationDialog(fragmentActivity,
R.string.track_list_delete_all_confirm_message, new DialogInterface.OnClickListener() { R.string.track_list_delete_all_confirm_message, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
@@ -50,14 +50,14 @@ public class DeleteAllTrackDialogFragment extends DialogFragment {
@Override @Override
public void run() { public void run() {
PreferencesUtils.setBoolean( PreferencesUtils.setBoolean(
activity, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT); fragmentActivity, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
SyncUtils.disableSync(activity); SyncUtils.disableSync(fragmentActivity);
MyTracksProviderUtils.Factory.get(activity).deleteAllTracks(); MyTracksProviderUtils.Factory.get(fragmentActivity).deleteAllTracks();
PreferencesUtils.setLong(activity, R.string.drive_largest_change_id_key, PreferencesUtils.setLong(fragmentActivity, R.string.drive_largest_change_id_key,
PreferencesUtils.DRIVE_LARGEST_CHANGE_ID_DEFAULT); PreferencesUtils.DRIVE_LARGEST_CHANGE_ID_DEFAULT);
// Clear the drive_deleted_list_key last // Clear the drive_deleted_list_key last
PreferencesUtils.setString(activity, R.string.drive_deleted_list_key, PreferencesUtils.setString(fragmentActivity, R.string.drive_deleted_list_key,
PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT); PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
} }
}).start(); }).start();
@@ -62,8 +62,8 @@ public class DeleteOneMarkerDialogFragment extends DialogFragment {
return deleteOneMarkerDialogFragment; return deleteOneMarkerDialogFragment;
} }
private FragmentActivity fragmentActivity;
private DeleteOneMarkerCaller caller; private DeleteOneMarkerCaller caller;
private FragmentActivity fragmentActivity;
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
@@ -67,8 +67,8 @@ public class DeleteOneTrackDialogFragment extends DialogFragment {
return deleteOneTrackDialogFragment; return deleteOneTrackDialogFragment;
} }
private FragmentActivity fragmentActivity;
private DeleteOneTrackCaller caller; private DeleteOneTrackCaller caller;
private FragmentActivity fragmentActivity;
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
@@ -16,11 +16,11 @@
package com.google.android.apps.mytracks.fragments; package com.google.android.apps.mytracks.fragments;
import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.util.EulaUtils; import com.google.android.apps.mytracks.util.EulaUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -37,6 +37,19 @@ import java.util.Locale;
*/ */
public class EulaDialogFragment extends DialogFragment { public class EulaDialogFragment extends DialogFragment {
/**
* Interface for caller of this dialog fragment.
*
* @author Jimmy Shih
*/
public interface EulaCaller {
/**
* Called when eula is done.
*/
public void onEulaDone();
}
public static final String EULA_DIALOG_TAG = "eulaDialog"; public static final String EULA_DIALOG_TAG = "eulaDialog";
private static final String KEY_HAS_ACCEPTED = "hasAccepted"; private static final String KEY_HAS_ACCEPTED = "hasAccepted";
private static final String GOOGLE_URL = "m.google.com"; private static final String GOOGLE_URL = "m.google.com";
@@ -56,65 +69,60 @@ public class EulaDialogFragment extends DialogFragment {
return eulaDialogFragment; return eulaDialogFragment;
} }
private FragmentActivity activity; private EulaCaller caller;
private FragmentActivity fragmentActivity;
@Override @Override
public void onCancel(DialogInterface arg0) { public void onAttach(Activity activity) {
if (!getArguments().getBoolean(KEY_HAS_ACCEPTED)) { super.onAttach(activity);
exitApp(); try {
caller = (EulaCaller) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
activity.toString() + " must implement " + EulaCaller.class.getSimpleName());
} }
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = getActivity(); fragmentActivity = getActivity();
boolean hasAccepted = getArguments().getBoolean(KEY_HAS_ACCEPTED); boolean hasAccepted = getArguments().getBoolean(KEY_HAS_ACCEPTED);
AlertDialog.Builder builder = new AlertDialog.Builder(activity) AlertDialog.Builder builder = new AlertDialog.Builder(fragmentActivity).setMessage(
.setMessage(getEulaText()) getEulaText()).setTitle(R.string.eula_title);
.setTitle(R.string.eula_title);
if (hasAccepted) { if (hasAccepted) {
builder.setPositiveButton(R.string.generic_ok, null); builder.setPositiveButton(R.string.generic_ok, null);
} else { } else {
builder.setNegativeButton(R.string.eula_decline, new DialogInterface.OnClickListener() { builder.setNegativeButton(R.string.eula_decline, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
exitApp(); caller.onEulaDone();
} }
}) }).setPositiveButton(R.string.eula_accept, new DialogInterface.OnClickListener() {
.setPositiveButton(R.string.eula_accept, new DialogInterface.OnClickListener() { @Override
@Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
EulaUtils.setAcceptEula(activity); EulaUtils.setAcceptEula(fragmentActivity);
PreferencesUtils.setBoolean( PreferencesUtils.setBoolean(
activity, R.string.metric_units_key, !Locale.US.equals(Locale.getDefault())); fragmentActivity, R.string.metric_units_key, !Locale.US.equals(Locale.getDefault()));
TrackListActivity trackListActivity = (TrackListActivity) activity; caller.onEulaDone();
trackListActivity.showStartupDialogs();
} }
}); });
} }
return builder.create(); return builder.create();
} }
/** @Override
* Exits the application. public void onCancel(DialogInterface arg0) {
*/ caller.onEulaDone();
private void exitApp() {
activity.finish();
} }
/** /**
* Gets the EULA text. * Gets the EULA text.
*
*/ */
private String getEulaText() { private String getEulaText() {
String tos = getString(R.string.eula_date) String tos = getString(R.string.eula_date) + "\n\n" + getString(R.string.eula_body, GOOGLE_URL)
+ "\n\n" + "\n\n" + getString(R.string.eula_footer, GOOGLE_URL) + "\n\n"
+ getString(R.string.eula_body, GOOGLE_URL)
+ "\n\n"
+ getString(R.string.eula_footer, GOOGLE_URL)
+ "\n\n"
+ getString(R.string.eula_copyright_year); + getString(R.string.eula_copyright_year);
boolean isKorean = getResources().getConfiguration().locale.getLanguage().equals(KOREAN); boolean isKorean = getResources().getConfiguration().locale.getLanguage().equals(KOREAN);
if (isKorean) { if (isKorean) {
@@ -52,23 +52,23 @@ public class FrequencyDialogFragment extends DialogFragment {
return frequencyDialogFragment; return frequencyDialogFragment;
} }
private FragmentActivity activity; private FragmentActivity fragmentActivity;
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = getActivity(); fragmentActivity = getActivity();
final int preferenceId = getArguments().getInt(KEY_PREFERENCE_ID); final int preferenceId = getArguments().getInt(KEY_PREFERENCE_ID);
int defaultValue = getArguments().getInt(KEY_DEFAULT_VALUE); int defaultValue = getArguments().getInt(KEY_DEFAULT_VALUE);
int titleId = getArguments().getInt(KEY_TITLE_ID); int titleId = getArguments().getInt(KEY_TITLE_ID);
int frequencyValue = PreferencesUtils.getInt(activity, preferenceId, defaultValue); int frequencyValue = PreferencesUtils.getInt(fragmentActivity, preferenceId, defaultValue);
return new AlertDialog.Builder(activity) return new AlertDialog.Builder(fragmentActivity)
.setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
int listIndex = ((AlertDialog) dialog).getListView().getCheckedItemPosition(); int listIndex = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
PreferencesUtils.setInt(activity, preferenceId, getFrequencyValue(listIndex)); PreferencesUtils.setInt(fragmentActivity, preferenceId, getFrequencyValue(listIndex));
} }
}) })
.setSingleChoiceItems(getFrequencyDisplayOptions(), getListIndex(frequencyValue), null) .setSingleChoiceItems(getFrequencyDisplayOptions(), getListIndex(frequencyValue), null)
@@ -81,8 +81,8 @@ public class FrequencyDialogFragment extends DialogFragment {
*/ */
private String[] getFrequencyDisplayOptions() { private String[] getFrequencyDisplayOptions() {
boolean metricUnits = PreferencesUtils.getBoolean( boolean metricUnits = PreferencesUtils.getBoolean(
activity, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT); fragmentActivity, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT);
return StringUtils.getFrequencyOptions(activity, metricUnits); return StringUtils.getFrequencyOptions(fragmentActivity, metricUnits);
} }
/** /**
@@ -39,12 +39,12 @@ public class InstallEarthDialogFragment extends DialogFragment {
public static final String INSTALL_EARTH_DIALOG_TAG = "installEarthDialog"; public static final String INSTALL_EARTH_DIALOG_TAG = "installEarthDialog";
private FragmentActivity activity; private FragmentActivity fragmentActivity;
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = getActivity(); fragmentActivity = getActivity();
return new AlertDialog.Builder(activity) return new AlertDialog.Builder(fragmentActivity)
.setMessage(R.string.track_detail_install_earth_message) .setMessage(R.string.track_detail_install_earth_message)
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@@ -55,7 +55,7 @@ public class InstallEarthDialogFragment extends DialogFragment {
startActivity(intent); startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
Toast.makeText( Toast.makeText(
activity, R.string.track_detail_install_earth_error, Toast.LENGTH_LONG) fragmentActivity, R.string.track_detail_install_earth_error, Toast.LENGTH_LONG)
.show(); .show();
} }
} }
@@ -16,10 +16,9 @@
package com.google.android.apps.mytracks.fragments; package com.google.android.apps.mytracks.fragments;
import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.util.EulaUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
@@ -34,33 +33,49 @@ import android.support.v4.app.FragmentActivity;
*/ */
public class WelcomeDialogFragment extends DialogFragment { public class WelcomeDialogFragment extends DialogFragment {
/**
* Interface for caller of this dialog fragment.
*
* @author Jimmy Shih
*/
public interface WelcomeCaller {
/**
* Called when choose account is done.
*/
public void onWelcomeDone();
}
public static final String WELCOME_DIALOG_TAG = "welcomeDialog"; public static final String WELCOME_DIALOG_TAG = "welcomeDialog";
private FragmentActivity activity; private WelcomeCaller caller;
private FragmentActivity fragmentActivity;
@Override @Override
public void onCancel(DialogInterface arg0) { public void onAttach(Activity activity) {
onDone(); super.onAttach(activity);
try {
caller = (WelcomeCaller) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
activity.toString() + " must implement " + WelcomeCaller.class.getSimpleName());
}
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
activity = getActivity(); fragmentActivity = getActivity();
return new AlertDialog.Builder(activity) return new AlertDialog.Builder(fragmentActivity).setPositiveButton(
.setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() { R.string.generic_ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
onDone(); caller.onWelcomeDone();
} }
}) }).setTitle(R.string.welcome_title).setMessage(R.string.welcome_message).create();
.setTitle(R.string.welcome_title)
.setMessage(R.string.welcome_message)
.create();
} }
private void onDone() { @Override
EulaUtils.setShowWelcome(activity); public void onCancel(DialogInterface arg0) {
TrackListActivity trackListActivity = (TrackListActivity) activity; caller.onWelcomeDone();
trackListActivity.showStartupDialogs();
} }
} }