blob: 289b08b148fb08e3cc2f9f883831e4fa9e9385a7 [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 }
86 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
87 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070088 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070089 }
90 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070091 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070092 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080093 public boolean isAllAppsButtonRank(int rank) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080094 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -070095 return false;
96 } else {
97 return rank == mAllAppsButtonRank;
98 }
Winson Chungf30ad5f2011-08-08 10:55:42 -070099 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700100
Winson Chung3a6e7f32013-10-09 15:50:52 -0700101 /** This returns the coordinates of an app in a given cell, relative to the DragLayer */
102 Rect getCellCoordinates(int cellX, int cellY) {
103 Rect coords = new Rect();
104 mContent.cellToRect(cellX, cellY, 1, 1, coords);
105 int[] hotseatInParent = new int[2];
106 Utilities.getDescendantCoordRelativeToParent(this, mLauncher.getDragLayer(),
107 hotseatInParent, false);
108 coords.offset(hotseatInParent[0], hotseatInParent[1]);
109
110 // Center the icon
111 int cWidth = mContent.getShortcutsAndWidgets().getCellContentWidth();
112 int cHeight = mContent.getShortcutsAndWidgets().getCellContentHeight();
113 int cellPaddingX = (int) Math.max(0, ((coords.width() - cWidth) / 2f));
114 int cellPaddingY = (int) Math.max(0, ((coords.height() - cHeight) / 2f));
115 coords.offset(cellPaddingX, cellPaddingY);
116
117 return coords;
118 }
119
Winson Chung3d503fb2011-07-13 17:25:49 -0700120 @Override
121 protected void onFinishInflate() {
122 super.onFinishInflate();
Winson Chung5f8afe62013-08-12 16:19:28 -0700123 LauncherAppState app = LauncherAppState.getInstance();
124 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
125
Winson Chungc58497e2013-09-03 17:48:37 -0700126 mAllAppsButtonRank = grid.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700127 mContent = (CellLayout) findViewById(R.id.layout);
Winson Chung5f8afe62013-08-12 16:19:28 -0700128 if (grid.isLandscape && !grid.isLargeTablet()) {
129 mContent.setGridSize(1, (int) grid.numHotseatIcons);
130 } else {
131 mContent.setGridSize((int) grid.numHotseatIcons, 1);
132 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800133 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700134
135 resetLayout();
136 }
137
138 void resetLayout() {
139 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700140
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800141 if (!LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700142 // Add the Apps button
143 Context context = getContext();
144
Winson Chungc58497e2013-09-03 17:48:37 -0700145 LayoutInflater inflater = LayoutInflater.from(context);
Adam Cohen61f560d2013-09-30 15:58:20 -0700146 TextView allAppsButton = (TextView)
147 inflater.inflate(R.layout.all_apps_button, mContent, false);
148 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700149
Winson Chung0dbd7342013-10-13 22:46:20 -0700150 Utilities.resizeIconDrawable(d);
Adam Cohen61f560d2013-09-30 15:58:20 -0700151 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700152
Adam Cohen61f560d2013-09-30 15:58:20 -0700153 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
Sunny Goyalb3726d92014-08-20 16:58:17 -0700154 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
Adam Cohen61f560d2013-09-30 15:58:20 -0700155 if (mLauncher != null) {
156 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
Anjali Koppal5ad44842014-03-10 20:34:39 -0700157 mLauncher.setAllAppsButton(allAppsButton);
158 allAppsButton.setOnClickListener(mLauncher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700159 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Adam Cohen61f560d2013-09-30 15:58:20 -0700160 }
Winson Chungc58497e2013-09-03 17:48:37 -0700161
162 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
163 // the hotseat in order regardless of which orientation they were added
164 int x = getCellXFromOrder(mAllAppsButtonRank);
165 int y = getCellYFromOrder(mAllAppsButtonRank);
166 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
167 lp.canReorder = false;
Sunny Goyale7de3b22014-07-18 09:35:28 -0700168 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chungc58497e2013-09-03 17:48:37 -0700169 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 }
Adam Cohene25af792013-06-06 23:08:25 -0700171
Adam Cohena5f4e482013-10-11 12:10:28 -0700172 @Override
173 public boolean onInterceptTouchEvent(MotionEvent ev) {
174 // We don't want any clicks to go through to the hotseat unless the workspace is in
175 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700176 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700177 return true;
178 }
179 return false;
180 }
181
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200182 void addAppsToAllAppsFolder(ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800183 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -0700184 View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
185 FolderIcon fi = null;
Adam Cohene25af792013-06-06 23:08:25 -0700186
Winson Chungc58497e2013-09-03 17:48:37 -0700187 if (v instanceof FolderIcon) {
188 fi = (FolderIcon) v;
189 } else {
190 return;
191 }
Adam Cohene25af792013-06-06 23:08:25 -0700192
Winson Chungc58497e2013-09-03 17:48:37 -0700193 FolderInfo info = fi.getFolderInfo();
194 for (AppInfo a: apps) {
195 ShortcutInfo si = a.makeShortcut();
196 info.add(si);
197 }
Adam Cohene25af792013-06-06 23:08:25 -0700198 }
199 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700200}