blob: cf461a5b826e6ed686d2e9c4cc9505904c726aab [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;
Adam Cohen59400422014-03-05 18:07:04 -080020import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.view.LayoutInflater;
23import android.view.MotionEvent;
24import android.view.View;
Jason Monk02dd7ae2014-04-15 15:23:31 -040025import android.view.ViewConfiguration;
Winson Chung97d85d22011-04-13 11:27:36 -070026import android.view.ViewGroup;
Adam Cohen06dff352012-06-01 17:17:08 -070027import android.widget.RemoteViews;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028
Adam Cohenb0f3d742013-10-08 19:16:14 -070029import com.android.launcher3.DragLayer.TouchCompleteListener;
30
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031/**
32 * {@inheritDoc}
33 */
Adam Cohenb0f3d742013-10-08 19:16:14 -070034public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
Sunny Goyalff572272014-07-23 13:58:07 -070035
36 LayoutInflater mInflater;
37
Winson Chung88f33452012-02-23 15:23:44 -080038 private CheckLongPressHelper mLongPressHelper;
Mady Mellor770f7c52015-06-02 15:02:14 -070039 private StylusEventHelper mStylusEventHelper;
Adam Cohen06dff352012-06-01 17:17:08 -070040 private Context mContext;
41 private int mPreviousOrientation;
Adam Cohenb0f3d742013-10-08 19:16:14 -070042 private DragLayer mDragLayer;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080043
Jason Monk02dd7ae2014-04-15 15:23:31 -040044 private float mSlop;
45
The Android Open Source Project7376fae2009-03-11 12:11:58 -070046 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047 super(context);
Adam Cohen06dff352012-06-01 17:17:08 -070048 mContext = context;
Winson Chung88f33452012-02-23 15:23:44 -080049 mLongPressHelper = new CheckLongPressHelper(this);
Mady Mellor770f7c52015-06-02 15:02:14 -070050 mStylusEventHelper = new StylusEventHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Adam Cohenb0f3d742013-10-08 19:16:14 -070052 mDragLayer = ((Launcher) context).getDragLayer();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080053 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070055
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 @Override
57 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070058 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 }
60
Adam Cohen59400422014-03-05 18:07:04 -080061 public void updateLastInflationOrientation() {
62 mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
63 }
64
Adam Cohen06dff352012-06-01 17:17:08 -070065 @Override
66 public void updateAppWidget(RemoteViews remoteViews) {
67 // Store the orientation in which the widget was inflated
Adam Cohen59400422014-03-05 18:07:04 -080068 updateLastInflationOrientation();
Adam Cohen06dff352012-06-01 17:17:08 -070069 super.updateAppWidget(remoteViews);
70 }
71
Sunny Goyalff572272014-07-23 13:58:07 -070072 public boolean isReinflateRequired() {
73 // Re-inflate is required if the orientation has changed since last inflated.
Adam Cohen06dff352012-06-01 17:17:08 -070074 int orientation = mContext.getResources().getConfiguration().orientation;
75 if (mPreviousOrientation != orientation) {
76 return true;
77 }
78 return false;
79 }
80
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen3798b642013-10-16 10:30:50 -070082 // 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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 // Consume any touch events for ourselves after longpress is triggered
Winson Chung88f33452012-02-23 15:23:44 -080089 if (mLongPressHelper.hasPerformedLongPress()) {
90 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 return true;
92 }
Adam Cohen19072da2011-05-31 14:30:45 -070093
Mady Mellor770f7c52015-06-02 15:02:14 -070094 // 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 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 switch (ev.getAction()) {
101 case MotionEvent.ACTION_DOWN: {
Mady Mellor770f7c52015-06-02 15:02:14 -0700102 if (!mStylusEventHelper.inStylusButtonPressed()) {
103 mLongPressHelper.postCheckForLongPress();
104 }
Adam Cohenb0f3d742013-10-08 19:16:14 -0700105 mDragLayer.setTouchCompleteListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 break;
107 }
Adam Cohen19072da2011-05-31 14:30:45 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 case MotionEvent.ACTION_UP:
110 case MotionEvent.ACTION_CANCEL:
Winson Chung88f33452012-02-23 15:23:44 -0800111 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400113 case MotionEvent.ACTION_MOVE:
114 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
115 mLongPressHelper.cancelLongPress();
116 }
117 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 }
Adam Cohen19072da2011-05-31 14:30:45 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 // Otherwise continue letting touch events fall through to children
121 return false;
122 }
Adam Cohend4844c32011-02-18 19:25:06 -0800123
Winson Chunge7a852e2013-08-16 11:10:59 -0700124 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;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400132 case MotionEvent.ACTION_MOVE:
133 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
134 mLongPressHelper.cancelLongPress();
135 }
136 break;
Winson Chunge7a852e2013-08-16 11:10:59 -0700137 }
138 return false;
139 }
140
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700141 @Override
Jason Monk02dd7ae2014-04-15 15:23:31 -0400142 protected void onAttachedToWindow() {
143 super.onAttachedToWindow();
144 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
145 }
146
147 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700148 public void cancelLongPress() {
149 super.cancelLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -0700150 mLongPressHelper.cancelLongPress();
151 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700152
Adam Cohenb0f3d742013-10-08 19:16:14 -0700153 @Override
Adam Cohen59400422014-03-05 18:07:04 -0800154 public AppWidgetProviderInfo getAppWidgetInfo() {
155 AppWidgetProviderInfo info = super.getAppWidgetInfo();
Sunny Goyal2434d402015-02-17 11:44:15 -0800156 if (info != null && !(info instanceof LauncherAppWidgetProviderInfo)) {
Adam Cohen59400422014-03-05 18:07:04 -0800157 throw new IllegalStateException("Launcher widget must have"
Sunny Goyal2434d402015-02-17 11:44:15 -0800158 + " LauncherAppWidgetProviderInfo");
Adam Cohen59400422014-03-05 18:07:04 -0800159 }
160 return info;
161 }
162
Adam Cohen2e6da152015-05-06 11:42:25 -0700163 public LauncherAppWidgetProviderInfo getLauncherAppWidgetProviderInfo() {
164 return (LauncherAppWidgetProviderInfo) getAppWidgetInfo();
165 }
166
Adam Cohen59400422014-03-05 18:07:04 -0800167 @Override
Adam Cohenb0f3d742013-10-08 19:16:14 -0700168 public void onTouchComplete() {
Adam Cohen3798b642013-10-16 10:30:50 -0700169 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 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700174 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800175
Winson Chung97d85d22011-04-13 11:27:36 -0700176 @Override
177 public int getDescendantFocusability() {
178 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180}