Home | History | Annotate | Download | only in launcher3
      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.launcher3;
     18 
     19 import android.appwidget.AppWidgetHostView;
     20 import android.content.Context;
     21 import android.view.LayoutInflater;
     22 import android.view.MotionEvent;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.RemoteViews;
     26 
     27 import com.android.launcher3.DragLayer.TouchCompleteListener;
     28 
     29 /**
     30  * {@inheritDoc}
     31  */
     32 public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
     33     private CheckLongPressHelper mLongPressHelper;
     34     private LayoutInflater mInflater;
     35     private Context mContext;
     36     private int mPreviousOrientation;
     37     private DragLayer mDragLayer;
     38 
     39     public LauncherAppWidgetHostView(Context context) {
     40         super(context);
     41         mContext = context;
     42         mLongPressHelper = new CheckLongPressHelper(this);
     43         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     44         mDragLayer = ((Launcher) context).getDragLayer();
     45     }
     46 
     47     @Override
     48     protected View getErrorView() {
     49         return mInflater.inflate(R.layout.appwidget_error, this, false);
     50     }
     51 
     52     @Override
     53     public void updateAppWidget(RemoteViews remoteViews) {
     54         // Store the orientation in which the widget was inflated
     55         mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
     56         super.updateAppWidget(remoteViews);
     57     }
     58 
     59     public boolean orientationChangedSincedInflation() {
     60         int orientation = mContext.getResources().getConfiguration().orientation;
     61         if (mPreviousOrientation != orientation) {
     62            return true;
     63        }
     64        return false;
     65     }
     66 
     67     public boolean onInterceptTouchEvent(MotionEvent ev) {
     68         // Consume any touch events for ourselves after longpress is triggered
     69         if (mLongPressHelper.hasPerformedLongPress()) {
     70             mLongPressHelper.cancelLongPress();
     71             return true;
     72         }
     73 
     74         // Watch for longpress events at this level to make sure
     75         // users can always pick up this widget
     76         switch (ev.getAction()) {
     77             case MotionEvent.ACTION_DOWN: {
     78                 mLongPressHelper.postCheckForLongPress();
     79                 mDragLayer.setTouchCompleteListener(this);
     80                 break;
     81             }
     82 
     83             case MotionEvent.ACTION_UP:
     84             case MotionEvent.ACTION_CANCEL:
     85                 mLongPressHelper.cancelLongPress();
     86                 break;
     87         }
     88 
     89         // Otherwise continue letting touch events fall through to children
     90         return false;
     91     }
     92 
     93     public boolean onTouchEvent(MotionEvent ev) {
     94         // If the widget does not handle touch, then cancel
     95         // long press when we release the touch
     96         switch (ev.getAction()) {
     97             case MotionEvent.ACTION_UP:
     98             case MotionEvent.ACTION_CANCEL:
     99                 mLongPressHelper.cancelLongPress();
    100                 break;
    101         }
    102         return false;
    103     }
    104 
    105     @Override
    106     public void cancelLongPress() {
    107         super.cancelLongPress();
    108         mLongPressHelper.cancelLongPress();
    109     }
    110 
    111     @Override
    112     public void onTouchComplete() {
    113         mLongPressHelper.cancelLongPress();
    114     }
    115 
    116     @Override
    117     public int getDescendantFocusability() {
    118         return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
    119     }
    120 
    121 
    122 }
    123