Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.android.launcher2; |
| 18 | |
| 19 | import android.appwidget.AppWidgetProviderInfo; |
| 20 | import android.content.Context; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.content.pm.ResolveInfo; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 23 | import android.content.res.Resources; |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.MotionEvent; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 27 | import android.view.View; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 28 | import android.widget.ImageView; |
| 29 | import android.widget.LinearLayout; |
| 30 | import android.widget.TextView; |
| 31 | |
| 32 | import com.android.launcher.R; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * The linear layout used strictly for the widget/wallpaper tab of the customization tray |
| 36 | */ |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 37 | public class PagedViewWidget extends LinearLayout { |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 38 | static final String TAG = "PagedViewWidgetLayout"; |
| 39 | |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 40 | private static boolean sDeletePreviewsWhenDetachedFromWindow = true; |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 41 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 42 | private String mDimensionsFormatString; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 43 | CheckForShortPress mPendingCheckForShortPress = null; |
| 44 | ShortPressListener mShortPressListener = null; |
| 45 | boolean mShortPressTriggered = false; |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 46 | static PagedViewWidget sShortpressTarget = null; |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 47 | boolean mIsAppWidget; |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 48 | private final Rect mOriginalImagePadding = new Rect(); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame^] | 49 | private Object mInfo; |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 50 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 51 | public PagedViewWidget(Context context) { |
| 52 | this(context, null); |
| 53 | } |
| 54 | |
| 55 | public PagedViewWidget(Context context, AttributeSet attrs) { |
| 56 | this(context, attrs, 0); |
| 57 | } |
| 58 | |
| 59 | public PagedViewWidget(Context context, AttributeSet attrs, int defStyle) { |
| 60 | super(context, attrs, defStyle); |
| 61 | |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 62 | final Resources r = context.getResources(); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 63 | mDimensionsFormatString = r.getString(R.string.widget_dims_format); |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 64 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 65 | setWillNotDraw(false); |
| 66 | setClipToPadding(false); |
| 67 | } |
| 68 | |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 69 | @Override |
| 70 | protected void onFinishInflate() { |
| 71 | super.onFinishInflate(); |
| 72 | |
| 73 | final ImageView image = (ImageView) findViewById(R.id.widget_preview); |
| 74 | mOriginalImagePadding.left = image.getPaddingLeft(); |
| 75 | mOriginalImagePadding.top = image.getPaddingTop(); |
| 76 | mOriginalImagePadding.right = image.getPaddingRight(); |
| 77 | mOriginalImagePadding.bottom = image.getPaddingBottom(); |
| 78 | } |
| 79 | |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 80 | public static void setDeletePreviewsWhenDetachedFromWindow(boolean value) { |
| 81 | sDeletePreviewsWhenDetachedFromWindow = value; |
| 82 | } |
| 83 | |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 84 | @Override |
| 85 | protected void onDetachedFromWindow() { |
| 86 | super.onDetachedFromWindow(); |
| 87 | |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 88 | if (sDeletePreviewsWhenDetachedFromWindow) { |
| 89 | final ImageView image = (ImageView) findViewById(R.id.widget_preview); |
| 90 | if (image != null) { |
| 91 | FastBitmapDrawable preview = (FastBitmapDrawable) image.getDrawable(); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame^] | 92 | if (mInfo != null && preview != null && preview.getBitmap() != null) { |
| 93 | WidgetPreviewLoader.releaseBitmap(mInfo, preview.getBitmap()); |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 94 | } |
| 95 | image.setImageDrawable(null); |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 96 | } |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 100 | public void applyFromAppWidgetProviderInfo(AppWidgetProviderInfo info, |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 101 | int maxWidth, int[] cellSpan) { |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 102 | mIsAppWidget = true; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame^] | 103 | mInfo = info; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 104 | final ImageView image = (ImageView) findViewById(R.id.widget_preview); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 105 | if (maxWidth > -1) { |
| 106 | image.setMaxWidth(maxWidth); |
| 107 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 108 | image.setContentDescription(info.label); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 109 | final TextView name = (TextView) findViewById(R.id.widget_name); |
| 110 | name.setText(info.label); |
| 111 | final TextView dims = (TextView) findViewById(R.id.widget_dims); |
Winson Chung | 35d4a25 | 2011-08-11 15:12:11 -0700 | [diff] [blame] | 112 | if (dims != null) { |
Winson Chung | e3e03bc | 2012-05-01 15:10:11 -0700 | [diff] [blame] | 113 | int hSpan = Math.min(cellSpan[0], LauncherModel.getCellCountX()); |
| 114 | int vSpan = Math.min(cellSpan[1], LauncherModel.getCellCountY()); |
| 115 | dims.setText(String.format(mDimensionsFormatString, hSpan, vSpan)); |
Winson Chung | 35d4a25 | 2011-08-11 15:12:11 -0700 | [diff] [blame] | 116 | } |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 119 | public void applyFromResolveInfo(PackageManager pm, ResolveInfo info) { |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 120 | mIsAppWidget = false; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame^] | 121 | mInfo = info; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 122 | CharSequence label = info.loadLabel(pm); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 123 | final ImageView image = (ImageView) findViewById(R.id.widget_preview); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 124 | image.setContentDescription(label); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 125 | final TextView name = (TextView) findViewById(R.id.widget_name); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 126 | name.setText(label); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 127 | final TextView dims = (TextView) findViewById(R.id.widget_dims); |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 128 | if (dims != null) { |
| 129 | dims.setText(String.format(mDimensionsFormatString, 1, 1)); |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 130 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 133 | public int[] getPreviewSize() { |
| 134 | final ImageView i = (ImageView) findViewById(R.id.widget_preview); |
| 135 | int[] maxSize = new int[2]; |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 136 | maxSize[0] = i.getWidth() - mOriginalImagePadding.left - mOriginalImagePadding.right; |
| 137 | maxSize[1] = i.getHeight() - mOriginalImagePadding.top; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 138 | return maxSize; |
| 139 | } |
| 140 | |
| 141 | void applyPreview(FastBitmapDrawable preview, int index) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 142 | final PagedViewWidgetImageView image = |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 143 | (PagedViewWidgetImageView) findViewById(R.id.widget_preview); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 144 | if (preview != null) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 145 | image.mAllowRequestLayout = false; |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 146 | image.setImageDrawable(preview); |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 147 | if (mIsAppWidget) { |
| 148 | // center horizontally |
| 149 | int[] imageSize = getPreviewSize(); |
| 150 | int centerAmount = (imageSize[0] - preview.getIntrinsicWidth()) / 2; |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 151 | image.setPadding(mOriginalImagePadding.left + centerAmount, |
| 152 | mOriginalImagePadding.top, |
| 153 | mOriginalImagePadding.right, |
| 154 | mOriginalImagePadding.bottom); |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 155 | } |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 156 | image.setAlpha(1f); |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 157 | image.mAllowRequestLayout = true; |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 161 | void setShortPressListener(ShortPressListener listener) { |
| 162 | mShortPressListener = listener; |
| 163 | } |
| 164 | |
| 165 | interface ShortPressListener { |
| 166 | void onShortPress(View v); |
| 167 | void cleanUpShortPress(View v); |
| 168 | } |
| 169 | |
| 170 | class CheckForShortPress implements Runnable { |
| 171 | public void run() { |
Adam Cohen | c93ea44 | 2012-05-16 17:15:26 -0700 | [diff] [blame] | 172 | if (sShortpressTarget != null) return; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 173 | if (mShortPressListener != null) { |
| 174 | mShortPressListener.onShortPress(PagedViewWidget.this); |
Adam Cohen | 263301a | 2012-05-17 12:24:40 -0700 | [diff] [blame] | 175 | sShortpressTarget = PagedViewWidget.this; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 176 | } |
| 177 | mShortPressTriggered = true; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private void checkForShortPress() { |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 182 | if (sShortpressTarget != null) return; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 183 | if (mPendingCheckForShortPress == null) { |
| 184 | mPendingCheckForShortPress = new CheckForShortPress(); |
| 185 | } |
| 186 | postDelayed(mPendingCheckForShortPress, 120); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Remove the longpress detection timer. |
| 191 | */ |
| 192 | private void removeShortPressCallback() { |
| 193 | if (mPendingCheckForShortPress != null) { |
| 194 | removeCallbacks(mPendingCheckForShortPress); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private void cleanUpShortPress() { |
| 199 | removeShortPressCallback(); |
| 200 | if (mShortPressTriggered) { |
| 201 | if (mShortPressListener != null) { |
| 202 | mShortPressListener.cleanUpShortPress(PagedViewWidget.this); |
| 203 | } |
| 204 | mShortPressTriggered = false; |
| 205 | } |
| 206 | } |
| 207 | |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 208 | static void resetShortPressTarget() { |
| 209 | sShortpressTarget = null; |
| 210 | } |
| 211 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 212 | @Override |
| 213 | public boolean onTouchEvent(MotionEvent event) { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 214 | super.onTouchEvent(event); |
| 215 | |
| 216 | switch (event.getAction()) { |
| 217 | case MotionEvent.ACTION_UP: |
| 218 | cleanUpShortPress(); |
| 219 | break; |
| 220 | case MotionEvent.ACTION_DOWN: |
| 221 | checkForShortPress(); |
| 222 | break; |
| 223 | case MotionEvent.ACTION_CANCEL: |
| 224 | cleanUpShortPress(); |
| 225 | break; |
| 226 | case MotionEvent.ACTION_MOVE: |
| 227 | break; |
| 228 | } |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 229 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 230 | // We eat up the touch events here, since the PagedView (which uses the same swiping |
| 231 | // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when |
| 232 | // the user is scrolling between pages. This means that if the pages themselves don't |
| 233 | // handle touch events, it gets forwarded up to PagedView itself, and it's own |
| 234 | // onTouchEvent() handling will prevent further intercept touch events from being called |
| 235 | // (it's the same view in that case). This is not ideal, but to prevent more changes, |
| 236 | // we just always mark the touch event as handled. |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 237 | return true; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 238 | } |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 239 | } |