Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/infobar/AccountChooserInfoBar.java

Issue 861103002: Credentials chooser UI for Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/AccountChooserInfoBar.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/AccountChooserInfoBar.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/AccountChooserInfoBar.java
new file mode 100644
index 0000000000000000000000000000000000000000..7b974140b80d022c7e48e9a6691bdd31a50c5380
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/AccountChooserInfoBar.java
@@ -0,0 +1,209 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.infobar;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.PopupMenu;
+import android.widget.PopupMenu.OnMenuItemClickListener;
+import android.widget.TextView;
+
+import org.chromium.base.CalledByNative;
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.ResourceId;
+import org.chromium.chrome.browser.preferences.PreferencesLauncher;
+import org.chromium.chrome.browser.widget.ButtonCompat;
+
+/**
+ * An infobar which allows user to choose credentials.
+ * Provides JNI methods for the infobar which allows user to choose account in
+ * Google Smart Lock.
+ */
+public class AccountChooserInfoBar extends InfoBar implements OnMenuItemClickListener {
+ private enum CredentialType {
+ EMPTY(0),
+ LOCAL(1),
+ FEDERATED(2);
+
+ private final int mType;
+ private CredentialType(int type) {
+ mType = type;
+ };
+
+ public int getValue() {
+ return mType;
+ }
+ }
+
+ private final String[] mUsernames;
+
+ /**
+ * Creates and shows the infobar wich allows user to choose credentials for login.
+ * @param nativeInfoBar Pointer to the native infobar.
+ * @param enumeratedIconId Enum ID corresponding to the icon that the infobar will show.
+ * @param usernames Usernames to display in the infobar.
+ */
+ @CalledByNative
+ private static InfoBar show(long nativeInfoBar, int enumeratedIconId, String[] usernames) {
+ return new AccountChooserInfoBar(
+ nativeInfoBar, ResourceId.mapToDrawableId(enumeratedIconId), usernames);
+ }
+
+ /**
+ * Creates and shows the infobar which allows user to choose credentials.
+ * @param nativeInfoBar Pointer to the native infobar.
+ * @param iconDrawableId Drawable ID corresponding to the icon that the infobar will show.
+ * @param usernames list of usernames to display in infobar.
+ */
+ public AccountChooserInfoBar(long nativeInfoBar, int iconDrawableId, String[] usernames) {
+ super(null /* Infobar Listener */, iconDrawableId, null /* bitmap*/,
+ null /* message to show */);
+ setNativeInfoBar(nativeInfoBar);
+ mUsernames = usernames;
+ }
+
+ /** Called when the user selects a contextual menu item */
newt (away) 2015/02/18 22:26:03 Don't add javadoc when overriding a method, unless
melandory 2015/02/19 13:38:00 Done.
+ @Override
+ public boolean onMenuItemClick(MenuItem item) {
+ if (item.getItemId() == R.id.settings) {
+ PreferencesLauncher.launchSettingsPage(getContext(), null);
+ return true;
+ }
+ // TODO(melandory): Learn more should open link to help center
+ // article which is not ready yet.
+ return false;
+ }
+
+ /**
+ * Called when the close button is clicked. Notifies the native infobar, which closes the
newt (away) 2015/02/18 22:26:03 Same deal: onCloseButtonClicked() is already docum
melandory 2015/02/19 13:38:00 Done.
+ * infobar.
+ */
+ @Override
+ public void onCloseButtonClicked() {
+ nativeOnCloseButtonClicked(mNativeInfoBarPtr);
+ }
+
+ @Override
+ public void onButtonClicked(boolean isPrimaryButton) {
+ onCloseButtonClicked();
+ }
+
+ /**
+ * Used to specify button layout and custom content. Makes infobar display list of
newt (away) 2015/02/18 22:26:03 Again, no javadoc needed.
melandory 2015/02/19 13:38:00 Done.
+ * credentials
+ * @param layout Handles user interface for the infobar.
+ */
+ @Override
+ public void createContent(InfoBarLayout layout) {
+ layout.setMessage(getContext().getString(R.string.account_chooser_infobar_title));
+ createAccountsView(layout);
+ createCustomButtonsView(layout);
+ }
+
+ private void createAccountsView(InfoBarLayout layout) {
+ ViewGroup accountsView =
+ (ViewGroup) LayoutInflater.from(getContext())
+ .inflate(R.layout.account_chooser_infobar_items, null, false);
+ ArrayAdapter<String> adapter = generateAccountsArrayAdapter(getContext(), mUsernames);
+ ListView listView = (ListView) accountsView.findViewById(R.id.account_chooser_layout);
+ listView.setAdapter(adapter);
+ if (adapter.getCount() >= 2) {
newt (away) 2015/02/18 22:26:03 I'd expect this to be "> 2" not ">= 2". Why did yo
melandory 2015/02/18 22:31:57 List view shows only one element by default. Behav
newt (away) 2015/02/19 00:00:48 Ok. Then how about this: float numVisibleItem
melandory 2015/02/19 13:38:00 Correct me if I'm wrong, but if I remove FrameLayo
+ double coeff = (adapter.getCount() > 2) ? 2.5 : 2;
+ ViewGroup.LayoutParams params = listView.getLayoutParams();
+ params.height = (int) (coeff
+ * getContext().getResources().getDimension(
+ R.dimen.account_chooser_infobar_item_height));
+ }
+ layout.setCustomContent(accountsView);
newt (away) 2015/02/18 22:26:03 To make the ListView expand to the full width of t
melandory 2015/02/19 13:38:01 Done.
+ }
+
+ private ArrayAdapter<String> generateAccountsArrayAdapter(Context context, String[] usernames) {
+ return new ArrayAdapter<String>(context, 0, usernames) {
+ class ViewHolder {
newt (away) 2015/02/18 22:26:03 Personally, I wouldn't bother with a ViewHolder cl
melandory 2015/02/19 13:38:00 Done.
+ ImageView mAvatar;
+ TextView mUsername;
+ TextView mDisplayName;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ ViewHolder viewHolder;
+ if (convertView == null) {
+ viewHolder = new ViewHolder();
+ convertView =
+ LayoutInflater.from(getContext())
+ .inflate(R.layout.account_chooser_infobar_item, null, false);
+ viewHolder.mAvatar = (ImageView) convertView.findViewById(R.id.profile_image);
+ viewHolder.mUsername = (TextView) convertView.findViewById(R.id.username);
+ viewHolder.mDisplayName =
+ (TextView) convertView.findViewById(R.id.display_name);
+ convertView.setTag(viewHolder);
+ } else {
+ viewHolder = (ViewHolder) convertView.getTag();
+ }
+ String username = getItem(position);
+ viewHolder.mUsername.setText(username);
+ // TODO(melandory): View should show the full name. Temporarily the view shows
+ // username.
+ viewHolder.mDisplayName.setText(username);
+ // TODO(melandory): View should show proper avatar. Temporarily the view shows
+ // blue man icon.
+ viewHolder.mAvatar.setImageResource(R.drawable.account_management_no_picture);
+ final int currentCredentialIndex = position;
+ convertView.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ passCredentialsToNative(currentCredentialIndex);
+ }
+ });
+ return convertView;
+ }
+ };
+ }
+
+ /**
+ * For the Account Chooser infobar different, than InfobarLayout has, appearance of button
+ * is required.
+ */
+ private void createCustomButtonsView(InfoBarLayout layout) {
+ layout.setButtons(getContext().getString(R.string.no_thanks), null);
+ Button moreButton = ButtonCompat.createBorderlessButton(getContext());
+ moreButton.setText(getContext().getString(R.string.account_chooser_infobar_more_button));
+ // TODO(melandory): Looks like spinner in mocks.
+ moreButton.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ showMorePopup(view);
+ }
+ });
+ layout.setCustomViewInButtonRow(moreButton);
+ }
+
+ private void passCredentialsToNative(Integer credentialIndex) {
+ // TODO(melandory): Adding federated login support should change this
+ // code.
+ nativeOnCredentialClicked(
+ mNativeInfoBarPtr, credentialIndex, CredentialType.LOCAL.getValue());
+ }
+
+ /** Pops up menu with two items: Setting and Learn More when user clicks more button*/
+ private void showMorePopup(View v) {
+ PopupMenu popup = new PopupMenu(getContext(), v);
+ popup.setOnMenuItemClickListener(this);
+ popup.inflate(R.menu.account_chooser_infobar_more_menu_popup);
+ popup.show();
+ }
+
+ private native void nativeOnCredentialClicked(
+ long nativeAccountChooserInfoBar, int credentialId, int credentialType);
+}

Powered by Google App Engine
This is Rietveld 408576698