1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.search.actionbar; 18 19 import android.annotation.NonNull; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.os.Bundle; 25 import android.view.Menu; 26 import android.view.MenuInflater; 27 import android.view.MenuItem; 28 29 import androidx.fragment.app.Fragment; 30 31 import com.android.settings.R; 32 import com.android.settings.Utils; 33 import com.android.settings.core.InstrumentedFragment; 34 import com.android.settings.core.InstrumentedPreferenceFragment; 35 import com.android.settings.overlay.FeatureFactory; 36 import com.android.settings.search.SearchFeatureProvider; 37 import com.android.settingslib.core.lifecycle.LifecycleObserver; 38 import com.android.settingslib.core.lifecycle.events.OnCreateOptionsMenu; 39 40 public class SearchMenuController implements LifecycleObserver, OnCreateOptionsMenu { 41 42 public static final String NEED_SEARCH_ICON_IN_ACTION_BAR = "need_search_icon_in_action_bar"; 43 44 private final Fragment mHost; 45 private final int mPageId; 46 init(@onNull InstrumentedPreferenceFragment host)47 public static void init(@NonNull InstrumentedPreferenceFragment host) { 48 host.getSettingsLifecycle().addObserver( 49 new SearchMenuController(host, host.getMetricsCategory())); 50 } 51 init(@onNull InstrumentedFragment host)52 public static void init(@NonNull InstrumentedFragment host) { 53 host.getSettingsLifecycle().addObserver( 54 new SearchMenuController(host, host.getMetricsCategory())); 55 } 56 SearchMenuController(@onNull Fragment host, int pageId)57 private SearchMenuController(@NonNull Fragment host, int pageId) { 58 mHost = host; 59 mPageId = pageId; 60 } 61 62 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)63 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 64 final Context context = mHost.getContext(); 65 final String SettingsIntelligencePkgName = context.getString( 66 R.string.config_settingsintelligence_package_name); 67 if (!Utils.isDeviceProvisioned(mHost.getContext())) { 68 return; 69 } 70 if (!Utils.isPackageEnabled(mHost.getContext(), SettingsIntelligencePkgName)) { 71 return; 72 } 73 if (menu == null) { 74 return; 75 } 76 final Bundle arguments = mHost.getArguments(); 77 if (arguments != null && !arguments.getBoolean(NEED_SEARCH_ICON_IN_ACTION_BAR, true)) { 78 return; 79 } 80 final MenuItem searchItem = menu.add(Menu.NONE, Menu.NONE, 0 /* order */, 81 R.string.search_menu); 82 searchItem.setIcon(R.drawable.ic_search_24dp); 83 searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 84 85 searchItem.setOnMenuItemClickListener(target -> { 86 final Intent intent = FeatureFactory.getFactory(context) 87 .getSearchFeatureProvider() 88 .buildSearchIntent(context, mPageId); 89 90 if (context.getPackageManager().queryIntentActivities(intent, 91 PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) { 92 return true; 93 } 94 95 FeatureFactory.getFactory(context).getMetricsFeatureProvider() 96 .action(context, SettingsEnums.ACTION_SEARCH_RESULTS); 97 mHost.startActivityForResult(intent, SearchFeatureProvider.REQUEST_CODE); 98 return true; 99 }); 100 } 101 } 102