LauncherAppWidgetHostView.java revision 26acafbe67565a9cc6aeb7d6c43a1c6d1cb9b73c
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.view.LayoutInflater;
22import android.view.MotionEvent;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.RemoteViews;
26
27/**
28 * {@inheritDoc}
29 */
30public class LauncherAppWidgetHostView extends AppWidgetHostView {
31    private CheckLongPressHelper mLongPressHelper;
32    private LayoutInflater mInflater;
33    private Context mContext;
34    private int mPreviousOrientation;
35
36    public LauncherAppWidgetHostView(Context context) {
37        super(context);
38        mContext = context;
39        mLongPressHelper = new CheckLongPressHelper(this);
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    @Override
49    public void updateAppWidget(RemoteViews remoteViews) {
50        // Store the orientation in which the widget was inflated
51        mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
52        super.updateAppWidget(remoteViews);
53    }
54
55    public boolean orientationChangedSincedInflation() {
56        int orientation = mContext.getResources().getConfiguration().orientation;
57        if (mPreviousOrientation != orientation) {
58           return true;
59       }
60       return false;
61    }
62
63    public boolean onInterceptTouchEvent(MotionEvent ev) {
64        // Consume any touch events for ourselves after longpress is triggered
65        if (mLongPressHelper.hasPerformedLongPress()) {
66            mLongPressHelper.cancelLongPress();
67            return true;
68        }
69
70        // Watch for longpress events at this level to make sure
71        // users can always pick up this widget
72        switch (ev.getAction()) {
73            case MotionEvent.ACTION_DOWN: {
74                mLongPressHelper.postCheckForLongPress();
75                break;
76            }
77
78            case MotionEvent.ACTION_UP:
79            case MotionEvent.ACTION_CANCEL:
80                mLongPressHelper.cancelLongPress();
81                break;
82        }
83
84        // Otherwise continue letting touch events fall through to children
85        return false;
86    }
87
88    public boolean onTouchEvent(MotionEvent ev) {
89        // If the widget does not handle touch, then cancel
90        // long press when we release the touch
91        switch (ev.getAction()) {
92            case MotionEvent.ACTION_UP:
93            case MotionEvent.ACTION_CANCEL:
94                mLongPressHelper.cancelLongPress();
95                break;
96        }
97        return false;
98    }
99
100    @Override
101    public void cancelLongPress() {
102        super.cancelLongPress();
103
104        mLongPressHelper.cancelLongPress();
105    }
106
107    @Override
108    public int getDescendantFocusability() {
109        return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
110    }
111}
112