Home | History | Annotate | Download | only in launcher3
      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.launcher3;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.res.Configuration;
     22 import android.content.res.Resources;
     23 import android.graphics.Rect;
     24 import android.graphics.drawable.Drawable;
     25 import android.util.AttributeSet;
     26 import android.util.Log;
     27 import android.view.LayoutInflater;
     28 import android.view.MotionEvent;
     29 import android.view.View;
     30 import android.widget.FrameLayout;
     31 import android.widget.TextView;
     32 
     33 import java.util.ArrayList;
     34 
     35 public class Hotseat extends FrameLayout {
     36     private static final String TAG = "Hotseat";
     37 
     38     private CellLayout mContent;
     39 
     40     private Launcher mLauncher;
     41 
     42     private int mAllAppsButtonRank;
     43 
     44     private boolean mTransposeLayoutWithOrientation;
     45     private boolean mIsLandscape;
     46 
     47     public Hotseat(Context context) {
     48         this(context, null);
     49     }
     50 
     51     public Hotseat(Context context, AttributeSet attrs) {
     52         this(context, attrs, 0);
     53     }
     54 
     55     public Hotseat(Context context, AttributeSet attrs, int defStyle) {
     56         super(context, attrs, defStyle);
     57 
     58         Resources r = context.getResources();
     59         mTransposeLayoutWithOrientation =
     60                 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
     61         mIsLandscape = context.getResources().getConfiguration().orientation ==
     62             Configuration.ORIENTATION_LANDSCAPE;
     63     }
     64 
     65     public void setup(Launcher launcher) {
     66         mLauncher = launcher;
     67         setOnKeyListener(new HotseatIconKeyEventListener());
     68     }
     69 
     70     CellLayout getLayout() {
     71         return mContent;
     72     }
     73 
     74     /**
     75      * Registers the specified listener on the cell layout of the hotseat.
     76      */
     77     @Override
     78     public void setOnLongClickListener(OnLongClickListener l) {
     79         mContent.setOnLongClickListener(l);
     80     }
     81 
     82     private boolean hasVerticalHotseat() {
     83         return (mIsLandscape && mTransposeLayoutWithOrientation);
     84     }
     85 
     86     /* Get the orientation invariant order of the item in the hotseat for persistence. */
     87     int getOrderInHotseat(int x, int y) {
     88         return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
     89     }
     90     /* Get the orientation specific coordinates given an invariant order in the hotseat. */
     91     int getCellXFromOrder(int rank) {
     92         return hasVerticalHotseat() ? 0 : rank;
     93     }
     94     int getCellYFromOrder(int rank) {
     95         return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
     96     }
     97     public boolean isAllAppsButtonRank(int rank) {
     98         if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
     99             return false;
    100         } else {
    101             return rank == mAllAppsButtonRank;
    102         }
    103     }
    104 
    105     /** This returns the coordinates of an app in a given cell, relative to the DragLayer */
    106     Rect getCellCoordinates(int cellX, int cellY) {
    107         Rect coords = new Rect();
    108         mContent.cellToRect(cellX, cellY, 1, 1, coords);
    109         int[] hotseatInParent = new int[2];
    110         Utilities.getDescendantCoordRelativeToParent(this, mLauncher.getDragLayer(),
    111                 hotseatInParent, false);
    112         coords.offset(hotseatInParent[0], hotseatInParent[1]);
    113 
    114         // Center the icon
    115         int cWidth = mContent.getShortcutsAndWidgets().getCellContentWidth();
    116         int cHeight = mContent.getShortcutsAndWidgets().getCellContentHeight();
    117         int cellPaddingX = (int) Math.max(0, ((coords.width() - cWidth) / 2f));
    118         int cellPaddingY = (int) Math.max(0, ((coords.height() - cHeight) / 2f));
    119         coords.offset(cellPaddingX, cellPaddingY);
    120 
    121         return coords;
    122     }
    123 
    124     @Override
    125     protected void onFinishInflate() {
    126         super.onFinishInflate();
    127         LauncherAppState app = LauncherAppState.getInstance();
    128         DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
    129 
    130         mAllAppsButtonRank = grid.hotseatAllAppsRank;
    131         mContent = (CellLayout) findViewById(R.id.layout);
    132         if (grid.isLandscape && !grid.isLargeTablet()) {
    133             mContent.setGridSize(1, (int) grid.numHotseatIcons);
    134         } else {
    135             mContent.setGridSize((int) grid.numHotseatIcons, 1);
    136         }
    137         mContent.setIsHotseat(true);
    138 
    139         resetLayout();
    140     }
    141 
    142     void resetLayout() {
    143         mContent.removeAllViewsInLayout();
    144 
    145         if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
    146             // Add the Apps button
    147             Context context = getContext();
    148 
    149             LayoutInflater inflater = LayoutInflater.from(context);
    150             TextView allAppsButton = (TextView)
    151                     inflater.inflate(R.layout.all_apps_button, mContent, false);
    152             Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
    153             Utilities.resizeIconDrawable(d);
    154             allAppsButton.setCompoundDrawables(null, d, null, null);
    155 
    156             allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
    157             if (mLauncher != null) {
    158                 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
    159             }
    160             allAppsButton.setOnClickListener(new View.OnClickListener() {
    161                 @Override
    162                 public void onClick(android.view.View v) {
    163                     if (mLauncher != null) {
    164                         mLauncher.onClickAllAppsButton(v);
    165                     }
    166                 }
    167             });
    168 
    169             // Note: We do this to ensure that the hotseat is always laid out in the orientation of
    170             // the hotseat in order regardless of which orientation they were added
    171             int x = getCellXFromOrder(mAllAppsButtonRank);
    172             int y = getCellYFromOrder(mAllAppsButtonRank);
    173             CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
    174             lp.canReorder = false;
    175             mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
    176         }
    177     }
    178 
    179     @Override
    180     public boolean onInterceptTouchEvent(MotionEvent ev) {
    181         // We don't want any clicks to go through to the hotseat unless the workspace is in
    182         // the normal state.
    183         if (mLauncher.getWorkspace().isSmall()) {
    184             return true;
    185         }
    186         return false;
    187     }
    188 
    189     void addAllAppsFolder(IconCache iconCache,
    190             ArrayList<AppInfo> allApps, ArrayList<ComponentName> onWorkspace,
    191             Launcher launcher, Workspace workspace) {
    192         if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
    193             FolderInfo fi = new FolderInfo();
    194 
    195             fi.cellX = getCellXFromOrder(mAllAppsButtonRank);
    196             fi.cellY = getCellYFromOrder(mAllAppsButtonRank);
    197             fi.spanX = 1;
    198             fi.spanY = 1;
    199             fi.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT;
    200             fi.screenId = mAllAppsButtonRank;
    201             fi.itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
    202             fi.title = "More Apps";
    203             LauncherModel.addItemToDatabase(launcher, fi, fi.container, fi.screenId, fi.cellX,
    204                     fi.cellY, false);
    205             FolderIcon folder = FolderIcon.fromXml(R.layout.folder_icon, launcher,
    206                     getLayout(), fi, iconCache);
    207             workspace.addInScreen(folder, fi.container, fi.screenId, fi.cellX, fi.cellY,
    208                     fi.spanX, fi.spanY);
    209 
    210             for (AppInfo info: allApps) {
    211                 ComponentName cn = info.intent.getComponent();
    212                 if (!onWorkspace.contains(cn)) {
    213                     Log.d(TAG, "Adding to 'more apps': " + info.intent);
    214                     ShortcutInfo si = info.makeShortcut();
    215                     fi.add(si);
    216                 }
    217             }
    218         }
    219     }
    220 
    221     void addAppsToAllAppsFolder(ArrayList<AppInfo> apps) {
    222         if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
    223             View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
    224             FolderIcon fi = null;
    225 
    226             if (v instanceof FolderIcon) {
    227                 fi = (FolderIcon) v;
    228             } else {
    229                 return;
    230             }
    231 
    232             FolderInfo info = fi.getFolderInfo();
    233             for (AppInfo a: apps) {
    234                 ShortcutInfo si = a.makeShortcut();
    235                 info.add(si);
    236             }
    237         }
    238     }
    239 }
    240