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