blob: e89414254a36ce23527d84e5fefaf2e8a02cd0dd [file] [log] [blame]
Winson Chung29d6fea2010-12-01 15:47:31 -08001/*
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
17package com.android.launcher2;
18
19import android.appwidget.AppWidgetProviderInfo;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Winson Chung59e1f9a2010-12-21 11:31:54 -080023import android.content.res.Resources;
Winson Chung29d6fea2010-12-01 15:47:31 -080024import android.util.AttributeSet;
25import android.view.MotionEvent;
Adam Cohened66b2b2012-01-23 17:28:51 -080026import android.view.View;
Winson Chung29d6fea2010-12-01 15:47:31 -080027import android.widget.ImageView;
28import android.widget.LinearLayout;
29import android.widget.TextView;
30
31import com.android.launcher.R;
Winson Chung29d6fea2010-12-01 15:47:31 -080032
33/**
34 * The linear layout used strictly for the widget/wallpaper tab of the customization tray
35 */
Michael Jurka82369a12012-01-12 08:08:38 -080036public class PagedViewWidget extends LinearLayout {
Winson Chung29d6fea2010-12-01 15:47:31 -080037 static final String TAG = "PagedViewWidgetLayout";
38
Michael Jurka141dbd02011-11-03 13:50:45 -070039 private static boolean sDeletePreviewsWhenDetachedFromWindow = true;
Winson Chunge4a647f2011-09-30 14:41:25 -070040
Winson Chung63257c12011-05-05 17:06:13 -070041 private String mDimensionsFormatString;
Adam Cohened66b2b2012-01-23 17:28:51 -080042 CheckForShortPress mPendingCheckForShortPress = null;
43 ShortPressListener mShortPressListener = null;
44 boolean mShortPressTriggered = false;
Adam Cohen0e56cc92012-05-11 15:57:05 -070045 static PagedViewWidget sShortpressTarget = null;
Winson Chung04998342011-01-05 13:54:43 -080046
Winson Chung29d6fea2010-12-01 15:47:31 -080047 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 Chung59e1f9a2010-12-21 11:31:54 -080058 final Resources r = context.getResources();
Winson Chung63257c12011-05-05 17:06:13 -070059 mDimensionsFormatString = r.getString(R.string.widget_dims_format);
Winson Chung59e1f9a2010-12-21 11:31:54 -080060
Winson Chung29d6fea2010-12-01 15:47:31 -080061 setWillNotDraw(false);
62 setClipToPadding(false);
63 }
64
Michael Jurka141dbd02011-11-03 13:50:45 -070065 public static void setDeletePreviewsWhenDetachedFromWindow(boolean value) {
66 sDeletePreviewsWhenDetachedFromWindow = value;
67 }
68
Winson Chung09945932011-09-20 14:22:40 -070069 @Override
70 protected void onDetachedFromWindow() {
71 super.onDetachedFromWindow();
72
Michael Jurka141dbd02011-11-03 13:50:45 -070073 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 Chung09945932011-09-20 14:22:40 -070082 }
83 }
84
Winson Chung29d6fea2010-12-01 15:47:31 -080085 public void applyFromAppWidgetProviderInfo(AppWidgetProviderInfo info,
Michael Jurka82369a12012-01-12 08:08:38 -080086 int maxWidth, int[] cellSpan) {
Winson Chung29d6fea2010-12-01 15:47:31 -080087 final ImageView image = (ImageView) findViewById(R.id.widget_preview);
Winson Chung4b576be2011-04-27 17:40:20 -070088 if (maxWidth > -1) {
89 image.setMaxWidth(maxWidth);
90 }
Winson Chung6a0f57d2011-06-29 20:10:49 -070091 image.setContentDescription(info.label);
Winson Chung29d6fea2010-12-01 15:47:31 -080092 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 Chung35d4a252011-08-11 15:12:11 -070095 if (dims != null) {
Winson Chunge3e03bc2012-05-01 15:10:11 -070096 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 Chung35d4a252011-08-11 15:12:11 -070099 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800100 }
101
Michael Jurka82369a12012-01-12 08:08:38 -0800102 public void applyFromResolveInfo(PackageManager pm, ResolveInfo info) {
Winson Chung6a0f57d2011-06-29 20:10:49 -0700103 CharSequence label = info.loadLabel(pm);
Winson Chung1ed747a2011-05-03 16:18:34 -0700104 final ImageView image = (ImageView) findViewById(R.id.widget_preview);
Winson Chung6a0f57d2011-06-29 20:10:49 -0700105 image.setContentDescription(label);
Winson Chung1ed747a2011-05-03 16:18:34 -0700106 final TextView name = (TextView) findViewById(R.id.widget_name);
Winson Chung6a0f57d2011-06-29 20:10:49 -0700107 name.setText(label);
Winson Chung1ed747a2011-05-03 16:18:34 -0700108 final TextView dims = (TextView) findViewById(R.id.widget_dims);
Winson Chung46af2e82011-05-09 16:00:53 -0700109 if (dims != null) {
110 dims.setText(String.format(mDimensionsFormatString, 1, 1));
Winson Chung46af2e82011-05-09 16:00:53 -0700111 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700112 }
113
Michael Jurka038f9d82011-11-03 13:50:45 -0700114 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 Jurka81efbad2011-11-03 13:50:45 -0700123 final PagedViewWidgetImageView image =
124 (PagedViewWidgetImageView) findViewById(R.id.widget_preview);
Winson Chunge4a647f2011-09-30 14:41:25 -0700125 if (preview != null) {
Michael Jurka81efbad2011-11-03 13:50:45 -0700126 image.mAllowRequestLayout = false;
Winson Chunge4a647f2011-09-30 14:41:25 -0700127 image.setImageDrawable(preview);
Winson Chung68e4c642011-11-10 15:48:25 -0800128 image.setAlpha(1f);
Michael Jurka81efbad2011-11-03 13:50:45 -0700129 image.mAllowRequestLayout = true;
Winson Chunge4a647f2011-09-30 14:41:25 -0700130 }
131 }
132
Adam Cohened66b2b2012-01-23 17:28:51 -0800133 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 Cohenc93ea442012-05-16 17:15:26 -0700144 if (sShortpressTarget != null) return;
Adam Cohened66b2b2012-01-23 17:28:51 -0800145 if (mShortPressListener != null) {
146 mShortPressListener.onShortPress(PagedViewWidget.this);
Adam Cohen263301a2012-05-17 12:24:40 -0700147 sShortpressTarget = PagedViewWidget.this;
Adam Cohened66b2b2012-01-23 17:28:51 -0800148 }
149 mShortPressTriggered = true;
150 }
151 }
152
153 private void checkForShortPress() {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700154 if (sShortpressTarget != null) return;
Adam Cohened66b2b2012-01-23 17:28:51 -0800155 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 Cohen0e56cc92012-05-11 15:57:05 -0700180 static void resetShortPressTarget() {
181 sShortpressTarget = null;
182 }
183
Winson Chung29d6fea2010-12-01 15:47:31 -0800184 @Override
185 public boolean onTouchEvent(MotionEvent event) {
Adam Cohened66b2b2012-01-23 17:28:51 -0800186 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 Cohen0e56cc92012-05-11 15:57:05 -0700201
Winson Chung29d6fea2010-12-01 15:47:31 -0800202 // 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 Cohened66b2b2012-01-23 17:28:51 -0800209 return true;
Winson Chung29d6fea2010-12-01 15:47:31 -0800210 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800211}