blob: 0dd1d83984dddba85f4c928444c6c65717acf465 [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 Jurka99b6a5b2011-01-07 15:37:17 -080032public class LauncherAppWidgetHostView extends AppWidgetHostView
33 implements VisibilityChangedBroadcaster {
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 private VisibilityChangedListener mOnVisibilityChangedListener;
38
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);
41 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
42 }
43
44 @Override
45 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070046 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047 }
48
49 public boolean onInterceptTouchEvent(MotionEvent ev) {
50 // Consume any touch events for ourselves after longpress is triggered
51 if (mHasPerformedLongPress) {
52 mHasPerformedLongPress = false;
53 return true;
54 }
55
56 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070057 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 switch (ev.getAction()) {
59 case MotionEvent.ACTION_DOWN: {
60 postCheckForLongClick();
61 break;
62 }
63
64 case MotionEvent.ACTION_UP:
65 case MotionEvent.ACTION_CANCEL:
66 mHasPerformedLongPress = false;
67 if (mPendingCheckForLongPress != null) {
68 removeCallbacks(mPendingCheckForLongPress);
69 }
70 break;
71 }
72
73 // Otherwise continue letting touch events fall through to children
74 return false;
75 }
Adam Cohend4844c32011-02-18 19:25:06 -080076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 class CheckForLongPress implements Runnable {
78 private int mOriginalWindowAttachCount;
79
80 public void run() {
81 if ((mParent != null) && hasWindowFocus()
82 && mOriginalWindowAttachCount == getWindowAttachCount()
83 && !mHasPerformedLongPress) {
84 if (performLongClick()) {
85 mHasPerformedLongPress = true;
86 }
87 }
88 }
89
90 public void rememberWindowAttachCount() {
91 mOriginalWindowAttachCount = getWindowAttachCount();
92 }
93 }
94
95 private void postCheckForLongClick() {
96 mHasPerformedLongPress = false;
97
98 if (mPendingCheckForLongPress == null) {
99 mPendingCheckForLongPress = new CheckForLongPress();
100 }
101 mPendingCheckForLongPress.rememberWindowAttachCount();
102 postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
103 }
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700104
105 @Override
106 public void cancelLongPress() {
107 super.cancelLongPress();
108
109 mHasPerformedLongPress = false;
110 if (mPendingCheckForLongPress != null) {
111 removeCallbacks(mPendingCheckForLongPress);
112 }
113 }
Michael Jurka99b6a5b2011-01-07 15:37:17 -0800114
115 public void setVisibilityChangedListener(VisibilityChangedListener listener) {
116 mOnVisibilityChangedListener = listener;
117 }
118
119 @Override
120 protected void onVisibilityChanged(View changedView, int visibility) {
121 if (mOnVisibilityChangedListener != null) {
122 mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
123 }
124 super.onVisibilityChanged(changedView, visibility);
125 }
Winson Chung97d85d22011-04-13 11:27:36 -0700126
127 @Override
128 public int getDescendantFocusability() {
129 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131}