Home | History | Annotate | Download | only in keyboard
      1 /*
      2  * Copyright (C) 2015 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 package com.google.android.car.kitchensink.keyboard;
     17 
     18 import android.os.Bundle;
     19 import android.os.Handler;
     20 import android.support.annotation.Nullable;
     21 import android.support.car.app.menu.CarDrawerActivity;
     22 import android.support.car.app.menu.SearchBoxEditListener;
     23 import android.support.v4.app.Fragment;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.Button;
     28 import android.widget.TextView;
     29 
     30 import com.google.android.car.kitchensink.R;
     31 
     32 public class KeyboardFragment extends Fragment {
     33     public static final int CARD = 0xfffafafa;
     34     public static final int TEXT_PRIMARY_DAY = 0xde000000;
     35     public static final int TEXT_SECONDARY_DAY = 0x8a000000;
     36 
     37     private Button mImeButton;
     38     private Button mCloseImeButton;
     39     private Button mShowHideInputButton;
     40     private CarDrawerActivity mActivity;
     41     private TextView mOnSearchText;
     42     private TextView mOnEditText;
     43 
     44     private final Handler mHandler = new Handler();
     45 
     46     @Override
     47     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
     48                              @Nullable Bundle savedInstanceState) {
     49         View v = inflater.inflate(R.layout.keyboard_test, container, false);
     50         mActivity = (CarDrawerActivity) getHost();
     51         mImeButton = (Button) v.findViewById(R.id.ime_button);
     52         mImeButton.setOnClickListener(new View.OnClickListener() {
     53             @Override
     54             public void onClick(View view) {
     55                 mActivity.startInput("Hint");
     56             }
     57         });
     58 
     59         mCloseImeButton = (Button) v.findViewById(R.id.stop_ime_button);
     60         mCloseImeButton.setOnClickListener(new View.OnClickListener() {
     61             @Override
     62             public void onClick(View view) {
     63                 mActivity.stopInput();
     64                 resetInput();
     65             }
     66         });
     67 
     68         mShowHideInputButton = (Button) v.findViewById(R.id.ime_button2);
     69         mShowHideInputButton.setOnClickListener(new View.OnClickListener() {
     70             @Override
     71             public void onClick(View view) {
     72                 if (mActivity.isShowingSearchBox()) {
     73                     mActivity.hideSearchBox();
     74                 } else {
     75                     resetInput();
     76                 }
     77             }
     78         });
     79 
     80         mOnSearchText = (TextView) v.findViewById(R.id.search_text);
     81         mOnEditText = (TextView) v.findViewById(R.id.edit_text);
     82         resetInput();
     83         mActivity.setSearchBoxEndView(View.inflate(getContext(), R.layout.keyboard_end_view, null));
     84 
     85         return v;
     86     }
     87 
     88     private void resetInput() {
     89         mActivity.showSearchBox(new View.OnClickListener() {
     90             @Override
     91             public void onClick(View view) {
     92                 mActivity.startInput("Hint");
     93             }
     94         });
     95         mActivity.setSearchBoxEditListener(mEditListener);
     96         mActivity.setSearchBoxColors(CARD, TEXT_SECONDARY_DAY,
     97                 TEXT_PRIMARY_DAY, TEXT_SECONDARY_DAY);
     98     }
     99 
    100 
    101     private final SearchBoxEditListener mEditListener = new SearchBoxEditListener() {
    102         @Override
    103         public void onSearch(final String text) {
    104             mHandler.post(new Runnable() {
    105                 @Override
    106                 public void run() {
    107                     mOnSearchText.setText("Search: " + text);
    108                     resetInput();
    109                 }
    110             });
    111         }
    112 
    113         @Override
    114         public void onEdit(final String text) {
    115             mHandler.post(new Runnable() {
    116                 @Override
    117                 public void run() {
    118                     mOnEditText.setText("Edit: " + text);
    119                 }
    120             });
    121         }
    122     };
    123 }
    124