2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-07-30 13:37:37 -07:00
|
|
|
package com.android.launcher2;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2009-03-11 12:11:58 -07:00
|
|
|
import android.appwidget.AppWidgetHostView;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewConfiguration;
|
|
|
|
|
|
2010-03-04 13:03:17 -08:00
|
|
|
import com.android.launcher.R;
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
2011-01-07 15:37:17 -08:00
|
|
|
public class LauncherAppWidgetHostView extends AppWidgetHostView
|
|
|
|
|
implements VisibilityChangedBroadcaster {
|
2009-03-03 19:32:27 -08:00
|
|
|
private boolean mHasPerformedLongPress;
|
|
|
|
|
private CheckForLongPress mPendingCheckForLongPress;
|
|
|
|
|
private LayoutInflater mInflater;
|
2011-01-07 15:37:17 -08:00
|
|
|
private VisibilityChangedListener mOnVisibilityChangedListener;
|
|
|
|
|
|
2009-03-11 12:11:58 -07:00
|
|
|
public LauncherAppWidgetHostView(Context context) {
|
2009-03-03 19:32:27 -08:00
|
|
|
super(context);
|
|
|
|
|
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected View getErrorView() {
|
2009-03-11 12:11:58 -07:00
|
|
|
return mInflater.inflate(R.layout.appwidget_error, this, false);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
|
|
|
|
// Consume any touch events for ourselves after longpress is triggered
|
|
|
|
|
if (mHasPerformedLongPress) {
|
|
|
|
|
mHasPerformedLongPress = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Watch for longpress events at this level to make sure
|
2009-03-11 12:11:58 -07:00
|
|
|
// users can always pick up this widget
|
2009-03-03 19:32:27 -08:00
|
|
|
switch (ev.getAction()) {
|
|
|
|
|
case MotionEvent.ACTION_DOWN: {
|
|
|
|
|
postCheckForLongClick();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
|
mHasPerformedLongPress = false;
|
|
|
|
|
if (mPendingCheckForLongPress != null) {
|
|
|
|
|
removeCallbacks(mPendingCheckForLongPress);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise continue letting touch events fall through to children
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-02-18 19:25:06 -08:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
class CheckForLongPress implements Runnable {
|
|
|
|
|
private int mOriginalWindowAttachCount;
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
if ((mParent != null) && hasWindowFocus()
|
|
|
|
|
&& mOriginalWindowAttachCount == getWindowAttachCount()
|
|
|
|
|
&& !mHasPerformedLongPress) {
|
|
|
|
|
if (performLongClick()) {
|
|
|
|
|
mHasPerformedLongPress = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void rememberWindowAttachCount() {
|
|
|
|
|
mOriginalWindowAttachCount = getWindowAttachCount();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void postCheckForLongClick() {
|
|
|
|
|
mHasPerformedLongPress = false;
|
|
|
|
|
|
|
|
|
|
if (mPendingCheckForLongPress == null) {
|
|
|
|
|
mPendingCheckForLongPress = new CheckForLongPress();
|
|
|
|
|
}
|
|
|
|
|
mPendingCheckForLongPress.rememberWindowAttachCount();
|
|
|
|
|
postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
|
|
|
|
|
}
|
2009-04-20 21:03:13 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void cancelLongPress() {
|
|
|
|
|
super.cancelLongPress();
|
|
|
|
|
|
|
|
|
|
mHasPerformedLongPress = false;
|
|
|
|
|
if (mPendingCheckForLongPress != null) {
|
|
|
|
|
removeCallbacks(mPendingCheckForLongPress);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-07 15:37:17 -08:00
|
|
|
|
|
|
|
|
public void setVisibilityChangedListener(VisibilityChangedListener listener) {
|
|
|
|
|
mOnVisibilityChangedListener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onVisibilityChanged(View changedView, int visibility) {
|
|
|
|
|
if (mOnVisibilityChangedListener != null) {
|
|
|
|
|
mOnVisibilityChangedListener.receiveVisibilityChangedMessage(this);
|
|
|
|
|
}
|
|
|
|
|
super.onVisibilityChanged(changedView, visibility);
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|