Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2009 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.AppWidgetHostView;
     20 import android.content.Context;
     21 import android.view.LayoutInflater;
     22 import android.view.MotionEvent;
     23 import android.view.View;
     24 import android.view.ViewConfiguration;
     25 import android.view.ViewGroup;
     26 
     27 import com.android.launcher.R;
     28 
     29 /**
     30  * {@inheritDoc}
     31  */
     32 public class LauncherAppWidgetHostView extends AppWidgetHostView {
     33     private boolean mHasPerformedLongPress;
     34     private CheckForLongPress mPendingCheckForLongPress;
     35     private LayoutInflater mInflater;
     36 
     37     public LauncherAppWidgetHostView(Context context) {
     38         super(context);
     39         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     40     }
     41 
     42     @Override
     43     protected View getErrorView() {
     44         return mInflater.inflate(R.layout.appwidget_error, this, false);
     45     }
     46 
     47     public boolean onInterceptTouchEvent(MotionEvent ev) {
     48         // Consume any touch events for ourselves after longpress is triggered
     49         if (mHasPerformedLongPress) {
     50             mHasPerformedLongPress = false;
     51             return true;
     52         }
     53 
     54         // Watch for longpress events at this level to make sure
     55         // users can always pick up this widget
     56         switch (ev.getAction()) {
     57             case MotionEvent.ACTION_DOWN: {
     58                 postCheckForLongClick();
     59                 break;
     60             }
     61 
     62             case MotionEvent.ACTION_UP:
     63             case MotionEvent.ACTION_CANCEL:
     64                 mHasPerformedLongPress = false;
     65                 if (mPendingCheckForLongPress != null) {
     66                     removeCallbacks(mPendingCheckForLongPress);
     67                 }
     68                 break;
     69         }
     70 
     71         // Otherwise continue letting touch events fall through to children
     72         return false;
     73     }
     74 
     75     class CheckForLongPress implements Runnable {
     76         private int mOriginalWindowAttachCount;
     77 
     78         public void run() {
     79             if ((mParent != null) && hasWindowFocus()
     80                     && mOriginalWindowAttachCount == getWindowAttachCount()
     81                     && !mHasPerformedLongPress) {
     82                 if (performLongClick()) {
     83                     mHasPerformedLongPress = true;
     84                 }
     85             }
     86         }
     87 
     88         public void rememberWindowAttachCount() {
     89             mOriginalWindowAttachCount = getWindowAttachCount();
     90         }
     91     }
     92 
     93     private void postCheckForLongClick() {
     94         mHasPerformedLongPress = false;
     95 
     96         if (mPendingCheckForLongPress == null) {
     97             mPendingCheckForLongPress = new CheckForLongPress();
     98         }
     99         mPendingCheckForLongPress.rememberWindowAttachCount();
    100         postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
    101     }
    102 
    103     @Override
    104     public void cancelLongPress() {
    105         super.cancelLongPress();
    106 
    107         mHasPerformedLongPress = false;
    108         if (mPendingCheckForLongPress != null) {
    109             removeCallbacks(mPendingCheckForLongPress);
    110         }
    111     }
    112 
    113     @Override
    114     public int getDescendantFocusability() {
    115         return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
    116     }
    117 }
    118