Moving createMarketSearchIntent to search controller to allow it to be customizable

Change-Id: I3a05abf7c0357a2f92c8a3fa2dc223f9eacc221e
This commit is contained in:
Sunny Goyal
2016-03-03 11:35:59 -08:00
parent 9e3fee1427
commit 2494c3f168
3 changed files with 50 additions and 73 deletions

View File

@@ -229,6 +229,7 @@ public class AllAppsContainerView extends BaseContainerView implements DragSourc
}
mSearchBarController = searchController;
mSearchBarController.initialize(mApps, mSearchInput, mLauncher, this);
mAdapter.setSearchController(mSearchBarController);
updateBackgroundAndPaddings();
}
@@ -261,7 +262,6 @@ public class AllAppsContainerView extends BaseContainerView implements DragSourc
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mAdapter.setRtl(Utilities.isRtl(getResources()));
// This is a focus listener that proxies focus from a view into the list view. This is to
// work around the search box from getting first focus and showing the cursor.

View File

@@ -324,20 +324,29 @@ public class AllAppsGridAdapter extends RecyclerView.Adapter<AllAppsGridAdapter.
}
}
private Launcher mLauncher;
private LayoutInflater mLayoutInflater;
@Thunk AlphabeticalAppsList mApps;
private GridLayoutManager mGridLayoutMgr;
private GridSpanSizer mGridSizer;
private GridItemDecoration mItemDecoration;
private View.OnTouchListener mTouchListener;
private View.OnClickListener mIconClickListener;
private View.OnLongClickListener mIconLongClickListener;
private final Launcher mLauncher;
private final LayoutInflater mLayoutInflater;
private final AlphabeticalAppsList mApps;
private final GridLayoutManager mGridLayoutMgr;
private final GridSpanSizer mGridSizer;
private final GridItemDecoration mItemDecoration;
private final View.OnTouchListener mTouchListener;
private final View.OnClickListener mIconClickListener;
private final View.OnLongClickListener mIconLongClickListener;
private final Rect mBackgroundPadding = new Rect();
private final boolean mIsRtl;
// Section drawing
private final int mSectionNamesMargin;
private final int mSectionHeaderOffset;
private final Paint mSectionTextPaint;
private final Paint mPredictedAppsDividerPaint;
private final int mPredictionBarDividerOffset;
private int mAppsPerRow;
private BindViewCallback mBindViewCallback;
@Thunk final Rect mBackgroundPadding = new Rect();
@Thunk int mPredictionBarDividerOffset;
@Thunk int mAppsPerRow;
@Thunk boolean mIsRtl;
private AllAppsSearchBarController mSearchController;
// The text to show when there are no search results and no market search handler.
private String mEmptySearchMessage;
@@ -349,14 +358,6 @@ public class AllAppsGridAdapter extends RecyclerView.Adapter<AllAppsGridAdapter.
private String mMarketSearchMessage;
// The intent to send off to the market app, updated each time the search query changes.
private Intent mMarketSearchIntent;
// The last query that the user entered into the search field
private String mLastSearchQuery;
// Section drawing
@Thunk int mSectionNamesMargin;
@Thunk int mSectionHeaderOffset;
@Thunk Paint mSectionTextPaint;
@Thunk Paint mPredictedAppsDividerPaint;
public AllAppsGridAdapter(Launcher launcher, AlphabeticalAppsList apps,
View.OnTouchListener touchListener, View.OnClickListener iconClickListener,
@@ -375,28 +376,19 @@ public class AllAppsGridAdapter extends RecyclerView.Adapter<AllAppsGridAdapter.
mIconLongClickListener = iconLongClickListener;
mSectionNamesMargin = res.getDimensionPixelSize(R.dimen.all_apps_grid_view_start_margin);
mSectionHeaderOffset = res.getDimensionPixelSize(R.dimen.all_apps_grid_section_y_offset);
mIsRtl = Utilities.isRtl(res);
mSectionTextPaint = new Paint();
mSectionTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mSectionTextPaint.setTextSize(res.getDimensionPixelSize(
R.dimen.all_apps_grid_section_text_size));
mSectionTextPaint.setColor(res.getColor(R.color.all_apps_grid_section_text_color));
mSectionTextPaint.setAntiAlias(true);
mPredictedAppsDividerPaint = new Paint();
mPredictedAppsDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPredictedAppsDividerPaint.setStrokeWidth(Utilities.pxFromDp(1f, res.getDisplayMetrics()));
mPredictedAppsDividerPaint.setColor(0x1E000000);
mPredictedAppsDividerPaint.setAntiAlias(true);
mPredictionBarDividerOffset =
(-res.getDimensionPixelSize(R.dimen.all_apps_prediction_icon_bottom_padding) +
res.getDimensionPixelSize(R.dimen.all_apps_icon_top_bottom_padding)) / 2;
// Resolve the market app handling additional searches
PackageManager pm = launcher.getPackageManager();
ResolveInfo marketInfo = pm.resolveActivity(createMarketSearchIntent(""),
PackageManager.MATCH_DEFAULT_ONLY);
if (marketInfo != null) {
mMarketAppName = marketInfo.loadLabel(pm).toString();
}
}
/**
@@ -407,11 +399,16 @@ public class AllAppsGridAdapter extends RecyclerView.Adapter<AllAppsGridAdapter.
mGridLayoutMgr.setSpanCount(appsPerRow);
}
/**
* Sets whether we are in RTL mode.
*/
public void setRtl(boolean rtl) {
mIsRtl = rtl;
public void setSearchController(AllAppsSearchBarController searchController) {
mSearchController = searchController;
// Resolve the market app handling additional searches
PackageManager pm = mLauncher.getPackageManager();
ResolveInfo marketInfo = pm.resolveActivity(mSearchController.createMarketSearchIntent(""),
PackageManager.MATCH_DEFAULT_ONLY);
if (marketInfo != null) {
mMarketAppName = marketInfo.loadLabel(pm).toString();
}
}
/**
@@ -421,12 +418,11 @@ public class AllAppsGridAdapter extends RecyclerView.Adapter<AllAppsGridAdapter.
public void setLastSearchQuery(String query) {
Resources res = mLauncher.getResources();
String formatStr = res.getString(R.string.all_apps_no_search_results);
mLastSearchQuery = query;
mEmptySearchMessage = String.format(formatStr, query);
if (mMarketAppName != null) {
mMarketSearchMessage = String.format(res.getString(R.string.all_apps_search_market_message),
mMarketAppName);
mMarketSearchIntent = createMarketSearchIntent(query);
mMarketSearchIntent = mSearchController.createMarketSearchIntent(query);
}
}
@@ -563,17 +559,4 @@ public class AllAppsGridAdapter extends RecyclerView.Adapter<AllAppsGridAdapter.
AlphabeticalAppsList.AdapterItem item = mApps.getAdapterItems().get(position);
return item.viewType;
}
/**
* Creates a new market search intent.
*/
public static Intent createMarketSearchIntent(String query) {
Uri marketSearchUri = Uri.parse("market://search")
.buildUpon()
.appendQueryParameter("q", query)
.build();
Intent marketSearchIntent = new Intent(Intent.ACTION_VIEW);
marketSearchIntent.setData(marketSearchUri);
return marketSearchIntent;
}
}

View File

@@ -16,12 +16,13 @@
package com.android.launcher3.allapps;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
@@ -70,11 +71,9 @@ public abstract class AllAppsSearchBarController
}
/**
* To be overridden by subclasses. This method will get called when the controller is set.
* To be implemented by subclasses. This method will get called when the controller is set.
*/
protected DefaultAppSearchAlgorithm onInitializeSearch() {
return null;
}
protected abstract DefaultAppSearchAlgorithm onInitializeSearch();
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@@ -109,8 +108,7 @@ public abstract class AllAppsSearchBarController
if (query.isEmpty()) {
return false;
}
return mLauncher.startActivitySafely(
v, AllAppsGridAdapter.createMarketSearchIntent(query), null);
return mLauncher.startActivitySafely(v, createMarketSearchIntent(query), null);
}
@Override
@@ -142,14 +140,6 @@ public abstract class AllAppsSearchBarController
}
}
/**
* Returns the search bar view.
* @param parent the parent to attach the search bar view to.
*/
public View getView(ViewGroup parent) {
return null;
}
/**
* Focuses the search field to handle key events.
*/
@@ -166,11 +156,15 @@ public abstract class AllAppsSearchBarController
}
/**
* Returns whether the prediction bar should currently be visible depending on the state of
* the search bar.
* Creates a new market search intent.
*/
public boolean shouldShowPredictionBar() {
return false;
public Intent createMarketSearchIntent(String query) {
Uri marketSearchUri = Uri.parse("market://search")
.buildUpon()
.appendQueryParameter("c", "apps")
.appendQueryParameter("q", query)
.build();
return new Intent(Intent.ACTION_VIEW).setData(marketSearchUri);
}
/**