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