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