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.Activity; 21 import android.app.settings.SettingsEnums; 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 import com.google.android.setupcompat.util.WizardManagerHelper; 41 42 public class SearchMenuController implements LifecycleObserver, OnCreateOptionsMenu { 43 44 public static final String NEED_SEARCH_ICON_IN_ACTION_BAR = "need_search_icon_in_action_bar"; 45 public static final int MENU_SEARCH = Menu.FIRST + 10; 46 47 private final Fragment mHost; 48 private final int mPageId; 49 init(@onNull InstrumentedPreferenceFragment host)50 public static void init(@NonNull InstrumentedPreferenceFragment host) { 51 host.getSettingsLifecycle().addObserver( 52 new SearchMenuController(host, host.getMetricsCategory())); 53 } 54 init(@onNull InstrumentedFragment host)55 public static void init(@NonNull InstrumentedFragment host) { 56 host.getSettingsLifecycle().addObserver( 57 new SearchMenuController(host, host.getMetricsCategory())); 58 } 59 SearchMenuController(@onNull Fragment host, int pageId)60 private SearchMenuController(@NonNull Fragment host, int pageId) { 61 mHost = host; 62 mPageId = pageId; 63 } 64 65 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)66 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 67 final Activity activity = mHost.getActivity(); 68 final String SettingsIntelligencePkgName = activity.getString( 69 R.string.config_settingsintelligence_package_name); 70 if (!WizardManagerHelper.isDeviceProvisioned(activity) 71 || WizardManagerHelper.isAnySetupWizard(activity.getIntent())) { 72 return; 73 } 74 if (!Utils.isPackageEnabled(activity, SettingsIntelligencePkgName)) { 75 return; 76 } 77 if (menu == null) { 78 return; 79 } 80 final Bundle arguments = mHost.getArguments(); 81 if (arguments != null && !arguments.getBoolean(NEED_SEARCH_ICON_IN_ACTION_BAR, true)) { 82 return; 83 } 84 // menu contains search item, skip it 85 if (menu.findItem(MENU_SEARCH) != null) { 86 return; 87 } 88 final MenuItem searchItem = menu.add(Menu.NONE, MENU_SEARCH, 0 /* order */, 89 R.string.search_menu); 90 searchItem.setIcon(R.drawable.ic_search_24dp); 91 searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 92 93 searchItem.setOnMenuItemClickListener(target -> { 94 final Intent intent = FeatureFactory.getFactory(activity) 95 .getSearchFeatureProvider() 96 .buildSearchIntent(activity, mPageId); 97 98 if (activity.getPackageManager().queryIntentActivities(intent, 99 PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) { 100 return true; 101 } 102 103 FeatureFactory.getFactory(activity).getMetricsFeatureProvider() 104 .action(activity, SettingsEnums.ACTION_SEARCH_RESULTS); 105 mHost.startActivityForResult(intent, SearchFeatureProvider.REQUEST_CODE); 106 return true; 107 }); 108 } 109 } 110