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.uiautomator.tests.cts.testapp;
18
19import android.app.AlertDialog;
20import android.os.Bundle;
21import android.support.v4.app.Fragment;
22import android.view.LayoutInflater;
23import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.Button;
27import android.widget.CheckBox;
28import android.widget.ImageButton;
29import android.widget.SeekBar;
30import android.widget.Spinner;
31
32public class Test5DetailFragment extends Fragment {
33
34    public static final String ARG_ITEM_ID = "item_id";
35
36    class PointerEvent {
37        int startX;
38        int startY;
39        int endX;
40        int endY;
41    }
42
43    private final PointerEvent mPointerEvent = new PointerEvent();
44
45    public Test5DetailFragment() {
46    }
47
48    @Override
49    public void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51    }
52
53    @Override
54    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
55        View rootView = inflater.inflate(R.layout.test5_detail_fragment, container, false);
56
57        // Set the content description for the following
58        Spinner spinner = (Spinner) rootView.findViewById(R.id.test_5_spinner);
59        spinner.setContentDescription("Spinner");
60        ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.test_5_imageButton);
61        imageButton.setContentDescription("Image button");
62
63        // Each time this view is displayed, reset the following states
64        SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.test_5_seekBar);
65        seekBar.setProgress(50);
66        seekBar.setContentDescription("Progress is 50 %");
67        CheckBox checkbox = (CheckBox) rootView.findViewById(R.id.test_5_checkBox);
68        checkbox.setChecked(false);
69
70        // Register click event handlers for the following
71        Button button = (Button) rootView.findViewById(R.id.test_5_button1);
72        button.setOnClickListener(new Button.OnClickListener() {
73            @Override
74            public void onClick(View v) {
75                // we want an artificial crash
76                throw new RuntimeException("Artificial crash to test UiWatcher");
77            }
78        });
79
80        imageButton.setOnTouchListener(new ImageButton.OnTouchListener() {
81
82            @Override
83            public boolean onTouch(View v, MotionEvent event) {
84                if (event.getAction() == MotionEvent.ACTION_DOWN) {
85                    resetTouchResults();
86                    collectStartAction(event, v);
87                } else if (event.getAction() == MotionEvent.ACTION_UP) {
88                    collectEndAction(event, v);
89                    displayTouchResults();
90                }
91                return false;
92            }
93        });
94
95        return rootView;
96    }
97
98    private void displayTouchResults() {
99        StringBuilder output = new StringBuilder();
100
101        output.append(String.format("%d,%d:%d,%d\n",
102                mPointerEvent.startX, mPointerEvent.startY, mPointerEvent.endX,
103                mPointerEvent.endY));
104
105        // display the submitted text
106        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
107        builder.setTitle(R.string.drag_item_touch_dialog_title);
108        builder.setPositiveButton(R.string.OK, null);
109        builder.setMessage(output.toString());
110        AlertDialog diag = builder.create();
111        diag.show();
112    }
113
114    /**
115     * Clears all collected pointer results
116     */
117    private void resetTouchResults() {
118         mPointerEvent.startX = mPointerEvent.startY =
119                    mPointerEvent.endX = mPointerEvent.endY = -1;
120    }
121
122    /**
123     * Collects pointer touch information converting from relative to absolute before
124     * storing it as starting touch coordinates.
125     *
126     * @param event
127     * @param view
128     * @param pointerIndex
129     */
130    private void collectStartAction(MotionEvent event, View view) {
131        int offsetInScreen[] = new int[2];
132        view.getLocationOnScreen(offsetInScreen);
133        mPointerEvent.startX = (int)(event.getX() + offsetInScreen[0]);
134        mPointerEvent.startY = (int)(event.getY() + offsetInScreen[1]);
135    }
136
137    /**
138     * Collects pointer touch information converting from relative to absolute before
139     * storing it as ending touch coordinates.
140     *
141     * @param event
142     * @param view
143     * @param pointerIndex
144     */
145    private void collectEndAction(MotionEvent event, View view) {
146        int offsetInScreen[] = new int[2];
147        view.getLocationOnScreen(offsetInScreen);
148        mPointerEvent.endX = (int)(event.getX() + offsetInScreen[0]);
149        mPointerEvent.endY = (int)(event.getY() + offsetInScreen[1]);
150    }
151}
152