1 /* 2 * Copyright 2019 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.car.media.widgets; 18 19 import android.content.Context; 20 import android.text.Editable; 21 import android.text.TextUtils; 22 import android.text.TextWatcher; 23 import android.util.AttributeSet; 24 import android.view.LayoutInflater; 25 import android.view.inputmethod.EditorInfo; 26 import android.view.inputmethod.InputMethodManager; 27 import android.widget.EditText; 28 import android.widget.ImageView; 29 import android.widget.LinearLayout; 30 31 import androidx.fragment.app.FragmentActivity; 32 33 import com.android.car.media.R; 34 import com.android.car.media.common.MediaAppSelectorWidget; 35 36 /** 37 * This widget represents a search bar that shows the media source's icon as part of the search bar 38 */ 39 public class SearchBar extends LinearLayout { 40 41 private final MediaAppSelectorWidget mAppIcon; 42 private final EditText mSearchText; 43 private final ImageView mCloseIcon; 44 45 private AppBarView.AppBarListener mListener; 46 SearchBar(Context context)47 public SearchBar(Context context) { 48 this(context, null); 49 } 50 SearchBar(Context context, AttributeSet attrs)51 public SearchBar(Context context, AttributeSet attrs) { 52 this(context, attrs, 0); 53 } 54 SearchBar(Context context, AttributeSet attrs, int defStyleAttr)55 public SearchBar(Context context, AttributeSet attrs, int defStyleAttr) { 56 this(context, attrs, defStyleAttr, 0); 57 } 58 SearchBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)59 public SearchBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 60 super(context, attrs, defStyleAttr, defStyleRes); 61 62 LayoutInflater inflater = LayoutInflater.from(context); 63 inflater.inflate(R.layout.search_bar, this, true); 64 65 mSearchText = findViewById(R.id.search_bar); 66 mCloseIcon = findViewById(R.id.search_close); 67 mCloseIcon.setOnClickListener(view -> mSearchText.getText().clear()); 68 mAppIcon = findViewById(R.id.app_icon_container); 69 70 mSearchText.setOnFocusChangeListener( 71 (view, hasFocus) -> { 72 if (hasFocus) { 73 mSearchText.setCursorVisible(true); 74 ((InputMethodManager) 75 context.getSystemService(Context.INPUT_METHOD_SERVICE)) 76 .showSoftInput(view, 0); 77 } else { 78 mSearchText.setCursorVisible(false); 79 ((InputMethodManager) 80 context.getSystemService(Context.INPUT_METHOD_SERVICE)) 81 .hideSoftInputFromWindow(view.getWindowToken(), 0); 82 } 83 }); 84 mSearchText.addTextChangedListener(new TextWatcher() { 85 @Override 86 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 87 } 88 89 @Override 90 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 91 } 92 93 @Override 94 public void afterTextChanged(Editable editable) { 95 onSearch(editable.toString()); 96 } 97 }); 98 mSearchText.setOnEditorActionListener((v, actionId, event) -> { 99 if (actionId == EditorInfo.IME_ACTION_DONE) { 100 mSearchText.setCursorVisible(false); 101 } 102 return false; 103 }); 104 } 105 106 /** Calling this is required so the widget can show the icon of the primary media source. */ setFragmentActivity(FragmentActivity activity)107 public void setFragmentActivity(FragmentActivity activity) { 108 109 mAppIcon.setFragmentActivity(activity); 110 } 111 setAppBarListener(AppBarView.AppBarListener listener)112 public void setAppBarListener(AppBarView.AppBarListener listener) { 113 mListener = listener; 114 } 115 showSearchBar(boolean visible)116 public void showSearchBar(boolean visible) { 117 if (visible) { 118 setVisibility(VISIBLE); 119 mSearchText.requestFocus(); 120 } else{ 121 setVisibility(GONE); 122 mSearchText.getText().clear(); 123 } 124 } 125 onSearch(String query)126 private void onSearch(String query) { 127 if (mListener == null || TextUtils.isEmpty(query)) { 128 return; 129 } 130 mListener.onSearch(query); 131 } 132 } 133