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