HandleView.java revision a5902524d4403885eb4c50360bf3465c6be796ef
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
18package com.android.launcher2;
19
20import android.widget.ImageView;
21import android.content.Context;
22import android.content.res.TypedArray;
23import android.util.AttributeSet;
24import android.view.View;
25import android.view.KeyEvent;
26
27public class HandleView extends ImageView {
28    private static final int ORIENTATION_HORIZONTAL = 1;
29
30    private Launcher mLauncher;
31    private int mOrientation = ORIENTATION_HORIZONTAL;
32
33    public HandleView(Context context) {
34        super(context);
35    }
36
37    public HandleView(Context context, AttributeSet attrs) {
38        this(context, attrs, 0);
39    }
40
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
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
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
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
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
88    void setLauncher(Launcher launcher) {
89        mLauncher = launcher;
90    }
91}
92