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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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; |
Adam Cohen | 41d5d6d | 2011-05-31 15:52:28 -0700 | [diff] [blame^] | 21 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.view.LayoutInflater; |
| 23 | import android.view.MotionEvent; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewConfiguration; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 26 | import android.view.ViewGroup; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 28 | import com.android.launcher.R; |
| 29 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | /** |
| 31 | * {@inheritDoc} |
| 32 | */ |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame] | 33 | public class LauncherAppWidgetHostView extends AppWidgetHostView |
| 34 | implements VisibilityChangedBroadcaster { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | private boolean mHasPerformedLongPress; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | private CheckForLongPress mPendingCheckForLongPress; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | private LayoutInflater mInflater; |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame] | 38 | private VisibilityChangedListener mOnVisibilityChangedListener; |
| 39 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 40 | public LauncherAppWidgetHostView(Context context) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | super(context); |
| 42 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Adam Cohen | 41d5d6d | 2011-05-31 15:52:28 -0700 | [diff] [blame^] | 43 | |
| 44 | Resources r = context.getResources(); |
| 45 | // We add necessary padding to the AppWidgetHostView |
| 46 | setPadding(r.getDimensionPixelSize(R.dimen.app_widget_padding_left), |
| 47 | r.getDimensionPixelSize(R.dimen.app_widget_padding_top), |
| 48 | r.getDimensionPixelSize(R.dimen.app_widget_padding_right), |
| 49 | r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | } |
Adam Cohen | 41d5d6d | 2011-05-31 15:52:28 -0700 | [diff] [blame^] | 51 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 52 | @Override |
| 53 | protected View getErrorView() { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 54 | return mInflater.inflate(R.layout.appwidget_error, this, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 58 | // Consume any touch events for ourselves after longpress is triggered |
| 59 | if (mHasPerformedLongPress) { |
| 60 | mHasPerformedLongPress = false; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | // 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] | 65 | // users can always pick up this widget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 66 | switch (ev.getAction()) { |
| 67 | case MotionEvent.ACTION_DOWN: { |
| 68 | postCheckForLongClick(); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | case MotionEvent.ACTION_UP: |
| 73 | case MotionEvent.ACTION_CANCEL: |
| 74 | mHasPerformedLongPress = false; |
| 75 | if (mPendingCheckForLongPress != null) { |
| 76 | removeCallbacks(mPendingCheckForLongPress); |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | // Otherwise continue letting touch events fall through to children |
| 82 | return false; |
| 83 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 84 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | class CheckForLongPress implements Runnable { |
| 86 | private int mOriginalWindowAttachCount; |
| 87 | |
| 88 | public void run() { |
| 89 | if ((mParent != null) && hasWindowFocus() |
| 90 | && mOriginalWindowAttachCount == getWindowAttachCount() |
| 91 | && !mHasPerformedLongPress) { |
| 92 | if (performLongClick()) { |
| 93 | mHasPerformedLongPress = true; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public void rememberWindowAttachCount() { |
| 99 | mOriginalWindowAttachCount = getWindowAttachCount(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private void postCheckForLongClick() { |
| 104 | mHasPerformedLongPress = false; |
| 105 | |
| 106 | if (mPendingCheckForLongPress == null) { |
| 107 | mPendingCheckForLongPress = new CheckForLongPress(); |
| 108 | } |
| 109 | mPendingCheckForLongPress.rememberWindowAttachCount(); |
| 110 | postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout()); |
| 111 | } |
Jeff Sharkey | 83f111d | 2009-04-20 21:03:13 -0700 | [diff] [blame] | 112 | |
| 113 | @Override |
| 114 | public void cancelLongPress() { |
| 115 | super.cancelLongPress(); |
| 116 | |
| 117 | mHasPerformedLongPress = false; |
| 118 | if (mPendingCheckForLongPress != null) { |
| 119 | removeCallbacks(mPendingCheckForLongPress); |
| 120 | } |
| 121 | } |
Michael Jurka | 99b6a5b | 2011-01-07 15:37:17 -0800 | [diff] [blame] | 122 | |
| 123 | public void setVisibilityChangedListener(VisibilityChangedListener listener) { |
| 124 | mOnVisibilityChangedListener = listener; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 129 | if (mOnVisibilityChangedListener != null) { |
| 130 | mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this); |
| 131 | } |
| 132 | super.onVisibilityChanged(changedView, visibility); |
| 133 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 134 | |
| 135 | @Override |
| 136 | public int getDescendantFocusability() { |
| 137 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 138 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | } |