blob: 2d3adf64d98355998785e4240e61ff1affeeb99a [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 Chung3d503fb2011-07-13 17:25:49 -070023import android.util.AttributeSet;
Winson Chung64359a52013-07-08 17:17:08 -070024import android.util.Log;
Winson Chung3d503fb2011-07-13 17:25:49 -070025import android.view.View;
26import android.widget.FrameLayout;
27
Adam Cohene25af792013-06-06 23:08:25 -070028import java.util.ArrayList;
29
Winson Chung3d503fb2011-07-13 17:25:49 -070030public class Hotseat extends FrameLayout {
Winson Chungf30ad5f2011-08-08 10:55:42 -070031 private static final String TAG = "Hotseat";
Winson Chung3d503fb2011-07-13 17:25:49 -070032
Winson Chung3d503fb2011-07-13 17:25:49 -070033 private CellLayout mContent;
34
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080035 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070036
Winson Chung0e721a42012-08-02 17:40:30 -070037 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070038 private boolean mIsLandscape;
39
40 public Hotseat(Context context) {
41 this(context, null);
42 }
43
44 public Hotseat(Context context, AttributeSet attrs) {
45 this(context, attrs, 0);
46 }
47
48 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
50
Winson Chung0e721a42012-08-02 17:40:30 -070051 Resources r = context.getResources();
Winson Chung0e721a42012-08-02 17:40:30 -070052 mTransposeLayoutWithOrientation =
53 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070054 mIsLandscape = context.getResources().getConfiguration().orientation ==
55 Configuration.ORIENTATION_LANDSCAPE;
56 }
57
58 public void setup(Launcher launcher) {
Adam Cohenac56cff2011-09-28 20:45:37 -070059 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070060 }
61
62 CellLayout getLayout() {
63 return mContent;
64 }
Winson Chung0e721a42012-08-02 17:40:30 -070065
66 private boolean hasVerticalHotseat() {
67 return (mIsLandscape && mTransposeLayoutWithOrientation);
68 }
Winson Chung3d503fb2011-07-13 17:25:49 -070069
70 /* Get the orientation invariant order of the item in the hotseat for persistence. */
71 int getOrderInHotseat(int x, int y) {
Winson Chung0e721a42012-08-02 17:40:30 -070072 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070073 }
74 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
75 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070076 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070077 }
78 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070079 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070080 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080081 public boolean isAllAppsButtonRank(int rank) {
Adam Cohen947dc542013-06-06 22:43:33 -070082 return false;
Winson Chungf30ad5f2011-08-08 10:55:42 -070083 }
Winson Chung3d503fb2011-07-13 17:25:49 -070084
85 @Override
86 protected void onFinishInflate() {
87 super.onFinishInflate();
Winson Chung5f8afe62013-08-12 16:19:28 -070088 LauncherAppState app = LauncherAppState.getInstance();
89 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
90
91 mAllAppsButtonRank = (int) (grid.numHotseatIcons / 2);
Winson Chung3d503fb2011-07-13 17:25:49 -070092 mContent = (CellLayout) findViewById(R.id.layout);
Winson Chung5f8afe62013-08-12 16:19:28 -070093 if (grid.isLandscape && !grid.isLargeTablet()) {
94 mContent.setGridSize(1, (int) grid.numHotseatIcons);
95 } else {
96 mContent.setGridSize((int) grid.numHotseatIcons, 1);
97 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080098 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -070099
100 resetLayout();
101 }
102
103 void resetLayout() {
104 mContent.removeAllViewsInLayout();
Winson Chung3d503fb2011-07-13 17:25:49 -0700105 }
Adam Cohene25af792013-06-06 23:08:25 -0700106
Winson Chung156ab5b2013-07-12 14:14:16 -0700107 void addAllAppsFolder(IconCache iconCache,
108 ArrayList<ApplicationInfo> allApps, ArrayList<ComponentName> onWorkspace,
109 Launcher launcher, Workspace workspace) {
Adam Cohene25af792013-06-06 23:08:25 -0700110 FolderInfo fi = new FolderInfo();
111
112 fi.cellX = getCellXFromOrder(mAllAppsButtonRank);
113 fi.cellY = getCellYFromOrder(mAllAppsButtonRank);
114 fi.spanX = 1;
115 fi.spanY = 1;
116 fi.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT;
Adam Cohendcd297f2013-06-18 13:13:40 -0700117 fi.screenId = mAllAppsButtonRank;
Adam Cohene25af792013-06-06 23:08:25 -0700118 fi.itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
Daniel Sandlerdd3204b2013-08-15 15:47:41 -0700119 fi.title = "More Apps";
Adam Cohendcd297f2013-06-18 13:13:40 -0700120 LauncherModel.addItemToDatabase(launcher, fi, fi.container, fi.screenId, fi.cellX,
Adam Cohene25af792013-06-06 23:08:25 -0700121 fi.cellY, false);
122 FolderIcon folder = FolderIcon.fromXml(R.layout.folder_icon, launcher,
123 getLayout(), fi, iconCache);
Winson Chung156ab5b2013-07-12 14:14:16 -0700124 workspace.addInScreen(folder, fi.container, fi.screenId, fi.cellX, fi.cellY,
125 fi.spanX, fi.spanY);
Adam Cohene25af792013-06-06 23:08:25 -0700126
127 for (ApplicationInfo info: allApps) {
128 ComponentName cn = info.intent.getComponent();
129 if (!onWorkspace.contains(cn)) {
Daniel Sandlerdd3204b2013-08-15 15:47:41 -0700130 Log.d(TAG, "Adding to 'more apps': " + info.intent);
Adam Cohene25af792013-06-06 23:08:25 -0700131 ShortcutInfo si = info.makeShortcut();
132 fi.add(si);
133 }
134 }
135 }
136
137 void addAppsToAllAppsFolder(ArrayList<ApplicationInfo> apps) {
138 View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
139 FolderIcon fi = null;
140
141 if (v instanceof FolderIcon) {
142 fi = (FolderIcon) v;
143 } else {
144 return;
145 }
146
147 FolderInfo info = fi.getFolderInfo();
148 for (ApplicationInfo a: apps) {
Adam Cohene25af792013-06-06 23:08:25 -0700149 ShortcutInfo si = a.makeShortcut();
150 info.add(si);
151 }
152 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700153}