| 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..6603ab022dc752f6536dce46d4ea044f55bc8bdd
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/AccountChooserInfoBar.java
|
| @@ -0,0 +1,184 @@
|
| +// 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.view.LayoutInflater;
|
| +import android.view.MenuItem;
|
| +import android.view.View;
|
| +import android.view.View.OnClickListener;
|
| +import android.view.ViewGroup;
|
| +import android.widget.Button;
|
| +import android.widget.ImageView;
|
| +import android.widget.LinearLayout;
|
| +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;
|
| +
|
| +/**
|
| + * 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
|
| + 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 titleText Message to display in the infobar.
|
| + * @param moreButtonText Text for More button.
|
| + * @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 */
|
| + @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
|
| + * infobar.
|
| + */
|
| + @Override
|
| + public void onCloseButtonClicked() {
|
| + nativeOnCloseButtonClicked(mNativeInfoBarPtr);
|
| + }
|
| +
|
| + /**
|
| + * Used to specify button layout and custom content. Makes infobar display list of
|
| + * 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));
|
| + ViewGroup frame = (ViewGroup) LayoutInflater.from(getContext())
|
| + .inflate(R.layout.credential_picker_infobar_items, null, false);
|
| + ViewGroup scroll = (ViewGroup) frame.findViewById(R.id.credential_scroll);
|
| + LinearLayout credentials_table =
|
| + (LinearLayout) scroll.findViewById(R.id.credential_picker_layout);
|
| + int credentialIndex = 0;
|
| + for (String username : mUsernames) {
|
| + LinearLayout row = makeCredentialsRow(username, credentialIndex);
|
| + credentials_table.addView(row);
|
| + ++credentialIndex;
|
| + }
|
| + // TODO(melandory): Restrict height to 2.5 elements in case length of usernames is more
|
| + // than 2 elements.
|
| + setupCustomButtonsRow(frame);
|
| + layout.setCustomContent(frame);
|
| + }
|
| +
|
| + /** Represents row in account chooser infobar, usual row has avatar, username and full name.
|
| + * @param username username to diplay.
|
| + * @param credentialIndex index of this row which will be back to native code in case user
|
| + * clicks on that row.
|
| + */
|
| + private LinearLayout makeCredentialsRow(String username, int credentialIndex) {
|
| + LinearLayout row = (LinearLayout) LayoutInflater.from(getContext())
|
| + .inflate(R.layout.credential_picker_infobar_item, null, false);
|
| + ViewGroup.LayoutParams row_params = row.getLayoutParams();
|
| + TextView username_view = (TextView) row.findViewById(R.id.credentials_app_name);
|
| + username_view.setText(username);
|
| + TextView app_name = (TextView) row.findViewById(R.id.credentials_app_credential_name);
|
| + // TODO(melandory): View should show the full name. Temporarily the view shows
|
| + // username.
|
| + app_name.setText(username);
|
| + // TODO(melandory): View should show proper avatar. Temporarily the view shows
|
| + // blue man icon.
|
| + ImageView avatar = (ImageView) row.findViewById(R.id.credentials_app_image);
|
| + avatar.setImageResource(R.drawable.account_management_no_picture);
|
| + final Integer current_credential_index = credentialIndex;
|
| + row.setOnClickListener(new View.OnClickListener() {
|
| + @Override
|
| + public void onClick(View view) {
|
| + passCredentialsToNative(current_credential_index);
|
| + }
|
| + });
|
| + return row;
|
| + }
|
| +
|
| + /** For the Account Chooser infobar different, than InfpbarLayout has, appearance of button
|
| + * is required.
|
| + * */
|
| + private void setupCustomButtonsRow(ViewGroup frame) {
|
| + Button moreButton = (Button) frame.findViewById(R.id.credential_picker_more);
|
| + moreButton.setText(getContext().getString(R.string.account_chooser_infobar_more_button));
|
| + moreButton.setOnClickListener(new OnClickListener() {
|
| + @Override
|
| + public void onClick(View view) {
|
| + showMorePopup(view);
|
| + }
|
| + });
|
| + }
|
| +
|
| + private void passCredentialsToNative(Integer credentialIndex) {
|
| + // TODO(melandory): Adding federated login support should change this
|
| + // code.
|
| + onChooseCredentials(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.credential_picker_infobar_more_menu_popup);
|
| + popup.show();
|
| + }
|
| +
|
| + private void onChooseCredentials(
|
| + long nativeAccountChooserInfoBar, int credentialId, int credentialType) {
|
| + nativeOnCredentialClicked(nativeAccountChooserInfoBar, credentialId, credentialType);
|
| + }
|
| +
|
| + private native void nativeOnCredentialClicked(
|
| + long nativeAccountChooserInfoBar, int credentialId, int credentialType);
|
| +}
|
|
|