blob: 1e21a19933ee0e2eb6c445deb32424bb04aa51f8 [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
17package com.android.launcher;
18
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
26/**
27 * {@inheritDoc}
28 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -070029public class LauncherAppWidgetHostView extends AppWidgetHostView {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030 private boolean mHasPerformedLongPress;
31
32 private CheckForLongPress mPendingCheckForLongPress;
33
34 private LayoutInflater mInflater;
35
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);
38 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
39 }
40
41 @Override
42 protected View getErrorView() {
The Android Open Source Project7376fae2009-03-11 12:11:58 -070043 return mInflater.inflate(R.layout.appwidget_error, this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044 }
45
46 public boolean onInterceptTouchEvent(MotionEvent ev) {
47 // Consume any touch events for ourselves after longpress is triggered
48 if (mHasPerformedLongPress) {
49 mHasPerformedLongPress = false;
50 return true;
51 }
52
53 // Watch for longpress events at this level to make sure
The Android Open Source Project7376fae2009-03-11 12:11:58 -070054 // users can always pick up this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 switch (ev.getAction()) {
56 case MotionEvent.ACTION_DOWN: {
57 postCheckForLongClick();
58 break;
59 }
60
61 case MotionEvent.ACTION_UP:
62 case MotionEvent.ACTION_CANCEL:
63 mHasPerformedLongPress = false;
64 if (mPendingCheckForLongPress != null) {
65 removeCallbacks(mPendingCheckForLongPress);
66 }
67 break;
68 }
69
70 // Otherwise continue letting touch events fall through to children
71 return false;
72 }
73
74 class CheckForLongPress implements Runnable {
75 private int mOriginalWindowAttachCount;
76
77 public void run() {
78 if ((mParent != null) && hasWindowFocus()
79 && mOriginalWindowAttachCount == getWindowAttachCount()
80 && !mHasPerformedLongPress) {
81 if (performLongClick()) {
82 mHasPerformedLongPress = true;
83 }
84 }
85 }
86
87 public void rememberWindowAttachCount() {
88 mOriginalWindowAttachCount = getWindowAttachCount();
89 }
90 }
91
92 private void postCheckForLongClick() {
93 mHasPerformedLongPress = false;
94
95 if (mPendingCheckForLongPress == null) {
96 mPendingCheckForLongPress = new CheckForLongPress();
97 }
98 mPendingCheckForLongPress.rememberWindowAttachCount();
99 postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
100 }
101}