1c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert/* 2c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * Copyright (C) 2010 The Android Open Source Project 3c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * 4c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 5c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * you may not use this file except in compliance with the License. 6c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * You may obtain a copy of the License at 7c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * 8c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 9c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * 10c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 11c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 12c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * See the License for the specific language governing permissions and 14c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * limitations under the License. 15c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert */ 16c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 17c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringertpackage com.android.quicksearchbox.ui; 18c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 19c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringertimport android.app.Activity; 20c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringertimport android.content.Context; 21c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringertimport android.util.AttributeSet; 22c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringertimport android.view.KeyEvent; 23c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringertimport android.view.inputmethod.InputMethodManager; 2411234b9966c6b0e5c17d00e3b973c0d49a8d1f57Bjorn Bringertimport android.widget.RelativeLayout; 25c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 26c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert/** 27c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * Finishes the containing activity on BACK, even if input method is showing. 28c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert */ 2911234b9966c6b0e5c17d00e3b973c0d49a8d1f57Bjorn Bringertpublic class SearchActivityView extends RelativeLayout { 30c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 3111234b9966c6b0e5c17d00e3b973c0d49a8d1f57Bjorn Bringert public SearchActivityView(Context context) { 32c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert super(context); 33c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 34c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 3511234b9966c6b0e5c17d00e3b973c0d49a8d1f57Bjorn Bringert public SearchActivityView(Context context, AttributeSet attrs) { 36c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert super(context, attrs); 37c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 38c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 3911234b9966c6b0e5c17d00e3b973c0d49a8d1f57Bjorn Bringert public SearchActivityView(Context context, AttributeSet attrs, int defStyle) { 40c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert super(context, attrs, defStyle); 41c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 42c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 43c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert private Activity getActivity() { 44c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert Context context = getContext(); 45c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert if (context instanceof Activity) { 46c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert return (Activity) context; 47c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } else { 48c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert return null; 49c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 50c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 51c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 52c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert /** 53c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * Hides the input method. 54c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert */ 55c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert protected void hideInputMethod() { 56c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert InputMethodManager imm = (InputMethodManager) 57c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 58c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert if (imm != null) { 59c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert imm.hideSoftInputFromWindow(getWindowToken(), 0); 60c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 61c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 62c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert 63c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert /** 64c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert * Overrides the handling of the back key to dismiss the activity. 65c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert */ 66c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert @Override 67c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert public boolean dispatchKeyEventPreIme(KeyEvent event) { 68c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert Activity activity = getActivity(); 69c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert if (activity != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { 70c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert KeyEvent.DispatcherState state = getKeyDispatcherState(); 71c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert if (state != null) { 72c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert if (event.getAction() == KeyEvent.ACTION_DOWN 73c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert && event.getRepeatCount() == 0) { 74c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert state.startTracking(event, this); 75c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert return true; 76c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } else if (event.getAction() == KeyEvent.ACTION_UP 77c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert && !event.isCanceled() && state.isTracking(event)) { 78c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert hideInputMethod(); 79c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert activity.onBackPressed(); 80c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert return true; 81c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 82c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 83c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 84c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert return super.dispatchKeyEventPreIme(event); 85c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert } 86c36735de23beadc017c484596b551d7c4eaff289Bjorn Bringert} 87