blob: 7eb005255840005fff87594470b6b27016f5d0f1 [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;
Sunny Goyal651077b2014-06-30 14:15:31 -070021import android.os.Bundle;
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 {
Winson Chung88f33452012-02-23 15:23:44 -080035 private CheckLongPressHelper mLongPressHelper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036 private LayoutInflater mInflater;
Adam Cohen06dff352012-06-01 17:17:08 -070037 private Context mContext;
38 private int mPreviousOrientation;
Adam Cohenb0f3d742013-10-08 19:16:14 -070039 private DragLayer mDragLayer;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080040
Jason Monk02dd7ae2014-04-15 15:23:31 -040041 private float mSlop;
42
Sunny Goyal651077b2014-06-30 14:15:31 -070043 private boolean mWidgetReady;
44
The Android Open Source Project7376fae2009-03-11 12:11:58 -070045 public LauncherAppWidgetHostView(Context context) {
Sunny Goyal651077b2014-06-30 14:15:31 -070046 this(context, true);
47 }
48
49 public LauncherAppWidgetHostView(Context context, boolean widgetReady) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 super(context);
Adam Cohen06dff352012-06-01 17:17:08 -070051 mContext = context;
Winson Chung88f33452012-02-23 15:23:44 -080052 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Adam Cohenb0f3d742013-10-08 19:16:14 -070054 mDragLayer = ((Launcher) context).getDragLayer();
Sunny Goyal651077b2014-06-30 14:15:31 -070055 mWidgetReady = widgetReady;
56 }
57
58 @Override
59 public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
60 int maxHeight) {
61 // If the widget is not yet ready, the app widget size cannot be updated.
62 if (mWidgetReady) {
63 super.updateAppWidgetSize(newOptions, minWidth, minHeight, maxWidth, maxHeight);
64 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070066
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 @Override
68 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070069 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 }
71
Adam Cohen06dff352012-06-01 17:17:08 -070072 @Override
Sunny Goyal651077b2014-06-30 14:15:31 -070073 protected View getDefaultView() {
74 if (mWidgetReady) {
75 return super.getDefaultView();
76 } else {
77 return mInflater.inflate(R.layout.appwidget_not_ready, this, false);
78 }
79 }
80
81 @Override
Adam Cohen06dff352012-06-01 17:17:08 -070082 public void updateAppWidget(RemoteViews remoteViews) {
83 // Store the orientation in which the widget was inflated
84 mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
85 super.updateAppWidget(remoteViews);
86 }
87
88 public boolean orientationChangedSincedInflation() {
89 int orientation = mContext.getResources().getConfiguration().orientation;
90 if (mPreviousOrientation != orientation) {
91 return true;
92 }
93 return false;
94 }
95
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen3798b642013-10-16 10:30:50 -070097 // Just in case the previous long press hasn't been cleared, we make sure to start fresh
98 // on touch down.
99 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
100 mLongPressHelper.cancelLongPress();
101 }
102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 // Consume any touch events for ourselves after longpress is triggered
Winson Chung88f33452012-02-23 15:23:44 -0800104 if (mLongPressHelper.hasPerformedLongPress()) {
105 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 return true;
107 }
Adam Cohen19072da2011-05-31 14:30:45 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 switch (ev.getAction()) {
112 case MotionEvent.ACTION_DOWN: {
Winson Chung88f33452012-02-23 15:23:44 -0800113 mLongPressHelper.postCheckForLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -0700114 mDragLayer.setTouchCompleteListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 break;
116 }
Adam Cohen19072da2011-05-31 14:30:45 -0700117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 case MotionEvent.ACTION_UP:
119 case MotionEvent.ACTION_CANCEL:
Winson Chung88f33452012-02-23 15:23:44 -0800120 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400122 case MotionEvent.ACTION_MOVE:
123 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
124 mLongPressHelper.cancelLongPress();
125 }
126 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 }
Adam Cohen19072da2011-05-31 14:30:45 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 // Otherwise continue letting touch events fall through to children
130 return false;
131 }
Adam Cohend4844c32011-02-18 19:25:06 -0800132
Winson Chunge7a852e2013-08-16 11:10:59 -0700133 public boolean onTouchEvent(MotionEvent ev) {
134 // If the widget does not handle touch, then cancel
135 // long press when we release the touch
136 switch (ev.getAction()) {
137 case MotionEvent.ACTION_UP:
138 case MotionEvent.ACTION_CANCEL:
139 mLongPressHelper.cancelLongPress();
140 break;
Jason Monk02dd7ae2014-04-15 15:23:31 -0400141 case MotionEvent.ACTION_MOVE:
142 if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
143 mLongPressHelper.cancelLongPress();
144 }
145 break;
Winson Chunge7a852e2013-08-16 11:10:59 -0700146 }
147 return false;
148 }
149
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700150 @Override
Jason Monk02dd7ae2014-04-15 15:23:31 -0400151 protected void onAttachedToWindow() {
152 super.onAttachedToWindow();
153 mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
154 }
155
156 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700157 public void cancelLongPress() {
158 super.cancelLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -0700159 mLongPressHelper.cancelLongPress();
160 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700161
Adam Cohenb0f3d742013-10-08 19:16:14 -0700162 @Override
163 public void onTouchComplete() {
Adam Cohen3798b642013-10-16 10:30:50 -0700164 if (!mLongPressHelper.hasPerformedLongPress()) {
165 // If a long press has been performed, we don't want to clear the record of that since
166 // we still may be receiving a touch up which we want to intercept
167 mLongPressHelper.cancelLongPress();
168 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700169 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800170
Winson Chung97d85d22011-04-13 11:27:36 -0700171 @Override
172 public int getDescendantFocusability() {
173 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175}