blob: fbbb09f5131a29164b269e10aedee610402f3c19 [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 Chungc58497e2013-09-03 17:48:37 -070023import android.graphics.Bitmap;
24import 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;
28import android.view.MotionEvent;
Winson Chung3d503fb2011-07-13 17:25:49 -070029import android.view.View;
30import android.widget.FrameLayout;
31
Adam Cohene25af792013-06-06 23:08:25 -070032import java.util.ArrayList;
33
Winson Chung3d503fb2011-07-13 17:25:49 -070034public class Hotseat extends FrameLayout {
Winson Chungf30ad5f2011-08-08 10:55:42 -070035 private static final String TAG = "Hotseat";
Winson Chung3d503fb2011-07-13 17:25:49 -070036
Winson Chung3d503fb2011-07-13 17:25:49 -070037 private CellLayout mContent;
38
Winson Chungc58497e2013-09-03 17:48:37 -070039 private Launcher mLauncher;
40
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080041 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070042
Winson Chung0e721a42012-08-02 17:40:30 -070043 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070044 private boolean mIsLandscape;
45
46 public Hotseat(Context context) {
47 this(context, null);
48 }
49
50 public Hotseat(Context context, AttributeSet attrs) {
51 this(context, attrs, 0);
52 }
53
54 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
55 super(context, attrs, defStyle);
56
Winson Chung0e721a42012-08-02 17:40:30 -070057 Resources r = context.getResources();
Winson Chung0e721a42012-08-02 17:40:30 -070058 mTransposeLayoutWithOrientation =
59 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070060 mIsLandscape = context.getResources().getConfiguration().orientation ==
61 Configuration.ORIENTATION_LANDSCAPE;
62 }
63
64 public void setup(Launcher launcher) {
Winson Chungc58497e2013-09-03 17:48:37 -070065 mLauncher = launcher;
Adam Cohenac56cff2011-09-28 20:45:37 -070066 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070067 }
68
69 CellLayout getLayout() {
70 return mContent;
71 }
Winson Chung11a1a532013-09-13 11:14:45 -070072
73 /**
74 * Registers the specified listener on the cell layout of the hotseat.
75 */
76 @Override
77 public void setOnLongClickListener(OnLongClickListener l) {
78 mContent.setOnLongClickListener(l);
79 }
Winson Chung0e721a42012-08-02 17:40:30 -070080
81 private boolean hasVerticalHotseat() {
82 return (mIsLandscape && mTransposeLayoutWithOrientation);
83 }
Winson Chung3d503fb2011-07-13 17:25:49 -070084
85 /* Get the orientation invariant order of the item in the hotseat for persistence. */
86 int getOrderInHotseat(int x, int y) {
Winson Chung0e721a42012-08-02 17:40:30 -070087 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070088 }
89 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
90 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070091 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070092 }
93 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070094 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070095 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080096 public boolean isAllAppsButtonRank(int rank) {
Winson Chungc58497e2013-09-03 17:48:37 -070097 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
98 return false;
99 } else {
100 return rank == mAllAppsButtonRank;
101 }
Winson Chungf30ad5f2011-08-08 10:55:42 -0700102 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700103
104 @Override
105 protected void onFinishInflate() {
106 super.onFinishInflate();
Winson Chung5f8afe62013-08-12 16:19:28 -0700107 LauncherAppState app = LauncherAppState.getInstance();
108 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
109
Winson Chungc58497e2013-09-03 17:48:37 -0700110 mAllAppsButtonRank = grid.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700111 mContent = (CellLayout) findViewById(R.id.layout);
Winson Chung5f8afe62013-08-12 16:19:28 -0700112 if (grid.isLandscape && !grid.isLargeTablet()) {
113 mContent.setGridSize(1, (int) grid.numHotseatIcons);
114 } else {
115 mContent.setGridSize((int) grid.numHotseatIcons, 1);
116 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800117 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700118
119 resetLayout();
120 }
121
122 void resetLayout() {
123 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700124
125 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
126 // Add the Apps button
127 Context context = getContext();
128
129 Drawable rawIcon =
130 context.getResources().getDrawable(R.drawable.all_apps_button_icon);
131 Bitmap icon = Utilities.createIconBitmap(rawIcon, context);
132
133 LayoutInflater inflater = LayoutInflater.from(context);
134 BubbleTextView allAppsButton = (BubbleTextView)
135 inflater.inflate(R.layout.application, mContent, false);
136 allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
137 new FastBitmapDrawable(icon), null, null);
138 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
139 allAppsButton.setOnTouchListener(new View.OnTouchListener() {
140 @Override
141 public boolean onTouch(View v, MotionEvent event) {
142 if (mLauncher != null &&
143 (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
144 mLauncher.onTouchDownAllAppsButton(v);
145 }
146 return false;
147 }
148 });
149
150 allAppsButton.setOnClickListener(new View.OnClickListener() {
151 @Override
152 public void onClick(android.view.View v) {
153 if (mLauncher != null) {
154 mLauncher.onClickAllAppsButton(v);
155 }
156 }
157 });
158
159 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
160 // the hotseat in order regardless of which orientation they were added
161 int x = getCellXFromOrder(mAllAppsButtonRank);
162 int y = getCellYFromOrder(mAllAppsButtonRank);
163 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
164 lp.canReorder = false;
165 mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
166 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700167 }
Adam Cohene25af792013-06-06 23:08:25 -0700168
Winson Chung156ab5b2013-07-12 14:14:16 -0700169 void addAllAppsFolder(IconCache iconCache,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200170 ArrayList<AppInfo> allApps, ArrayList<ComponentName> onWorkspace,
Winson Chung156ab5b2013-07-12 14:14:16 -0700171 Launcher launcher, Workspace workspace) {
Winson Chungc58497e2013-09-03 17:48:37 -0700172 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
173 FolderInfo fi = new FolderInfo();
Adam Cohene25af792013-06-06 23:08:25 -0700174
Winson Chungc58497e2013-09-03 17:48:37 -0700175 fi.cellX = getCellXFromOrder(mAllAppsButtonRank);
176 fi.cellY = getCellYFromOrder(mAllAppsButtonRank);
177 fi.spanX = 1;
178 fi.spanY = 1;
179 fi.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT;
180 fi.screenId = mAllAppsButtonRank;
181 fi.itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
182 fi.title = "More Apps";
183 LauncherModel.addItemToDatabase(launcher, fi, fi.container, fi.screenId, fi.cellX,
184 fi.cellY, false);
185 FolderIcon folder = FolderIcon.fromXml(R.layout.folder_icon, launcher,
186 getLayout(), fi, iconCache);
187 workspace.addInScreen(folder, fi.container, fi.screenId, fi.cellX, fi.cellY,
188 fi.spanX, fi.spanY);
Adam Cohene25af792013-06-06 23:08:25 -0700189
Winson Chungc58497e2013-09-03 17:48:37 -0700190 for (AppInfo info: allApps) {
191 ComponentName cn = info.intent.getComponent();
192 if (!onWorkspace.contains(cn)) {
193 Log.d(TAG, "Adding to 'more apps': " + info.intent);
194 ShortcutInfo si = info.makeShortcut();
195 fi.add(si);
196 }
Adam Cohene25af792013-06-06 23:08:25 -0700197 }
198 }
199 }
200
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 void addAppsToAllAppsFolder(ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -0700202 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
203 View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
204 FolderIcon fi = null;
Adam Cohene25af792013-06-06 23:08:25 -0700205
Winson Chungc58497e2013-09-03 17:48:37 -0700206 if (v instanceof FolderIcon) {
207 fi = (FolderIcon) v;
208 } else {
209 return;
210 }
Adam Cohene25af792013-06-06 23:08:25 -0700211
Winson Chungc58497e2013-09-03 17:48:37 -0700212 FolderInfo info = fi.getFolderInfo();
213 for (AppInfo a: apps) {
214 ShortcutInfo si = a.makeShortcut();
215 info.add(si);
216 }
Adam Cohene25af792013-06-06 23:08:25 -0700217 }
218 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700219}