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.content.res.Configuration;
     22 import android.os.Bundle;
     23 import android.os.Parcel;
     24 import android.view.LayoutInflater;
     25 import android.view.MotionEvent;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.RemoteViews;
     29 
     30 import com.android.launcher.R;
     31 
     32 /**
     33  * {@inheritDoc}
     34  */
     35 public class LauncherAppWidgetHostView extends AppWidgetHostView {
     36     private CheckLongPressHelper mLongPressHelper;
     37     private LayoutInflater mInflater;
     38     private Context mContext;
     39     private int mPreviousOrientation;
     40 
     41     public LauncherAppWidgetHostView(Context context) {
     42         super(context);
     43         mContext = context;
     44         mLongPressHelper = new CheckLongPressHelper(this);
     45         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     46     }
     47 
     48     @Override
     49     protected View getErrorView() {
     50         return mInflater.inflate(R.layout.appwidget_error, this, false);
     51     }
     52 
     53     @Override
     54     public void updateAppWidget(RemoteViews remoteViews) {
     55         // Store the orientation in which the widget was inflated
     56         mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
     57         super.updateAppWidget(remoteViews);
     58     }
     59 
     60     public boolean orientationChangedSincedInflation() {
     61         int orientation = mContext.getResources().getConfiguration().orientation;
     62         if (mPreviousOrientation != orientation) {
     63            return true;
     64        }
     65        return false;
     66     }
     67 
     68     public boolean onInterceptTouchEvent(MotionEvent ev) {
     69         // Consume any touch events for ourselves after longpress is triggered
     70         if (mLongPressHelper.hasPerformedLongPress()) {
     71             mLongPressHelper.cancelLongPress();
     72             return true;
     73         }
     74 
     75         // Watch for longpress events at this level to make sure
     76         // users can always pick up this widget
     77         switch (ev.getAction()) {
     78             case MotionEvent.ACTION_DOWN: {
     79                 mLongPressHelper.postCheckForLongPress();
     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     @Override
     94     public void cancelLongPress() {
     95         super.cancelLongPress();
     96 
     97         mLongPressHelper.cancelLongPress();
     98     }
     99 
    100     @Override
    101     public int getDescendantFocusability() {
    102         return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
    103     }
    104 }
    105