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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillDialogContentView.java

Issue 14886012: Implement layout transition animations for the Autofill dialog (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: findbugs errors Created 7 years, 7 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/autofill/AutofillDialogContentView.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillDialogContentView.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillDialogContentView.java
deleted file mode 100644
index 8a310d3aa4b77c2f1fdfd3fe48185b6af45ee3d2..0000000000000000000000000000000000000000
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillDialogContentView.java
+++ /dev/null
@@ -1,522 +0,0 @@
-// Copyright (c) 2013 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.autofill;
-
-import android.content.Context;
-import android.content.res.Resources;
-import android.graphics.Bitmap;
-import android.graphics.drawable.BitmapDrawable;
-import android.os.Build;
-import android.text.TextUtils;
-import android.util.AttributeSet;
-import android.view.KeyEvent;
-import android.view.View;
-import android.view.ViewGroup;
-import android.view.animation.Animation;
-import android.view.animation.AnimationSet;
-import android.view.animation.ScaleAnimation;
-import android.view.animation.TranslateAnimation;
-import android.widget.ArrayAdapter;
-import android.widget.Button;
-import android.widget.EditText;
-import android.widget.ImageView;
-import android.widget.LinearLayout;
-import android.widget.Spinner;
-import android.widget.TextView;
-import android.widget.AdapterView.OnItemSelectedListener;
-
-import static org.chromium.chrome.browser.autofill.AutofillDialogConstants.NUM_SECTIONS;
-import static org.chromium.chrome.browser.autofill.AutofillDialogConstants.SECTION_EMAIL;
-import static org.chromium.chrome.browser.autofill.AutofillDialogConstants.SECTION_CC;
-import static org.chromium.chrome.browser.autofill.AutofillDialogConstants.SECTION_CC_BILLING;
-import static org.chromium.chrome.browser.autofill.AutofillDialogConstants.SECTION_BILLING;
-import static org.chromium.chrome.browser.autofill.AutofillDialogConstants.SECTION_SHIPPING;
-
-import org.chromium.chrome.R;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * This is the parent layout that contains the layouts for different states of
- * autofill dialog. In principle it shouldn't contain any logic related with the
- * actual workflow, but rather respond to any UI update messages coming to it
- * from the AutofillDialog.
- */
-public class AutofillDialogContentView extends LinearLayout {
- private static final int ANIMATION_DURATION_MS = 1000;
- static final int INVALID_LAYOUT = -1;
- static final int LAYOUT_EDITING_EMAIL = 0;
- static final int LAYOUT_EDITING_SHIPPING = 1;
- static final int LAYOUT_EDITING_CC = 2;
- static final int LAYOUT_EDITING_BILLING = 3;
- static final int LAYOUT_EDITING_CC_BILLING = 4;
- static final int LAYOUT_FETCHING = 5;
- static final int LAYOUT_STEADY = 6;
- private final Runnable mDismissSteadyLayoutRunnable = new Runnable() {
- @Override
- public void run() {
- mSteadyLayout.setVisibility(GONE);
- }
- };
- private AutofillDialog mDialog;
- private Spinner[] mSpinners = new Spinner[NUM_SECTIONS];
- private AutofillDialogMenuAdapter[] mAdapters = new AutofillDialogMenuAdapter[NUM_SECTIONS];
- private ViewGroup mSteadyLayout;
- private ViewGroup[] mEditLayouts = new ViewGroup[NUM_SECTIONS];
- private int mCurrentLayout = -1;
- private String mCVCHint;
- private Bitmap mCVCIcon;
- private OnItemSelectedListener mOnItemSelectedListener;
- private OnItemEditButtonClickedListener mOnItemEditButtonClickedListener;
-
- /**
- * Interface definition for a callback to be invoked when an "Edit" button
- * in the AutofillDialogMenuAdapter is clicked.
- */
- public interface OnItemEditButtonClickedListener {
- /**
- * Callback method to be invoked when an "Edit" button has been clicked.
- * @param section The dialog section associated with the adapter.
- * @param position The position of the view in the adapter
- */
- void onItemEditButtonClicked(int section, int position);
- }
-
- public AutofillDialogContentView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- @Override
- protected void onFinishInflate () {
- mSteadyLayout = (ViewGroup) findViewById(R.id.general_layout);
-
- for (int i = 0; i < AutofillDialogConstants.NUM_SECTIONS; i++) {
- mEditLayouts[i] = (ViewGroup) findViewById(
- AutofillDialogUtils.getLayoutIDForSection(i));
- int id = AutofillDialogUtils.getSpinnerIDForSection(i);
- mSpinners[i] = (Spinner) findViewById(id);
- }
-
- changeLayoutTo(LAYOUT_FETCHING);
- }
-
- /**
- * Sets the controller dialog for this content view.
- * @param dialog The autofill Dialog that should be set as the controller.
- */
- public void setAutofillDialog(AutofillDialog dialog) {
- mDialog = dialog;
- }
-
- /**
- * Prompts the content view to create the adapters for each section. This is
- * separated to be able to control the timing in a flexible manner.
- */
- public void createAdapters() {
- for (int i = 0; i < AutofillDialogConstants.NUM_SECTIONS; i++) {
- AutofillDialogMenuAdapter adapter =
- new AutofillDialogMenuAdapter(i, getContext(),
- new ArrayList<AutofillDialogMenuItem>());
- adapter.setOnItemEditButtonClickedListener(mOnItemEditButtonClickedListener);
- mAdapters[i] = adapter;
- mSpinners[i].setAdapter(adapter);
- }
-
- initializeSpinner(SECTION_SHIPPING, AutofillDialogConstants.ADDRESS_HOME_COUNTRY);
- initializeSpinner(SECTION_BILLING, AutofillDialogConstants.ADDRESS_HOME_COUNTRY);
- initializeSpinner(SECTION_CC, AutofillDialogConstants.CREDIT_CARD_EXP_MONTH);
- initializeSpinner(SECTION_CC, AutofillDialogConstants.CREDIT_CARD_EXP_4_DIGIT_YEAR);
- }
-
- private void initializeSpinner(int section, int field) {
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
- R.layout.autofill_editing_spinner_item);
- adapter.addAll(Arrays.asList(mDialog.getListForField(field)));
- ((Spinner) findViewById(AutofillDialogUtils
- .getViewIDForField(section, field))).setAdapter(adapter);
- }
-
- /**
- * Initializes the labels for each section.
- * @param labels The label strings to be used for each section.
- */
- public void initializeLabelsForEachSection(String[] labels) {
- TextView labelView;
- for (int i = 0; i < labels.length && i < NUM_SECTIONS; i++) {
- labelView = (TextView) findViewById(AutofillDialogUtils.getLabelIDForSection(i));
- labelView.setText(labels[i]);
- }
- }
-
- @Override
- protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- for (int i = 0; i < NUM_SECTIONS; i++) {
- mSpinners[i].setDropDownWidth(mSpinners[i].getMeasuredWidth());
- mSpinners[i].setDropDownVerticalOffset(-mSpinners[i].getMeasuredHeight());
- }
- }
- }
-
- /**
- * Set the listener for all the dropdown members in the layout.
- * @param listener The listener object to attach to the dropdowns.
- */
- public void setOnItemSelectedListener(OnItemSelectedListener listener) {
- mOnItemSelectedListener = listener;
- for (int i = 0; i < NUM_SECTIONS; i++) mSpinners[i].setOnItemSelectedListener(listener);
- }
-
- /**
- * Set the listener for all the dropdown members in the layout.
- * @param listener The listener object to attach to the dropdowns.
- */
- public void setOnItemEditButtonClickedListener(OnItemEditButtonClickedListener listener) {
- mOnItemEditButtonClickedListener = listener;
- for (int i = 0; i < NUM_SECTIONS; i++) {
- if (mAdapters[i] != null) {
- mAdapters[i].setOnItemEditButtonClickedListener(listener);
- }
- }
- }
-
- /**
- * @return Whether the current layout is one of the editing layouts.
- */
- public boolean isInEditingMode() {
- return mCurrentLayout != INVALID_LAYOUT
- && mCurrentLayout != LAYOUT_STEADY
- && mCurrentLayout != LAYOUT_FETCHING;
- }
-
- /**
- * @return The current section if we are in editing mode, INVALID_SECTION otherwise.
- */
- public int getCurrentSection() {
- return getSectionForLayoutMode(mCurrentLayout);
- }
-
- /**
- * Updates the legal documents footer.
- * @param legalDocumentsText The text to show, or a null/empty string.
- */
- public void updateLegalDocumentsText(String legalDocumentsText) {
- TextView legalView = (TextView) findViewById(R.id.terms_info);
- if (TextUtils.isEmpty(legalDocumentsText)) {
- legalView.setVisibility(View.GONE);
- return;
- }
-
- legalView.setText(legalDocumentsText);
- legalView.setVisibility(View.VISIBLE);
- }
-
- /**
- * Updates a dropdown with the given items and adds default items to the end.
- * @param section The dialog section.
- * @param suggestionText The suggestion text.
- * @param suggestionIcon The suggestion icon.
- * @param suggestionTextExtra The suggestion text extra.
- * @param suggestionIconExtra The suggestion icon extra.
- * @param suggestionSectionEditable Whether the section is editable.
- * @param items The {@link AutofillDialogMenuItem} array to update the dropdown with.
- * @param selectedMenuItem The index of the selected menu item, or -1.
- */
- public void updateMenuItemsForSection(int section,
- String suggestionText, Bitmap suggestionIcon,
- String suggestionTextExtra, Bitmap suggestionIconExtra,
- boolean suggestionSectionEditable,
- List<AutofillDialogMenuItem> items, final int selectedMenuItem) {
- final Spinner spinner = mSpinners[section];
- // Set the listener to null and reset it after updating the menu items to avoid getting an
- // onItemSelected call when the first item is selected after updating the items.
- spinner.setOnItemSelectedListener(null);
- AutofillDialogMenuAdapter adapter = mAdapters[section];
- adapter.clear();
- adapter.setSuggestionExtra(suggestionTextExtra,
- createFieldIconDrawable(suggestionIconExtra));
- adapter.addAll(items);
- spinner.post(new Runnable() {
- @Override
- public void run() {
- spinner.setSelection(selectedMenuItem);
- spinner.setOnItemSelectedListener(mOnItemSelectedListener);
- }
- });
- }
-
- /**
- * Updates a dropdown selection.
- * @param section The dialog section.
- * @param selectedMenuItem The index of the selected menu item, or -1.
- */
- public void updateMenuSelectionForSection(int section, int selectedMenuItem) {
- final Spinner spinner = mSpinners[section];
- spinner.setSelection(selectedMenuItem);
- }
-
- /**
- * Transitions the layout shown to a given layout.
- * @param mode The layout mode to transition to.
- */
- public void changeLayoutTo(int mode) {
- assert(mode != INVALID_LAYOUT);
- if (mode == mCurrentLayout || mode == INVALID_LAYOUT) return;
-
- mCurrentLayout = mode;
- removeCallbacks(mDismissSteadyLayoutRunnable);
- setVisibilityForEditLayouts(false);
- if (mode == LAYOUT_FETCHING) {
- mSteadyLayout.setVisibility(GONE);
- findViewById(R.id.loading_icon).setVisibility(VISIBLE);
- return;
- }
- findViewById(R.id.loading_icon).setVisibility(GONE);
- mSteadyLayout.setVisibility(VISIBLE);
- if (mode == LAYOUT_STEADY) return;
-
- addTranslateAnimations(mode);
- addDisappearAnimationForSteadyLayout();
- View centeredLayout = mEditLayouts[getSectionForLayoutMode(mode)];
- addAppearAnimationForEditLayout(mode, centeredLayout);
-
- centeredLayout.setVisibility(VISIBLE);
- if (mode == LAYOUT_EDITING_CC_BILLING) {
- mEditLayouts[SECTION_CC].setVisibility(VISIBLE);
- mEditLayouts[SECTION_BILLING].setVisibility(VISIBLE);
- }
- ((View) centeredLayout.getParent()).setVisibility(VISIBLE);
- centeredLayout.animate();
- mSteadyLayout.animate();
- postDelayed(mDismissSteadyLayoutRunnable, ANIMATION_DURATION_MS);
-
- mCurrentLayout = mode;
- }
-
- private void setVisibilityForEditLayouts(boolean visible) {
- int visibility = visible ? VISIBLE : GONE;
- for (int i = 0; i < NUM_SECTIONS; i++) {
- if (mEditLayouts[i] != null) mEditLayouts[i].setVisibility(visibility);
- }
- }
-
- /**
- * Sets the visibility for all items related with the given section.
- * @param section The section that will change visibility.
- * @param visible Whether the section should be visible.
- */
- public void setVisibilityForSection(int section, boolean visible) {
- int visibility = visible ? VISIBLE : GONE;
- mSpinners[section].setVisibility(visibility);
- View labelView = findViewById(AutofillDialogUtils.getLabelIDForSection(section));
- if (labelView != null) labelView.setVisibility(visibility);
- }
-
- /**
- * @param bitmap A bitmap.
- * @return A drawable with the dimensions of a field icon and the contents from the bitmap.
- */
- public BitmapDrawable createFieldIconDrawable(Bitmap bitmap) {
- if (bitmap == null) return null;
-
- Resources resources = getContext().getResources();
- BitmapDrawable drawable = new BitmapDrawable(resources, bitmap);
- int width = resources.getDimensionPixelSize(R.dimen.autofill_field_icon_width);
- int height = resources.getDimensionPixelSize(R.dimen.autofill_field_icon_height);
- drawable.setBounds(0, 0, width, height);
- return drawable;
- }
-
- private void addAppearAnimationForEditLayout(int mode, View layout) {
- View centerView = mSpinners[getSectionForLayoutMode(mode)];
- float yOffset = centerView.getY() - (float) centerView.getHeight() / 2;
-
- TranslateAnimation toLocationAnimation = new TranslateAnimation(0, 0, yOffset, 0);
- toLocationAnimation.setDuration(ANIMATION_DURATION_MS);
- ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1, 0, 1,
- Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
- scaleAnimation.setDuration(ANIMATION_DURATION_MS);
- scaleAnimation.setStartOffset(ANIMATION_DURATION_MS / 4);
-
- AnimationSet appearAnimation = new AnimationSet(true);
- appearAnimation.addAnimation(toLocationAnimation);
- appearAnimation.addAnimation(scaleAnimation);
-
- layout.setAnimation(appearAnimation);
- }
-
- private void addDisappearAnimationForSteadyLayout() {
- ScaleAnimation scaleDownAnimation = new ScaleAnimation(1, 1, 1, 0,
- Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
- scaleDownAnimation.setDuration(ANIMATION_DURATION_MS / 2);
- scaleDownAnimation.setStartOffset(ANIMATION_DURATION_MS / 2);
-
- mSteadyLayout.setAnimation(scaleDownAnimation);
- }
-
- private void addTranslateAnimations(int mode) {
- float distance = getResources().
- getDimensionPixelSize(R.dimen.autofill_translation_anim_distance);
- TranslateAnimation toTopAnimation = new TranslateAnimation(0, 0, 0, -distance);
- toTopAnimation.setDuration(ANIMATION_DURATION_MS);
- TranslateAnimation toBottomAnimation = new TranslateAnimation(0, 0, 0, distance);
- toBottomAnimation.setDuration(ANIMATION_DURATION_MS);
- for (int i = 0; i < mSteadyLayout.getChildCount(); i++) {
- View currentChild = mSteadyLayout.getChildAt(i);
- if (currentChild.getVisibility() == GONE) continue;
-
- if (currentChild.getTop() <=
- mSpinners[getSectionForLayoutMode(mode)].getTop()) {
- currentChild.setAnimation(toTopAnimation);
- currentChild.animate();
- } else if (currentChild.getTop() >
- mSpinners[getSectionForLayoutMode(mode)].getTop()) {
- currentChild.setAnimation(toBottomAnimation);
- currentChild.animate();
- }
- }
- }
-
- private static int getSectionForLayoutMode(int mode) {
- switch (mode) {
- case LAYOUT_EDITING_EMAIL:
- return SECTION_EMAIL;
- case LAYOUT_EDITING_CC:
- return SECTION_CC;
- case LAYOUT_EDITING_BILLING:
- return SECTION_BILLING;
- case LAYOUT_EDITING_CC_BILLING:
- return SECTION_CC_BILLING;
- case LAYOUT_EDITING_SHIPPING:
- return SECTION_SHIPPING;
- default:
- assert(false);
- return AutofillDialogUtils.INVALID_SECTION;
- }
- }
-
- /**
- * Returns the layout mode for editing the given section.
- * @param section The section to look up.
- * @return The layout mode for editing the given section.
- */
- public static int getLayoutModeForSection(int section) {
- assert(section != AutofillDialogUtils.INVALID_SECTION);
- for (int i = 0; i < AutofillDialogConstants.NUM_SECTIONS; i++) {
- if (getSectionForLayoutMode(i) == section) return i;
- }
- assert(false);
- return INVALID_LAYOUT;
- }
-
- private static class AutofillDialogMenuAdapter extends ArrayAdapter<AutofillDialogMenuItem> {
- private int mSection;
- private OnItemEditButtonClickedListener mOnItemEditButtonClickedListener;
- private String mSuggestionTextExtra;
- private BitmapDrawable mSuggestionIconExtraDrawable;
-
- public AutofillDialogMenuAdapter(
- int section,
- Context context,
- List<AutofillDialogMenuItem> objects) {
- super(context, R.layout.autofill_menu_item, objects);
- mSection = section;
- }
-
- public void setOnItemEditButtonClickedListener(OnItemEditButtonClickedListener listener) {
- mOnItemEditButtonClickedListener = listener;
- }
-
- public void setSuggestionExtra(String suggestionTextExtra,
- BitmapDrawable suggestionIconExtraDrawable) {
- mSuggestionTextExtra = suggestionTextExtra;
- mSuggestionIconExtraDrawable = suggestionIconExtraDrawable;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- return initView(position, convertView, parent, false);
- }
-
- @Override
- public View getDropDownView(int position, View convertView, ViewGroup parent) {
- return initView(position, convertView, parent, true);
- }
-
- private View initView(
- final int position, View convertView, final ViewGroup parent, boolean isDropDown) {
- if (convertView == null) {
- convertView = View.inflate(getContext(), R.layout.autofill_menu_item, null);
- }
-
- AutofillDialogMenuItem item = getItem(position);
- ImageView icon = (ImageView) convertView.findViewById(R.id.cc_icon);
- TextView line1 = (TextView) convertView.findViewById(R.id.adapter_item_line_1);
- TextView line2 = (TextView) convertView.findViewById(R.id.adapter_item_line_2);
- Button button = (Button) convertView.findViewById(R.id.adapter_item_edit_button);
- EditText extraEdit = (EditText) convertView.findViewById(R.id.cvc_challenge);
-
- if (icon != null) {
- if (item.mIcon != null) {
- icon.setImageBitmap(item.mIcon);
- icon.setVisibility(VISIBLE);
- } else {
- icon.setImageBitmap(null);
- icon.setVisibility(GONE);
- }
- }
- if (line1 != null) line1.setText(item.mLine1);
- if (line2 != null) {
- if (!TextUtils.isEmpty(item.mLine2)) {
- line2.setVisibility(VISIBLE);
- line2.setText(item.mLine2);
- } else {
- line2.setVisibility(GONE);
- }
- }
-
- if (extraEdit != null) {
- if (!isDropDown && !TextUtils.isEmpty(mSuggestionTextExtra)) {
- extraEdit.setVisibility(VISIBLE);
- extraEdit.setHint(mSuggestionTextExtra);
- extraEdit.setCompoundDrawables(
- null, null, mSuggestionIconExtraDrawable, null);
- } else {
- extraEdit.setVisibility(GONE);
- }
- }
-
- if (button != null) {
- if (isDropDown && item.mShowButton) {
- button.setText(item.mButtonLabelResourceId);
- button.setOnClickListener(new OnClickListener() {
- // TODO(aruslan): http://crbug.com/236101.
- @Override
- public void onClick(View view) {
- View root = parent.getRootView();
- root.dispatchKeyEvent(
- new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
- root.dispatchKeyEvent(
- new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
- assert mOnItemEditButtonClickedListener != null;
- if (mOnItemEditButtonClickedListener != null) {
- mOnItemEditButtonClickedListener.onItemEditButtonClicked(
- mSection, position);
- }
- }
- });
- button.setVisibility(VISIBLE);
- } else {
- button.setOnClickListener(null);
- button.setVisibility(GONE);
- }
- }
- return convertView;
- }
- }
-}

Powered by Google App Engine
This is Rietveld 408576698