1/*
2 * Copyright (C) 2013 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
17package com.android.internal.policy.impl.keyguard;
18
19import android.content.Context;
20import android.view.MotionEvent;
21import android.view.View;
22import android.view.accessibility.AccessibilityManager;
23
24/**
25 * Hover listener that implements lift-to-activate interaction for
26 * accessibility. May be added to multiple views.
27 */
28class LiftToActivateListener implements View.OnHoverListener {
29    /** Manager used to query accessibility enabled state. */
30    private final AccessibilityManager mAccessibilityManager;
31
32    private boolean mCachedClickableState;
33
34    public LiftToActivateListener(Context context) {
35        mAccessibilityManager = (AccessibilityManager) context.getSystemService(
36                Context.ACCESSIBILITY_SERVICE);
37    }
38
39    @Override
40    public boolean onHover(View v, MotionEvent event) {
41        // When touch exploration is turned on, lifting a finger while
42        // inside the view bounds should perform a click action.
43        if (mAccessibilityManager.isEnabled()
44                && mAccessibilityManager.isTouchExplorationEnabled()) {
45            switch (event.getActionMasked()) {
46                case MotionEvent.ACTION_HOVER_ENTER:
47                    // Lift-to-type temporarily disables double-tap
48                    // activation by setting the view as not clickable.
49                    mCachedClickableState = v.isClickable();
50                    v.setClickable(false);
51                    break;
52                case MotionEvent.ACTION_HOVER_EXIT:
53                    final int x = (int) event.getX();
54                    final int y = (int) event.getY();
55                    if ((x > v.getPaddingLeft()) && (y > v.getPaddingTop())
56                            && (x < v.getWidth() - v.getPaddingRight())
57                            && (y < v.getHeight() - v.getPaddingBottom())) {
58                        v.performClick();
59                    }
60                    v.setClickable(mCachedClickableState);
61                    break;
62            }
63        }
64
65        // Pass the event to View.onHoverEvent() to handle accessibility.
66        v.onHoverEvent(event);
67
68        // Consume the event so it doesn't fall through to other views.
69        return true;
70    }
71}