-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a dialog for selecting an account when the screen is large enough
- Loading branch information
1 parent
fdb63ca
commit 3a1c783
Showing
5 changed files
with
340 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/com/pixmob/droidlink/ui/AuthenticationErrorDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.