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