diff --git a/MyTracks/src/com/google/android/apps/mytracks/fragments/ChooseAccountDialogFragment.java b/MyTracks/src/com/google/android/apps/mytracks/fragments/ChooseAccountDialogFragment.java new file mode 100644 index 000000000..b8818a2df --- /dev/null +++ b/MyTracks/src/com/google/android/apps/mytracks/fragments/ChooseAccountDialogFragment.java @@ -0,0 +1,131 @@ +/* + * 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.apps.mytracks.Constants; +import com.google.android.apps.mytracks.util.PreferencesUtils; +import com.google.android.maps.mytracks.R; + +import android.accounts.Account; +import android.accounts.AccountManager; +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; + +/** + * A DialogFragment to choose an account. + * + * @author Jimmy Shih + */ +public class ChooseAccountDialogFragment extends DialogFragment { + + /** + * Interface for caller of this dialog fragment. + * + * @author Jimmy Shih + */ + public interface ChooseAccountCaller { + + /** + * Called when choose account is done. + */ + public void onChooseAccountDone(); + } + + public static final String CHOOSE_ACCOUNT_DIALOG_TAG = "chooseAccount"; + + private ChooseAccountCaller caller; + private Account[] accounts; + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + caller = (ChooseAccountCaller) activity; + } catch (ClassCastException e) { + throw new ClassCastException( + activity.toString() + " must implement " + ChooseAccountCaller.class.getSimpleName()); + } + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + accounts = AccountManager.get(getActivity()).getAccountsByType(Constants.ACCOUNT_TYPE); + + if (accounts.length == 1) { + PreferencesUtils.setString(getActivity(), R.string.google_account_key, accounts[0].name); + dismiss(); + caller.onChooseAccountDone(); + return; + } + + String googleAccount = PreferencesUtils.getString( + getActivity(), R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT); + for (int i = 0; i < accounts.length; i++) { + if (accounts[i].name.equals(googleAccount)) { + dismiss(); + caller.onChooseAccountDone(); + return; + } + } + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + if (accounts.length == 0) { + return new AlertDialog.Builder(getActivity()).setMessage( + R.string.send_google_no_account_message).setTitle(R.string.send_google_no_account_title) + .setPositiveButton(R.string.generic_ok, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + caller.onChooseAccountDone(); + } + }).create(); + } + String[] choices = new String[accounts.length]; + for (int i = 0; i < accounts.length; i++) { + choices[i] = accounts[i].name; + } + return new AlertDialog.Builder(getActivity()).setNegativeButton( + R.string.generic_cancel, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + caller.onChooseAccountDone(); + } + }).setPositiveButton(R.string.generic_ok, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition(); + PreferencesUtils.setString( + getActivity(), R.string.google_account_key, accounts[position].name); + caller.onChooseAccountDone(); + } + }).setSingleChoiceItems(choices, 0, null).setTitle(R.string.send_google_choose_account_title) + .create(); + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + caller.onChooseAccountDone(); + } +} \ No newline at end of file diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/AccountChooserActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/AccountChooserActivity.java index e6cac0a1d..fc6249240 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/AccountChooserActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/AccountChooserActivity.java @@ -19,6 +19,8 @@ package com.google.android.apps.mytracks.io.sendtogoogle; import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.fragments.AddEmailsDialogFragment; import com.google.android.apps.mytracks.fragments.AddEmailsDialogFragment.AddEmailsCaller; +import com.google.android.apps.mytracks.fragments.ChooseAccountDialogFragment; +import com.google.android.apps.mytracks.fragments.ChooseAccountDialogFragment.ChooseAccountCaller; import com.google.android.apps.mytracks.io.drive.SendDriveActivity; import com.google.android.apps.mytracks.io.fusiontables.SendFusionTablesActivity; import com.google.android.apps.mytracks.io.gdata.maps.MapsConstants; @@ -36,9 +38,6 @@ import android.accounts.AccountManagerFuture; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; @@ -52,43 +51,19 @@ import java.io.IOException; * * @author Jimmy Shih */ -public class AccountChooserActivity extends FragmentActivity implements AddEmailsCaller { +public class AccountChooserActivity extends FragmentActivity + implements ChooseAccountCaller, AddEmailsCaller { private static final String TAG = AccountChooserActivity.class.getSimpleName(); - private static final int DIALOG_NO_ACCOUNT_ID = 0; - private static final int DIALOG_CHOOSER_ID = 1; private SendRequest sendRequest; - private Account[] accounts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sendRequest = getIntent().getParcelableExtra(SendRequest.SEND_REQUEST_KEY); - accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE); - - if (accounts.length == 0) { - showDialog(DIALOG_NO_ACCOUNT_ID); - return; - } - - if (accounts.length == 1) { - sendRequest.setAccount(accounts[0]); - PreferencesUtils.setString(this, R.string.google_account_key, accounts[0].name); - checkDrivePermission(); - return; - } - - String googleAccount = PreferencesUtils.getString( - this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT); - for (int i = 0; i < accounts.length; i++) { - if (accounts[i].name.equals(googleAccount)) { - sendRequest.setAccount(accounts[i]); - checkDrivePermission(); - return; - } - } - showDialog(DIALOG_CHOOSER_ID); + new ChooseAccountDialogFragment().show( + getSupportFragmentManager(), ChooseAccountDialogFragment.CHOOSE_ACCOUNT_DIALOG_TAG); } @Override @@ -124,26 +99,15 @@ public class AccountChooserActivity extends FragmentActivity implements AddEmail } @Override - protected Dialog onCreateDialog(int id) { - switch (id) { - case DIALOG_NO_ACCOUNT_ID: - return new AlertDialog.Builder(this).setCancelable(true) - .setMessage(R.string.send_google_no_account_message) - .setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - finish(); - } - }).setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - finish(); - } - }).setTitle(R.string.send_google_no_account_title).create(); - case DIALOG_CHOOSER_ID: - return createChooserDialog(); - default: - return null; + 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)) { + finish(); + return; } + sendRequest.setAccount(new Account(googleAccount, Constants.ACCOUNT_TYPE)); + checkDrivePermission(); } @Override @@ -157,37 +121,6 @@ public class AccountChooserActivity extends FragmentActivity implements AddEmail finish(); } - /** - * Creates a chooser dialog. - */ - private Dialog createChooserDialog() { - String[] choices = new String[accounts.length]; - for (int i = 0; i < accounts.length; i++) { - choices[i] = accounts[i].name; - } - return new AlertDialog.Builder(this).setCancelable(true) - .setNegativeButton(R.string.generic_cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - finish(); - } - }).setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - finish(); - } - }).setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition(); - Account account = accounts[position]; - PreferencesUtils.setString( - AccountChooserActivity.this, R.string.google_account_key, account.name); - sendRequest.setAccount(account); - checkDrivePermission(); - } - }).setSingleChoiceItems(choices, 0, null) - .setTitle(R.string.send_google_choose_account_title).create(); - } - /** * Checks the Drive permission. */ @@ -223,7 +156,7 @@ public class AccountChooserActivity extends FragmentActivity implements AddEmail fusionTablesCallback.onSuccess(); } } - + /** * Checks the Spreadsheet permission. */ @@ -334,9 +267,11 @@ public class AccountChooserActivity extends FragmentActivity implements AddEmail *
* !sendMaps && sendFusionTables -> {@link SendFusionTablesActivity} *
- * !sendMaps && !sendFusionTables && sendSpreadsheets -> {@link SendSpreadsheetsActivity} + * !sendMaps && !sendFusionTables && sendSpreadsheets -> + * {@link SendSpreadsheetsActivity} *
- * !sendMaps && !sendFusionTables && !sendSpreadsheets -> {@link UploadResultActivity} + * !sendMaps && !sendFusionTables && !sendSpreadsheets -> + * {@link UploadResultActivity} */ private void startNextActivity() { Class> next;