blob: 6e33d1014104f90ac6076f5b7b7592282dcc8914 [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 Chungc58497e2013-09-03 17:48:37 -070022import android.graphics.drawable.Drawable;
Winson Chung8f1eff72015-05-28 17:33:40 -070023import android.os.Bundle;
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 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
Winson Chung0e721a42012-08-02 17:40:30 -070039 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070040 private boolean mIsLandscape;
41
42 public Hotseat(Context context) {
43 this(context, null);
44 }
45
46 public Hotseat(Context context, AttributeSet attrs) {
47 this(context, attrs, 0);
48 }
49
50 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
51 super(context, attrs, defStyle);
52
Winson Chung0e721a42012-08-02 17:40:30 -070053 Resources r = context.getResources();
Winson Chung0e721a42012-08-02 17:40:30 -070054 mTransposeLayoutWithOrientation =
55 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070056 mIsLandscape = context.getResources().getConfiguration().orientation ==
57 Configuration.ORIENTATION_LANDSCAPE;
Adam Cohen2e6da152015-05-06 11:42:25 -070058 mLauncher = (Launcher) context;
Winson Chung3d503fb2011-07-13 17:25:49 -070059 }
60
61 CellLayout getLayout() {
62 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 }
Winson Chung0e721a42012-08-02 17:40:30 -070079
80 private boolean hasVerticalHotseat() {
81 return (mIsLandscape && mTransposeLayoutWithOrientation);
82 }
Winson Chung3d503fb2011-07-13 17:25:49 -070083
84 /* Get the orientation invariant order of the item in the hotseat for persistence. */
85 int getOrderInHotseat(int x, int y) {
Winson Chung0e721a42012-08-02 17:40:30 -070086 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070087 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080088
Winson Chung3d503fb2011-07-13 17:25:49 -070089 /* 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 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080093
Winson Chung3d503fb2011-07-13 17:25:49 -070094 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070095 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070096 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080097
98 public int getAllAppsButtonRank() {
Sunny Goyalc9acdd52015-02-26 12:34:42 -080099 return mAllAppsButtonRank;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800100 }
101
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800102 public boolean isAllAppsButtonRank(int rank) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800103 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -0700104 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700105
106 @Override
107 protected void onFinishInflate() {
108 super.onFinishInflate();
Adam Cohen2e6da152015-05-06 11:42:25 -0700109 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung5f8afe62013-08-12 16:19:28 -0700110
Adam Cohen2e6da152015-05-06 11:42:25 -0700111 mAllAppsButtonRank = grid.inv.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700112 mContent = (CellLayout) findViewById(R.id.layout);
Sunny Goyalc6205602015-05-21 20:46:33 -0700113 if (grid.isLandscape && !grid.isLargeTablet) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700114 mContent.setGridSize(1, (int) grid.inv.numHotseatIcons);
Winson Chung5f8afe62013-08-12 16:19:28 -0700115 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 mContent.setGridSize((int) grid.inv.numHotseatIcons, 1);
Winson Chung5f8afe62013-08-12 16:19:28 -0700117 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800118 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700119
120 resetLayout();
121 }
122
123 void resetLayout() {
124 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700125
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800126 // Add the Apps button
127 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -0700128
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800129 LayoutInflater inflater = LayoutInflater.from(context);
130 TextView allAppsButton = (TextView)
131 inflater.inflate(R.layout.all_apps_button, mContent, false);
132 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700133
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700134 mLauncher.resizeIconDrawable(d);
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800135 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700136
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800137 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
138 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
139 if (mLauncher != null) {
140 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
141 mLauncher.setAllAppsButton(allAppsButton);
142 allAppsButton.setOnClickListener(mLauncher);
143 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Winson Chungc58497e2013-09-03 17:48:37 -0700144 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800145
146 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
147 // the hotseat in order regardless of which orientation they were added
148 int x = getCellXFromOrder(mAllAppsButtonRank);
149 int y = getCellYFromOrder(mAllAppsButtonRank);
150 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
151 lp.canReorder = false;
152 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700153 }
Adam Cohene25af792013-06-06 23:08:25 -0700154
Adam Cohena5f4e482013-10-11 12:10:28 -0700155 @Override
156 public boolean onInterceptTouchEvent(MotionEvent ev) {
157 // We don't want any clicks to go through to the hotseat unless the workspace is in
158 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700159 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700160 return true;
161 }
162 return false;
163 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700164
165 @Override
166 public void fillInLaunchSourceData(Bundle sourceData) {
167 sourceData.putString(Stats.SOURCE_EXTRA_CONTAINER, Stats.CONTAINER_HOTSEAT);
168 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700169}