Hotseat.java revision 53d7ee4285842732517edcccbdcaa92dccb1e107
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 17package com.android.launcher3; 18 19import android.content.Context; 20import android.content.res.Configuration; 21import android.content.res.Resources; 22import android.graphics.drawable.Drawable; 23import android.os.Bundle; 24import android.util.AttributeSet; 25import android.view.LayoutInflater; 26import android.view.MotionEvent; 27import android.widget.FrameLayout; 28import android.widget.TextView; 29 30public class Hotseat extends FrameLayout 31 implements Stats.LaunchSourceProvider{ 32 33 private CellLayout mContent; 34 35 private Launcher mLauncher; 36 37 private int mAllAppsButtonRank; 38 39 private boolean mTransposeLayoutWithOrientation; 40 private boolean mIsLandscape; 41 42 public Hotseat(Context context) { 43 this(context, null); 44 } 45 46 public Hotseat(Context context, AttributeSet attrs) { 47 this(context, attrs, 0); 48 } 49 50 public Hotseat(Context context, AttributeSet attrs, int defStyle) { 51 super(context, attrs, defStyle); 52 53 Resources r = context.getResources(); 54 mTransposeLayoutWithOrientation = 55 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); 56 mIsLandscape = context.getResources().getConfiguration().orientation == 57 Configuration.ORIENTATION_LANDSCAPE; 58 mLauncher = (Launcher) context; 59 } 60 61 CellLayout getLayout() { 62 return mContent; 63 } 64 65 /** 66 * Returns whether there are other icons than the all apps button in the hotseat. 67 */ 68 public boolean hasIcons() { 69 return mContent.getShortcutsAndWidgets().getChildCount() > 1; 70 } 71 72 /** 73 * Registers the specified listener on the cell layout of the hotseat. 74 */ 75 @Override 76 public void setOnLongClickListener(OnLongClickListener l) { 77 mContent.setOnLongClickListener(l); 78 } 79 80 private boolean hasVerticalHotseat() { 81 return (mIsLandscape && mTransposeLayoutWithOrientation); 82 } 83 84 /* Get the orientation invariant order of the item in the hotseat for persistence. */ 85 int getOrderInHotseat(int x, int y) { 86 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x; 87 } 88 89 /* Get the orientation specific coordinates given an invariant order in the hotseat. */ 90 int getCellXFromOrder(int rank) { 91 return hasVerticalHotseat() ? 0 : rank; 92 } 93 94 int getCellYFromOrder(int rank) { 95 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0; 96 } 97 98 public int getAllAppsButtonRank() { 99 return mAllAppsButtonRank; 100 } 101 102 public boolean isAllAppsButtonRank(int rank) { 103 return rank == mAllAppsButtonRank; 104 } 105 106 @Override 107 protected void onFinishInflate() { 108 super.onFinishInflate(); 109 DeviceProfile grid = mLauncher.getDeviceProfile(); 110 111 mAllAppsButtonRank = grid.inv.hotseatAllAppsRank; 112 mContent = (CellLayout) findViewById(R.id.layout); 113 if (grid.isLandscape && !grid.isLargeTablet) { 114 mContent.setGridSize(1, (int) grid.inv.numHotseatIcons); 115 } else { 116 mContent.setGridSize((int) grid.inv.numHotseatIcons, 1); 117 } 118 mContent.setIsHotseat(true); 119 120 resetLayout(); 121 } 122 123 void resetLayout() { 124 mContent.removeAllViewsInLayout(); 125 126 // Add the Apps button 127 Context context = getContext(); 128 129 LayoutInflater inflater = LayoutInflater.from(context); 130 TextView allAppsButton = (TextView) 131 inflater.inflate(R.layout.all_apps_button, mContent, false); 132 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon); 133 134 mLauncher.resizeIconDrawable(d); 135 allAppsButton.setCompoundDrawables(null, d, null, null); 136 137 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); 138 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener()); 139 if (mLauncher != null) { 140 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener()); 141 mLauncher.setAllAppsButton(allAppsButton); 142 allAppsButton.setOnClickListener(mLauncher); 143 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler); 144 } 145 146 // Note: We do this to ensure that the hotseat is always laid out in the orientation of 147 // the hotseat in order regardless of which orientation they were added 148 int x = getCellXFromOrder(mAllAppsButtonRank); 149 int y = getCellYFromOrder(mAllAppsButtonRank); 150 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1); 151 lp.canReorder = false; 152 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true); 153 } 154 155 @Override 156 public boolean onInterceptTouchEvent(MotionEvent ev) { 157 // We don't want any clicks to go through to the hotseat unless the workspace is in 158 // the normal state. 159 if (mLauncher.getWorkspace().workspaceInModalState()) { 160 return true; 161 } 162 return false; 163 } 164 165 @Override 166 public void fillInLaunchSourceData(Bundle sourceData) { 167 sourceData.putString(Stats.SOURCE_EXTRA_CONTAINER, Stats.CONTAINER_HOTSEAT); 168 } 169} 170