blob: 954d2d7116325ef3f48e722a27cecf0d26787cb5 [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;
Adam Cohen06dff352012-06-01 17:17:08 -070039 private Context mContext;
40 private int mPreviousOrientation;
Adam Cohenb0f3d742013-10-08 19:16:14 -070041 private DragLayer mDragLayer;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080042
Jason Monk02dd7ae2014-04-15 15:23:31 -040043 private float mSlop;
44
The Android Open Source Project7376fae2009-03-11 12:11:58 -070045 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046 super(context);
Adam Cohen06dff352012-06-01 17:17:08 -070047 mContext = context;
Winson Chung88f33452012-02-23 15:23:44 -080048 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Adam Cohenb0f3d742013-10-08 19:16:14 -070050 mDragLayer = ((Launcher) context).getDragLayer();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080051 setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070053
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 @Override
55 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070056 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 }
58
Adam Cohen59400422014-03-05 18:07:04 -080059 public void updateLastInflationOrientation() {
60 mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
61 }
62
Adam Cohen06dff352012-06-01 17:17:08 -070063 @Override
64 public void updateAppWidget(RemoteViews remoteViews) {
65 // Store the orientation in which the widget was inflated
Adam Cohen59400422014-03-05 18:07:04 -080066 updateLastInflationOrientation();
Adam Cohen06dff352012-06-01 17:17:08 -070067 super.updateAppWidget(remoteViews);
68 }
69
Sunny Goyalff572272014-07-23 13:58:07 -070070 public boolean isReinflateRequired() {
71 // Re-inflate is required if the orientation has changed since last inflated.
Adam Cohen06dff352012-06-01 17:17:08 -070072 int orientation = mContext.getResources().getConfiguration().orientation;
73 if (mPreviousOrientation != orientation) {
74 return true;
75 }
76 return false;
77 }
78
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen3798b642013-10-16 10:30:50 -070080 // Just in case the previous long press hasn't been cleared, we make sure to start fresh
81 // on touch down.
82 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
83 mLongPressHelper.cancelLongPress();
84 }
85
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 // Consume any touch events for ourselves after longpress is triggered
Winson Chung88f33452012-02-23 15:23:44 -080087 if (mLongPressHelper.hasPerformedLongPress()) {
88 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 return true;
90 }
Adam Cohen19072da2011-05-31 14:30:45 -070091
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070093 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 switch (ev.getAction()) {
95 case MotionEvent.ACTION_DOWN: {
Winson Chung88f33452012-02-23 15:23:44 -080096 mLongPressHelper.postCheckForLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -070097 mDragLayer.setTouchCompleteListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 break;
99 }
Adam Cohen19072da2011-05-31 14:30:45 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 case MotionEvent.ACTION_UP:
102 case MotionEvent.ACTION_CANCEL:
Winson Chung88f33452012-02-23 15:23:44 -0800103 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400105 case MotionEvent.ACTION_MOVE:
106 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
107 mLongPressHelper.cancelLongPress();
108 }
109 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 }
Adam Cohen19072da2011-05-31 14:30:45 -0700111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 // Otherwise continue letting touch events fall through to children
113 return false;
114 }
Adam Cohend4844c32011-02-18 19:25:06 -0800115
Winson Chunge7a852e2013-08-16 11:10:59 -0700116 public boolean onTouchEvent(MotionEvent ev) {
117 // If the widget does not handle touch, then cancel
118 // long press when we release the touch
119 switch (ev.getAction()) {
120 case MotionEvent.ACTION_UP:
121 case MotionEvent.ACTION_CANCEL:
122 mLongPressHelper.cancelLongPress();
123 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400124 case MotionEvent.ACTION_MOVE:
125 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
126 mLongPressHelper.cancelLongPress();
127 }
128 break;
Winson Chunge7a852e2013-08-16 11:10:59 -0700129 }
130 return false;
131 }
132
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700133 @Override
Jason Monk02dd7ae2014-04-15 15:23:31 -0400134 protected void onAttachedToWindow() {
135 super.onAttachedToWindow();
136 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
137 }
138
139 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700140 public void cancelLongPress() {
141 super.cancelLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -0700142 mLongPressHelper.cancelLongPress();
143 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700144
Adam Cohenb0f3d742013-10-08 19:16:14 -0700145 @Override
Adam Cohen59400422014-03-05 18:07:04 -0800146 public AppWidgetProviderInfo getAppWidgetInfo() {
147 AppWidgetProviderInfo info = super.getAppWidgetInfo();
Sunny Goyal2434d402015-02-17 11:44:15 -0800148 if (info != null && !(info instanceof LauncherAppWidgetProviderInfo)) {
Adam Cohen59400422014-03-05 18:07:04 -0800149 throw new IllegalStateException("Launcher widget must have"
Sunny Goyal2434d402015-02-17 11:44:15 -0800150 + " LauncherAppWidgetProviderInfo");
Adam Cohen59400422014-03-05 18:07:04 -0800151 }
152 return info;
153 }
154
155 @Override
Adam Cohenb0f3d742013-10-08 19:16:14 -0700156 public void onTouchComplete() {
Adam Cohen3798b642013-10-16 10:30:50 -0700157 if (!mLongPressHelper.hasPerformedLongPress()) {
158 // If a long press has been performed, we don't want to clear the record of that since
159 // we still may be receiving a touch up which we want to intercept
160 mLongPressHelper.cancelLongPress();
161 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700162 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800163
Winson Chung97d85d22011-04-13 11:27:36 -0700164 @Override
165 public int getDescendantFocusability() {
166 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168}