1 /*
2  * Copyright (C) 2021 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.launcher3.widget.picker.search;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.ImageButton;
22 import android.widget.LinearLayout;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 
27 import com.android.launcher3.ExtendedEditText;
28 import com.android.launcher3.R;
29 import com.android.launcher3.popup.PopupDataProvider;
30 
31 /**
32  * View for a search bar with an edit text with a cancel button.
33  */
34 public class LauncherWidgetsSearchBar extends LinearLayout implements WidgetsSearchBar {
35     private WidgetsSearchBarController mController;
36     private ExtendedEditText mEditText;
37     private ImageButton mCancelButton;
38 
LauncherWidgetsSearchBar(Context context)39     public LauncherWidgetsSearchBar(Context context) {
40         this(context, null, 0);
41     }
42 
LauncherWidgetsSearchBar(@onNull Context context, @Nullable AttributeSet attrs)43     public LauncherWidgetsSearchBar(@NonNull Context context,
44             @Nullable AttributeSet attrs) {
45         this(context, attrs, 0);
46     }
47 
LauncherWidgetsSearchBar(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)48     public LauncherWidgetsSearchBar(@NonNull Context context, @Nullable AttributeSet attrs,
49             int defStyleAttr) {
50         super(context, attrs, defStyleAttr);
51     }
52 
53     @Override
initialize(PopupDataProvider dataProvider, SearchModeListener searchModeListener)54     public void initialize(PopupDataProvider dataProvider, SearchModeListener searchModeListener) {
55         mController = new WidgetsSearchBarController(
56                 new SimpleWidgetsSearchAlgorithm(dataProvider),
57                 mEditText, mCancelButton, searchModeListener);
58     }
59 
60     @Override
reset()61     public void reset() {
62         mController.clearSearchResult();
63     }
64 
65     @Override
onFinishInflate()66     protected void onFinishInflate() {
67         super.onFinishInflate();
68         mEditText = findViewById(R.id.widgets_search_bar_edit_text);
69         mCancelButton = findViewById(R.id.widgets_search_cancel_button);
70     }
71 
72     @Override
onDetachedFromWindow()73     protected void onDetachedFromWindow() {
74         super.onDetachedFromWindow();
75         mController.onDestroy();
76     }
77 
78     @Override
isSearchBarFocused()79     public boolean isSearchBarFocused() {
80         return mEditText.isFocused();
81     }
82 
83     @Override
clearSearchBarFocus()84     public void clearSearchBarFocus() {
85         mController.clearFocus();
86     }
87 }
88