blob: 054ef2fc1fc286cb05c742362e42255e89cd3b29 [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;
Adam Cohene25af792013-06-06 23:08:25 -070021import android.content.Intent;
Winson Chung3d503fb2011-07-13 17:25:49 -070022import android.content.res.Configuration;
Winson Chung0e721a42012-08-02 17:40:30 -070023import android.content.res.Resources;
Winson Chung3d503fb2011-07-13 17:25:49 -070024import android.content.res.TypedArray;
25import android.util.AttributeSet;
26import android.view.LayoutInflater;
Winson Chung64359a52013-07-08 17:17:08 -070027import android.util.Log;
Michael Jurka5130e402011-10-13 04:55:35 -070028import android.view.MotionEvent;
Winson Chung3d503fb2011-07-13 17:25:49 -070029import android.view.View;
30import android.widget.FrameLayout;
31
Daniel Sandler325dc232013-06-05 22:57:57 -040032import com.android.launcher3.R;
Winson Chung3d503fb2011-07-13 17:25:49 -070033
Adam Cohene25af792013-06-06 23:08:25 -070034import java.util.ArrayList;
35
Winson Chung3d503fb2011-07-13 17:25:49 -070036public class Hotseat extends FrameLayout {
Michael Jurka3a9fced2012-04-13 14:44:29 -070037 @SuppressWarnings("unused")
Winson Chungf30ad5f2011-08-08 10:55:42 -070038 private static final String TAG = "Hotseat";
Winson Chung3d503fb2011-07-13 17:25:49 -070039
40 private Launcher mLauncher;
41 private CellLayout mContent;
42
43 private int mCellCountX;
44 private int mCellCountY;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080045 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070046
Winson Chung0e721a42012-08-02 17:40:30 -070047 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070048 private boolean mIsLandscape;
49
50 public Hotseat(Context context) {
51 this(context, null);
52 }
53
54 public Hotseat(Context context, AttributeSet attrs) {
55 this(context, attrs, 0);
56 }
57
58 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
59 super(context, attrs, defStyle);
60
61 TypedArray a = context.obtainStyledAttributes(attrs,
62 R.styleable.Hotseat, defStyle, 0);
Winson Chung0e721a42012-08-02 17:40:30 -070063 Resources r = context.getResources();
Winson Chung3d503fb2011-07-13 17:25:49 -070064 mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
65 mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
Winson Chung0e721a42012-08-02 17:40:30 -070066 mAllAppsButtonRank = r.getInteger(R.integer.hotseat_all_apps_index);
67 mTransposeLayoutWithOrientation =
68 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070069 mIsLandscape = context.getResources().getConfiguration().orientation ==
70 Configuration.ORIENTATION_LANDSCAPE;
71 }
72
73 public void setup(Launcher launcher) {
74 mLauncher = launcher;
Adam Cohenac56cff2011-09-28 20:45:37 -070075 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070076 }
77
78 CellLayout getLayout() {
79 return mContent;
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) {
Adam Cohen947dc542013-06-06 22:43:33 -070098 return false;
Winson Chungf30ad5f2011-08-08 10:55:42 -070099 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700100
101 @Override
102 protected void onFinishInflate() {
103 super.onFinishInflate();
104 if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX();
105 if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY();
106 mContent = (CellLayout) findViewById(R.id.layout);
107 mContent.setGridSize(mCellCountX, mCellCountY);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800108 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700109
110 resetLayout();
111 }
112
113 void resetLayout() {
114 mContent.removeAllViewsInLayout();
Winson Chung3d503fb2011-07-13 17:25:49 -0700115 }
Adam Cohene25af792013-06-06 23:08:25 -0700116
117 void addAllAppsFolder(IconCache iconCache, ArrayList<ApplicationInfo> allApps,
118 ArrayList<ComponentName> onWorkspace, Launcher launcher) {
119 FolderInfo fi = new FolderInfo();
120
121 fi.cellX = getCellXFromOrder(mAllAppsButtonRank);
122 fi.cellY = getCellYFromOrder(mAllAppsButtonRank);
123 fi.spanX = 1;
124 fi.spanY = 1;
125 fi.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT;
Adam Cohendcd297f2013-06-18 13:13:40 -0700126 fi.screenId = mAllAppsButtonRank;
Adam Cohene25af792013-06-06 23:08:25 -0700127 fi.itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
128 fi.title = "All Apps";
Adam Cohendcd297f2013-06-18 13:13:40 -0700129 LauncherModel.addItemToDatabase(launcher, fi, fi.container, fi.screenId, fi.cellX,
Adam Cohene25af792013-06-06 23:08:25 -0700130 fi.cellY, false);
131 FolderIcon folder = FolderIcon.fromXml(R.layout.folder_icon, launcher,
132 getLayout(), fi, iconCache);
133
134 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(fi.cellX,fi.cellY,1,1);
135 mContent.addViewToCellLayout(folder, -1, 0, lp, true);
136
137 for (ApplicationInfo info: allApps) {
138 ComponentName cn = info.intent.getComponent();
139 if (!onWorkspace.contains(cn)) {
Winson Chung64359a52013-07-08 17:17:08 -0700140 Log.d(TAG, "Adding to all apps: " + info.intent);
Adam Cohene25af792013-06-06 23:08:25 -0700141 ShortcutInfo si = info.makeShortcut();
142 fi.add(si);
143 }
144 }
145 }
146
147 void addAppsToAllAppsFolder(ArrayList<ApplicationInfo> apps) {
148 View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
149 FolderIcon fi = null;
150
151 if (v instanceof FolderIcon) {
152 fi = (FolderIcon) v;
153 } else {
154 return;
155 }
156
157 FolderInfo info = fi.getFolderInfo();
158 for (ApplicationInfo a: apps) {
159 ComponentName cn = a.intent.getComponent();
160 ShortcutInfo si = a.makeShortcut();
161 info.add(si);
162 }
163 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700164}