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