The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 19 | import android.appwidget.AppWidgetHostView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | */ |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 29 | public class LauncherAppWidgetHostView extends AppWidgetHostView { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | private boolean mHasPerformedLongPress; |
| 31 | |
| 32 | private CheckForLongPress mPendingCheckForLongPress; |
| 33 | |
| 34 | private LayoutInflater mInflater; |
| 35 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 36 | public LauncherAppWidgetHostView(Context context) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | super(context); |
| 38 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected View getErrorView() { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 43 | return mInflater.inflate(R.layout.appwidget_error, this, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 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 |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 54 | // users can always pick up this widget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
| 77 | public void run() { |
| 78 | if ((mParent != null) && hasWindowFocus() |
| 79 | && mOriginalWindowAttachCount == getWindowAttachCount() |
| 80 | && !mHasPerformedLongPress) { |
| 81 | if (performLongClick()) { |
| 82 | mHasPerformedLongPress = true; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public void rememberWindowAttachCount() { |
| 88 | mOriginalWindowAttachCount = getWindowAttachCount(); |
| 89 | } |
| 90 | } |
| 91 | |
| 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 | } |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame^] | 101 | |
| 102 | @Override |
| 103 | public void cancelLongPress() { |
| 104 | super.cancelLongPress(); |
| 105 | |
| 106 | mHasPerformedLongPress = false; |
| 107 | if (mPendingCheckForLongPress != null) { |
| 108 | removeCallbacks(mPendingCheckForLongPress); |
| 109 | } |
| 110 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 111 | } |