blob: 1af76682fa1f5782a4ed715a45ee125f33ef5cdc [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;
Tony Wickhamd6b40372015-09-23 18:37:57 -070020import android.graphics.Rect;
Winson Chungc58497e2013-09-03 17:48:37 -070021import android.graphics.drawable.Drawable;
Winson Chung3d503fb2011-07-13 17:25:49 -070022import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070023import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070024import android.view.MotionEvent;
Winsona49b1f72015-10-16 14:57:24 -070025import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080026import android.view.ViewDebug;
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
Hyunyoung Songddec1c72016-04-12 18:32:04 -070030import com.android.launcher3.logging.UserEventLogger;
31import com.android.launcher3.userevent.nano.LauncherLogProto;
32import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
33
Winson Chung8f1eff72015-05-28 17:33:40 -070034public class Hotseat extends FrameLayout
Hyunyoung Songddec1c72016-04-12 18:32:04 -070035 implements UserEventLogger.LaunchSourceProvider{
Winson Chung3d503fb2011-07-13 17:25:49 -070036
Winson Chung3d503fb2011-07-13 17:25:49 -070037 private CellLayout mContent;
38
Winson Chungc58497e2013-09-03 17:48:37 -070039 private Launcher mLauncher;
40
Sunny Goyal4ffec482016-02-09 11:28:52 -080041 @ViewDebug.ExportedProperty(category = "launcher")
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080042 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070043
Sunny Goyal4ffec482016-02-09 11:28:52 -080044 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal4f3e9382015-06-05 00:13:25 -070045 private final boolean mHasVerticalHotseat;
Winson Chung3d503fb2011-07-13 17:25:49 -070046
47 public Hotseat(Context context) {
48 this(context, null);
49 }
50
51 public Hotseat(Context context, AttributeSet attrs) {
52 this(context, attrs, 0);
53 }
54
55 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
56 super(context, attrs, defStyle);
Adam Cohen2e6da152015-05-06 11:42:25 -070057 mLauncher = (Launcher) context;
Sunny Goyal4f3e9382015-06-05 00:13:25 -070058 mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
Winson Chung3d503fb2011-07-13 17:25:49 -070059 }
60
Tony Wickham95cdb3a2016-02-18 14:37:07 -080061 public CellLayout getLayout() {
Winson Chung3d503fb2011-07-13 17:25:49 -070062 return mContent;
63 }
Winson Chung11a1a532013-09-13 11:14:45 -070064
65 /**
Winson Chungc393b072015-05-20 15:03:13 -070066 * Returns whether there are other icons than the all apps button in the hotseat.
67 */
68 public boolean hasIcons() {
69 return mContent.getShortcutsAndWidgets().getChildCount() > 1;
70 }
71
72 /**
Winson Chung11a1a532013-09-13 11:14:45 -070073 * Registers the specified listener on the cell layout of the hotseat.
74 */
75 @Override
76 public void setOnLongClickListener(OnLongClickListener l) {
77 mContent.setOnLongClickListener(l);
78 }
Tony Wickham6cbd2222015-11-09 17:51:08 -080079
Winson Chung3d503fb2011-07-13 17:25:49 -070080 /* Get the orientation invariant order of the item in the hotseat for persistence. */
81 int getOrderInHotseat(int x, int y) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070082 return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070083 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080084
Winson Chung3d503fb2011-07-13 17:25:49 -070085 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
86 int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070087 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070088 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080089
Winson Chung3d503fb2011-07-13 17:25:49 -070090 int getCellYFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070091 return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
Hyunyoung Song31178b82015-02-24 14:12:51 -080092 }
93
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080094 public boolean isAllAppsButtonRank(int rank) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -080095 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -070096 }
Winson Chung3d503fb2011-07-13 17:25:49 -070097
98 @Override
99 protected void onFinishInflate() {
100 super.onFinishInflate();
Adam Cohen2e6da152015-05-06 11:42:25 -0700101 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung5f8afe62013-08-12 16:19:28 -0700102
Adam Cohen2e6da152015-05-06 11:42:25 -0700103 mAllAppsButtonRank = grid.inv.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700104 mContent = (CellLayout) findViewById(R.id.layout);
Sunny Goyalc6205602015-05-21 20:46:33 -0700105 if (grid.isLandscape && !grid.isLargeTablet) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700106 mContent.setGridSize(1, (int) grid.inv.numHotseatIcons);
Winson Chung5f8afe62013-08-12 16:19:28 -0700107 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -0700108 mContent.setGridSize((int) grid.inv.numHotseatIcons, 1);
Winson Chung5f8afe62013-08-12 16:19:28 -0700109 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800110 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700111
112 resetLayout();
113 }
114
115 void resetLayout() {
116 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700117
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800118 // Add the Apps button
119 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -0700120
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800121 LayoutInflater inflater = LayoutInflater.from(context);
122 TextView allAppsButton = (TextView)
123 inflater.inflate(R.layout.all_apps_button, mContent, false);
124 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700125
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700126 mLauncher.resizeIconDrawable(d);
Tony Wickhamd6b40372015-09-23 18:37:57 -0700127 int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
128 Rect bounds = d.getBounds();
129 d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx,
130 bounds.bottom - scaleDownPx / 2);
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800131 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700132
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800133 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
134 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
135 if (mLauncher != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800136 mLauncher.setAllAppsButton(allAppsButton);
Winson Chung76648c52015-07-10 14:33:23 -0700137 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800138 allAppsButton.setOnClickListener(mLauncher);
Winson Chung76648c52015-07-10 14:33:23 -0700139 allAppsButton.setOnLongClickListener(mLauncher);
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800140 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Winson Chungc58497e2013-09-03 17:48:37 -0700141 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800142
143 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
144 // the hotseat in order regardless of which orientation they were added
145 int x = getCellXFromOrder(mAllAppsButtonRank);
146 int y = getCellYFromOrder(mAllAppsButtonRank);
147 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
148 lp.canReorder = false;
149 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700150 }
Adam Cohene25af792013-06-06 23:08:25 -0700151
Adam Cohena5f4e482013-10-11 12:10:28 -0700152 @Override
153 public boolean onInterceptTouchEvent(MotionEvent ev) {
154 // We don't want any clicks to go through to the hotseat unless the workspace is in
155 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700156 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700157 return true;
158 }
159 return false;
160 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700161
162 @Override
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700163 public void fillInLaunchSourceData(View v, ItemInfo info, Target target, Target targetParent) {
164 target.itemType = LauncherLogProto.APP_ICON;
165 target.gridX = info.cellX;
166 target.gridY = info.cellY;
167 targetParent.containerType = LauncherLogProto.HOTSEAT;
Winson Chung8f1eff72015-05-28 17:33:40 -0700168 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700169}