Skip to content

Commit

Permalink
Use a dialog for selecting an account when the screen is large enough
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreroman committed Sep 26, 2011
1 parent fdb63ca commit 3a1c783
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 177 deletions.
9 changes: 7 additions & 2 deletions src/com/pixmob/droidlink/ui/AccountInitTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.pixmob.appengine.client.AppEngineAuthenticationException;
Expand All @@ -53,14 +54,14 @@ class AccountInitTask extends AsyncTask<String, Void, Integer> {
private static final int AUTH_OK = 0;
private static final int AUTH_FAIL = 1;
private static final int AUTH_PENDING = 2;
private final AccountsFragment fragment;
private final Fragment fragment;
private final SharedPreferences prefs;
private final SharedPreferences.Editor prefsEditor;
private final ContentResolver contentResolver;
private final Account[] accounts;
private Intent authPendingIntent;

public AccountInitTask(final AccountsFragment fragment) {
public AccountInitTask(final Fragment fragment) {
this.fragment = fragment;

final Activity context = fragment.getActivity();
Expand All @@ -70,6 +71,10 @@ public AccountInitTask(final AccountsFragment fragment) {
accounts = Accounts.list(context);
}

protected Fragment getFragment() {
return fragment;
}

@Override
protected Integer doInBackground(String... params) {
final String newAccount = params[0];
Expand Down
302 changes: 130 additions & 172 deletions src/com/pixmob/droidlink/ui/AccountsFragment.java
Original file line number Diff line number Diff line change
@@ -1,172 +1,130 @@
/*
* Copyright (C) 2011 Pixmob (http://github.com/pixmob)
*
* 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.pixmob.droidlink.ui;

import static com.pixmob.droidlink.Constants.TAG;
import android.accounts.Account;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.pixmob.droidlink.R;
import com.pixmob.droidlink.util.Accounts;

/**
* Fragment for displaying accounts.
* @author Pixmob
*/
public class AccountsFragment extends ListFragment {
private static final int GRANT_AUTH_PERMISSION_REQUEST = 1;
private AccountAdapter accountAdapter;
private String selectedAccount;
private InternalAccountInitTask task;
private AuthDialog authDialog;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

authDialog = AuthDialog.newInstance();

final Account[] accounts = Accounts.list(getActivity());
if (accounts.length == 0) {
// This may happen on a device without Google apps:
// the application cannot run.
Log.wtf(TAG, "No accounts available");
} else {
if (selectedAccount != null) {
// Check if the selected account still exists:
// the user may have deleted an account before going back to
// this activity.
boolean accountFound = false;
for (final Account account : accounts) {
if (account.name.equals(selectedAccount)) {
// The selected account exists: select it.
accountFound = true;
break;
}
}
if (!accountFound) {
selectedAccount = null;
}
}

accountAdapter = new AccountAdapter(getActivity(), accounts);
setListAdapter(accountAdapter);
}
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
selectedAccount = ((Account) l.getItemAtPosition(position)).name;
checkAccount();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (GRANT_AUTH_PERMISSION_REQUEST == requestCode) {
if (Activity.RESULT_OK == resultCode) {
checkAccount();
} else {
selectedAccount = null;
}
}
}

private void checkAccount() {
task = new InternalAccountInitTask();
task.execute(selectedAccount);
}

/**
* Internal task for checking a Google account. A new dialog may be opened,
* asking the user for granting its permission to use its account.
* @author Pixmob
*/
private class InternalAccountInitTask extends AccountInitTask {
public InternalAccountInitTask() {
super(AccountsFragment.this);
}

@Override
protected void onPreExecute() {
authDialog.show(getSupportFragmentManager(), "auth");
}

@Override
protected void onAuthenticationSuccess() {
authDialog.dismiss();
getActivity().setResult(Activity.RESULT_OK);
getActivity().finish();
}

@Override
protected void onAuthenticationError() {
authDialog.dismiss();
ErrorDialog.newInstance().show(getSupportFragmentManager(), "error");
}

@Override
protected void onAuthenticationPending(Intent authPendingIntent) {
authDialog.dismiss();
getActivity().startActivityForResult(authPendingIntent, GRANT_AUTH_PERMISSION_REQUEST);
}
}

/**
* Progress dialog when the authentication is running.
* @author Pixmob
*/
public static class AuthDialog extends DialogFragment {
public static AuthDialog newInstance() {
return new AuthDialog();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setCancelable(false);
dialog.setMessage(getString(R.string.auth_pending));
return dialog;
}
}

/**
* Error dialog when the authentication failed.
* @author Pixmob
*/
public static class ErrorDialog extends DialogFragment {
public static ErrorDialog newInstance() {
return new ErrorDialog();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle(R.string.error).setIcon(
R.drawable.ic_dialog_alert).setMessage(R.string.auth_error).setPositiveButton(
android.R.string.ok, null).create();
}
}
}
/*
* Copyright (C) 2011 Pixmob (http://github.com/pixmob)
*
* 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.pixmob.droidlink.ui;

import static com.pixmob.droidlink.Constants.TAG;
import android.accounts.Account;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.pixmob.droidlink.util.Accounts;

/**
* Fragment for displaying accounts.
* @author Pixmob
*/
public class AccountsFragment extends ListFragment {
private static final int GRANT_AUTH_PERMISSION_REQUEST = 1;
private AccountAdapter accountAdapter;
private String selectedAccount;
private AuthenticationProgressDialog authDialog;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

authDialog = AuthenticationProgressDialog.newInstance();

final Account[] accounts = Accounts.list(getActivity());
if (accounts.length == 0) {
// This may happen on a device without Google apps:
// the application cannot run.
Log.wtf(TAG, "No accounts available");
} else {
if (selectedAccount != null) {
// Check if the selected account still exists:
// the user may have deleted an account before going back to
// this activity.
boolean accountFound = false;
for (final Account account : accounts) {
if (account.name.equals(selectedAccount)) {
// The selected account exists: select it.
accountFound = true;
break;
}
}
if (!accountFound) {
selectedAccount = null;
}
}

accountAdapter = new AccountAdapter(getActivity(), accounts);
setListAdapter(accountAdapter);
}
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
selectedAccount = ((Account) l.getItemAtPosition(position)).name;
checkAccount();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (GRANT_AUTH_PERMISSION_REQUEST == requestCode) {
if (Activity.RESULT_OK == resultCode) {
checkAccount();
} else {
selectedAccount = null;
}
}
}

private void checkAccount() {
new InternalAccountInitTask().execute(selectedAccount);
}

/**
* Internal task for checking a Google account. A new dialog may be opened,
* asking the user for granting its permission to use its account.
* @author Pixmob
*/
private class InternalAccountInitTask extends AccountInitTask {
public InternalAccountInitTask() {
super(AccountsFragment.this);
}

@Override
protected void onPreExecute() {
authDialog.show(getSupportFragmentManager(), "auth");
}

@Override
protected void onAuthenticationSuccess() {
authDialog.dismiss();
getActivity().setResult(Activity.RESULT_OK);
getActivity().finish();
}

@Override
protected void onAuthenticationError() {
authDialog.dismiss();
AuthenticationErrorDialog.newInstance().show(getSupportFragmentManager(), "error");
}

@Override
protected void onAuthenticationPending(Intent authPendingIntent) {
authDialog.dismiss();
getActivity().startActivityForResult(authPendingIntent, GRANT_AUTH_PERMISSION_REQUEST);
}
}
}
40 changes: 40 additions & 0 deletions src/com/pixmob/droidlink/ui/AuthenticationErrorDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2011 Pixmob (http://github.com/pixmob)
*
* 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.pixmob.droidlink.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import com.pixmob.droidlink.R;

/**
* Error dialog when the authentication failed.
* @author Pixmob
*/
public class AuthenticationErrorDialog extends DialogFragment {
public static AuthenticationErrorDialog newInstance() {
return new AuthenticationErrorDialog();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle(R.string.error)
.setIcon(R.drawable.ic_dialog_alert).setMessage(R.string.auth_error)
.setPositiveButton(android.R.string.ok, null).create();
}
}
Loading

0 comments on commit 3a1c783

Please sign in to comment.