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