1 /*
2  * Copyright (C) 2017 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 package com.example.android.autofill.app.view.autofillable;
17 
18 import android.content.Context;
19 import android.support.annotation.Nullable;
20 import android.util.AttributeSet;
21 import android.util.Log;
22 import android.view.GestureDetector;
23 import android.view.MotionEvent;
24 
25 /**
26  * A version of {@link CustomVirtualView} that uses gesture to provide scrolling.
27  */
28 public class ScrollableCustomVirtualView extends CustomVirtualView
29         implements GestureDetector.OnGestureListener {
30 
31     private static final String TAG = "ScrollableCustomView";
32 
33     private GestureDetector mGestureDetector;
34 
ScrollableCustomVirtualView(Context context)35     public ScrollableCustomVirtualView(Context context) {
36         this(context, null);
37     }
38 
ScrollableCustomVirtualView(Context context, AttributeSet attrs)39     public ScrollableCustomVirtualView(Context context, AttributeSet attrs) {
40         this(context, attrs, 0);
41 
42     }
43 
ScrollableCustomVirtualView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)44     public ScrollableCustomVirtualView(Context context, @Nullable AttributeSet attrs,
45             int defStyleAttr) {
46         this(context, attrs, defStyleAttr, 0);
47     }
48 
ScrollableCustomVirtualView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)49     public ScrollableCustomVirtualView(Context context, @Nullable AttributeSet attrs,
50             int defStyleAttr, int defStyleRes) {
51         super(context, attrs, defStyleAttr, defStyleRes);
52         mGestureDetector = new GestureDetector(context, this);
53     }
54 
55     /**
56      * Resets the UI to the intial state.
57      */
resetPositions()58     public void resetPositions() {
59         super.resetCoordinates();
60     }
61 
62     @Override
onTouchEvent(MotionEvent event)63     public boolean onTouchEvent(MotionEvent event) {
64         return mGestureDetector.onTouchEvent(event);
65     }
66 
67     /*
68      * Methods below implement GestureDetector.OnGestureListener
69      */
70     @Override
onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)71     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
72         if (VERBOSE) Log.v(TAG, "onScroll(): " + distanceX + " - " + distanceY);
73         if (mFocusedLine != null) {
74             mAutofillManager.notifyViewExited(this, mFocusedLine.mFieldTextItem.id);
75         }
76         mTopMargin -= distanceY;
77         mLeftMargin -= distanceX;
78         invalidate();
79         return true;
80     }
81 
82     @Override
onDown(MotionEvent event)83     public boolean onDown(MotionEvent event) {
84         onMotion((int) event.getY());
85         return true;
86     }
87 
88     @Override
onShowPress(MotionEvent e)89     public void onShowPress(MotionEvent e) {
90     }
91 
92     @Override
onSingleTapUp(MotionEvent e)93     public boolean onSingleTapUp(MotionEvent e) {
94         return true;
95     }
96 
97     @Override
onLongPress(MotionEvent e)98     public void onLongPress(MotionEvent e) {
99     }
100 
101     @Override
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)102     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
103         return true;
104     }
105 }