blob: 0c3bdcaf8f6f44d471e3e129dc92dfd96eb413b0 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.view.LayoutInflater;
22import android.view.MotionEvent;
23import android.view.View;
24import android.view.ViewConfiguration;
Winson Chung97d85d22011-04-13 11:27:36 -070025import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026
Romain Guyedcce092010-03-04 13:03:17 -080027import com.android.launcher.R;
28
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029/**
30 * {@inheritDoc}
31 */
Michael Jurka08ee7702011-08-11 16:53:35 -070032public class LauncherAppWidgetHostView extends AppWidgetHostView {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033 private boolean mHasPerformedLongPress;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034 private CheckForLongPress mPendingCheckForLongPress;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035 private LayoutInflater mInflater;
Michael Jurka99b6a5b2011-01-07 15:37:17 -080036
The Android Open Source Project7376fae2009-03-11 12:11:58 -070037 public LauncherAppWidgetHostView(Context context) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038 super(context);
39 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
40 }
Adam Cohen41d5d6d2011-05-31 15:52:28 -070041
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042 @Override
43 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070044 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 }
46
47 public boolean onInterceptTouchEvent(MotionEvent ev) {
48 // Consume any touch events for ourselves after longpress is triggered
49 if (mHasPerformedLongPress) {
50 mHasPerformedLongPress = false;
51 return true;
52 }
Adam Cohen19072da2011-05-31 14:30:45 -070053
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070055 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056 switch (ev.getAction()) {
57 case MotionEvent.ACTION_DOWN: {
58 postCheckForLongClick();
59 break;
60 }
Adam Cohen19072da2011-05-31 14:30:45 -070061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 case MotionEvent.ACTION_UP:
63 case MotionEvent.ACTION_CANCEL:
64 mHasPerformedLongPress = false;
65 if (mPendingCheckForLongPress != null) {
66 removeCallbacks(mPendingCheckForLongPress);
67 }
68 break;
69 }
Adam Cohen19072da2011-05-31 14:30:45 -070070
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 // Otherwise continue letting touch events fall through to children
72 return false;
73 }
Adam Cohend4844c32011-02-18 19:25:06 -080074
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075 class CheckForLongPress implements Runnable {
76 private int mOriginalWindowAttachCount;
77
78 public void run() {
79 if ((mParent != null) && hasWindowFocus()
80 && mOriginalWindowAttachCount == getWindowAttachCount()
81 && !mHasPerformedLongPress) {
82 if (performLongClick()) {
83 mHasPerformedLongPress = true;
84 }
85 }
86 }
87
88 public void rememberWindowAttachCount() {
89 mOriginalWindowAttachCount = getWindowAttachCount();
90 }
91 }
92
93 private void postCheckForLongClick() {
94 mHasPerformedLongPress = false;
95
96 if (mPendingCheckForLongPress == null) {
97 mPendingCheckForLongPress = new CheckForLongPress();
98 }
99 mPendingCheckForLongPress.rememberWindowAttachCount();
100 postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
101 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700102
103 @Override
104 public void cancelLongPress() {
105 super.cancelLongPress();
106
107 mHasPerformedLongPress = false;
108 if (mPendingCheckForLongPress != null) {
109 removeCallbacks(mPendingCheckForLongPress);
110 }
111 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800112
Winson Chung97d85d22011-04-13 11:27:36 -0700113 @Override
114 public int getDescendantFocusability() {
115 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117}