• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.example.android.apis.view;
18 
19 import com.example.android.apis.R;
20 
21 import android.content.Context;
22 import android.util.AttributeSet;
23 import android.view.MotionEvent;
24 import android.widget.LinearLayout;
25 import 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  */
40 public class HoverInterceptorView extends LinearLayout {
41     private boolean mInterceptHover;
42 
HoverInterceptorView(Context context, AttributeSet attrs)43     public HoverInterceptorView(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
47     @Override
onInterceptHoverEvent(MotionEvent event)48     public boolean onInterceptHoverEvent(MotionEvent event) {
49         if (mInterceptHover) {
50             return true;
51         }
52         return super.onInterceptHoverEvent(event);
53     }
54 
55     @Override
onHoverEvent(MotionEvent event)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 
setInterceptHover(boolean intercept)68     public void setInterceptHover(boolean intercept) {
69         mInterceptHover = intercept;
70     }
71 }
72