• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 /**
31  * View for a search bar with an edit text with a cancel button.
32  */
33 public class LauncherWidgetsSearchBar extends LinearLayout implements WidgetsSearchBar {
34     private WidgetsSearchBarController mController;
35     private ExtendedEditText mEditText;
36     private ImageButton mCancelButton;
37 
LauncherWidgetsSearchBar(Context context)38     public LauncherWidgetsSearchBar(Context context) {
39         this(context, null, 0);
40     }
41 
LauncherWidgetsSearchBar(@onNull Context context, @Nullable AttributeSet attrs)42     public LauncherWidgetsSearchBar(@NonNull Context context,
43             @Nullable AttributeSet attrs) {
44         this(context, attrs, 0);
45     }
46 
LauncherWidgetsSearchBar(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)47     public LauncherWidgetsSearchBar(@NonNull Context context, @Nullable AttributeSet attrs,
48             int defStyleAttr) {
49         super(context, attrs, defStyleAttr);
50     }
51 
52     @Override
initialize(WidgetsSearchDataProvider dataProvider, SearchModeListener searchModeListener)53     public void initialize(WidgetsSearchDataProvider dataProvider,
54             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