Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2011 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.content.Context;
     20 import android.content.res.Configuration;
     21 import android.content.res.TypedArray;
     22 import android.util.AttributeSet;
     23 import android.view.LayoutInflater;
     24 import android.view.MotionEvent;
     25 import android.view.View;
     26 import android.widget.FrameLayout;
     27 
     28 import com.android.launcher.R;
     29 
     30 public class Hotseat extends FrameLayout {
     31     private static final String TAG = "Hotseat";
     32     private static final int sAllAppsButtonRank = 2; // In the middle of the dock
     33 
     34     private Launcher mLauncher;
     35     private CellLayout mContent;
     36 
     37     private int mCellCountX;
     38     private int mCellCountY;
     39     private boolean mIsLandscape;
     40 
     41     public Hotseat(Context context) {
     42         this(context, null);
     43     }
     44 
     45     public Hotseat(Context context, AttributeSet attrs) {
     46         this(context, attrs, 0);
     47     }
     48 
     49     public Hotseat(Context context, AttributeSet attrs, int defStyle) {
     50         super(context, attrs, defStyle);
     51 
     52         TypedArray a = context.obtainStyledAttributes(attrs,
     53                 R.styleable.Hotseat, defStyle, 0);
     54         mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
     55         mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
     56         mIsLandscape = context.getResources().getConfiguration().orientation ==
     57             Configuration.ORIENTATION_LANDSCAPE;
     58     }
     59 
     60     public void setup(Launcher launcher) {
     61         mLauncher = launcher;
     62         setOnKeyListener(new HotseatIconKeyEventListener());
     63     }
     64 
     65     CellLayout getLayout() {
     66         return mContent;
     67     }
     68 
     69     /* Get the orientation invariant order of the item in the hotseat for persistence. */
     70     int getOrderInHotseat(int x, int y) {
     71         return mIsLandscape ? (mContent.getCountY() - y - 1) : x;
     72     }
     73     /* Get the orientation specific coordinates given an invariant order in the hotseat. */
     74     int getCellXFromOrder(int rank) {
     75         return mIsLandscape ? 0 : rank;
     76     }
     77     int getCellYFromOrder(int rank) {
     78         return mIsLandscape ? (mContent.getCountY() - (rank + 1)) : 0;
     79     }
     80     public static boolean isAllAppsButtonRank(int rank) {
     81         return rank == sAllAppsButtonRank;
     82     }
     83 
     84     @Override
     85     protected void onFinishInflate() {
     86         super.onFinishInflate();
     87         if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX();
     88         if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY();
     89         mContent = (CellLayout) findViewById(R.id.layout);
     90         mContent.setGridSize(mCellCountX, mCellCountY);
     91 
     92         resetLayout();
     93     }
     94 
     95     void resetLayout() {
     96         mContent.removeAllViewsInLayout();
     97 
     98         // Add the Apps button
     99         Context context = getContext();
    100         LayoutInflater inflater = LayoutInflater.from(context);
    101         BubbleTextView allAppsButton = (BubbleTextView)
    102                 inflater.inflate(R.layout.application, mContent, false);
    103         allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
    104                 context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
    105         // allAppsButton.setText(context.getString(R.string.all_apps_button_label));
    106         allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
    107         allAppsButton.setOnTouchListener(new View.OnTouchListener() {
    108             @Override
    109             public boolean onTouch(View v, MotionEvent event) {
    110                 if (mLauncher != null &&
    111                     (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
    112                     mLauncher.onTouchDownAllAppsButton(v);
    113                 }
    114                 return false;
    115             }
    116         });
    117 
    118         allAppsButton.setOnClickListener(new View.OnClickListener() {
    119             @Override
    120             public void onClick(android.view.View v) {
    121                 if (mLauncher != null) {
    122                     mLauncher.onClickAllAppsButton(v);
    123                 }
    124             }
    125         });
    126 
    127         // Note: We do this to ensure that the hotseat is always laid out in the orientation of
    128         // the hotseat in order regardless of which orientation they were added
    129         int x = getCellXFromOrder(sAllAppsButtonRank);
    130         int y = getCellYFromOrder(sAllAppsButtonRank);
    131         mContent.addViewToCellLayout(allAppsButton, -1, 0, new CellLayout.LayoutParams(x,y,1,1),
    132                 true);
    133     }
    134 }
    135