blob: 85a80f94364f7b10e083b88da0fbd8bae73714ae [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;
25
Romain Guyedcce092010-03-04 13:03:17 -080026import com.android.launcher.R;
27
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028/**
29 * {@inheritDoc}
30 */
Michael Jurka99b6a5b2011-01-07 15:37:17 -080031public class LauncherAppWidgetHostView extends AppWidgetHostView
32 implements VisibilityChangedBroadcaster {
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 private VisibilityChangedListener mOnVisibilityChangedListener;
37
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);
41 }
42
43 @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
48 public boolean onInterceptTouchEvent(MotionEvent ev) {
49 // Consume any touch events for ourselves after longpress is triggered
50 if (mHasPerformedLongPress) {
51 mHasPerformedLongPress = false;
52 return true;
53 }
54
55 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070056 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 switch (ev.getAction()) {
58 case MotionEvent.ACTION_DOWN: {
59 postCheckForLongClick();
60 break;
61 }
62
63 case MotionEvent.ACTION_UP:
64 case MotionEvent.ACTION_CANCEL:
65 mHasPerformedLongPress = false;
66 if (mPendingCheckForLongPress != null) {
67 removeCallbacks(mPendingCheckForLongPress);
68 }
69 break;
70 }
71
72 // Otherwise continue letting touch events fall through to children
73 return false;
74 }
75
76 class CheckForLongPress implements Runnable {
77 private int mOriginalWindowAttachCount;
78
79 public void run() {
80 if ((mParent != null) && hasWindowFocus()
81 && mOriginalWindowAttachCount == getWindowAttachCount()
82 && !mHasPerformedLongPress) {
83 if (performLongClick()) {
84 mHasPerformedLongPress = true;
85 }
86 }
87 }
88
89 public void rememberWindowAttachCount() {
90 mOriginalWindowAttachCount = getWindowAttachCount();
91 }
92 }
93
94 private void postCheckForLongClick() {
95 mHasPerformedLongPress = false;
96
97 if (mPendingCheckForLongPress == null) {
98 mPendingCheckForLongPress = new CheckForLongPress();
99 }
100 mPendingCheckForLongPress.rememberWindowAttachCount();
101 postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
102 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700103
104 @Override
105 public void cancelLongPress() {
106 super.cancelLongPress();
107
108 mHasPerformedLongPress = false;
109 if (mPendingCheckForLongPress != null) {
110 removeCallbacks(mPendingCheckForLongPress);
111 }
112 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800113
114 public void setVisibilityChangedListener(VisibilityChangedListener listener) {
115 mOnVisibilityChangedListener = listener;
116 }
117
118 @Override
119 protected void onVisibilityChanged(View changedView, int visibility) {
120 if (mOnVisibilityChangedListener != null) {
121 mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
122 }
123 super.onVisibilityChanged(changedView, visibility);
124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125}