blob: bd6c21ab318c8db9de693e1322747dc92fd8b048 [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
19import android.content.Context;
20import android.content.res.Configuration;
Winson Chung0e721a42012-08-02 17:40:30 -070021import android.content.res.Resources;
Winson Chung3a6e7f32013-10-09 15:50:52 -070022import android.graphics.Rect;
Winson Chungc58497e2013-09-03 17:48:37 -070023import android.graphics.drawable.Drawable;
Winson Chung3d503fb2011-07-13 17:25:49 -070024import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070025import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070026import android.view.MotionEvent;
Winson Chung3d503fb2011-07-13 17:25:49 -070027import android.view.View;
28import android.widget.FrameLayout;
Adam Cohen61f560d2013-09-30 15:58:20 -070029import android.widget.TextView;
Winson Chung3d503fb2011-07-13 17:25:49 -070030
Adam Cohene25af792013-06-06 23:08:25 -070031import java.util.ArrayList;
32
Winson Chung3d503fb2011-07-13 17:25:49 -070033public class Hotseat extends FrameLayout {
Winson Chung3d503fb2011-07-13 17:25:49 -070034
Winson Chung3d503fb2011-07-13 17:25:49 -070035 private CellLayout mContent;
36
Winson Chungc58497e2013-09-03 17:48:37 -070037 private Launcher mLauncher;
38
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080039 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070040
Winson Chung0e721a42012-08-02 17:40:30 -070041 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070042 private boolean mIsLandscape;
43
44 public Hotseat(Context context) {
45 this(context, null);
46 }
47
48 public Hotseat(Context context, AttributeSet attrs) {
49 this(context, attrs, 0);
50 }
51
52 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
53 super(context, attrs, defStyle);
54
Winson Chung0e721a42012-08-02 17:40:30 -070055 Resources r = context.getResources();
Winson Chung0e721a42012-08-02 17:40:30 -070056 mTransposeLayoutWithOrientation =
57 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070058 mIsLandscape = context.getResources().getConfiguration().orientation ==
59 Configuration.ORIENTATION_LANDSCAPE;
60 }
61
62 public void setup(Launcher launcher) {
Winson Chungc58497e2013-09-03 17:48:37 -070063 mLauncher = launcher;
Winson Chung3d503fb2011-07-13 17:25:49 -070064 }
65
66 CellLayout getLayout() {
67 return mContent;
68 }
Winson Chung11a1a532013-09-13 11:14:45 -070069
70 /**
71 * Registers the specified listener on the cell layout of the hotseat.
72 */
73 @Override
74 public void setOnLongClickListener(OnLongClickListener l) {
75 mContent.setOnLongClickListener(l);
76 }
Winson Chung0e721a42012-08-02 17:40:30 -070077
78 private boolean hasVerticalHotseat() {
79 return (mIsLandscape && mTransposeLayoutWithOrientation);
80 }
Winson Chung3d503fb2011-07-13 17:25:49 -070081
82 /* Get the orientation invariant order of the item in the hotseat for persistence. */
83 int getOrderInHotseat(int x, int y) {
Winson Chung0e721a42012-08-02 17:40:30 -070084 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070085 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080086
Winson Chung3d503fb2011-07-13 17:25:49 -070087 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
88 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070089 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070090 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080091
Winson Chung3d503fb2011-07-13 17:25:49 -070092 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070093 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070094 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080095
96 public int getAllAppsButtonRank() {
97 if (LauncherAppState.isDisableAllApps()) {
98 return -1;
99 } else {
100 return mAllAppsButtonRank;
101 }
102 }
103
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800104 public boolean isAllAppsButtonRank(int rank) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800105 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700106 return false;
107 } else {
108 return rank == mAllAppsButtonRank;
109 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700110 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700111
Winson Chung3a6e7f32013-10-09 15:50:52 -0700112 /** This returns the coordinates of an app in a given cell, relative to the DragLayer */
113 Rect getCellCoordinates(int cellX, int cellY) {
114 Rect coords = new Rect();
115 mContent.cellToRect(cellX, cellY, 1, 1, coords);
116 int[] hotseatInParent = new int[2];
117 Utilities.getDescendantCoordRelativeToParent(this, mLauncher.getDragLayer(),
118 hotseatInParent, false);
119 coords.offset(hotseatInParent[0], hotseatInParent[1]);
120
121 // Center the icon
122 int cWidth = mContent.getShortcutsAndWidgets().getCellContentWidth();
123 int cHeight = mContent.getShortcutsAndWidgets().getCellContentHeight();
124 int cellPaddingX = (int) Math.max(0, ((coords.width() - cWidth) / 2f));
125 int cellPaddingY = (int) Math.max(0, ((coords.height() - cHeight) / 2f));
126 coords.offset(cellPaddingX, cellPaddingY);
127
128 return coords;
129 }
130
Winson Chung3d503fb2011-07-13 17:25:49 -0700131 @Override
132 protected void onFinishInflate() {
133 super.onFinishInflate();
Winson Chung5f8afe62013-08-12 16:19:28 -0700134 LauncherAppState app = LauncherAppState.getInstance();
135 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
136
Winson Chungc58497e2013-09-03 17:48:37 -0700137 mAllAppsButtonRank = grid.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700138 mContent = (CellLayout) findViewById(R.id.layout);
Winson Chung5f8afe62013-08-12 16:19:28 -0700139 if (grid.isLandscape && !grid.isLargeTablet()) {
140 mContent.setGridSize(1, (int) grid.numHotseatIcons);
141 } else {
142 mContent.setGridSize((int) grid.numHotseatIcons, 1);
143 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800144 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700145
146 resetLayout();
147 }
148
149 void resetLayout() {
150 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700151
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800152 if (!LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700153 // Add the Apps button
154 Context context = getContext();
155
Winson Chungc58497e2013-09-03 17:48:37 -0700156 LayoutInflater inflater = LayoutInflater.from(context);
Adam Cohen61f560d2013-09-30 15:58:20 -0700157 TextView allAppsButton = (TextView)
158 inflater.inflate(R.layout.all_apps_button, mContent, false);
159 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700160
Winson Chung0dbd7342013-10-13 22:46:20 -0700161 Utilities.resizeIconDrawable(d);
Adam Cohen61f560d2013-09-30 15:58:20 -0700162 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700163
Adam Cohen61f560d2013-09-30 15:58:20 -0700164 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
Sunny Goyalb3726d92014-08-20 16:58:17 -0700165 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
Adam Cohen61f560d2013-09-30 15:58:20 -0700166 if (mLauncher != null) {
167 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
Anjali Koppal5ad44842014-03-10 20:34:39 -0700168 mLauncher.setAllAppsButton(allAppsButton);
169 allAppsButton.setOnClickListener(mLauncher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700170 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Adam Cohen61f560d2013-09-30 15:58:20 -0700171 }
Winson Chungc58497e2013-09-03 17:48:37 -0700172
173 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
174 // the hotseat in order regardless of which orientation they were added
175 int x = getCellXFromOrder(mAllAppsButtonRank);
176 int y = getCellYFromOrder(mAllAppsButtonRank);
177 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
178 lp.canReorder = false;
Sunny Goyale7de3b22014-07-18 09:35:28 -0700179 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chungc58497e2013-09-03 17:48:37 -0700180 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 }
Adam Cohene25af792013-06-06 23:08:25 -0700182
Adam Cohena5f4e482013-10-11 12:10:28 -0700183 @Override
184 public boolean onInterceptTouchEvent(MotionEvent ev) {
185 // We don't want any clicks to go through to the hotseat unless the workspace is in
186 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700187 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700188 return true;
189 }
190 return false;
191 }
192
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 void addAppsToAllAppsFolder(ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800194 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700195 View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
196 FolderIcon fi = null;
Adam Cohene25af792013-06-06 23:08:25 -0700197
Winson Chungc58497e2013-09-03 17:48:37 -0700198 if (v instanceof FolderIcon) {
199 fi = (FolderIcon) v;
200 } else {
201 return;
202 }
Adam Cohene25af792013-06-06 23:08:25 -0700203
Winson Chungc58497e2013-09-03 17:48:37 -0700204 FolderInfo info = fi.getFolderInfo();
205 for (AppInfo a: apps) {
206 ShortcutInfo si = a.makeShortcut();
207 info.add(si);
208 }
Adam Cohene25af792013-06-06 23:08:25 -0700209 }
210 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700211}