blob: 7c3b993cee974ce9f8d5b41aa1b670b4ba601799 [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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;
Adam Cohen41d5d6d2011-05-31 15:52:28 -070021import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.view.LayoutInflater;
23import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewConfiguration;
Winson Chung97d85d22011-04-13 11:27:36 -070026import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027
Romain Guyedcce092010-03-04 13:03:17 -080028import com.android.launcher.R;
29
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030/**
31 * {@inheritDoc}
32 */
Michael Jurka08ee7702011-08-11 16:53:35 -070033public class LauncherAppWidgetHostView extends AppWidgetHostView {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034 private boolean mHasPerformedLongPress;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035 private CheckForLongPress mPendingCheckForLongPress;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036 private LayoutInflater mInflater;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080037
The Android Open Source Project7376fae2009-03-11 12:11:58 -070038 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039 super(context);
40 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Adam Cohen41d5d6d2011-05-31 15:52:28 -070041
42 Resources r = context.getResources();
43 // We add necessary padding to the AppWidgetHostView
44 setPadding(r.getDimensionPixelSize(R.dimen.app_widget_padding_left),
45 r.getDimensionPixelSize(R.dimen.app_widget_padding_top),
46 r.getDimensionPixelSize(R.dimen.app_widget_padding_right),
47 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom));
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070049
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 @Override
51 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070052 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 }
54
55 public boolean onInterceptTouchEvent(MotionEvent ev) {
56 // Consume any touch events for ourselves after longpress is triggered
57 if (mHasPerformedLongPress) {
58 mHasPerformedLongPress = false;
59 return true;
60 }
Adam Cohen19072da2011-05-31 14:30:45 -070061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070063 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 switch (ev.getAction()) {
65 case MotionEvent.ACTION_DOWN: {
66 postCheckForLongClick();
67 break;
68 }
Adam Cohen19072da2011-05-31 14:30:45 -070069
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 case MotionEvent.ACTION_UP:
71 case MotionEvent.ACTION_CANCEL:
72 mHasPerformedLongPress = false;
73 if (mPendingCheckForLongPress != null) {
74 removeCallbacks(mPendingCheckForLongPress);
75 }
76 break;
77 }
Adam Cohen19072da2011-05-31 14:30:45 -070078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 // Otherwise continue letting touch events fall through to children
80 return false;
81 }
Adam Cohend4844c32011-02-18 19:25:06 -080082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 class CheckForLongPress implements Runnable {
84 private int mOriginalWindowAttachCount;
85
86 public void run() {
87 if ((mParent != null) && hasWindowFocus()
88 && mOriginalWindowAttachCount == getWindowAttachCount()
89 && !mHasPerformedLongPress) {
90 if (performLongClick()) {
91 mHasPerformedLongPress = true;
92 }
93 }
94 }
95
96 public void rememberWindowAttachCount() {
97 mOriginalWindowAttachCount = getWindowAttachCount();
98 }
99 }
100
101 private void postCheckForLongClick() {
102 mHasPerformedLongPress = false;
103
104 if (mPendingCheckForLongPress == null) {
105 mPendingCheckForLongPress = new CheckForLongPress();
106 }
107 mPendingCheckForLongPress.rememberWindowAttachCount();
108 postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
109 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700110
111 @Override
112 public void cancelLongPress() {
113 super.cancelLongPress();
114
115 mHasPerformedLongPress = false;
116 if (mPendingCheckForLongPress != null) {
117 removeCallbacks(mPendingCheckForLongPress);
118 }
119 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800120
Winson Chung97d85d22011-04-13 11:27:36 -0700121 @Override
122 public int getDescendantFocusability() {
123 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125}