• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.launcher;
18 
19 import android.appwidget.AppWidgetHostView;
20 import android.content.Context;
21 import android.view.LayoutInflater;
22 import android.view.MotionEvent;
23 import android.view.View;
24 import android.view.ViewConfiguration;
25 
26 /**
27  * {@inheritDoc}
28  */
29 public class LauncherAppWidgetHostView extends AppWidgetHostView {
30     private boolean mHasPerformedLongPress;
31 
32     private CheckForLongPress mPendingCheckForLongPress;
33 
34     private LayoutInflater mInflater;
35 
LauncherAppWidgetHostView(Context context)36     public LauncherAppWidgetHostView(Context context) {
37         super(context);
38         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
39     }
40 
41     @Override
getErrorView()42     protected View getErrorView() {
43         return mInflater.inflate(R.layout.appwidget_error, this, false);
44     }
45 
onInterceptTouchEvent(MotionEvent ev)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
54         // users can always pick up this widget
55         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 
run()77         public void run() {
78             if ((mParent != null) && hasWindowFocus()
79                     && mOriginalWindowAttachCount == getWindowAttachCount()
80                     && !mHasPerformedLongPress) {
81                 if (performLongClick()) {
82                     mHasPerformedLongPress = true;
83                 }
84             }
85         }
86 
rememberWindowAttachCount()87         public void rememberWindowAttachCount() {
88             mOriginalWindowAttachCount = getWindowAttachCount();
89         }
90     }
91 
postCheckForLongClick()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 
102     @Override
cancelLongPress()103     public void cancelLongPress() {
104         super.cancelLongPress();
105 
106         mHasPerformedLongPress = false;
107         if (mPendingCheckForLongPress != null) {
108             removeCallbacks(mPendingCheckForLongPress);
109         }
110     }
111 }
112