Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Commit

Permalink
Bug 1046485 - Move search bar out of SearchFragment. r=eedens
Browse files Browse the repository at this point in the history
  • Loading branch information
leibovic committed Aug 5, 2014
1 parent a9524e6 commit 59c99e7
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 202 deletions.
Expand Up @@ -9,7 +9,7 @@
import org.mozilla.search.Constants;
import org.mozilla.search.MainActivity;
import org.mozilla.search.PostSearchFragment;
import org.mozilla.search.autocomplete.SearchFragment;
import org.mozilla.search.autocomplete.SuggestionsFragment;
import org.mozilla.search.PreSearchFragment;

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
Expand Down Expand Up @@ -83,8 +83,8 @@ private PreSearchFragment getPreSearchFragment() {
.getSupportFragmentManager().findFragmentByTag(Constants.PRESEARCH_FRAGMENT);
}

private SearchFragment getSearchFragment() {
return (SearchFragment) getActivity()
private SuggestionsFragment getSearchFragment() {
return (SuggestionsFragment) getActivity()
.getSupportFragmentManager().findFragmentByTag(Constants.SEARCH_FRAGMENT);
}

Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/org/mozilla/search/AcceptsSearchQuery.java
Expand Up @@ -10,6 +10,13 @@
* Allows fragments to pass a search event to the main activity.
*/
public interface AcceptsSearchQuery {

/**
* Shows search suggestions.
* @param query
*/
void onSuggest(String query);

/**
* Starts a search.
*
Expand All @@ -30,6 +37,5 @@ public interface AcceptsSearchQuery {
*/
public interface SuggestionAnimation {
public Rect getStartBounds();
public void onAnimationEnd();
}
}
128 changes: 97 additions & 31 deletions app/src/main/java/org/mozilla/search/MainActivity.java
Expand Up @@ -7,9 +7,9 @@
import android.content.AsyncQueryHandler;
import android.content.ContentValues;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
Expand All @@ -22,27 +22,40 @@
import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.db.BrowserContract.SearchHistory;
import org.mozilla.search.autocomplete.SearchFragment;
import org.mozilla.search.autocomplete.ClearableEditText;
import org.mozilla.search.autocomplete.SuggestionsFragment;

/**
* The main entrance for the Android search intent.
* <p/>
* State management is delegated to child fragments. Fragments communicate
* with each other by passing messages through this activity. The only message passing right
* now, the only message passing occurs when a user wants to submit a search query. That
* passes through the onSearch method here.
* with each other by passing messages through this activity.
*/
public class MainActivity extends FragmentActivity implements AcceptsSearchQuery {

enum State {
START,
static enum SearchState {
PRESEARCH,
POSTSEARCH
}

private State state = State.START;
static enum EditState {
WAITING,
EDITING
}

private SearchState searchState;
private EditState editState;

private AsyncQueryHandler queryHandler;

// Main views in layout.
private ClearableEditText editText;
private View preSearch;
private View postSearch;

private View suggestionsContainer;
private SuggestionsFragment suggestionsFragment;

private static final int SUGGESTION_TRANSITION_DURATION = 300;
private static final Interpolator SUGGESTION_TRANSITION_INTERPOLATOR =
new AccelerateDecelerateInterpolator();
Expand All @@ -65,6 +78,47 @@ protected void onCreate(Bundle stateBundle) {

queryHandler = new AsyncQueryHandler(getContentResolver()) {};

editText = (ClearableEditText) findViewById(R.id.search_edit_text);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setEditState(EditState.EDITING);
}
});

editText.setTextListener(new ClearableEditText.TextListener() {
@Override
public void onChange(String text) {
// Only load suggestions if we're in edit mode.
if (editState == EditState.EDITING) {
suggestionsFragment.loadSuggestions(text);
}
}

@Override
public void onSubmit(String text) {
// Don't submit an empty query.
final String trimmedQuery = text.trim();
if (!TextUtils.isEmpty(trimmedQuery)) {
onSearch(trimmedQuery);
}
}
});

preSearch = findViewById(R.id.presearch);
postSearch = findViewById(R.id.postsearch);

suggestionsContainer = findViewById(R.id.suggestions_container);
suggestionsFragment = (SuggestionsFragment) getSupportFragmentManager().findFragmentById(R.id.suggestions);

// Dismiss edit mode when the user taps outside of the suggestions.
findViewById(R.id.suggestions_container).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setEditState(EditState.WAITING);
}
});

animationText = (TextView) findViewById(R.id.animation_text);
animationCard = findViewById(R.id.animation_card);

Expand All @@ -77,7 +131,11 @@ protected void onCreate(Bundle stateBundle) {
protected void onDestroy() {
super.onDestroy();
queryHandler = null;

editText = null;
preSearch = null;
postSearch = null;
suggestionsFragment = null;
suggestionsContainer = null;
animationText = null;
animationCard = null;
}
Expand All @@ -97,8 +155,14 @@ protected void onStop() {
@Override
protected void onResume() {
super.onResume();

// When the app launches, make sure we're in presearch *always*
startPresearch();
setSearchState(SearchState.PRESEARCH);
}

@Override
public void onSuggest(String query) {
editText.setText(query);
}

@Override
Expand All @@ -118,7 +182,8 @@ public void onSearch(String query, SuggestionAnimation suggestionAnimation) {
animateSuggestion(query, suggestionAnimation);
} else {
// Otherwise immediately switch to the results view.
startPostsearch();
setEditState(EditState.WAITING);
setSearchState(SearchState.POSTSEARCH);
}
}

Expand Down Expand Up @@ -164,15 +229,10 @@ public void onAnimationStart(Animator animation) {

@Override
public void onAnimationEnd(Animator animation) {
// This is crappy, but right now we need to make sure we call this before
// setSearchTerm so that we don't kick off a suggestion request.
suggestionAnimation.onAnimationEnd();
setEditState(EditState.WAITING);
setSearchState(SearchState.POSTSEARCH);

// TODO: Find a way to do this without needing to access SearchFragment.
final SearchFragment searchFragment = ((SearchFragment) getSupportFragmentManager().findFragmentById(R.id.search_fragment));
searchFragment.setSearchTerm(query);

startPostsearch();
editText.setText(query);

// We need to manually clear the animation for the views to be hidden on gingerbread.
animationText.clearAnimation();
Expand All @@ -197,26 +257,32 @@ public void onAnimationRepeat(Animator animation) {
set.start();
}

private void startPresearch() {
if (state != State.PRESEARCH) {
state = State.PRESEARCH;
findViewById(R.id.postsearch).setVisibility(View.INVISIBLE);
findViewById(R.id.presearch).setVisibility(View.VISIBLE);
private void setEditState(EditState editState) {
if (this.editState == editState) {
return;
}
this.editState = editState;

editText.setActive(editState == EditState.EDITING);
suggestionsContainer.setVisibility(editState == EditState.EDITING ? View.VISIBLE : View.INVISIBLE);
}

private void startPostsearch() {
if (state != State.POSTSEARCH) {
state = State.POSTSEARCH;
findViewById(R.id.presearch).setVisibility(View.INVISIBLE);
findViewById(R.id.postsearch).setVisibility(View.VISIBLE);
private void setSearchState(SearchState searchState) {
if (this.searchState == searchState) {
return;
}
this.searchState = searchState;

preSearch.setVisibility(searchState == SearchState.PRESEARCH ? View.VISIBLE : View.INVISIBLE);
postSearch.setVisibility(searchState == SearchState.POSTSEARCH ? View.VISIBLE : View.INVISIBLE);
}

@Override
public void onBackPressed() {
if (state == State.POSTSEARCH) {
startPresearch();
if (editState == EditState.EDITING) {
setEditState(EditState.WAITING);
} else if (searchState == SearchState.POSTSEARCH) {
setSearchState(SearchState.PRESEARCH);
} else {
super.onBackPressed();
}
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/org/mozilla/search/PreSearchFragment.java
Expand Up @@ -110,10 +110,6 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
public Rect getStartBounds() {
return startBounds;
}

@Override
public void onAnimationEnd() {
}
});
}
}
Expand Down

This file was deleted.

Expand Up @@ -11,8 +11,9 @@
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.mozilla.search.AcceptsSearchQuery;
import org.mozilla.search.R;
import org.mozilla.search.autocomplete.SearchFragment.Suggestion;
import org.mozilla.search.autocomplete.SuggestionsFragment.Suggestion;

import java.util.List;

Expand All @@ -21,15 +22,20 @@
*/
class AutoCompleteAdapter extends ArrayAdapter<Suggestion> {

private final AcceptsJumpTaps acceptsJumpTaps;
private final AcceptsSearchQuery searchListener;

private final LayoutInflater inflater;

public AutoCompleteAdapter(Context context, AcceptsJumpTaps acceptsJumpTaps) {
public AutoCompleteAdapter(Context context) {
// Uses '0' for the template id since we are overriding getView
// and supplying our own view.
super(context, 0);
this.acceptsJumpTaps = acceptsJumpTaps;

if (context instanceof AcceptsSearchQuery) {
searchListener = (AcceptsSearchQuery) context;
} else {
throw new ClassCastException(context.toString() + " must implement AcceptsSearchQuery.");
}

// Disable notifying on change. We will notify ourselves in update.
setNotifyOnChange(false);
Expand All @@ -40,7 +46,7 @@ public AutoCompleteAdapter(Context context, AcceptsJumpTaps acceptsJumpTaps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.search_auto_complete_row, null);
convertView = inflater.inflate(R.layout.search_suggestions_row, null);
}

final Suggestion suggestion = getItem(position);
Expand All @@ -52,7 +58,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
jumpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
acceptsJumpTaps.onJumpTap(suggestion.value);
searchListener.onSuggest(suggestion.value);
}
});

Expand Down

0 comments on commit 59c99e7

Please sign in to comment.