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;
|
2014-03-05 18:07:04 -08:00
|
|
|
import android.appwidget.AppWidgetProviderInfo;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.Context;
|
2015-10-28 23:28:21 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.view.KeyEvent;
|
2009-03-03 19:32:27 -08:00
|
|
|
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;
|
|
|
|
|
|
2015-10-28 23:28:21 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
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;
|
2015-06-02 15:02:14 -07:00
|
|
|
private StylusEventHelper mStylusEventHelper;
|
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;
|
|
|
|
|
|
2015-10-28 23:28:21 -07:00
|
|
|
private boolean mChildrenFocused;
|
|
|
|
|
|
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);
|
2015-06-02 15:02:14 -07:00
|
|
|
mStylusEventHelper = new StylusEventHelper(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();
|
2015-01-08 16:59:04 -08:00
|
|
|
setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
|
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
|
|
|
}
|
|
|
|
|
|
2014-03-05 18:07:04 -08:00
|
|
|
public void updateLastInflationOrientation() {
|
|
|
|
|
mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-01 17:17:08 -07:00
|
|
|
@Override
|
|
|
|
|
public void updateAppWidget(RemoteViews remoteViews) {
|
|
|
|
|
// Store the orientation in which the widget was inflated
|
2014-03-05 18:07:04 -08:00
|
|
|
updateLastInflationOrientation();
|
2012-06-01 17:17:08 -07:00
|
|
|
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
|
|
|
|
2015-06-02 15:02:14 -07:00
|
|
|
// Watch for longpress or stylus button press events at this level to
|
|
|
|
|
// make sure users can always pick up this widget
|
|
|
|
|
if (mStylusEventHelper.checkAndPerformStylusEvent(ev)) {
|
|
|
|
|
mLongPressHelper.cancelLongPress();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
switch (ev.getAction()) {
|
|
|
|
|
case MotionEvent.ACTION_DOWN: {
|
2015-06-02 15:02:14 -07:00
|
|
|
if (!mStylusEventHelper.inStylusButtonPressed()) {
|
|
|
|
|
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
|
|
|
|
2014-03-05 18:07:04 -08:00
|
|
|
@Override
|
|
|
|
|
public AppWidgetProviderInfo getAppWidgetInfo() {
|
|
|
|
|
AppWidgetProviderInfo info = super.getAppWidgetInfo();
|
2015-02-17 11:44:15 -08:00
|
|
|
if (info != null && !(info instanceof LauncherAppWidgetProviderInfo)) {
|
2014-03-05 18:07:04 -08:00
|
|
|
throw new IllegalStateException("Launcher widget must have"
|
2015-02-17 11:44:15 -08:00
|
|
|
+ " LauncherAppWidgetProviderInfo");
|
2014-03-05 18:07:04 -08:00
|
|
|
}
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-06 11:42:25 -07:00
|
|
|
public LauncherAppWidgetProviderInfo getLauncherAppWidgetProviderInfo() {
|
|
|
|
|
return (LauncherAppWidgetProviderInfo) getAppWidgetInfo();
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
2015-10-28 23:28:21 -07:00
|
|
|
return mChildrenFocused ? ViewGroup.FOCUS_BEFORE_DESCENDANTS
|
|
|
|
|
: ViewGroup.FOCUS_BLOCK_DESCENDANTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
|
|
|
|
if (mChildrenFocused && event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE
|
|
|
|
|
&& event.getAction() == KeyEvent.ACTION_UP) {
|
|
|
|
|
mChildrenFocused = false;
|
|
|
|
|
requestFocus();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return super.dispatchKeyEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
|
if (!mChildrenFocused && keyCode == KeyEvent.KEYCODE_ENTER) {
|
|
|
|
|
event.startTracking();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
|
|
|
|
if (event.isTracking()) {
|
|
|
|
|
if (!mChildrenFocused && keyCode == KeyEvent.KEYCODE_ENTER) {
|
|
|
|
|
mChildrenFocused = true;
|
|
|
|
|
ArrayList<View> focusableChildren = getFocusables(FOCUS_FORWARD);
|
|
|
|
|
focusableChildren.remove(this);
|
|
|
|
|
int childrenCount = focusableChildren.size();
|
|
|
|
|
switch (childrenCount) {
|
|
|
|
|
case 0:
|
|
|
|
|
mChildrenFocused = false;
|
|
|
|
|
break;
|
|
|
|
|
case 1: {
|
|
|
|
|
if (getTag() instanceof ItemInfo) {
|
|
|
|
|
ItemInfo item = (ItemInfo) getTag();
|
|
|
|
|
if (item.spanX == 1 && item.spanY == 1) {
|
|
|
|
|
focusableChildren.get(0).performClick();
|
|
|
|
|
mChildrenFocused = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// continue;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
focusableChildren.get(0).requestFocus();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return super.onKeyUp(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
|
|
|
|
|
if (gainFocus) {
|
|
|
|
|
mChildrenFocused = false;
|
|
|
|
|
}
|
|
|
|
|
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
|
2011-04-13 11:27:36 -07:00
|
|
|
}
|
2015-12-01 13:21:49 -08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void requestChildFocus(View child, View focused) {
|
|
|
|
|
super.requestChildFocus(child, focused);
|
|
|
|
|
dispatchChildFocus(focused != null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void clearChildFocus(View child) {
|
|
|
|
|
super.clearChildFocus(child);
|
|
|
|
|
dispatchChildFocus(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean dispatchUnhandledMove(View focused, int direction) {
|
|
|
|
|
return mChildrenFocused;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void dispatchChildFocus(boolean focused) {
|
|
|
|
|
if (getOnFocusChangeListener() != null) {
|
|
|
|
|
getOnFocusChangeListener().onFocusChange(this, focused || isFocused());
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|