LauncherAppWidgetHostView.java revision 651077bdd603bb182be039925fd17bdf0da15016
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.content.Context;
21import android.os.Bundle;
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    private CheckLongPressHelper mLongPressHelper;
36    private LayoutInflater mInflater;
37    private Context mContext;
38    private int mPreviousOrientation;
39    private DragLayer mDragLayer;
40
41    private float mSlop;
42
43    private boolean mWidgetReady;
44
45    public LauncherAppWidgetHostView(Context context) {
46        this(context, true);
47    }
48
49    public LauncherAppWidgetHostView(Context context, boolean widgetReady) {
50        super(context);
51        mContext = context;
52        mLongPressHelper = new CheckLongPressHelper(this);
53        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
54        mDragLayer = ((Launcher) context).getDragLayer();
55        mWidgetReady = widgetReady;
56    }
57
58    @Override
59    public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
60            int maxHeight) {
61        // If the widget is not yet ready, the app widget size cannot be updated.
62        if (mWidgetReady) {
63            super.updateAppWidgetSize(newOptions, minWidth, minHeight, maxWidth, maxHeight);
64        }
65    }
66
67    @Override
68    protected View getErrorView() {
69        return mInflater.inflate(R.layout.appwidget_error, this, false);
70    }
71
72    @Override
73    protected View getDefaultView() {
74        if (mWidgetReady) {
75            return super.getDefaultView();
76        } else {
77            return mInflater.inflate(R.layout.appwidget_not_ready, this, false);
78        }
79    }
80
81    @Override
82    public void updateAppWidget(RemoteViews remoteViews) {
83        // Store the orientation in which the widget was inflated
84        mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
85        super.updateAppWidget(remoteViews);
86    }
87
88    public boolean orientationChangedSincedInflation() {
89        int orientation = mContext.getResources().getConfiguration().orientation;
90        if (mPreviousOrientation != orientation) {
91           return true;
92       }
93       return false;
94    }
95
96    public boolean onInterceptTouchEvent(MotionEvent ev) {
97        // Just in case the previous long press hasn't been cleared, we make sure to start fresh
98        // on touch down.
99        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
100            mLongPressHelper.cancelLongPress();
101        }
102
103        // Consume any touch events for ourselves after longpress is triggered
104        if (mLongPressHelper.hasPerformedLongPress()) {
105            mLongPressHelper.cancelLongPress();
106            return true;
107        }
108
109        // Watch for longpress events at this level to make sure
110        // users can always pick up this widget
111        switch (ev.getAction()) {
112            case MotionEvent.ACTION_DOWN: {
113                mLongPressHelper.postCheckForLongPress();
114                mDragLayer.setTouchCompleteListener(this);
115                break;
116            }
117
118            case MotionEvent.ACTION_UP:
119            case MotionEvent.ACTION_CANCEL:
120                mLongPressHelper.cancelLongPress();
121                break;
122            case MotionEvent.ACTION_MOVE:
123                if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
124                    mLongPressHelper.cancelLongPress();
125                }
126                break;
127        }
128
129        // Otherwise continue letting touch events fall through to children
130        return false;
131    }
132
133    public boolean onTouchEvent(MotionEvent ev) {
134        // If the widget does not handle touch, then cancel
135        // long press when we release the touch
136        switch (ev.getAction()) {
137            case MotionEvent.ACTION_UP:
138            case MotionEvent.ACTION_CANCEL:
139                mLongPressHelper.cancelLongPress();
140                break;
141            case MotionEvent.ACTION_MOVE:
142                if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
143                    mLongPressHelper.cancelLongPress();
144                }
145                break;
146        }
147        return false;
148    }
149
150    @Override
151    protected void onAttachedToWindow() {
152        super.onAttachedToWindow();
153        mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
154    }
155
156    @Override
157    public void cancelLongPress() {
158        super.cancelLongPress();
159        mLongPressHelper.cancelLongPress();
160    }
161
162    @Override
163    public void onTouchComplete() {
164        if (!mLongPressHelper.hasPerformedLongPress()) {
165            // If a long press has been performed, we don't want to clear the record of that since
166            // we still may be receiving a touch up which we want to intercept
167            mLongPressHelper.cancelLongPress();
168        }
169    }
170
171    @Override
172    public int getDescendantFocusability() {
173        return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
174    }
175}
176