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