1 /*
2  * Copyright (C) 2016 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 android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.MotionEvent;
22 import android.view.PointerIcon;
23 import android.widget.Button;
24 
25 public class SystemPointerIconButton extends Button {
26 
SystemPointerIconButton(Context context)27     public SystemPointerIconButton(Context context) {
28         this(context, null);
29     }
30 
SystemPointerIconButton(Context context, AttributeSet attrs)31     public SystemPointerIconButton(Context context, AttributeSet attrs) {
32         super(context, attrs);
33     }
34 
SystemPointerIconButton(Context context, AttributeSet attrs, int defStyleAttr)35     public SystemPointerIconButton(Context context, AttributeSet attrs, int defStyleAttr) {
36         super(context, attrs, defStyleAttr);
37     }
38 
SystemPointerIconButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)39     public SystemPointerIconButton(Context context, AttributeSet attrs,
40                                     int defStyleAttr, int defStyleRes) {
41         super(context, attrs, defStyleAttr, defStyleRes);
42     }
43 
44     @Override
onResolvePointerIcon(MotionEvent event, int pointerIndex)45     public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
46         final int minX = getWidth() / 4;
47         final int maxX = getWidth() - minX;
48         final int minY = getHeight() / 4;
49         final int maxY = getHeight() - minY;
50         final float x = event.getX(pointerIndex);
51         final float y = event.getY(pointerIndex);
52         int type;
53         if ((x < minX && y < minY) || (x > maxX && y > maxY)) {
54             // Top/left or bottom/right corner.
55             type = PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW;
56         } else if ((x < minX && y > maxY) || (x > maxX && y < minY)) {
57             // Top/rightor bottom/left corner.
58             type = PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW;
59         } else if (x < minX || x > maxX) {
60             // Left or right edge.
61             type = PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW;
62         } else if (y < minY || y > maxY) {
63             // Top or bottom edge edge.
64             type = PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW;
65         } else {
66             // Everything else (the middle).
67             type = PointerIcon.TYPE_ALL_SCROLL;
68         }
69         return PointerIcon.getSystemIcon(getContext(), type);
70     }
71 }
72