Home | History | Annotate | Download | only in launcher2
      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.launcher2;
     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.launcher.R;
     28 
     29 /**
     30  * {@inheritDoc}
     31  */
     32 public class LauncherAppWidgetHostView extends AppWidgetHostView {
     33     private CheckLongPressHelper mLongPressHelper;
     34     private LayoutInflater mInflater;
     35     private Context mContext;
     36     private int mPreviousOrientation;
     37 
     38     public LauncherAppWidgetHostView(Context context) {
     39         super(context);
     40         mContext = context;
     41         mLongPressHelper = new CheckLongPressHelper(this);
     42         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     43     }
     44 
     45     @Override
     46     protected View getErrorView() {
     47         return mInflater.inflate(R.layout.appwidget_error, this, false);
     48     }
     49 
     50     @Override
     51     public void updateAppWidget(RemoteViews remoteViews) {
     52         // Store the orientation in which the widget was inflated
     53         mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
     54         super.updateAppWidget(remoteViews);
     55     }
     56 
     57     public boolean orientationChangedSincedInflation() {
     58         int orientation = mContext.getResources().getConfiguration().orientation;
     59         if (mPreviousOrientation != orientation) {
     60            return true;
     61        }
     62        return false;
     63     }
     64 
     65     public boolean onInterceptTouchEvent(MotionEvent ev) {
     66         // Consume any touch events for ourselves after longpress is triggered
     67         if (mLongPressHelper.hasPerformedLongPress()) {
     68             mLongPressHelper.cancelLongPress();
     69             return true;
     70         }
     71 
     72         // Watch for longpress events at this level to make sure
     73         // users can always pick up this widget
     74         switch (ev.getAction()) {
     75             case MotionEvent.ACTION_DOWN: {
     76                 mLongPressHelper.postCheckForLongPress();
     77                 break;
     78             }
     79 
     80             case MotionEvent.ACTION_UP:
     81             case MotionEvent.ACTION_CANCEL:
     82                 mLongPressHelper.cancelLongPress();
     83                 break;
     84         }
     85 
     86         // Otherwise continue letting touch events fall through to children
     87         return false;
     88     }
     89 
     90     @Override
     91     public void cancelLongPress() {
     92         super.cancelLongPress();
     93 
     94         mLongPressHelper.cancelLongPress();
     95     }
     96 
     97     @Override
     98     public int getDescendantFocusability() {
     99         return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
    100     }
    101 }
    102