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.launcher3; 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 import android.widget.RemoteViews; 27 28 import com.android.launcher3.DragLayer.TouchCompleteListener; 29 30 /** 31 * {@inheritDoc} 32 */ 33 public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener { 34 35 LayoutInflater mInflater; 36 37 private CheckLongPressHelper mLongPressHelper; 38 private Context mContext; 39 private int mPreviousOrientation; 40 private DragLayer mDragLayer; 41 42 private float mSlop; 43 44 public LauncherAppWidgetHostView(Context context) { 45 super(context); 46 mContext = context; 47 mLongPressHelper = new CheckLongPressHelper(this); 48 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 49 mDragLayer = ((Launcher) context).getDragLayer(); 50 } 51 52 @Override 53 protected View getErrorView() { 54 return mInflater.inflate(R.layout.appwidget_error, this, false); 55 } 56 57 @Override 58 public void updateAppWidget(RemoteViews remoteViews) { 59 // Store the orientation in which the widget was inflated 60 mPreviousOrientation = mContext.getResources().getConfiguration().orientation; 61 super.updateAppWidget(remoteViews); 62 } 63 64 public boolean isReinflateRequired() { 65 // Re-inflate is required if the orientation has changed since last inflated. 66 int orientation = mContext.getResources().getConfiguration().orientation; 67 if (mPreviousOrientation != orientation) { 68 return true; 69 } 70 return false; 71 } 72 73 public boolean onInterceptTouchEvent(MotionEvent ev) { 74 // Just in case the previous long press hasn't been cleared, we make sure to start fresh 75 // on touch down. 76 if (ev.getAction() == MotionEvent.ACTION_DOWN) { 77 mLongPressHelper.cancelLongPress(); 78 } 79 80 // Consume any touch events for ourselves after longpress is triggered 81 if (mLongPressHelper.hasPerformedLongPress()) { 82 mLongPressHelper.cancelLongPress(); 83 return true; 84 } 85 86 // Watch for longpress events at this level to make sure 87 // users can always pick up this widget 88 switch (ev.getAction()) { 89 case MotionEvent.ACTION_DOWN: { 90 mLongPressHelper.postCheckForLongPress(); 91 mDragLayer.setTouchCompleteListener(this); 92 break; 93 } 94 95 case MotionEvent.ACTION_UP: 96 case MotionEvent.ACTION_CANCEL: 97 mLongPressHelper.cancelLongPress(); 98 break; 99 case MotionEvent.ACTION_MOVE: 100 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) { 101 mLongPressHelper.cancelLongPress(); 102 } 103 break; 104 } 105 106 // Otherwise continue letting touch events fall through to children 107 return false; 108 } 109 110 public boolean onTouchEvent(MotionEvent ev) { 111 // If the widget does not handle touch, then cancel 112 // long press when we release the touch 113 switch (ev.getAction()) { 114 case MotionEvent.ACTION_UP: 115 case MotionEvent.ACTION_CANCEL: 116 mLongPressHelper.cancelLongPress(); 117 break; 118 case MotionEvent.ACTION_MOVE: 119 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) { 120 mLongPressHelper.cancelLongPress(); 121 } 122 break; 123 } 124 return false; 125 } 126 127 @Override 128 protected void onAttachedToWindow() { 129 super.onAttachedToWindow(); 130 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); 131 } 132 133 @Override 134 public void cancelLongPress() { 135 super.cancelLongPress(); 136 mLongPressHelper.cancelLongPress(); 137 } 138 139 @Override 140 public void onTouchComplete() { 141 if (!mLongPressHelper.hasPerformedLongPress()) { 142 // If a long press has been performed, we don't want to clear the record of that since 143 // we still may be receiving a touch up which we want to intercept 144 mLongPressHelper.cancelLongPress(); 145 } 146 } 147 148 @Override 149 public int getDescendantFocusability() { 150 return ViewGroup.FOCUS_BLOCK_DESCENDANTS; 151 } 152 } 153