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; |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.LayoutInflater; |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 27 | import android.util.Log; |
Michael Jurka | 5130e40 | 2011-10-13 04:55:35 -0700 | [diff] [blame] | 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; |
| 31 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 32 | import com.android.launcher3.R; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 33 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 36 | public class Hotseat extends FrameLayout { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 37 | @SuppressWarnings("unused") |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 38 | private static final String TAG = "Hotseat"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 39 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 40 | private CellLayout mContent; |
| 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 | |
| 58 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 59 | R.styleable.Hotseat, defStyle, 0); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 60 | Resources r = context.getResources(); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 61 | mTransposeLayoutWithOrientation = |
| 62 | r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 63 | mIsLandscape = context.getResources().getConfiguration().orientation == |
| 64 | Configuration.ORIENTATION_LANDSCAPE; |
| 65 | } |
| 66 | |
| 67 | public void setup(Launcher launcher) { |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 68 | setOnKeyListener(new HotseatIconKeyEventListener()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | CellLayout getLayout() { |
| 72 | return mContent; |
| 73 | } |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 74 | |
| 75 | private boolean hasVerticalHotseat() { |
| 76 | return (mIsLandscape && mTransposeLayoutWithOrientation); |
| 77 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 78 | |
| 79 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 80 | int getOrderInHotseat(int x, int y) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 81 | return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 82 | } |
| 83 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 84 | int getCellXFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 85 | return hasVerticalHotseat() ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 86 | } |
| 87 | int getCellYFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 88 | return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 89 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 90 | public boolean isAllAppsButtonRank(int rank) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 91 | return false; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 92 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | protected void onFinishInflate() { |
| 96 | super.onFinishInflate(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame^] | 97 | LauncherAppState app = LauncherAppState.getInstance(); |
| 98 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 99 | |
| 100 | mAllAppsButtonRank = (int) (grid.numHotseatIcons / 2); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 101 | mContent = (CellLayout) findViewById(R.id.layout); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame^] | 102 | if (grid.isLandscape && !grid.isLargeTablet()) { |
| 103 | mContent.setGridSize(1, (int) grid.numHotseatIcons); |
| 104 | } else { |
| 105 | mContent.setGridSize((int) grid.numHotseatIcons, 1); |
| 106 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 107 | mContent.setIsHotseat(true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 108 | |
| 109 | resetLayout(); |
| 110 | } |
| 111 | |
| 112 | void resetLayout() { |
| 113 | mContent.removeAllViewsInLayout(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 114 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 115 | |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 116 | void addAllAppsFolder(IconCache iconCache, |
| 117 | ArrayList<ApplicationInfo> allApps, ArrayList<ComponentName> onWorkspace, |
| 118 | Launcher launcher, Workspace workspace) { |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 119 | FolderInfo fi = new FolderInfo(); |
| 120 | |
| 121 | fi.cellX = getCellXFromOrder(mAllAppsButtonRank); |
| 122 | fi.cellY = getCellYFromOrder(mAllAppsButtonRank); |
| 123 | fi.spanX = 1; |
| 124 | fi.spanY = 1; |
| 125 | fi.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 126 | fi.screenId = mAllAppsButtonRank; |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 127 | fi.itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER; |
Daniel Sandler | dd3204b | 2013-08-15 15:47:41 -0700 | [diff] [blame] | 128 | fi.title = "More Apps"; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 129 | LauncherModel.addItemToDatabase(launcher, fi, fi.container, fi.screenId, fi.cellX, |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 130 | fi.cellY, false); |
| 131 | FolderIcon folder = FolderIcon.fromXml(R.layout.folder_icon, launcher, |
| 132 | getLayout(), fi, iconCache); |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 133 | workspace.addInScreen(folder, fi.container, fi.screenId, fi.cellX, fi.cellY, |
| 134 | fi.spanX, fi.spanY); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 135 | |
| 136 | for (ApplicationInfo info: allApps) { |
| 137 | ComponentName cn = info.intent.getComponent(); |
| 138 | if (!onWorkspace.contains(cn)) { |
Daniel Sandler | dd3204b | 2013-08-15 15:47:41 -0700 | [diff] [blame] | 139 | Log.d(TAG, "Adding to 'more apps': " + info.intent); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 140 | ShortcutInfo si = info.makeShortcut(); |
| 141 | fi.add(si); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void addAppsToAllAppsFolder(ArrayList<ApplicationInfo> apps) { |
| 147 | View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank)); |
| 148 | FolderIcon fi = null; |
| 149 | |
| 150 | if (v instanceof FolderIcon) { |
| 151 | fi = (FolderIcon) v; |
| 152 | } else { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | FolderInfo info = fi.getFolderInfo(); |
| 157 | for (ApplicationInfo a: apps) { |
| 158 | ComponentName cn = a.intent.getComponent(); |
| 159 | ShortcutInfo si = a.makeShortcut(); |
| 160 | info.add(si); |
| 161 | } |
| 162 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 163 | } |