blob: 51a649a07f573079873fdfb836bb2f962b9390cc [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;
Winson Chung97d85d22011-04-13 11:27:36 -070024import android.view.ViewGroup;
Adam Cohen06dff352012-06-01 17:17:08 -070025import android.widget.RemoteViews;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026
Adam Cohenb0f3d742013-10-08 19:16:14 -070027import com.android.launcher3.DragLayer.TouchCompleteListener;
28
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029/**
30 * {@inheritDoc}
31 */
Adam Cohenb0f3d742013-10-08 19:16:14 -070032public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
Winson Chung88f33452012-02-23 15:23:44 -080033 private CheckLongPressHelper mLongPressHelper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034 private LayoutInflater mInflater;
Adam Cohen06dff352012-06-01 17:17:08 -070035 private Context mContext;
36 private int mPreviousOrientation;
Adam Cohenb0f3d742013-10-08 19:16:14 -070037 private DragLayer mDragLayer;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080038
The Android Open Source Project7376fae2009-03-11 12:11:58 -070039 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040 super(context);
Adam Cohen06dff352012-06-01 17:17:08 -070041 mContext = context;
Winson Chung88f33452012-02-23 15:23:44 -080042 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Adam Cohenb0f3d742013-10-08 19:16:14 -070044 mDragLayer = ((Launcher) context).getDragLayer();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070046
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047 @Override
48 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070049 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 }
51
Adam Cohen06dff352012-06-01 17:17:08 -070052 @Override
53 public void updateAppWidget(RemoteViews remoteViews) {
54 // Store the orientation in which the widget was inflated
55 mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
56 super.updateAppWidget(remoteViews);
57 }
58
59 public boolean orientationChangedSincedInflation() {
60 int orientation = mContext.getResources().getConfiguration().orientation;
61 if (mPreviousOrientation != orientation) {
62 return true;
63 }
64 return false;
65 }
66
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohen3798b642013-10-16 10:30:50 -070068 // Just in case the previous long press hasn't been cleared, we make sure to start fresh
69 // on touch down.
70 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
71 mLongPressHelper.cancelLongPress();
72 }
73
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 // Consume any touch events for ourselves after longpress is triggered
Winson Chung88f33452012-02-23 15:23:44 -080075 if (mLongPressHelper.hasPerformedLongPress()) {
76 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 return true;
78 }
Adam Cohen19072da2011-05-31 14:30:45 -070079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070081 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 switch (ev.getAction()) {
83 case MotionEvent.ACTION_DOWN: {
Winson Chung88f33452012-02-23 15:23:44 -080084 mLongPressHelper.postCheckForLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -070085 mDragLayer.setTouchCompleteListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 break;
87 }
Adam Cohen19072da2011-05-31 14:30:45 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 case MotionEvent.ACTION_UP:
90 case MotionEvent.ACTION_CANCEL:
Winson Chung88f33452012-02-23 15:23:44 -080091 mLongPressHelper.cancelLongPress();
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 // Otherwise continue letting touch events fall through to children
96 return false;
97 }
Adam Cohend4844c32011-02-18 19:25:06 -080098
Winson Chunge7a852e2013-08-16 11:10:59 -070099 public boolean onTouchEvent(MotionEvent ev) {
100 // If the widget does not handle touch, then cancel
101 // long press when we release the touch
102 switch (ev.getAction()) {
103 case MotionEvent.ACTION_UP:
104 case MotionEvent.ACTION_CANCEL:
105 mLongPressHelper.cancelLongPress();
106 break;
107 }
108 return false;
109 }
110
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700111 @Override
112 public void cancelLongPress() {
113 super.cancelLongPress();
Adam Cohenb0f3d742013-10-08 19:16:14 -0700114 mLongPressHelper.cancelLongPress();
115 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700116
Adam Cohenb0f3d742013-10-08 19:16:14 -0700117 @Override
118 public void onTouchComplete() {
Adam Cohen3798b642013-10-16 10:30:50 -0700119 if (!mLongPressHelper.hasPerformedLongPress()) {
120 // If a long press has been performed, we don't want to clear the record of that since
121 // we still may be receiving a touch up which we want to intercept
122 mLongPressHelper.cancelLongPress();
123 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700124 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800125
Winson Chung97d85d22011-04-13 11:27:36 -0700126 @Override
127 public int getDescendantFocusability() {
128 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130}