blob: f47fd13ecd77cd3a24d878af3fccf0e8c97930f9 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
The Android Open Source Project7376fae2009-03-11 12:11:58 -070019import android.appwidget.AppWidgetHostView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.view.LayoutInflater;
22import android.view.MotionEvent;
23import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040024import android.view.ViewConfiguration;
Winson Chung97d85d22011-04-13 11:27:36 -070025import android.view.ViewGroup;
Adam Cohen06dff352012-06-01 17:17:08 -070026import android.widget.RemoteViews;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027
Adam Cohenb0f3d742013-10-08 19:16:14 -070028import com.android.launcher3.DragLayer.TouchCompleteListener;
29
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030/**
31 * {@inheritDoc}
32 */
Adam Cohenb0f3d742013-10-08 19:16:14 -070033public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
Winson Chung88f33452012-02-23 15:23:44 -080034 private CheckLongPressHelper mLongPressHelper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035 private LayoutInflater mInflater;
Adam Cohen06dff352012-06-01 17:17:08 -070036 private Context mContext;
37 private int mPreviousOrientation;
Adam Cohenb0f3d742013-10-08 19:16:14 -070038 private DragLayer mDragLayer;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080039
Jason Monk02dd7ae2014-04-15 15:23:31 -040040 private float mSlop;
41
The Android Open Source Project7376fae2009-03-11 12:11:58 -070042 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 super(context);
Adam Cohen06dff352012-06-01 17:17:08 -070044 mContext = context;
Winson Chung88f33452012-02-23 15:23:44 -080045 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Adam Cohenb0f3d742013-10-08 19:16:14 -070047 mDragLayer = ((Launcher) context).getDragLayer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070049
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 @Override
51 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070052 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 }
54
Adam Cohen06dff352012-06-01 17:17:08 -070055 @Override
56 public void updateAppWidget(RemoteViews remoteViews) {
57 // Store the orientation in which the widget was inflated
58 mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
59 super.updateAppWidget(remoteViews);
60 }
61
62 public boolean orientationChangedSincedInflation() {
63 int orientation = mContext.getResources().getConfiguration().orientation;
64 if (mPreviousOrientation != orientation) {
65 return true;
66 }
67 return false;
68 }
69
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen3798b642013-10-16 10:30:50 -070071 // Just in case the previous long press hasn't been cleared, we make sure to start fresh
72 // on touch down.
73 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
74 mLongPressHelper.cancelLongPress();
75 }
76
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 // Consume any touch events for ourselves after longpress is triggered
Winson Chung88f33452012-02-23 15:23:44 -080078 if (mLongPressHelper.hasPerformedLongPress()) {
79 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 return true;
81 }
Adam Cohen19072da2011-05-31 14:30:45 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070084 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 switch (ev.getAction()) {
86 case MotionEvent.ACTION_DOWN: {
Winson Chung88f33452012-02-23 15:23:44 -080087 mLongPressHelper.postCheckForLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -070088 mDragLayer.setTouchCompleteListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 break;
90 }
Adam Cohen19072da2011-05-31 14:30:45 -070091
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 case MotionEvent.ACTION_UP:
93 case MotionEvent.ACTION_CANCEL:
Winson Chung88f33452012-02-23 15:23:44 -080094 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -040096 case MotionEvent.ACTION_MOVE:
97 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
98 mLongPressHelper.cancelLongPress();
99 }
100 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
Adam Cohen19072da2011-05-31 14:30:45 -0700102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 // Otherwise continue letting touch events fall through to children
104 return false;
105 }
Adam Cohend4844c32011-02-18 19:25:06 -0800106
Winson Chunge7a852e2013-08-16 11:10:59 -0700107 public boolean onTouchEvent(MotionEvent ev) {
108 // If the widget does not handle touch, then cancel
109 // long press when we release the touch
110 switch (ev.getAction()) {
111 case MotionEvent.ACTION_UP:
112 case MotionEvent.ACTION_CANCEL:
113 mLongPressHelper.cancelLongPress();
114 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400115 case MotionEvent.ACTION_MOVE:
116 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
117 mLongPressHelper.cancelLongPress();
118 }
119 break;
Winson Chunge7a852e2013-08-16 11:10:59 -0700120 }
121 return false;
122 }
123
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700124 @Override
Jason Monk02dd7ae2014-04-15 15:23:31 -0400125 protected void onAttachedToWindow() {
126 super.onAttachedToWindow();
127 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
128 }
129
130 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700131 public void cancelLongPress() {
132 super.cancelLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -0700133 mLongPressHelper.cancelLongPress();
134 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700135
Adam Cohenb0f3d742013-10-08 19:16:14 -0700136 @Override
137 public void onTouchComplete() {
Adam Cohen3798b642013-10-16 10:30:50 -0700138 if (!mLongPressHelper.hasPerformedLongPress()) {
139 // If a long press has been performed, we don't want to clear the record of that since
140 // we still may be receiving a touch up which we want to intercept
141 mLongPressHelper.cancelLongPress();
142 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700143 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800144
Winson Chung97d85d22011-04-13 11:27:36 -0700145 @Override
146 public int getDescendantFocusability() {
147 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149}