blob: 4b2543328f95910f9e58b100c2ef14bdedde3add [file] [log] [blame]
Winson Chung3d503fb2011-07-13 17:25:49 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung3d503fb2011-07-13 17:25:49 -070018
Adam Cohene25af792013-06-06 23:08:25 -070019import android.content.ComponentName;
Winson Chung3d503fb2011-07-13 17:25:49 -070020import android.content.Context;
21import android.content.res.Configuration;
Winson Chung0e721a42012-08-02 17:40:30 -070022import android.content.res.Resources;
Winson Chung3a6e7f32013-10-09 15:50:52 -070023import android.graphics.Rect;
Winson Chungc58497e2013-09-03 17:48:37 -070024import android.graphics.drawable.Drawable;
Winson Chung3d503fb2011-07-13 17:25:49 -070025import android.util.AttributeSet;
Winson Chung64359a52013-07-08 17:17:08 -070026import android.util.Log;
Winson Chungc58497e2013-09-03 17:48:37 -070027import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070028import android.view.MotionEvent;
Winson Chung3d503fb2011-07-13 17:25:49 -070029import android.view.View;
30import android.widget.FrameLayout;
Adam Cohen61f560d2013-09-30 15:58:20 -070031import android.widget.TextView;
Winson Chung3d503fb2011-07-13 17:25:49 -070032
Adam Cohene25af792013-06-06 23:08:25 -070033import java.util.ArrayList;
34
Winson Chung3d503fb2011-07-13 17:25:49 -070035public class Hotseat extends FrameLayout {
Winson Chungf30ad5f2011-08-08 10:55:42 -070036 private static final String TAG = "Hotseat";
Winson Chung3d503fb2011-07-13 17:25:49 -070037
Winson Chung3d503fb2011-07-13 17:25:49 -070038 private CellLayout mContent;
39
Winson Chungc58497e2013-09-03 17:48:37 -070040 private Launcher mLauncher;
41
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080042 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070043
Winson Chung0e721a42012-08-02 17:40:30 -070044 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070045 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 Chung0e721a42012-08-02 17:40:30 -070058 Resources r = context.getResources();
Winson Chung0e721a42012-08-02 17:40:30 -070059 mTransposeLayoutWithOrientation =
60 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070061 mIsLandscape = context.getResources().getConfiguration().orientation ==
62 Configuration.ORIENTATION_LANDSCAPE;
63 }
64
65 public void setup(Launcher launcher) {
Winson Chungc58497e2013-09-03 17:48:37 -070066 mLauncher = launcher;
Adam Cohenac56cff2011-09-28 20:45:37 -070067 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070068 }
69
70 CellLayout getLayout() {
71 return mContent;
72 }
Winson Chung11a1a532013-09-13 11:14:45 -070073
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 Chung0e721a42012-08-02 17:40:30 -070081
82 private boolean hasVerticalHotseat() {
83 return (mIsLandscape && mTransposeLayoutWithOrientation);
84 }
Winson Chung3d503fb2011-07-13 17:25:49 -070085
86 /* Get the orientation invariant order of the item in the hotseat for persistence. */
87 int getOrderInHotseat(int x, int y) {
Winson Chung0e721a42012-08-02 17:40:30 -070088 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070089 }
90 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
91 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070092 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070093 }
94 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070095 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070096 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080097 public boolean isAllAppsButtonRank(int rank) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080098 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -070099 return false;
100 } else {
101 return rank == mAllAppsButtonRank;
102 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700103 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700104
Winson Chung3a6e7f32013-10-09 15:50:52 -0700105 /** This returns the coordinates of an app in a given cell, relative to the DragLayer */
106 Rect getCellCoordinates(int cellX, int cellY) {
107 Rect coords = new Rect();
108 mContent.cellToRect(cellX, cellY, 1, 1, coords);
109 int[] hotseatInParent = new int[2];
110 Utilities.getDescendantCoordRelativeToParent(this, mLauncher.getDragLayer(),
111 hotseatInParent, false);
112 coords.offset(hotseatInParent[0], hotseatInParent[1]);
113
114 // Center the icon
115 int cWidth = mContent.getShortcutsAndWidgets().getCellContentWidth();
116 int cHeight = mContent.getShortcutsAndWidgets().getCellContentHeight();
117 int cellPaddingX = (int) Math.max(0, ((coords.width() - cWidth) / 2f));
118 int cellPaddingY = (int) Math.max(0, ((coords.height() - cHeight) / 2f));
119 coords.offset(cellPaddingX, cellPaddingY);
120
121 return coords;
122 }
123
Winson Chung3d503fb2011-07-13 17:25:49 -0700124 @Override
125 protected void onFinishInflate() {
126 super.onFinishInflate();
Winson Chung5f8afe62013-08-12 16:19:28 -0700127 LauncherAppState app = LauncherAppState.getInstance();
128 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
129
Winson Chungc58497e2013-09-03 17:48:37 -0700130 mAllAppsButtonRank = grid.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700131 mContent = (CellLayout) findViewById(R.id.layout);
Winson Chung5f8afe62013-08-12 16:19:28 -0700132 if (grid.isLandscape && !grid.isLargeTablet()) {
133 mContent.setGridSize(1, (int) grid.numHotseatIcons);
134 } else {
135 mContent.setGridSize((int) grid.numHotseatIcons, 1);
136 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800137 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700138
139 resetLayout();
140 }
141
142 void resetLayout() {
143 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700144
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800145 if (!LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700146 // Add the Apps button
147 Context context = getContext();
148
Winson Chungc58497e2013-09-03 17:48:37 -0700149 LayoutInflater inflater = LayoutInflater.from(context);
Adam Cohen61f560d2013-09-30 15:58:20 -0700150 TextView allAppsButton = (TextView)
151 inflater.inflate(R.layout.all_apps_button, mContent, false);
152 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700153
154 if (Utilities.isLmp()) {
155 d = context.getResources().getDrawable(R.drawable.all_apps_button_icon_l);
156 }
157
Winson Chung0dbd7342013-10-13 22:46:20 -0700158 Utilities.resizeIconDrawable(d);
Adam Cohen61f560d2013-09-30 15:58:20 -0700159 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700160
Adam Cohen61f560d2013-09-30 15:58:20 -0700161 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
162 if (mLauncher != null) {
163 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
Anjali Koppal5ad44842014-03-10 20:34:39 -0700164 mLauncher.setAllAppsButton(allAppsButton);
165 allAppsButton.setOnClickListener(mLauncher);
Adam Cohen61f560d2013-09-30 15:58:20 -0700166 }
Winson Chungc58497e2013-09-03 17:48:37 -0700167
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 Chung3d503fb2011-07-13 17:25:49 -0700176 }
Adam Cohene25af792013-06-06 23:08:25 -0700177
Adam Cohena5f4e482013-10-11 12:10:28 -0700178 @Override
179 public boolean onInterceptTouchEvent(MotionEvent ev) {
180 // We don't want any clicks to go through to the hotseat unless the workspace is in
181 // the normal state.
182 if (mLauncher.getWorkspace().isSmall()) {
183 return true;
184 }
185 return false;
186 }
187
Winson Chung156ab5b2013-07-12 14:14:16 -0700188 void addAllAppsFolder(IconCache iconCache,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200189 ArrayList<AppInfo> allApps, ArrayList<ComponentName> onWorkspace,
Winson Chung156ab5b2013-07-12 14:14:16 -0700190 Launcher launcher, Workspace workspace) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800191 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700192 FolderInfo fi = new FolderInfo();
Adam Cohene25af792013-06-06 23:08:25 -0700193
Winson Chungc58497e2013-09-03 17:48:37 -0700194 fi.cellX = getCellXFromOrder(mAllAppsButtonRank);
195 fi.cellY = getCellYFromOrder(mAllAppsButtonRank);
196 fi.spanX = 1;
197 fi.spanY = 1;
198 fi.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT;
199 fi.screenId = mAllAppsButtonRank;
200 fi.itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
201 fi.title = "More Apps";
202 LauncherModel.addItemToDatabase(launcher, fi, fi.container, fi.screenId, fi.cellX,
203 fi.cellY, false);
204 FolderIcon folder = FolderIcon.fromXml(R.layout.folder_icon, launcher,
205 getLayout(), fi, iconCache);
206 workspace.addInScreen(folder, fi.container, fi.screenId, fi.cellX, fi.cellY,
207 fi.spanX, fi.spanY);
Adam Cohene25af792013-06-06 23:08:25 -0700208
Winson Chungc58497e2013-09-03 17:48:37 -0700209 for (AppInfo info: allApps) {
210 ComponentName cn = info.intent.getComponent();
211 if (!onWorkspace.contains(cn)) {
212 Log.d(TAG, "Adding to 'more apps': " + info.intent);
213 ShortcutInfo si = info.makeShortcut();
214 fi.add(si);
215 }
Adam Cohene25af792013-06-06 23:08:25 -0700216 }
217 }
218 }
219
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200220 void addAppsToAllAppsFolder(ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800221 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700222 View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
223 FolderIcon fi = null;
Adam Cohene25af792013-06-06 23:08:25 -0700224
Winson Chungc58497e2013-09-03 17:48:37 -0700225 if (v instanceof FolderIcon) {
226 fi = (FolderIcon) v;
227 } else {
228 return;
229 }
Adam Cohene25af792013-06-06 23:08:25 -0700230
Winson Chungc58497e2013-09-03 17:48:37 -0700231 FolderInfo info = fi.getFolderInfo();
232 for (AppInfo a: apps) {
233 ShortcutInfo si = a.makeShortcut();
234 info.add(si);
235 }
Adam Cohene25af792013-06-06 23:08:25 -0700236 }
237 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700238}