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 | |
| 40 | private Launcher mLauncher; |
| 41 | private CellLayout mContent; |
| 42 | |
| 43 | private int mCellCountX; |
| 44 | private int mCellCountY; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 45 | private int mAllAppsButtonRank; |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 46 | |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 47 | private boolean mTransposeLayoutWithOrientation; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 48 | private boolean mIsLandscape; |
| 49 | |
| 50 | public Hotseat(Context context) { |
| 51 | this(context, null); |
| 52 | } |
| 53 | |
| 54 | public Hotseat(Context context, AttributeSet attrs) { |
| 55 | this(context, attrs, 0); |
| 56 | } |
| 57 | |
| 58 | public Hotseat(Context context, AttributeSet attrs, int defStyle) { |
| 59 | super(context, attrs, defStyle); |
| 60 | |
| 61 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 62 | R.styleable.Hotseat, defStyle, 0); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 63 | Resources r = context.getResources(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 64 | mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1); |
| 65 | mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 66 | mAllAppsButtonRank = r.getInteger(R.integer.hotseat_all_apps_index); |
| 67 | mTransposeLayoutWithOrientation = |
| 68 | r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 69 | mIsLandscape = context.getResources().getConfiguration().orientation == |
| 70 | Configuration.ORIENTATION_LANDSCAPE; |
| 71 | } |
| 72 | |
| 73 | public void setup(Launcher launcher) { |
| 74 | mLauncher = launcher; |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 75 | setOnKeyListener(new HotseatIconKeyEventListener()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | CellLayout getLayout() { |
| 79 | return mContent; |
| 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) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 98 | return false; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 99 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 100 | |
| 101 | @Override |
| 102 | protected void onFinishInflate() { |
| 103 | super.onFinishInflate(); |
| 104 | if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX(); |
| 105 | if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY(); |
| 106 | mContent = (CellLayout) findViewById(R.id.layout); |
| 107 | mContent.setGridSize(mCellCountX, mCellCountY); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 108 | mContent.setIsHotseat(true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 109 | |
| 110 | resetLayout(); |
| 111 | } |
| 112 | |
| 113 | void resetLayout() { |
| 114 | mContent.removeAllViewsInLayout(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 115 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 116 | |
| 117 | void addAllAppsFolder(IconCache iconCache, ArrayList<ApplicationInfo> allApps, |
| 118 | ArrayList<ComponentName> onWorkspace, Launcher launcher) { |
| 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; |
| 128 | fi.title = "All 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); |
| 133 | |
| 134 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(fi.cellX,fi.cellY,1,1); |
| 135 | mContent.addViewToCellLayout(folder, -1, 0, lp, true); |
| 136 | |
| 137 | for (ApplicationInfo info: allApps) { |
| 138 | ComponentName cn = info.intent.getComponent(); |
| 139 | if (!onWorkspace.contains(cn)) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame^] | 140 | Log.d(TAG, "Adding to all apps: " + info.intent); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 141 | ShortcutInfo si = info.makeShortcut(); |
| 142 | fi.add(si); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void addAppsToAllAppsFolder(ArrayList<ApplicationInfo> apps) { |
| 148 | View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank)); |
| 149 | FolderIcon fi = null; |
| 150 | |
| 151 | if (v instanceof FolderIcon) { |
| 152 | fi = (FolderIcon) v; |
| 153 | } else { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | FolderInfo info = fi.getFolderInfo(); |
| 158 | for (ApplicationInfo a: apps) { |
| 159 | ComponentName cn = a.intent.getComponent(); |
| 160 | ShortcutInfo si = a.makeShortcut(); |
| 161 | info.add(si); |
| 162 | } |
| 163 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 164 | } |