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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
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;
|
2014-04-15 15:23:31 -04:00
|
|
|
import android.view.ViewConfiguration;
|
2011-04-13 11:27:36 -07:00
|
|
|
import android.view.ViewGroup;
|
2012-06-01 17:17:08 -07:00
|
|
|
import android.widget.RemoteViews;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2013-10-08 19:16:14 -07:00
|
|
|
import com.android.launcher3.DragLayer.TouchCompleteListener;
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
2013-10-08 19:16:14 -07:00
|
|
|
public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
|
2014-07-23 13:58:07 -07:00
|
|
|
|
|
|
|
|
LayoutInflater mInflater;
|
|
|
|
|
|
2012-02-23 15:23:44 -08:00
|
|
|
private CheckLongPressHelper mLongPressHelper;
|
2012-06-01 17:17:08 -07:00
|
|
|
private Context mContext;
|
|
|
|
|
private int mPreviousOrientation;
|
2013-10-08 19:16:14 -07:00
|
|
|
private DragLayer mDragLayer;
|
2011-01-07 15:37:17 -08:00
|
|
|
|
2014-04-15 15:23:31 -04:00
|
|
|
private float mSlop;
|
|
|
|
|
|
2009-03-11 12:11:58 -07:00
|
|
|
public LauncherAppWidgetHostView(Context context) {
|
2009-03-03 19:32:27 -08:00
|
|
|
super(context);
|
2012-06-01 17:17:08 -07:00
|
|
|
mContext = context;
|
2012-02-23 15:23:44 -08:00
|
|
|
mLongPressHelper = new CheckLongPressHelper(this);
|
2009-03-03 19:32:27 -08:00
|
|
|
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2013-10-08 19:16:14 -07:00
|
|
|
mDragLayer = ((Launcher) context).getDragLayer();
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2011-05-31 15:52:28 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
2012-06-01 17:17:08 -07:00
|
|
|
@Override
|
|
|
|
|
public void updateAppWidget(RemoteViews remoteViews) {
|
|
|
|
|
// Store the orientation in which the widget was inflated
|
|
|
|
|
mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
|
|
|
|
|
super.updateAppWidget(remoteViews);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 13:58:07 -07:00
|
|
|
public boolean isReinflateRequired() {
|
|
|
|
|
// Re-inflate is required if the orientation has changed since last inflated.
|
2012-06-01 17:17:08 -07:00
|
|
|
int orientation = mContext.getResources().getConfiguration().orientation;
|
|
|
|
|
if (mPreviousOrientation != orientation) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
2013-10-16 10:30:50 -07:00
|
|
|
// Just in case the previous long press hasn't been cleared, we make sure to start fresh
|
|
|
|
|
// on touch down.
|
|
|
|
|
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
|
|
|
|
|
mLongPressHelper.cancelLongPress();
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
// Consume any touch events for ourselves after longpress is triggered
|
2012-02-23 15:23:44 -08:00
|
|
|
if (mLongPressHelper.hasPerformedLongPress()) {
|
|
|
|
|
mLongPressHelper.cancelLongPress();
|
2009-03-03 19:32:27 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
2011-05-31 14:30:45 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
// 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: {
|
2012-02-23 15:23:44 -08:00
|
|
|
mLongPressHelper.postCheckForLongPress();
|
2013-10-08 19:16:14 -07:00
|
|
|
mDragLayer.setTouchCompleteListener(this);
|
2009-03-03 19:32:27 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2011-05-31 14:30:45 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
2012-02-23 15:23:44 -08:00
|
|
|
mLongPressHelper.cancelLongPress();
|
2009-03-03 19:32:27 -08:00
|
|
|
break;
|
2014-04-15 15:23:31 -04:00
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
|
if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
|
|
|
|
|
mLongPressHelper.cancelLongPress();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2011-05-31 14:30:45 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
// Otherwise continue letting touch events fall through to children
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-02-18 19:25:06 -08:00
|
|
|
|
2013-08-16 11:10:59 -07:00
|
|
|
public boolean onTouchEvent(MotionEvent ev) {
|
|
|
|
|
// If the widget does not handle touch, then cancel
|
|
|
|
|
// long press when we release the touch
|
|
|
|
|
switch (ev.getAction()) {
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
|
mLongPressHelper.cancelLongPress();
|
|
|
|
|
break;
|
2014-04-15 15:23:31 -04:00
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
|
if (!Utilities.pointInView(this, ev.getX(), ev.getY(), mSlop)) {
|
|
|
|
|
mLongPressHelper.cancelLongPress();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2013-08-16 11:10:59 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 15:23:31 -04:00
|
|
|
@Override
|
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
|
mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-20 21:03:13 -07:00
|
|
|
@Override
|
|
|
|
|
public void cancelLongPress() {
|
|
|
|
|
super.cancelLongPress();
|
2013-10-08 19:16:14 -07:00
|
|
|
mLongPressHelper.cancelLongPress();
|
|
|
|
|
}
|
2009-04-20 21:03:13 -07:00
|
|
|
|
2013-10-08 19:16:14 -07:00
|
|
|
@Override
|
|
|
|
|
public void onTouchComplete() {
|
2013-10-16 10:30:50 -07:00
|
|
|
if (!mLongPressHelper.hasPerformedLongPress()) {
|
|
|
|
|
// If a long press has been performed, we don't want to clear the record of that since
|
|
|
|
|
// we still may be receiving a touch up which we want to intercept
|
|
|
|
|
mLongPressHelper.cancelLongPress();
|
|
|
|
|
}
|
2009-04-20 21:03:13 -07:00
|
|
|
}
|
2011-01-07 15:37:17 -08:00
|
|
|
|
2011-04-13 11:27:36 -07:00
|
|
|
@Override
|
|
|
|
|
public int getDescendantFocusability() {
|
|
|
|
|
return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|