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