Bug 1049600 - Show search engine branding. r=wesj

--HG--
rename : mobile/android/search/java/org/mozilla/search/autocomplete/ClearableEditText.java => mobile/android/search/java/org/mozilla/search/autocomplete/SearchBar.java
rename : mobile/android/search/res/layout/clearable_edit_text.xml => mobile/android/search/res/layout/search_bar.xml
This commit is contained in:
Margaret Leibovic 2014-09-19 16:15:40 -07:00
Родитель 57dd686aab
Коммит 365a93c274
11 изменённых файлов: 95 добавлений и 40 удалений

Просмотреть файл

@ -3,7 +3,10 @@
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<!ENTITY search_app_name '&brandShortName; Search'>
<!ENTITY search_for_something 'Search for something'>
<!-- Localization note (search_bar_hint): The &formatS; will be replaced with the name of
the currently selected search engine. -->
<!ENTITY search_bar_hint 'Search with &formatS;'>
<!ENTITY search_empty_title '&brandShortName; Search'>
<!ENTITY search_empty_message 'Quickly search for anything you want'>

Просмотреть файл

@ -23,7 +23,7 @@ import org.mozilla.gecko.LocaleAware;
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.db.BrowserContract.SearchHistory;
import org.mozilla.search.autocomplete.ClearableEditText;
import org.mozilla.search.autocomplete.SearchBar;
import org.mozilla.search.autocomplete.SuggestionsFragment;
import org.mozilla.search.providers.SearchEngine;
import org.mozilla.search.providers.SearchEngineManager;
@ -67,7 +67,7 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
private AsyncQueryHandler queryHandler;
// Main views in layout.
private ClearableEditText editText;
private SearchBar searchBar;
private View preSearch;
private View postSearch;
@ -106,15 +106,15 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
queryHandler = new AsyncQueryHandler(getContentResolver()) {};
editText = (ClearableEditText) findViewById(R.id.search_edit_text);
editText.setOnClickListener(new View.OnClickListener() {
searchBar = (SearchBar) findViewById(R.id.search_bar);
searchBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setEditState(EditState.EDITING);
}
});
editText.setTextListener(new ClearableEditText.TextListener() {
searchBar.setTextListener(new SearchBar.TextListener() {
@Override
public void onChange(String text) {
// Only load suggestions if we're in edit mode.
@ -165,7 +165,7 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
setEditState(EditState.valueOf(savedInstanceState.getString(KEY_EDIT_STATE)));
final String query = savedInstanceState.getString(KEY_QUERY);
editText.setText(query);
searchBar.setText(query);
// If we're in the postsearch state, we need to re-do the query.
if (searchState == SearchState.POSTSEARCH) {
@ -187,7 +187,7 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
suggestionsFragment = null;
postSearchFragment = null;
queryHandler = null;
editText = null;
searchBar = null;
preSearch = null;
postSearch = null;
settingsButton = null;
@ -216,7 +216,7 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
// Enter editing mode and reset the query. We must reset the query after entering
// edit mode in order for the suggestions to update.
setEditState(EditState.EDITING);
editText.setText("");
searchBar.setText("");
}
@Override
@ -225,12 +225,12 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
outState.putString(KEY_SEARCH_STATE, searchState.toString());
outState.putString(KEY_EDIT_STATE, editState.toString());
outState.putString(KEY_QUERY, editText.getText());
outState.putString(KEY_QUERY, searchBar.getText());
}
@Override
public void onSuggest(String query) {
editText.setText(query);
searchBar.setText(query);
}
@Override
@ -281,6 +281,7 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
public void execute(SearchEngine engine) {
this.engine = engine;
suggestionsFragment.setEngine(engine);
searchBar.setEngine(engine);
}
/**
@ -328,7 +329,7 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
setEditState(EditState.WAITING);
setSearchState(SearchState.POSTSEARCH);
editText.setText(query);
searchBar.setText(query);
// We need to manually clear the animation for the views to be hidden on gingerbread.
animationText.clearAnimation();
@ -361,7 +362,7 @@ public class MainActivity extends LocaleAware.LocaleAwareFragmentActivity
updateSettingsButtonVisibility();
editText.setActive(editState == EditState.EDITING);
searchBar.setActive(editState == EditState.EDITING);
suggestions.setVisibility(editState == EditState.EDITING ? View.VISIBLE : View.INVISIBLE);
}

Просмотреть файл

@ -5,6 +5,11 @@
package org.mozilla.search.autocomplete;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
@ -17,17 +22,25 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.search.R;
import org.mozilla.search.providers.SearchEngine;
public class ClearableEditText extends FrameLayout {
public class SearchBar extends FrameLayout {
private EditText editText;
private ImageButton clearButton;
private InputMethodManager inputMethodManager;
private final EditText editText;
private final ImageButton clearButton;
private final ImageView engineIcon;
private final Drawable focusedBackground;
private final Drawable defaultBackgound;
private final InputMethodManager inputMethodManager;
private TextListener listener;
@ -39,10 +52,10 @@ public class ClearableEditText extends FrameLayout {
public void onFocusChange(boolean hasFocus);
}
public ClearableEditText(Context context, AttributeSet attrs) {
public SearchBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.clearable_edit_text, this);
LayoutInflater.from(context).inflate(R.layout.search_bar, this);
editText = (EditText) findViewById(R.id.edit_text);
editText.addTextChangedListener(new TextWatcher() {
@ -94,6 +107,10 @@ public class ClearableEditText extends FrameLayout {
editText.setText("");
}
});
engineIcon = (ImageView) findViewById(R.id.engine_icon);
focusedBackground = getResources().getDrawable(R.drawable.edit_text_focused);
defaultBackgound = getResources().getDrawable(R.drawable.edit_text_default);
inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
}
@ -109,6 +126,24 @@ public class ClearableEditText extends FrameLayout {
return editText.getText().toString();
}
public void setEngine(SearchEngine engine) {
int color = engine.getColor();
if (color == Color.TRANSPARENT) {
// Fall back to default orange if the search engine doesn't specify a color.
color = getResources().getColor(R.color.highlight_orange);
}
// Update the focused background color.
focusedBackground.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
final String iconURL = engine.getIconURL();
final BitmapDrawable d = new BitmapDrawable(getResources(), BitmapUtils.getBitmapFromDataURI(iconURL));
engineIcon.setImageDrawable(d);
engineIcon.setContentDescription(engine.getName());
editText.setHint(getResources().getString(R.string.search_bar_hint, engine.getName()));
}
@SuppressWarnings("deprecation")
public void setActive(boolean active) {
if (this.active == active) {
return;
@ -123,6 +158,11 @@ public class ClearableEditText extends FrameLayout {
final int leftDrawable = active ? R.drawable.search_icon_active : R.drawable.search_icon_inactive;
editText.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, 0, 0, 0);
// We can't use a selector drawable because we apply a color filter to the focused
// background at run time.
// TODO: setBackgroundDrawable is deprecated in API level 16
editText.setBackgroundDrawable(active ? focusedBackground : defaultBackgound);
if (active) {
editText.requestFocus();
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
@ -136,6 +176,7 @@ public class ClearableEditText extends FrameLayout {
// Only show the clear button when there is text in the input.
final boolean visible = active && (editText.getText().length() > 0);
clearButton.setVisibility(visible ? View.VISIBLE : View.GONE);
engineIcon.setVisibility(visible ? View.GONE : View.VISIBLE);
}
public void setTextListener(TextListener listener) {

Просмотреть файл

@ -4,6 +4,7 @@
package org.mozilla.search.providers;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.util.Xml;
@ -194,6 +195,14 @@ public class SearchEngine {
return iconURL;
}
public int getColor() {
// TOOD: Add brand colors to search plugin XML.
if (identifier.equals("yahoo")) {
return 0xFF500095;
}
return Color.TRANSPARENT;
}
/**
* Determine whether a particular url belongs to this search engine. If not,
* the url will be sent to Fennec.

Просмотреть файл

@ -1,10 +0,0 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/edit_text_focused"/>
<item android:drawable="@drawable/edit_text_default"/>
</selector>

Просмотреть файл

@ -6,9 +6,9 @@
<!-- Make sure the border only appears at the bottom of the background -->
<item
android:top="-1dp"
android:right="-1dp"
android:left="-1dp">
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<!-- Padding creates vertical space between the text and the underline -->
<padding

Просмотреть файл

@ -10,8 +10,13 @@
android:right="-2dp"
android:left="-2dp">
<shape>
<!-- Padding creates vertical space between the text and the underline -->
<padding
android:top="@dimen/search_bar_padding_y"
android:bottom="@dimen/search_bar_padding_y"/>
<solid android:color="@android:color/transparent"/>
<stroke android:width="2dp" android:color="@color/highlight_orange"/>
<!-- We apply a color filter to set the color for the selected search engine -->
<stroke android:width="2dp" android:color="@android:color/white"/>
</shape>
</item>

Просмотреть файл

@ -7,8 +7,8 @@
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<org.mozilla.search.autocomplete.ClearableEditText
android:id="@+id/search_edit_text"
<org.mozilla.search.autocomplete.SearchBar
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/search_bar_height"
android:paddingTop="@dimen/search_bar_padding_y"

Просмотреть файл

@ -14,14 +14,12 @@
android:drawableLeft="@drawable/search_icon_inactive"
android:drawablePadding="5dp"
android:textSize="@dimen/query_text_size"
android:background="@drawable/edit_text_background"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorHighlight="@color/highlight_orange"
android:textSelectHandle="@drawable/handle_middle"
android:textSelectHandleLeft="@drawable/handle_start"
android:textSelectHandleRight="@drawable/handle_end"
android:hint="@string/search_for_something" />
android:textSelectHandleRight="@drawable/handle_end" />
<ImageButton
android:id="@+id/clear_button"
@ -34,4 +32,12 @@
android:scaleType="centerInside"
android:visibility="gone"/>
<ImageView
android:id="@+id/engine_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="right|center_vertical"
android:background="@android:color/transparent"
android:visibility="gone"/>
</merge>

Просмотреть файл

@ -7,7 +7,7 @@
search_activity_sources = [
'java/org/mozilla/search/AcceptsSearchQuery.java',
'java/org/mozilla/search/autocomplete/AutoCompleteAdapter.java',
'java/org/mozilla/search/autocomplete/ClearableEditText.java',
'java/org/mozilla/search/autocomplete/SearchBar.java',
'java/org/mozilla/search/autocomplete/SuggestionsFragment.java',
'java/org/mozilla/search/Constants.java',
'java/org/mozilla/search/MainActivity.java',

Просмотреть файл

@ -1,7 +1,7 @@
<string name="search_plus_content_description">&search_plus_content_description;</string>
<string name="search_app_name">&search_app_name;</string>
<string name="search_for_something">&search_for_something;</string>
<string name="search_bar_hint">&search_bar_hint;</string>
<string name="search_empty_title">&search_empty_title;</string>
<string name="search_empty_message">&search_empty_message;</string>