Q版本设置-搜索流程
前言
借鉴下面三篇O版本设置搜索流程的介绍排版,从界面,实现原理,如何新增搜索项和跳转方法这4个方面介绍Q版本的搜索流程。方便对比Q版本和O版本的区别
https://blog.csdn.net/qq_34149526/article/details/83341195
https://blog.csdn.net/qq_34149526/article/details/83378184
https://blog.csdn.net/qq_34149526/article/details/83414364
界面
位于Settings主界面布局文件settings_homepage_container.xml中:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/settings_homepage_container"
...>
...
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/search_bar"/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
搜索栏是否显示的逻辑:
SettingsHomepageActivity.java
——>onCreate():
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
final Toolbar toolbar = findViewById(R.id.search_action_bar);
FeatureFactory.getFactory(this).getSearchFeatureProvider()
.initSearchToolbar(this /* activity */, toolbar, SettingsEnums.SETTINGS_HOMEPAGE);
...
}
SearchFeatureProvider
/**
* Initializes the search toolbar.
*/
default void initSearchToolbar(Activity activity, Toolbar toolbar, int pageId) {
...
if (!Utils.isDeviceProvisioned(activity) ||
!Utils.isPackageEnabled(activity, getSettingsIntelligencePkgName(activity))) {
final ViewGroup parent = (ViewGroup) toolbar.getParent();
if (parent != null) {
parent.setVisibility(View.GONE);
}
return;
}
// Please forgive me for what I am about to do.
//
// Need to make the navigation icon non-clickable so that the entire card is clickable
// and goes to the search UI. Also set the background to null so there's no ripple.
final View navView = toolbar.getNavigationView();
navView.setClickable(false);
navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
navView.setBackground(null);
toolbar.setOnClickListener(tb -> {
final Context context = activity.getApplicationContext();
final Intent intent = buildSearchIntent(context, pageId);
if (activity.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
return;
}
FeatureFactory.getFactory(context).getSlicesFeatureProvider()
.indexSliceDataAsync(context);
FeatureFactory.getFactory(context).getMetricsFeatureProvider()
.action(context, SettingsEnums.ACTION_SEARCH_RESULTS);
activity.startActivityForResult(intent, REQUEST_CODE);
});
}
此处仅仅是按钮作用,实际是没有编辑框的。Q版本把搜索模块分出去了,不在Settings这个项目里面,分到了SettingsIntelligence(P版本之前都是在Settings项目内,版本之间改动都不大)
ps:这个模块还包含设置建议的功能
当点击搜索栏时会跳转到
packages/apps/SettingsIntelligence/src/com/android/settings/intelligence/search/SearchActivity.java
界面的显示在SearchFragment.java中实现:
布对应的布局文件search_panel.xml(与历史版本几乎一样):