PagedViewWidget.java revision 0e56cc9fd6814af5813e73ba7a71bf1d51d4208a
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
17package com.android.launcher2;
18
19import android.appwidget.AppWidgetProviderInfo;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.content.res.Resources;
24import android.util.AttributeSet;
25import android.view.MotionEvent;
26import android.view.View;
27import android.widget.ImageView;
28import android.widget.LinearLayout;
29import android.widget.TextView;
30
31import com.android.launcher.R;
32
33/**
34 * The linear layout used strictly for the widget/wallpaper tab of the customization tray
35 */
36public class PagedViewWidget extends LinearLayout {
37    static final String TAG = "PagedViewWidgetLayout";
38
39    private static boolean sDeletePreviewsWhenDetachedFromWindow = true;
40
41    private String mDimensionsFormatString;
42    CheckForShortPress mPendingCheckForShortPress = null;
43    ShortPressListener mShortPressListener = null;
44    boolean mShortPressTriggered = false;
45    static PagedViewWidget sShortpressTarget = null;
46
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
58        final Resources r = context.getResources();
59        mDimensionsFormatString = r.getString(R.string.widget_dims_format);
60
61        setWillNotDraw(false);
62        setClipToPadding(false);
63    }
64
65    public static void setDeletePreviewsWhenDetachedFromWindow(boolean value) {
66        sDeletePreviewsWhenDetachedFromWindow = value;
67    }
68
69    @Override
70    protected void onDetachedFromWindow() {
71        super.onDetachedFromWindow();
72
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                }
82        }
83    }
84
85    public void applyFromAppWidgetProviderInfo(AppWidgetProviderInfo info,
86            int maxWidth, int[] cellSpan) {
87        final ImageView image = (ImageView) findViewById(R.id.widget_preview);
88        if (maxWidth > -1) {
89            image.setMaxWidth(maxWidth);
90        }
91        image.setContentDescription(info.label);
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);
95        if (dims != null) {
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));
99        }
100    }
101
102    public void applyFromResolveInfo(PackageManager pm, ResolveInfo info) {
103        CharSequence label = info.loadLabel(pm);
104        final ImageView image = (ImageView) findViewById(R.id.widget_preview);
105        image.setContentDescription(label);
106        final TextView name = (TextView) findViewById(R.id.widget_name);
107        name.setText(label);
108        final TextView dims = (TextView) findViewById(R.id.widget_dims);
109        if (dims != null) {
110            dims.setText(String.format(mDimensionsFormatString, 1, 1));
111        }
112    }
113
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) {
123        final PagedViewWidgetImageView image =
124                (PagedViewWidgetImageView) findViewById(R.id.widget_preview);
125        if (preview != null) {
126            image.mAllowRequestLayout = false;
127            image.setImageDrawable(preview);
128            image.setAlpha(1f);
129            image.mAllowRequestLayout = true;
130        }
131    }
132
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() {
144            if (mShortPressListener != null) {
145                if (sShortpressTarget != null) return;
146                mShortPressListener.onShortPress(PagedViewWidget.this);
147            }
148            sShortpressTarget = PagedViewWidget.this;
149            mShortPressTriggered = true;
150        }
151    }
152
153    private void checkForShortPress() {
154        if (sShortpressTarget != null) return;
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
180    static void resetShortPressTarget() {
181        sShortpressTarget = null;
182    }
183
184    @Override
185    public boolean onTouchEvent(MotionEvent event) {
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        }
201
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.
209        return true;
210    }
211}
212