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; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 57 | mLauncher = (Launcher) context; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | CellLayout getLayout() { |
| 61 | return mContent; |
| 62 | } |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
Winson Chung | c393b07 | 2015-05-20 15:03:13 -0700 | [diff] [blame] | 65 | * Returns whether there are other icons than the all apps button in the hotseat. |
| 66 | */ |
| 67 | public boolean hasIcons() { |
| 68 | return mContent.getShortcutsAndWidgets().getChildCount() > 1; |
| 69 | } |
| 70 | |
| 71 | /** |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 72 | * Registers the specified listener on the cell layout of the hotseat. |
| 73 | */ |
| 74 | @Override |
| 75 | public void setOnLongClickListener(OnLongClickListener l) { |
| 76 | mContent.setOnLongClickListener(l); |
| 77 | } |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 78 | |
| 79 | private boolean hasVerticalHotseat() { |
| 80 | return (mIsLandscape && mTransposeLayoutWithOrientation); |
| 81 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 82 | |
| 83 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 84 | int getOrderInHotseat(int x, int y) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 85 | return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 86 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 87 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 88 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 89 | int getCellXFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 90 | return hasVerticalHotseat() ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 91 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 92 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 93 | int getCellYFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 94 | return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 95 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 96 | |
| 97 | public int getAllAppsButtonRank() { |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 98 | return mAllAppsButtonRank; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 101 | public boolean isAllAppsButtonRank(int rank) { |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 102 | return rank == mAllAppsButtonRank; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 103 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | protected void onFinishInflate() { |
| 107 | super.onFinishInflate(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 108 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 109 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 110 | mAllAppsButtonRank = grid.inv.hotseatAllAppsRank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 111 | mContent = (CellLayout) findViewById(R.id.layout); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame^] | 112 | if (grid.isLandscape && !grid.isLargeTablet) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 113 | mContent.setGridSize(1, (int) grid.inv.numHotseatIcons); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 114 | } else { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 115 | mContent.setGridSize((int) grid.inv.numHotseatIcons, 1); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 116 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 117 | mContent.setIsHotseat(true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 118 | |
| 119 | resetLayout(); |
| 120 | } |
| 121 | |
| 122 | void resetLayout() { |
| 123 | mContent.removeAllViewsInLayout(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 124 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 125 | // Add the Apps button |
| 126 | Context context = getContext(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 127 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 128 | LayoutInflater inflater = LayoutInflater.from(context); |
| 129 | TextView allAppsButton = (TextView) |
| 130 | inflater.inflate(R.layout.all_apps_button, mContent, false); |
| 131 | Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon); |
Adam Cohen | 71b0473 | 2014-06-11 10:36:14 -0700 | [diff] [blame] | 132 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 133 | Utilities.resizeIconDrawable(d); |
| 134 | allAppsButton.setCompoundDrawables(null, d, null, null); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 135 | |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 136 | allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); |
| 137 | allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener()); |
| 138 | if (mLauncher != null) { |
| 139 | allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener()); |
| 140 | mLauncher.setAllAppsButton(allAppsButton); |
| 141 | allAppsButton.setOnClickListener(mLauncher); |
| 142 | allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 143 | } |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 144 | |
| 145 | // Note: We do this to ensure that the hotseat is always laid out in the orientation of |
| 146 | // the hotseat in order regardless of which orientation they were added |
| 147 | int x = getCellXFromOrder(mAllAppsButtonRank); |
| 148 | int y = getCellYFromOrder(mAllAppsButtonRank); |
| 149 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1); |
| 150 | lp.canReorder = false; |
| 151 | mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 152 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 153 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 156 | // We don't want any clicks to go through to the hotseat unless the workspace is in |
| 157 | // the normal state. |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 158 | if (mLauncher.getWorkspace().workspaceInModalState()) { |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | return false; |
| 162 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 163 | } |