Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Configuration; |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 22 | import android.graphics.Rect; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 27 | import android.widget.FrameLayout; |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 28 | import android.widget.TextView; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 29 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 30 | public class Hotseat extends FrameLayout { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 31 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 32 | private CellLayout mContent; |
| 33 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 34 | private Launcher mLauncher; |
| 35 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 36 | private int mAllAppsButtonRank; |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 37 | |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 38 | private boolean mTransposeLayoutWithOrientation; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 39 | 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 Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 52 | Resources r = context.getResources(); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 53 | mTransposeLayoutWithOrientation = |
| 54 | r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 55 | mIsLandscape = context.getResources().getConfiguration().orientation == |
| 56 | Configuration.ORIENTATION_LANDSCAPE; |
| 57 | } |
| 58 | |
| 59 | public void setup(Launcher launcher) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 60 | mLauncher = launcher; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | CellLayout getLayout() { |
| 64 | return mContent; |
| 65 | } |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
Winson Chung | c393b07 | 2015-05-20 15:03:13 -0700 | [diff] [blame^] | 68 | * 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 Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 75 | * 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 Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 81 | |
| 82 | private boolean hasVerticalHotseat() { |
| 83 | return (mIsLandscape && mTransposeLayoutWithOrientation); |
| 84 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 85 | |
| 86 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 87 | int getOrderInHotseat(int x, int y) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 88 | return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 89 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 90 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 91 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 92 | int getCellXFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 93 | return hasVerticalHotseat() ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 94 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 95 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 96 | int getCellYFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 97 | return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 98 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 99 | |
| 100 | public int getAllAppsButtonRank() { |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 101 | return mAllAppsButtonRank; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 104 | public boolean isAllAppsButtonRank(int rank) { |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 105 | return rank == mAllAppsButtonRank; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 106 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | protected void onFinishInflate() { |
| 110 | super.onFinishInflate(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 111 | LauncherAppState app = LauncherAppState.getInstance(); |
| 112 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 113 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 114 | mAllAppsButtonRank = grid.hotseatAllAppsRank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 115 | mContent = (CellLayout) findViewById(R.id.layout); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 116 | if (grid.isLandscape && !grid.isLargeTablet()) { |
| 117 | mContent.setGridSize(1, (int) grid.numHotseatIcons); |
| 118 | } else { |
| 119 | mContent.setGridSize((int) grid.numHotseatIcons, 1); |
| 120 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 121 | mContent.setIsHotseat(true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 122 | |
| 123 | resetLayout(); |
| 124 | } |
| 125 | |
| 126 | void resetLayout() { |
| 127 | mContent.removeAllViewsInLayout(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 128 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 129 | // Add the Apps button |
| 130 | Context context = getContext(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 131 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 132 | 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 Cohen | 71b0473 | 2014-06-11 10:36:14 -0700 | [diff] [blame] | 136 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 137 | Utilities.resizeIconDrawable(d); |
| 138 | allAppsButton.setCompoundDrawables(null, d, null, null); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 139 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 140 | 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 Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 147 | } |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 148 | |
| 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 Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 156 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 157 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 158 | @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 Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 162 | if (mLauncher.getWorkspace().workspaceInModalState()) { |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 167 | } |