blob: 93f39ff3b8f654bce20377bb81e4dcf6aaf70847 [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;
Michael Jurka5130e402011-10-13 04:55:35 -070027import android.view.MotionEvent;
Winson Chung3d503fb2011-07-13 17:25:49 -070028import android.view.View;
29import android.widget.FrameLayout;
30
Daniel Sandler325dc232013-06-05 22:57:57 -040031import com.android.launcher3.R;
Winson Chung3d503fb2011-07-13 17:25:49 -070032
Adam Cohene25af792013-06-06 23:08:25 -070033import java.util.ArrayList;
34
Winson Chung3d503fb2011-07-13 17:25:49 -070035public class Hotseat extends FrameLayout {
Michael Jurka3a9fced2012-04-13 14:44:29 -070036 @SuppressWarnings("unused")
Winson Chungf30ad5f2011-08-08 10:55:42 -070037 private static final String TAG = "Hotseat";
Winson Chung3d503fb2011-07-13 17:25:49 -070038
39 private Launcher mLauncher;
40 private CellLayout mContent;
41
42 private int mCellCountX;
43 private int mCellCountY;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080044 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070045
Winson Chung0e721a42012-08-02 17:40:30 -070046 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070047 private boolean mIsLandscape;
48
49 public Hotseat(Context context) {
50 this(context, null);
51 }
52
53 public Hotseat(Context context, AttributeSet attrs) {
54 this(context, attrs, 0);
55 }
56
57 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
59
60 TypedArray a = context.obtainStyledAttributes(attrs,
61 R.styleable.Hotseat, defStyle, 0);
Winson Chung0e721a42012-08-02 17:40:30 -070062 Resources r = context.getResources();
Winson Chung3d503fb2011-07-13 17:25:49 -070063 mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
64 mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
Winson Chung0e721a42012-08-02 17:40:30 -070065 mAllAppsButtonRank = r.getInteger(R.integer.hotseat_all_apps_index);
66 mTransposeLayoutWithOrientation =
67 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070068 mIsLandscape = context.getResources().getConfiguration().orientation ==
69 Configuration.ORIENTATION_LANDSCAPE;
70 }
71
72 public void setup(Launcher launcher) {
73 mLauncher = launcher;
Adam Cohenac56cff2011-09-28 20:45:37 -070074 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070075 }
76
77 CellLayout getLayout() {
78 return mContent;
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) {
Adam Cohen947dc542013-06-06 22:43:33 -070097 return false;
Winson Chungf30ad5f2011-08-08 10:55:42 -070098 }
Winson Chung3d503fb2011-07-13 17:25:49 -070099
100 @Override
101 protected void onFinishInflate() {
102 super.onFinishInflate();
103 if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX();
104 if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY();
105 mContent = (CellLayout) findViewById(R.id.layout);
106 mContent.setGridSize(mCellCountX, mCellCountY);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800107 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700108
109 resetLayout();
110 }
111
112 void resetLayout() {
113 mContent.removeAllViewsInLayout();
Winson Chung3d503fb2011-07-13 17:25:49 -0700114 }
Adam Cohene25af792013-06-06 23:08:25 -0700115
116 void addAllAppsFolder(IconCache iconCache, ArrayList<ApplicationInfo> allApps,
117 ArrayList<ComponentName> onWorkspace, Launcher launcher) {
118 FolderInfo fi = new FolderInfo();
119
120 fi.cellX = getCellXFromOrder(mAllAppsButtonRank);
121 fi.cellY = getCellYFromOrder(mAllAppsButtonRank);
122 fi.spanX = 1;
123 fi.spanY = 1;
124 fi.container = LauncherSettings.Favorites.CONTAINER_HOTSEAT;
Adam Cohendcd297f2013-06-18 13:13:40 -0700125 fi.screenId = mAllAppsButtonRank;
Adam Cohene25af792013-06-06 23:08:25 -0700126 fi.itemType = LauncherSettings.Favorites.ITEM_TYPE_FOLDER;
127 fi.title = "All Apps";
Adam Cohendcd297f2013-06-18 13:13:40 -0700128 LauncherModel.addItemToDatabase(launcher, fi, fi.container, fi.screenId, fi.cellX,
Adam Cohene25af792013-06-06 23:08:25 -0700129 fi.cellY, false);
130 FolderIcon folder = FolderIcon.fromXml(R.layout.folder_icon, launcher,
131 getLayout(), fi, iconCache);
132
133 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(fi.cellX,fi.cellY,1,1);
134 mContent.addViewToCellLayout(folder, -1, 0, lp, true);
135
136 for (ApplicationInfo info: allApps) {
137 ComponentName cn = info.intent.getComponent();
138 if (!onWorkspace.contains(cn)) {
139 System.out.println("Adding to all apps: " + info.intent);
140 ShortcutInfo si = info.makeShortcut();
141 fi.add(si);
142 }
143 }
144 }
145
146 void addAppsToAllAppsFolder(ArrayList<ApplicationInfo> apps) {
147 View v = mContent.getChildAt(getCellXFromOrder(mAllAppsButtonRank), getCellYFromOrder(mAllAppsButtonRank));
148 FolderIcon fi = null;
149
150 if (v instanceof FolderIcon) {
151 fi = (FolderIcon) v;
152 } else {
153 return;
154 }
155
156 FolderInfo info = fi.getFolderInfo();
157 for (ApplicationInfo a: apps) {
158 ComponentName cn = a.intent.getComponent();
159 ShortcutInfo si = a.makeShortcut();
160 info.add(si);
161 }
162 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700163}