blob: 902b6ec61d4de8ad4bfa6a525789bf52bf87b9c1 [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 Chung8f1eff72015-05-28 17:33:40 -070022import android.os.Bundle;
Winson Chung3d503fb2011-07-13 17:25:49 -070023import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070024import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070025import android.view.MotionEvent;
Winsona49b1f72015-10-16 14:57:24 -070026import android.view.View;
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 Chung8f1eff72015-05-28 17:33:40 -070030public class Hotseat extends FrameLayout
31 implements Stats.LaunchSourceProvider{
Winson Chung3d503fb2011-07-13 17:25:49 -070032
Winson Chung3d503fb2011-07-13 17:25:49 -070033 private CellLayout mContent;
34
Winson Chungc58497e2013-09-03 17:48:37 -070035 private Launcher mLauncher;
36
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080037 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070038
Sunny Goyal4f3e9382015-06-05 00:13:25 -070039 private final boolean mHasVerticalHotseat;
Winson Chung3d503fb2011-07-13 17:25:49 -070040
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);
Adam Cohen2e6da152015-05-06 11:42:25 -070051 mLauncher = (Launcher) context;
Sunny Goyal4f3e9382015-06-05 00:13:25 -070052 mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
Winson Chung3d503fb2011-07-13 17:25:49 -070053 }
54
55 CellLayout getLayout() {
56 return mContent;
57 }
Winson Chung11a1a532013-09-13 11:14:45 -070058
59 /**
Winson Chungc393b072015-05-20 15:03:13 -070060 * Returns whether there are other icons than the all apps button in the hotseat.
61 */
62 public boolean hasIcons() {
63 return mContent.getShortcutsAndWidgets().getChildCount() > 1;
64 }
65
66 /**
Winson Chung11a1a532013-09-13 11:14:45 -070067 * Registers the specified listener on the cell layout of the hotseat.
68 */
69 @Override
70 public void setOnLongClickListener(OnLongClickListener l) {
71 mContent.setOnLongClickListener(l);
72 }
Winson Chung0e721a42012-08-02 17:40:30 -070073
Winson Chung3d503fb2011-07-13 17:25:49 -070074 /* Get the orientation invariant order of the item in the hotseat for persistence. */
75 int getOrderInHotseat(int x, int y) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070076 return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070077 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080078
Winson Chung3d503fb2011-07-13 17:25:49 -070079 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
80 int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070081 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070082 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080083
Winson Chung3d503fb2011-07-13 17:25:49 -070084 int getCellYFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070085 return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
Hyunyoung Song31178b82015-02-24 14:12:51 -080086 }
87
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080088 public boolean isAllAppsButtonRank(int rank) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -080089 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -070090 }
Winson Chung3d503fb2011-07-13 17:25:49 -070091
92 @Override
93 protected void onFinishInflate() {
94 super.onFinishInflate();
Adam Cohen2e6da152015-05-06 11:42:25 -070095 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung5f8afe62013-08-12 16:19:28 -070096
Adam Cohen2e6da152015-05-06 11:42:25 -070097 mAllAppsButtonRank = grid.inv.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -070098 mContent = (CellLayout) findViewById(R.id.layout);
Sunny Goyalc6205602015-05-21 20:46:33 -070099 if (grid.isLandscape && !grid.isLargeTablet) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700100 mContent.setGridSize(1, (int) grid.inv.numHotseatIcons);
Winson Chung5f8afe62013-08-12 16:19:28 -0700101 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -0700102 mContent.setGridSize((int) grid.inv.numHotseatIcons, 1);
Winson Chung5f8afe62013-08-12 16:19:28 -0700103 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800104 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700105
106 resetLayout();
107 }
108
109 void resetLayout() {
110 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700111
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800112 // Add the Apps button
113 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -0700114
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800115 LayoutInflater inflater = LayoutInflater.from(context);
116 TextView allAppsButton = (TextView)
117 inflater.inflate(R.layout.all_apps_button, mContent, false);
118 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700119
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700120 mLauncher.resizeIconDrawable(d);
Tony Wickhamd6b40372015-09-23 18:37:57 -0700121 int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
122 Rect bounds = d.getBounds();
123 d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx,
124 bounds.bottom - scaleDownPx / 2);
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800125 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700126
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800127 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
128 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
129 if (mLauncher != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800130 mLauncher.setAllAppsButton(allAppsButton);
Winson Chung76648c52015-07-10 14:33:23 -0700131 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800132 allAppsButton.setOnClickListener(mLauncher);
Winson Chung76648c52015-07-10 14:33:23 -0700133 allAppsButton.setOnLongClickListener(mLauncher);
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800134 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Winson Chungc58497e2013-09-03 17:48:37 -0700135 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800136
137 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
138 // the hotseat in order regardless of which orientation they were added
139 int x = getCellXFromOrder(mAllAppsButtonRank);
140 int y = getCellYFromOrder(mAllAppsButtonRank);
141 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
142 lp.canReorder = false;
143 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700144 }
Adam Cohene25af792013-06-06 23:08:25 -0700145
Adam Cohena5f4e482013-10-11 12:10:28 -0700146 @Override
147 public boolean onInterceptTouchEvent(MotionEvent ev) {
148 // We don't want any clicks to go through to the hotseat unless the workspace is in
149 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700150 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700151 return true;
152 }
153 return false;
154 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700155
156 @Override
Winsona49b1f72015-10-16 14:57:24 -0700157 public void fillInLaunchSourceData(View v, Bundle sourceData) {
Winson Chung8f1eff72015-05-28 17:33:40 -0700158 sourceData.putString(Stats.SOURCE_EXTRA_CONTAINER, Stats.CONTAINER_HOTSEAT);
159 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700160}