From ce3fffb5fb446e031e52ee0c5f2a4a5cf86a8c81 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 21 Jul 2016 10:42:30 -0700 Subject: [PATCH] Updating the search hint to contain the icon as a span object. This makes the icon part of the hint text, and removes any need for custom focus event handling. Also the icon will now appear at the start of the text based on the direction of the text and not the direction of the layout Change-Id: I06c5cf99492d75b1005c69ec7c521cde0fdab010 --- res/layout/all_apps.xml | 14 +---- .../allapps/AllAppsContainerView.java | 14 ++++- .../allapps/AllAppsSearchBarController.java | 27 +------- .../graphics/TintedDrawableSpan.java | 62 +++++++++++++++++++ 4 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 src/com/android/launcher3/graphics/TintedDrawableSpan.java diff --git a/res/layout/all_apps.xml b/res/layout/all_apps.xml index 822c8b6878..5c2c4b8696 100644 --- a/res/layout/all_apps.xml +++ b/res/layout/all_apps.xml @@ -65,18 +65,6 @@ android:orientation="horizontal" android:saveEnabled="false"> - - diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java index 3ef510c3f7..8b1f95087f 100644 --- a/src/com/android/launcher3/allapps/AllAppsContainerView.java +++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java @@ -22,6 +22,8 @@ import android.graphics.Point; import android.graphics.Rect; import android.support.v7.widget.RecyclerView; import android.text.Selection; +import android.text.Spannable; +import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.method.TextKeyListener; import android.util.AttributeSet; @@ -47,6 +49,7 @@ import com.android.launcher3.Utilities; import com.android.launcher3.Workspace; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.folder.Folder; +import com.android.launcher3.graphics.TintedDrawableSpan; import com.android.launcher3.keyboard.FocusedItemDecorator; import com.android.launcher3.util.ComponentKey; @@ -240,7 +243,6 @@ public class AllAppsContainerView extends BaseContainerView implements DragSourc } mSearchBarController = searchController; mSearchBarController.initialize(mApps, mSearchInput, mLauncher, this); - mSearchBarController.setHintView(findViewById(R.id.search_hint)); mAdapter.setSearchController(mSearchBarController); } @@ -308,6 +310,16 @@ public class AllAppsContainerView extends BaseContainerView implements DragSourc mSearchContainer = findViewById(R.id.search_container); mSearchInput = (ExtendedEditText) findViewById(R.id.search_box_input); + + // Update the hint to contain the icon. + // Prefix the original hint with two spaces. The first space gets replaced by the icon + // using span. The second space is used for a singe space character between the hint + // and the icon. + SpannableString spanned = new SpannableString(" " + mSearchInput.getHint()); + spanned.setSpan(new TintedDrawableSpan(getContext(), R.drawable.ic_allapps_search), + 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); + mSearchInput.setHint(spanned); + mSearchContainerOffsetTop = getResources().getDimensionPixelSize( R.dimen.all_apps_search_bar_margin_top); diff --git a/src/com/android/launcher3/allapps/AllAppsSearchBarController.java b/src/com/android/launcher3/allapps/AllAppsSearchBarController.java index b4a71caf8b..ac35932384 100644 --- a/src/com/android/launcher3/allapps/AllAppsSearchBarController.java +++ b/src/com/android/launcher3/allapps/AllAppsSearchBarController.java @@ -39,8 +39,7 @@ import java.util.ArrayList; * An interface to a search box that AllApps can command. */ public abstract class AllAppsSearchBarController - implements TextWatcher, OnEditorActionListener, ExtendedEditText.OnBackKeyListener, - View.OnFocusChangeListener { + implements TextWatcher, OnEditorActionListener, ExtendedEditText.OnBackKeyListener { protected Launcher mLauncher; protected AlphabeticalAppsList mApps; @@ -50,8 +49,6 @@ public abstract class AllAppsSearchBarController protected DefaultAppSearchAlgorithm mSearchAlgorithm; protected InputMethodManager mInputMethodManager; - protected View mHintView; - public void setVisibility(int visibility) { mInput.setVisibility(visibility); } @@ -69,7 +66,6 @@ public abstract class AllAppsSearchBarController mInput.addTextChangedListener(this); mInput.setOnEditorActionListener(this); mInput.setOnBackKeyListener(this); - mInput.setOnFocusChangeListener(this); mInputMethodManager = (InputMethodManager) mInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); @@ -130,27 +126,6 @@ public abstract class AllAppsSearchBarController return false; } - @Override - public void onFocusChange(View view, boolean focused) { - if (mHintView != null) { - mHintView.setVisibility(focused ? View.INVISIBLE : View.VISIBLE); - } - } - - /** - * Sets a view to serve as the search field's hint. - */ - public void setHintView(View hintView) { - mHintView = hintView; - } - - /** - * Returns the search field's hint view. - */ - public View getHintView() { - return mHintView; - } - /** * Resets the search bar state. */ diff --git a/src/com/android/launcher3/graphics/TintedDrawableSpan.java b/src/com/android/launcher3/graphics/TintedDrawableSpan.java new file mode 100644 index 0000000000..f72ce03f43 --- /dev/null +++ b/src/com/android/launcher3/graphics/TintedDrawableSpan.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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.android.launcher3.graphics; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.FontMetricsInt; +import android.graphics.drawable.Drawable; +import android.text.style.DynamicDrawableSpan; + +/** + * {@link DynamicDrawableSpan} which draws a drawable tinted with the current paint color. + */ +public class TintedDrawableSpan extends DynamicDrawableSpan { + + private final Drawable mDrawable; + private int mOldTint; + + public TintedDrawableSpan(Context context, int resourceId) { + super(ALIGN_BOTTOM); + mDrawable = context.getDrawable(resourceId); + mOldTint = 0; + } + + @Override + public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) { + fm = fm == null ? paint.getFontMetricsInt() : fm; + int iconSize = fm.bottom - fm.top; + mDrawable.setBounds(0, 0, iconSize, iconSize); + return super.getSize(paint, text, start, end, fm); + } + + @Override + public void draw(Canvas canvas, CharSequence text, + int start, int end, float x, int top, int y, int bottom, Paint paint) { + int color = paint.getColor(); + if (mOldTint != color) { + mOldTint = color; + mDrawable.setTint(mOldTint); + } + super.draw(canvas, text, start, end, x, top, y, bottom, paint); + } + + @Override + public Drawable getDrawable() { + return mDrawable; + } +}