Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Configuration; |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 22 | import android.graphics.Rect; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.widget.FrameLayout; |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 29 | import android.widget.TextView; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 30 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 33 | public class Hotseat extends FrameLayout { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 34 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 35 | private CellLayout mContent; |
| 36 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 37 | private Launcher mLauncher; |
| 38 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 39 | private int mAllAppsButtonRank; |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 40 | |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 41 | private boolean mTransposeLayoutWithOrientation; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 42 | private boolean mIsLandscape; |
| 43 | |
| 44 | public Hotseat(Context context) { |
| 45 | this(context, null); |
| 46 | } |
| 47 | |
| 48 | public Hotseat(Context context, AttributeSet attrs) { |
| 49 | this(context, attrs, 0); |
| 50 | } |
| 51 | |
| 52 | public Hotseat(Context context, AttributeSet attrs, int defStyle) { |
| 53 | super(context, attrs, defStyle); |
| 54 | |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 55 | Resources r = context.getResources(); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 56 | mTransposeLayoutWithOrientation = |
| 57 | r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 58 | mIsLandscape = context.getResources().getConfiguration().orientation == |
| 59 | Configuration.ORIENTATION_LANDSCAPE; |
| 60 | } |
| 61 | |
| 62 | public void setup(Launcher launcher) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 63 | mLauncher = launcher; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | CellLayout getLayout() { |
| 67 | return mContent; |
| 68 | } |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Registers the specified listener on the cell layout of the hotseat. |
| 72 | */ |
| 73 | @Override |
| 74 | public void setOnLongClickListener(OnLongClickListener l) { |
| 75 | mContent.setOnLongClickListener(l); |
| 76 | } |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 77 | |
| 78 | private boolean hasVerticalHotseat() { |
| 79 | return (mIsLandscape && mTransposeLayoutWithOrientation); |
| 80 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 81 | |
| 82 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 83 | int getOrderInHotseat(int x, int y) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 84 | return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 85 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 86 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 87 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 88 | int getCellXFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 89 | return hasVerticalHotseat() ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 90 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 91 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 92 | int getCellYFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 93 | return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 94 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 95 | |
| 96 | public int getAllAppsButtonRank() { |
| 97 | if (LauncherAppState.isDisableAllApps()) { |
| 98 | return -1; |
| 99 | } else { |
| 100 | return mAllAppsButtonRank; |
| 101 | } |
| 102 | } |
| 103 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 104 | public boolean isAllAppsButtonRank(int rank) { |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 105 | if (LauncherAppState.isDisableAllApps()) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } else { |
| 108 | return rank == mAllAppsButtonRank; |
| 109 | } |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 110 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 111 | |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 112 | /** This returns the coordinates of an app in a given cell, relative to the DragLayer */ |
| 113 | Rect getCellCoordinates(int cellX, int cellY) { |
| 114 | Rect coords = new Rect(); |
| 115 | mContent.cellToRect(cellX, cellY, 1, 1, coords); |
| 116 | int[] hotseatInParent = new int[2]; |
| 117 | Utilities.getDescendantCoordRelativeToParent(this, mLauncher.getDragLayer(), |
| 118 | hotseatInParent, false); |
| 119 | coords.offset(hotseatInParent[0], hotseatInParent[1]); |
| 120 | |
| 121 | // Center the icon |
| 122 | int cWidth = mContent.getShortcutsAndWidgets().getCellContentWidth(); |
| 123 | int cHeight = mContent.getShortcutsAndWidgets().getCellContentHeight(); |
| 124 | int cellPaddingX = (int) Math.max(0, ((coords.width() - cWidth) / 2f)); |
| 125 | int cellPaddingY = (int) Math.max(0, ((coords.height() - cHeight) / 2f)); |
| 126 | coords.offset(cellPaddingX, cellPaddingY); |
| 127 | |
| 128 | return coords; |
| 129 | } |
| 130 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 131 | @Override |
| 132 | protected void onFinishInflate() { |
| 133 | super.onFinishInflate(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 134 | LauncherAppState app = LauncherAppState.getInstance(); |
| 135 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 136 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 137 | mAllAppsButtonRank = grid.hotseatAllAppsRank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 138 | mContent = (CellLayout) findViewById(R.id.layout); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 139 | if (grid.isLandscape && !grid.isLargeTablet()) { |
| 140 | mContent.setGridSize(1, (int) grid.numHotseatIcons); |
| 141 | } else { |
| 142 | mContent.setGridSize((int) grid.numHotseatIcons, 1); |
| 143 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 144 | mContent.setIsHotseat(true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 145 | |
| 146 | resetLayout(); |
| 147 | } |
| 148 | |
| 149 | void resetLayout() { |
| 150 | mContent.removeAllViewsInLayout(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 151 | |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 152 | if (!LauncherAppState.isDisableAllApps()) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 153 | // Add the Apps button |
| 154 | Context context = getContext(); |
| 155 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 156 | LayoutInflater inflater = LayoutInflater.from(context); |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 157 | TextView allAppsButton = (TextView) |
| 158 | inflater.inflate(R.layout.all_apps_button, mContent, false); |
| 159 | Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon); |
Adam Cohen | 71b0473 | 2014-06-11 10:36:14 -0700 | [diff] [blame] | 160 | |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 161 | Utilities.resizeIconDrawable(d); |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 162 | allAppsButton.setCompoundDrawables(null, d, null, null); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 163 | |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 164 | allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 165 | allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener()); |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 166 | if (mLauncher != null) { |
| 167 | allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener()); |
Anjali Koppal | 5ad4484 | 2014-03-10 20:34:39 -0700 | [diff] [blame] | 168 | mLauncher.setAllAppsButton(allAppsButton); |
| 169 | allAppsButton.setOnClickListener(mLauncher); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 170 | allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler); |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 171 | } |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 172 | |
| 173 | // Note: We do this to ensure that the hotseat is always laid out in the orientation of |
| 174 | // the hotseat in order regardless of which orientation they were added |
| 175 | int x = getCellXFromOrder(mAllAppsButtonRank); |
| 176 | int y = getCellYFromOrder(mAllAppsButtonRank); |
| 177 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1); |
| 178 | lp.canReorder = false; |
Sunny Goyal | e7de3b2 | 2014-07-18 09:35:28 -0700 | [diff] [blame] | 179 | mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 180 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 181 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 182 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 185 | // We don't want any clicks to go through to the hotseat unless the workspace is in |
| 186 | // the normal state. |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 187 | if (mLauncher.getWorkspace().workspaceInModalState()) { |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 193 | void addAppsToAllAppsFolder(ArrayList<AppInfo> apps) { |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 194 | if (LauncherAppState.isDisableAllApps()) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 195 | View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank)); |
| 196 | FolderIcon fi = null; |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 197 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 198 | if (v instanceof FolderIcon) { |
| 199 | fi = (FolderIcon) v; |
| 200 | } else { |
| 201 | return; |
| 202 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 203 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 204 | FolderInfo info = fi.getFolderInfo(); |
| 205 | for (AppInfo a: apps) { |
| 206 | ShortcutInfo si = a.makeShortcut(); |
| 207 | info.add(si); |
| 208 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 211 | } |