blob: e39727b175f3adaee74e36214c650de5a176448c [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 {
Sunny Goyalff572272014-07-23 13:58:07 -070034
35 LayoutInflater mInflater;
36
Winson Chung88f33452012-02-23 15:23:44 -080037 private CheckLongPressHelper mLongPressHelper;
Adam Cohen06dff352012-06-01 17:17:08 -070038 private Context mContext;
39 private int mPreviousOrientation;
Adam Cohenb0f3d742013-10-08 19:16:14 -070040 private DragLayer mDragLayer;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080041
Jason Monk02dd7ae2014-04-15 15:23:31 -040042 private float mSlop;
43
The Android Open Source Project7376fae2009-03-11 12:11:58 -070044 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 super(context);
Adam Cohen06dff352012-06-01 17:17:08 -070046 mContext = context;
Winson Chung88f33452012-02-23 15:23:44 -080047 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Adam Cohenb0f3d742013-10-08 19:16:14 -070049 mDragLayer = ((Launcher) context).getDragLayer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070051
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 @Override
53 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070054 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 }
56
Adam Cohen06dff352012-06-01 17:17:08 -070057 @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
Sunny Goyalff572272014-07-23 13:58:07 -070064 public boolean isReinflateRequired() {
65 // Re-inflate is required if the orientation has changed since last inflated.
Adam Cohen06dff352012-06-01 17:17:08 -070066 int orientation = mContext.getResources().getConfiguration().orientation;
67 if (mPreviousOrientation != orientation) {
68 return true;
69 }
70 return false;
71 }
72
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen3798b642013-10-16 10:30:50 -070074 // 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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 // Consume any touch events for ourselves after longpress is triggered
Winson Chung88f33452012-02-23 15:23:44 -080081 if (mLongPressHelper.hasPerformedLongPress()) {
82 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 return true;
84 }
Adam Cohen19072da2011-05-31 14:30:45 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070087 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 switch (ev.getAction()) {
89 case MotionEvent.ACTION_DOWN: {
Winson Chung88f33452012-02-23 15:23:44 -080090 mLongPressHelper.postCheckForLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -070091 mDragLayer.setTouchCompleteListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 break;
93 }
Adam Cohen19072da2011-05-31 14:30:45 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095 case MotionEvent.ACTION_UP:
96 case MotionEvent.ACTION_CANCEL:
Winson Chung88f33452012-02-23 15:23:44 -080097 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -040099 case MotionEvent.ACTION_MOVE:
100 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
101 mLongPressHelper.cancelLongPress();
102 }
103 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 }
Adam Cohen19072da2011-05-31 14:30:45 -0700105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 // Otherwise continue letting touch events fall through to children
107 return false;
108 }
Adam Cohend4844c32011-02-18 19:25:06 -0800109
Winson Chunge7a852e2013-08-16 11:10:59 -0700110 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;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400118 case MotionEvent.ACTION_MOVE:
119 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
120 mLongPressHelper.cancelLongPress();
121 }
122 break;
Winson Chunge7a852e2013-08-16 11:10:59 -0700123 }
124 return false;
125 }
126
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700127 @Override
Jason Monk02dd7ae2014-04-15 15:23:31 -0400128 protected void onAttachedToWindow() {
129 super.onAttachedToWindow();
130 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
131 }
132
133 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700134 public void cancelLongPress() {
135 super.cancelLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -0700136 mLongPressHelper.cancelLongPress();
137 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700138
Adam Cohenb0f3d742013-10-08 19:16:14 -0700139 @Override
140 public void onTouchComplete() {
Adam Cohen3798b642013-10-16 10:30:50 -0700141 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 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700146 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800147
Winson Chung97d85d22011-04-13 11:27:36 -0700148 @Override
149 public int getDescendantFocusability() {
150 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152}