LauncherAppWidgetHostView.java revision 71b5c0b988a64b3a0613ded5403749bc537ee8a5
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.launcher3;
18
19import android.appwidget.AppWidgetHostView;
20import android.appwidget.AppWidgetProviderInfo;
21import android.content.Context;
22import android.view.LayoutInflater;
23import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewConfiguration;
26import android.view.ViewGroup;
27import android.widget.RemoteViews;
28
29import com.android.launcher3.DragLayer.TouchCompleteListener;
30
31/**
32 * {@inheritDoc}
33 */
34public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
35
36    LayoutInflater mInflater;
37
38    private CheckLongPressHelper mLongPressHelper;
39    private Context mContext;
40    private int mPreviousOrientation;
41    private DragLayer mDragLayer;
42
43    private float mSlop;
44
45    public LauncherAppWidgetHostView(Context context) {
46        super(context);
47        mContext = context;
48        mLongPressHelper = new CheckLongPressHelper(this);
49        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
50        mDragLayer = ((Launcher) context).getDragLayer();
51        setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
52    }
53
54    @Override
55    protected View getErrorView() {
56        return mInflater.inflate(R.layout.appwidget_error, this, false);
57    }
58
59    public void updateLastInflationOrientation() {
60        mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
61    }
62
63    @Override
64    public void updateAppWidget(RemoteViews remoteViews) {
65        // Store the orientation in which the widget was inflated
66        updateLastInflationOrientation();
67        super.updateAppWidget(remoteViews);
68    }
69
70    public boolean isReinflateRequired() {
71        // Re-inflate is required if the orientation has changed since last inflated.
72        int orientation = mContext.getResources().getConfiguration().orientation;
73        if (mPreviousOrientation != orientation) {
74           return true;
75       }
76       return false;
77    }
78
79    public boolean onInterceptTouchEvent(MotionEvent ev) {
80        // Just in case the previous long press hasn't been cleared, we make sure to start fresh
81        // on touch down.
82        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
83            mLongPressHelper.cancelLongPress();
84        }
85
86        // Consume any touch events for ourselves after longpress is triggered
87        if (mLongPressHelper.hasPerformedLongPress()) {
88            mLongPressHelper.cancelLongPress();
89            return true;
90        }
91
92        // Watch for longpress events at this level to make sure
93        // users can always pick up this widget
94        switch (ev.getAction()) {
95            case MotionEvent.ACTION_DOWN: {
96                mLongPressHelper.postCheckForLongPress();
97                mDragLayer.setTouchCompleteListener(this);
98                break;
99            }
100
101            case MotionEvent.ACTION_UP:
102            case MotionEvent.ACTION_CANCEL:
103                mLongPressHelper.cancelLongPress();
104                break;
105            case MotionEvent.ACTION_MOVE:
106                if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
107                    mLongPressHelper.cancelLongPress();
108                }
109                break;
110        }
111
112        // Otherwise continue letting touch events fall through to children
113        return false;
114    }
115
116    public boolean onTouchEvent(MotionEvent ev) {
117        // If the widget does not handle touch, then cancel
118        // long press when we release the touch
119        switch (ev.getAction()) {
120            case MotionEvent.ACTION_UP:
121            case MotionEvent.ACTION_CANCEL:
122                mLongPressHelper.cancelLongPress();
123                break;
124            case MotionEvent.ACTION_MOVE:
125                if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
126                    mLongPressHelper.cancelLongPress();
127                }
128                break;
129        }
130        return false;
131    }
132
133    @Override
134    protected void onAttachedToWindow() {
135        super.onAttachedToWindow();
136        mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
137    }
138
139    @Override
140    public void cancelLongPress() {
141        super.cancelLongPress();
142        mLongPressHelper.cancelLongPress();
143    }
144
145    @Override
146    public AppWidgetProviderInfo getAppWidgetInfo() {
147        AppWidgetProviderInfo info = super.getAppWidgetInfo();
148        if (!(info instanceof LauncherAppWidgetProviderInfo)) {
149            throw new IllegalStateException("Launcher widget must have"
150                    + "LauncherAppWidgetProviderInfo");
151        }
152        return info;
153    }
154
155    @Override
156    public void onTouchComplete() {
157        if (!mLongPressHelper.hasPerformedLongPress()) {
158            // If a long press has been performed, we don't want to clear the record of that since
159            // we still may be receiving a touch up which we want to intercept
160            mLongPressHelper.cancelLongPress();
161        }
162    }
163
164    @Override
165    public int getDescendantFocusability() {
166        return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
167    }
168}
169