1/*
2 * Copyright (C) 2011 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.example.android.apis.view;
18
19import com.example.android.apis.R;
20
21import android.content.Context;
22import android.util.AttributeSet;
23import android.view.MotionEvent;
24import android.widget.LinearLayout;
25import android.widget.TextView;
26
27/**
28 * Part of the {@link Hover} demo activity.
29 *
30 * The Interceptor view is a simple subclass of LinearLayout whose sole purpose
31 * is to override {@link #onInterceptHoverEvent}.  When the checkbox in the
32 * hover activity is checked, the interceptor view will intercept hover events.
33 *
34 * When this view intercepts hover events, its children will not receive
35 * hover events.  This can be useful in some cases when implementing a custom
36 * view group that would like to prevent its children from being hovered
37 * under certain situations.  Usually such custom views will be much more
38 * interesting and complex than our little Interceptor example here.
39 */
40public class HoverInterceptorView extends LinearLayout {
41    private boolean mInterceptHover;
42
43    public HoverInterceptorView(Context context, AttributeSet attrs) {
44        super(context, attrs);
45    }
46
47    @Override
48    public boolean onInterceptHoverEvent(MotionEvent event) {
49        if (mInterceptHover) {
50            return true;
51        }
52        return super.onInterceptHoverEvent(event);
53    }
54
55    @Override
56    public boolean onHoverEvent(MotionEvent event) {
57        TextView textView = (TextView) findViewById(R.id.intercept_message);
58        if (mInterceptHover && event.getAction() != MotionEvent.ACTION_HOVER_EXIT) {
59            textView.setText(getResources().getString(
60                    R.string.hover_intercept_message_intercepted));
61            return true;
62        }
63        textView.setText(getResources().getString(
64                R.string.hover_intercept_message_initial));
65        return super.onHoverEvent(event);
66    }
67
68    public void setInterceptHover(boolean intercept) {
69        mInterceptHover = intercept;
70    }
71}
72