1/*
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.launcher2;
18
19import android.appwidget.AppWidgetHostView;
20import android.content.Context;
21import android.view.LayoutInflater;
22import android.view.MotionEvent;
23import android.view.View;
24import android.view.ViewConfiguration;
25
26import com.android.launcher.R;
27
28/**
29 * {@inheritDoc}
30 */
31public class LauncherAppWidgetHostView extends AppWidgetHostView {
32    private boolean mHasPerformedLongPress;
33
34    private CheckForLongPress mPendingCheckForLongPress;
35
36    private LayoutInflater mInflater;
37
38    public LauncherAppWidgetHostView(Context context) {
39        super(context);
40        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
41    }
42
43    @Override
44    protected View getErrorView() {
45        return mInflater.inflate(R.layout.appwidget_error, this, false);
46    }
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
56        // users can always pick up this widget
57        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    }
103
104    @Override
105    public void cancelLongPress() {
106        super.cancelLongPress();
107
108        mHasPerformedLongPress = false;
109        if (mPendingCheckForLongPress != null) {
110            removeCallbacks(mPendingCheckForLongPress);
111        }
112    }
113}
114