blob: b8337b6a499468b47ff346349f63e0dffff926f6 [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 /**
Winson Chungc393b072015-05-20 15:03:13 -070068 * Returns whether there are other icons than the all apps button in the hotseat.
69 */
70 public boolean hasIcons() {
71 return mContent.getShortcutsAndWidgets().getChildCount() > 1;
72 }
73
74 /**
Winson Chung11a1a532013-09-13 11:14:45 -070075 * Registers the specified listener on the cell layout of the hotseat.
76 */
77 @Override
78 public void setOnLongClickListener(OnLongClickListener l) {
79 mContent.setOnLongClickListener(l);
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 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080090
Winson Chung3d503fb2011-07-13 17:25:49 -070091 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
92 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070093 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070094 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080095
Winson Chung3d503fb2011-07-13 17:25:49 -070096 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070097 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070098 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080099
100 public int getAllAppsButtonRank() {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800101 return mAllAppsButtonRank;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800102 }
103
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800104 public boolean isAllAppsButtonRank(int rank) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800105 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -0700106 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700107
108 @Override
109 protected void onFinishInflate() {
110 super.onFinishInflate();
Winson Chung5f8afe62013-08-12 16:19:28 -0700111 LauncherAppState app = LauncherAppState.getInstance();
112 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
113
Winson Chungc58497e2013-09-03 17:48:37 -0700114 mAllAppsButtonRank = grid.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700115 mContent = (CellLayout) findViewById(R.id.layout);
Winson Chung5f8afe62013-08-12 16:19:28 -0700116 if (grid.isLandscape && !grid.isLargeTablet()) {
117 mContent.setGridSize(1, (int) grid.numHotseatIcons);
118 } else {
119 mContent.setGridSize((int) grid.numHotseatIcons, 1);
120 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800121 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700122
123 resetLayout();
124 }
125
126 void resetLayout() {
127 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700128
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800129 // Add the Apps button
130 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -0700131
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800132 LayoutInflater inflater = LayoutInflater.from(context);
133 TextView allAppsButton = (TextView)
134 inflater.inflate(R.layout.all_apps_button, mContent, false);
135 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700136
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800137 Utilities.resizeIconDrawable(d);
138 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700139
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800140 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
141 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
142 if (mLauncher != null) {
143 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
144 mLauncher.setAllAppsButton(allAppsButton);
145 allAppsButton.setOnClickListener(mLauncher);
146 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Winson Chungc58497e2013-09-03 17:48:37 -0700147 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800148
149 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
150 // the hotseat in order regardless of which orientation they were added
151 int x = getCellXFromOrder(mAllAppsButtonRank);
152 int y = getCellYFromOrder(mAllAppsButtonRank);
153 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
154 lp.canReorder = false;
155 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700156 }
Adam Cohene25af792013-06-06 23:08:25 -0700157
Adam Cohena5f4e482013-10-11 12:10:28 -0700158 @Override
159 public boolean onInterceptTouchEvent(MotionEvent ev) {
160 // We don't want any clicks to go through to the hotseat unless the workspace is in
161 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700162 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700163 return true;
164 }
165 return false;
166 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700167}