1 /* 2 * Copyright (C) 2008 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 18 package com.android.launcher; 19 20 import android.widget.ImageView; 21 import android.content.Context; 22 import android.content.res.TypedArray; 23 import android.util.AttributeSet; 24 import android.view.View; 25 import android.view.KeyEvent; 26 27 public class HandleView extends ImageView { 28 private static final int ORIENTATION_HORIZONTAL = 1; 29 30 private Launcher mLauncher; 31 private int mOrientation = ORIENTATION_HORIZONTAL; 32 HandleView(Context context)33 public HandleView(Context context) { 34 super(context); 35 } 36 HandleView(Context context, AttributeSet attrs)37 public HandleView(Context context, AttributeSet attrs) { 38 this(context, attrs, 0); 39 } 40 HandleView(Context context, AttributeSet attrs, int defStyle)41 public HandleView(Context context, AttributeSet attrs, int defStyle) { 42 super(context, attrs, defStyle); 43 44 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HandleView, defStyle, 0); 45 mOrientation = a.getInt(R.styleable.HandleView_direction, ORIENTATION_HORIZONTAL); 46 a.recycle(); 47 } 48 49 @Override focusSearch(int direction)50 public View focusSearch(int direction) { 51 View newFocus = super.focusSearch(direction); 52 if (newFocus == null && mLauncher.isDrawerDown()) { 53 final Workspace workspace = mLauncher.getWorkspace(); 54 workspace.dispatchUnhandledMove(null, direction); 55 return (mOrientation == ORIENTATION_HORIZONTAL && direction == FOCUS_DOWN) ? 56 this : workspace; 57 } 58 return newFocus; 59 } 60 61 @Override onKeyDown(int keyCode, KeyEvent event)62 public boolean onKeyDown(int keyCode, KeyEvent event) { 63 final boolean handled = super.onKeyDown(keyCode, event); 64 65 if (!handled && !mLauncher.isDrawerDown() && !isDirectionKey(keyCode)) { 66 return mLauncher.getApplicationsGrid().onKeyDown(keyCode, event); 67 } 68 69 return handled; 70 } 71 72 @Override onKeyUp(int keyCode, KeyEvent event)73 public boolean onKeyUp(int keyCode, KeyEvent event) { 74 final boolean handled = super.onKeyUp(keyCode, event); 75 76 if (!handled && !mLauncher.isDrawerDown() && !isDirectionKey(keyCode)) { 77 return mLauncher.getApplicationsGrid().onKeyUp(keyCode, event); 78 } 79 80 return handled; 81 } 82 isDirectionKey(int keyCode)83 private static boolean isDirectionKey(int keyCode) { 84 return keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_LEFT || 85 keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_DPAD_UP; 86 } 87 setLauncher(Launcher launcher)88 void setLauncher(Launcher launcher) { 89 mLauncher = launcher; 90 } 91 } 92