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.incallui;
18
19import android.os.Bundle;
20import android.telecom.VideoProfile;
21import android.view.GestureDetector;
22import android.view.GestureDetector.SimpleOnGestureListener;
23import android.view.LayoutInflater;
24import android.view.MotionEvent;
25import android.view.View;
26import android.view.ViewGroup;
27
28import com.android.dialer.R;
29
30/**
31 * AnswerFragment to use when touch exploration is enabled in accessibility.
32 */
33public class AccessibleAnswerFragment extends AnswerFragment {
34
35    private static final String TAG = AccessibleAnswerFragment.class.getSimpleName();
36    private static final int SWIPE_THRESHOLD = 100;
37
38    private View mAnswer;
39    private View mDecline;
40    private View mText;
41
42    private TouchListener mTouchListener;
43    private GestureDetector mGestureDetector;
44
45    @Override
46    public View onCreateView(LayoutInflater inflater, ViewGroup container,
47            Bundle savedInstanceState) {
48        ViewGroup group = (ViewGroup) inflater.inflate(R.layout.accessible_answer_fragment,
49                container, false);
50
51        mTouchListener = new TouchListener();
52        mGestureDetector = new GestureDetector(getContext(), new SimpleOnGestureListener() {
53            @Override
54            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
55                    float velocityY) {
56                return AccessibleAnswerFragment.this.onFling(e1, e2, velocityX, velocityX);
57            }
58        });
59
60        mAnswer = group.findViewById(R.id.accessible_answer_fragment_answer);
61        mAnswer.setOnClickListener(new View.OnClickListener() {
62            @Override
63            public void onClick(View v) {
64                Log.d(TAG, "Answer Button Clicked");
65                onAnswer(VideoProfile.STATE_AUDIO_ONLY, getContext());
66            }
67        });
68        mDecline = group.findViewById(R.id.accessible_answer_fragment_decline);
69        mDecline.setOnClickListener(new View.OnClickListener() {
70            @Override
71            public void onClick(View v) {
72                Log.d(TAG, "Decline Button Clicked");
73                onDecline(getContext());
74            }
75        });
76
77        mText = group.findViewById(R.id.accessible_answer_fragment_text);
78        mText.setOnClickListener(new View.OnClickListener() {
79            @Override
80            public void onClick(View v) {
81                Log.d(TAG, "Text Button Clicked");
82                onText();
83            }
84        });
85        return group;
86    }
87
88    @Override
89    public void onResume() {
90        super.onResume();
91        // Intercept all touch events for full screen swiping gesture.
92        InCallActivity activity = (InCallActivity) getActivity();
93        activity.setDispatchTouchEventListener(mTouchListener);
94    }
95
96    @Override
97    public void onPause() {
98        super.onPause();
99        InCallActivity activity = (InCallActivity) getActivity();
100        activity.setDispatchTouchEventListener(null);
101    }
102
103    private class TouchListener implements View.OnTouchListener {
104        @Override
105        public boolean onTouch(View v, MotionEvent event) {
106            return mGestureDetector.onTouchEvent(event);
107        }
108    }
109
110    private boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
111            float velocityY) {
112        if (hasPendingDialogs()) {
113            return false;
114        }
115
116        float diffY = e2.getY() - e1.getY();
117        float diffX = e2.getX() - e1.getX();
118        if (Math.abs(diffX) > Math.abs(diffY)) {
119            if (Math.abs(diffX) > SWIPE_THRESHOLD) {
120                if (diffX > 0) {
121                    onSwipeRight();
122                } else {
123                    onSwipeLeft();
124                }
125            }
126            return true;
127        } else if (Math.abs(diffY) > SWIPE_THRESHOLD) {
128            if (diffY > 0) {
129                onSwipeDown();
130            } else {
131                onSwipeUp();
132            }
133            return true;
134        }
135
136        return false;
137    }
138
139    private void onSwipeUp() {
140        Log.d(TAG, "onSwipeUp");
141        onText();
142    }
143
144    private void onSwipeDown() {
145        Log.d(TAG, "onSwipeDown");
146    }
147
148    private void onSwipeLeft() {
149        Log.d(TAG, "onSwipeLeft");
150        onDecline(getContext());
151    }
152
153    private void onSwipeRight() {
154        Log.d(TAG, "onSwipeRight");
155        onAnswer(VideoProfile.STATE_AUDIO_ONLY, getContext());
156    }
157}
158