blob: 59bd307f2c6611f38bdbe000e910e79245dd4f52 [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
27/**
28 * {@inheritDoc}
29 */
Michael Jurka08ee7702011-08-11 16:53:35 -070030public class LauncherAppWidgetHostView extends AppWidgetHostView {
Winson Chung88f33452012-02-23 15:23:44 -080031 private CheckLongPressHelper mLongPressHelper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032 private LayoutInflater mInflater;
Adam Cohen06dff352012-06-01 17:17:08 -070033 private Context mContext;
34 private int mPreviousOrientation;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080035
The Android Open Source Project7376fae2009-03-11 12:11:58 -070036 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037 super(context);
Adam Cohen06dff352012-06-01 17:17:08 -070038 mContext = context;
Winson Chung88f33452012-02-23 15:23:44 -080039 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
41 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070042
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 @Override
44 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070045 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046 }
47
Adam Cohen06dff352012-06-01 17:17:08 -070048 @Override
49 public void updateAppWidget(RemoteViews remoteViews) {
50 // Store the orientation in which the widget was inflated
51 mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
52 super.updateAppWidget(remoteViews);
53 }
54
55 public boolean orientationChangedSincedInflation() {
56 int orientation = mContext.getResources().getConfiguration().orientation;
57 if (mPreviousOrientation != orientation) {
58 return true;
59 }
60 return false;
61 }
62
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 public boolean onInterceptTouchEvent(MotionEvent ev) {
64 // Consume any touch events for ourselves after longpress is triggered
Winson Chung88f33452012-02-23 15:23:44 -080065 if (mLongPressHelper.hasPerformedLongPress()) {
66 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 return true;
68 }
Adam Cohen19072da2011-05-31 14:30:45 -070069
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 switch (ev.getAction()) {
73 case MotionEvent.ACTION_DOWN: {
Winson Chung88f33452012-02-23 15:23:44 -080074 mLongPressHelper.postCheckForLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 break;
76 }
Adam Cohen19072da2011-05-31 14:30:45 -070077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 case MotionEvent.ACTION_UP:
79 case MotionEvent.ACTION_CANCEL:
Winson Chung88f33452012-02-23 15:23:44 -080080 mLongPressHelper.cancelLongPress();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 break;
82 }
Adam Cohen19072da2011-05-31 14:30:45 -070083
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 // Otherwise continue letting touch events fall through to children
85 return false;
86 }
Adam Cohend4844c32011-02-18 19:25:06 -080087
Winson Chunge7a852e2013-08-16 11:10:59 -070088 public boolean onTouchEvent(MotionEvent ev) {
89 // If the widget does not handle touch, then cancel
90 // long press when we release the touch
91 switch (ev.getAction()) {
92 case MotionEvent.ACTION_UP:
93 case MotionEvent.ACTION_CANCEL:
94 mLongPressHelper.cancelLongPress();
95 break;
96 }
97 return false;
98 }
99
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700100 @Override
101 public void cancelLongPress() {
102 super.cancelLongPress();
103
Winson Chung88f33452012-02-23 15:23:44 -0800104 mLongPressHelper.cancelLongPress();
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700105 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800106
Winson Chung97d85d22011-04-13 11:27:36 -0700107 @Override
108 public int getDescendantFocusability() {
109 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111}