HandleView.java revision 7bb1749c69384faf00b238f0684d3b2e23406451
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.MotionEvent;
25import android.view.KeyEvent;
26import android.view.View;
27
28public class HandleView extends ImageView {
29    private static final int ORIENTATION_HORIZONTAL = 1;
30
31    private Launcher mLauncher;
32    private int mOrientation = ORIENTATION_HORIZONTAL;
33
34    public HandleView(Context context) {
35        super(context);
36    }
37
38    public HandleView(Context context, AttributeSet attrs) {
39        this(context, attrs, 0);
40    }
41
42    public HandleView(Context context, AttributeSet attrs, int defStyle) {
43        super(context, attrs, defStyle);
44
45        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HandleView, defStyle, 0);
46        mOrientation = a.getInt(R.styleable.HandleView_direction, ORIENTATION_HORIZONTAL);
47        a.recycle();
48    }
49
50    @Override
51    public View focusSearch(int direction) {
52        View newFocus = super.focusSearch(direction);
53        if (newFocus == null && !mLauncher.isAllAppsVisible()) {
54            final Workspace workspace = mLauncher.getWorkspace();
55            workspace.dispatchUnhandledMove(null, direction);
56            return (mOrientation == ORIENTATION_HORIZONTAL && direction == FOCUS_DOWN) ?
57                    this : workspace;
58        }
59        return newFocus;
60    }
61
62    @Override
63    public boolean onKeyDown(int keyCode, KeyEvent event) {
64        final boolean handled = super.onKeyDown(keyCode, event);
65
66        /* TODO
67        if (!handled && !mLauncher.isDrawerDown() && !isDirectionKey(keyCode)) {
68            return mLauncher.getApplicationsGrid().onKeyDown(keyCode, event);
69        }
70        */
71
72        return handled;
73    }
74
75    @Override
76    public boolean onKeyUp(int keyCode, KeyEvent event) {
77        final boolean handled = super.onKeyUp(keyCode, event);
78
79        /* TODO
80        if (!handled && !mLauncher.isDrawerDown() && !isDirectionKey(keyCode)) {
81            return mLauncher.getApplicationsGrid().onKeyUp(keyCode, event);
82        }
83        */
84
85        return handled;
86    }
87
88    @Override
89    public boolean onTouchEvent(MotionEvent ev) {
90        if (ev.getAction() == MotionEvent.ACTION_DOWN && mLauncher.isAllAppsVisible()) {
91            return false;
92        }
93        return super.onTouchEvent(ev);
94    }
95
96    private static boolean isDirectionKey(int keyCode) {
97        return keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_LEFT ||
98                keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_DPAD_UP;
99    }
100
101    void setLauncher(Launcher launcher) {
102        mLauncher = launcher;
103    }
104}
105