Andrii Kulian | 962e434 | 2018-01-23 20:17:31 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2018 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.google.android.test.activityview; |
| 18 | |
| 19 | import static android.view.MotionEvent.ACTION_CANCEL; |
| 20 | import static android.view.MotionEvent.ACTION_DOWN; |
| 21 | import static android.view.MotionEvent.ACTION_MOVE; |
| 22 | import static android.view.MotionEvent.ACTION_UP; |
| 23 | |
| 24 | import android.app.Activity; |
| 25 | import android.content.res.Configuration; |
| 26 | import android.os.Bundle; |
| 27 | import android.view.MotionEvent; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewTreeObserver; |
| 30 | import android.widget.TextView; |
| 31 | |
| 32 | public class ActivityViewTestActivity extends Activity implements View.OnTouchListener { |
| 33 | |
| 34 | private TextView mTextView; |
| 35 | private TextView mWidthTextView; |
| 36 | private TextView mHeightTextView; |
| 37 | private TextView mTouchStateTextView; |
| 38 | private View mTouchInterceptView; |
| 39 | |
| 40 | @Override |
| 41 | protected void onCreate(Bundle savedInstanceState) { |
| 42 | super.onCreate(savedInstanceState); |
| 43 | setContentView(R.layout.activity_view_test_activity); |
| 44 | |
| 45 | mTextView = findViewById(R.id.test_activity_title); |
| 46 | mWidthTextView = findViewById(R.id.test_activity_width_text); |
| 47 | mHeightTextView = findViewById(R.id.test_activity_height_text); |
| 48 | mTouchStateTextView = findViewById(R.id.test_activity_touch_state); |
| 49 | mTouchInterceptView = findViewById(R.id.touch_intercept_view); |
| 50 | mTouchInterceptView.setOnTouchListener(this); |
| 51 | ViewTreeObserver viewTreeObserver = mTouchInterceptView.getViewTreeObserver(); |
| 52 | if (viewTreeObserver.isAlive()) { |
| 53 | viewTreeObserver.addOnGlobalLayoutListener(this::updateDimensionTexts); |
| 54 | } |
| 55 | updateStateText("CREATED"); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void onStart() { |
| 60 | super.onStart(); |
| 61 | updateStateText("STARTED"); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | protected void onResume() { |
| 66 | super.onResume(); |
| 67 | updateStateText("RESUMED"); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected void onPause() { |
| 72 | super.onPause(); |
| 73 | updateStateText("PAUSED"); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onStop() { |
| 78 | super.onStop(); |
| 79 | updateStateText("STOPPED"); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onConfigurationChanged(Configuration newConfig) { |
| 84 | super.onConfigurationChanged(newConfig); |
| 85 | updateDimensionTexts(); |
| 86 | } |
| 87 | |
| 88 | private void updateStateText(String state) { |
| 89 | mTextView.setText(state); |
| 90 | } |
| 91 | |
| 92 | private void updateDimensionTexts() { |
| 93 | mWidthTextView.setText("" + mTouchInterceptView.getWidth()); |
| 94 | mHeightTextView.setText("" + mTouchInterceptView.getHeight()); |
| 95 | } |
| 96 | |
| 97 | private void updateTouchState(MotionEvent event) { |
| 98 | switch (event.getAction()) { |
| 99 | case ACTION_DOWN: |
| 100 | case ACTION_MOVE: |
| 101 | mTouchStateTextView.setText("[" + event.getX() + "," + event.getY() + "]"); |
| 102 | break; |
| 103 | case ACTION_UP: |
| 104 | case ACTION_CANCEL: |
| 105 | mTouchStateTextView.setText(""); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean onTouch(View v, MotionEvent event) { |
| 112 | updateTouchState(event); |
| 113 | return true; |
| 114 | } |
| 115 | } |