blob: b614bc6287f5c5d5514fd42406b4faa3c1264465 [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.widget.FrameLayout;
Adam Cohen61f560d2013-09-30 15:58:20 -070028import android.widget.TextView;
Winson Chung3d503fb2011-07-13 17:25:49 -070029
Winson Chung3d503fb2011-07-13 17:25:49 -070030public class Hotseat extends FrameLayout {
Winson Chung3d503fb2011-07-13 17:25:49 -070031
Winson Chung3d503fb2011-07-13 17:25:49 -070032 private CellLayout mContent;
33
Winson Chungc58497e2013-09-03 17:48:37 -070034 private Launcher mLauncher;
35
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080036 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070037
Winson Chung0e721a42012-08-02 17:40:30 -070038 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070039 private boolean mIsLandscape;
40
41 public Hotseat(Context context) {
42 this(context, null);
43 }
44
45 public Hotseat(Context context, AttributeSet attrs) {
46 this(context, attrs, 0);
47 }
48
49 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
50 super(context, attrs, defStyle);
51
Winson Chung0e721a42012-08-02 17:40:30 -070052 Resources r = context.getResources();
Winson Chung0e721a42012-08-02 17:40:30 -070053 mTransposeLayoutWithOrientation =
54 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070055 mIsLandscape = context.getResources().getConfiguration().orientation ==
56 Configuration.ORIENTATION_LANDSCAPE;
57 }
58
59 public void setup(Launcher launcher) {
Winson Chungc58497e2013-09-03 17:48:37 -070060 mLauncher = launcher;
Winson Chung3d503fb2011-07-13 17:25:49 -070061 }
62
63 CellLayout getLayout() {
64 return mContent;
65 }
Winson Chung11a1a532013-09-13 11:14:45 -070066
67 /**
68 * Registers the specified listener on the cell layout of the hotseat.
69 */
70 @Override
71 public void setOnLongClickListener(OnLongClickListener l) {
72 mContent.setOnLongClickListener(l);
73 }
Winson Chung0e721a42012-08-02 17:40:30 -070074
75 private boolean hasVerticalHotseat() {
76 return (mIsLandscape && mTransposeLayoutWithOrientation);
77 }
Winson Chung3d503fb2011-07-13 17:25:49 -070078
79 /* Get the orientation invariant order of the item in the hotseat for persistence. */
80 int getOrderInHotseat(int x, int y) {
Winson Chung0e721a42012-08-02 17:40:30 -070081 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070082 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080083
Winson Chung3d503fb2011-07-13 17:25:49 -070084 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
85 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070086 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070087 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080088
Winson Chung3d503fb2011-07-13 17:25:49 -070089 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070090 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070091 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080092
93 public int getAllAppsButtonRank() {
Sunny Goyalc9acdd52015-02-26 12:34:42 -080094 return mAllAppsButtonRank;
Hyunyoung Song31178b82015-02-24 14:12:51 -080095 }
96
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080097 public boolean isAllAppsButtonRank(int rank) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -080098 return rank == mAllAppsButtonRank;
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
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800141 // Add the Apps button
142 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -0700143
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800144 LayoutInflater inflater = LayoutInflater.from(context);
145 TextView allAppsButton = (TextView)
146 inflater.inflate(R.layout.all_apps_button, mContent, false);
147 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700148
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800149 Utilities.resizeIconDrawable(d);
150 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700151
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800152 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
153 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
154 if (mLauncher != null) {
155 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
156 mLauncher.setAllAppsButton(allAppsButton);
157 allAppsButton.setOnClickListener(mLauncher);
158 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Winson Chungc58497e2013-09-03 17:48:37 -0700159 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800160
161 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
162 // the hotseat in order regardless of which orientation they were added
163 int x = getCellXFromOrder(mAllAppsButtonRank);
164 int y = getCellYFromOrder(mAllAppsButtonRank);
165 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
166 lp.canReorder = false;
167 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700168 }
Adam Cohene25af792013-06-06 23:08:25 -0700169
Adam Cohena5f4e482013-10-11 12:10:28 -0700170 @Override
171 public boolean onInterceptTouchEvent(MotionEvent ev) {
172 // We don't want any clicks to go through to the hotseat unless the workspace is in
173 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700174 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700175 return true;
176 }
177 return false;
178 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700179}