The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 19 | import android.appwidget.AppWidgetHostView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.view.LayoutInflater; |
| 22 | import android.view.MotionEvent; |
| 23 | import android.view.View; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 24 | import android.view.ViewConfiguration; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 25 | import android.view.ViewGroup; |
Adam Cohen | 06dff35 | 2012-06-01 17:17:08 -0700 | [diff] [blame] | 26 | import android.widget.RemoteViews; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 28 | import com.android.launcher3.DragLayer.TouchCompleteListener; |
| 29 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | /** |
| 31 | * {@inheritDoc} |
| 32 | */ |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 33 | public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 34 | private CheckLongPressHelper mLongPressHelper; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | private LayoutInflater mInflater; |
Adam Cohen | 06dff35 | 2012-06-01 17:17:08 -0700 | [diff] [blame] | 36 | private Context mContext; |
| 37 | private int mPreviousOrientation; |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 38 | private DragLayer mDragLayer; |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame] | 39 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 40 | private float mSlop; |
| 41 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 42 | public LauncherAppWidgetHostView(Context context) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | super(context); |
Adam Cohen | 06dff35 | 2012-06-01 17:17:08 -0700 | [diff] [blame] | 44 | mContext = context; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 45 | mLongPressHelper = new CheckLongPressHelper(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 47 | mDragLayer = ((Launcher) context).getDragLayer(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 48 | } |
Adam Cohen | 41d5d6d | 2011-05-31 15:52:28 -0700 | [diff] [blame] | 49 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | @Override |
| 51 | protected View getErrorView() { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 52 | return mInflater.inflate(R.layout.appwidget_error, this, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Adam Cohen | 06dff35 | 2012-06-01 17:17:08 -0700 | [diff] [blame] | 55 | @Override |
| 56 | public void updateAppWidget(RemoteViews remoteViews) { |
| 57 | // Store the orientation in which the widget was inflated |
| 58 | mPreviousOrientation = mContext.getResources().getConfiguration().orientation; |
| 59 | super.updateAppWidget(remoteViews); |
| 60 | } |
| 61 | |
| 62 | public boolean orientationChangedSincedInflation() { |
| 63 | int orientation = mContext.getResources().getConfiguration().orientation; |
| 64 | if (mPreviousOrientation != orientation) { |
| 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Adam Cohen | 3798b64 | 2013-10-16 10:30:50 -0700 | [diff] [blame] | 71 | // Just in case the previous long press hasn't been cleared, we make sure to start fresh |
| 72 | // on touch down. |
| 73 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 74 | mLongPressHelper.cancelLongPress(); |
| 75 | } |
| 76 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | // Consume any touch events for ourselves after longpress is triggered |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 78 | if (mLongPressHelper.hasPerformedLongPress()) { |
| 79 | mLongPressHelper.cancelLongPress(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | return true; |
| 81 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 83 | // Watch for longpress events at this level to make sure |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 84 | // users can always pick up this widget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | switch (ev.getAction()) { |
| 86 | case MotionEvent.ACTION_DOWN: { |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 87 | mLongPressHelper.postCheckForLongPress(); |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 88 | mDragLayer.setTouchCompleteListener(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 89 | break; |
| 90 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 91 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 92 | case MotionEvent.ACTION_UP: |
| 93 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 94 | mLongPressHelper.cancelLongPress(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 95 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 96 | case MotionEvent.ACTION_MOVE: |
| 97 | if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) { |
| 98 | mLongPressHelper.cancelLongPress(); |
| 99 | } |
| 100 | break; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 102 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 103 | // Otherwise continue letting touch events fall through to children |
| 104 | return false; |
| 105 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 106 | |
Winson Chung | e7a852e | 2013-08-16 11:10:59 -0700 | [diff] [blame] | 107 | public boolean onTouchEvent(MotionEvent ev) { |
| 108 | // If the widget does not handle touch, then cancel |
| 109 | // long press when we release the touch |
| 110 | switch (ev.getAction()) { |
| 111 | case MotionEvent.ACTION_UP: |
| 112 | case MotionEvent.ACTION_CANCEL: |
| 113 | mLongPressHelper.cancelLongPress(); |
| 114 | break; |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 115 | case MotionEvent.ACTION_MOVE: |
| 116 | if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) { |
| 117 | mLongPressHelper.cancelLongPress(); |
| 118 | } |
| 119 | break; |
Winson Chung | e7a852e | 2013-08-16 11:10:59 -0700 | [diff] [blame] | 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 124 | @Override |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 125 | protected void onAttachedToWindow() { |
| 126 | super.onAttachedToWindow(); |
| 127 | mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); |
| 128 | } |
| 129 | |
| 130 | @Override |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 131 | public void cancelLongPress() { |
| 132 | super.cancelLongPress(); |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 133 | mLongPressHelper.cancelLongPress(); |
| 134 | } |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 135 | |
Adam Cohen | b0f3d74 | 2013-10-08 19:16:14 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void onTouchComplete() { |
Adam Cohen | 3798b64 | 2013-10-16 10:30:50 -0700 | [diff] [blame] | 138 | if (!mLongPressHelper.hasPerformedLongPress()) { |
| 139 | // If a long press has been performed, we don't want to clear the record of that since |
| 140 | // we still may be receiving a touch up which we want to intercept |
| 141 | mLongPressHelper.cancelLongPress(); |
| 142 | } |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 143 | } |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame] | 144 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public int getDescendantFocusability() { |
| 147 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 148 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 149 | } |