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 | |
| 17 | package com.android.launcher2; |
| 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 | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.LayoutInflater; |
Michael Jurka | 5130e40 | 2011-10-13 04:55:35 -0700 | [diff] [blame] | 25 | import android.view.MotionEvent; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.widget.FrameLayout; |
| 28 | |
| 29 | import com.android.launcher.R; |
| 30 | |
| 31 | public class Hotseat extends FrameLayout { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 32 | @SuppressWarnings("unused") |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 33 | private static final String TAG = "Hotseat"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 34 | |
| 35 | private Launcher mLauncher; |
| 36 | private CellLayout mContent; |
| 37 | |
| 38 | private int mCellCountX; |
| 39 | private int mCellCountY; |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 40 | private int mAllAppsButtonRank; |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame^] | 41 | |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 42 | private boolean mTransposeLayoutWithOrientation; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 43 | private boolean mIsLandscape; |
| 44 | |
| 45 | public Hotseat(Context context) { |
| 46 | this(context, null); |
| 47 | } |
| 48 | |
| 49 | public Hotseat(Context context, AttributeSet attrs) { |
| 50 | this(context, attrs, 0); |
| 51 | } |
| 52 | |
| 53 | public Hotseat(Context context, AttributeSet attrs, int defStyle) { |
| 54 | super(context, attrs, defStyle); |
| 55 | |
| 56 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 57 | R.styleable.Hotseat, defStyle, 0); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 58 | Resources r = context.getResources(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 59 | mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1); |
| 60 | mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1); |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 61 | mAllAppsButtonRank = r.getInteger(R.integer.hotseat_all_apps_index); |
| 62 | mTransposeLayoutWithOrientation = |
| 63 | r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 64 | mIsLandscape = context.getResources().getConfiguration().orientation == |
| 65 | Configuration.ORIENTATION_LANDSCAPE; |
| 66 | } |
| 67 | |
| 68 | public void setup(Launcher launcher) { |
| 69 | mLauncher = launcher; |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 70 | setOnKeyListener(new HotseatIconKeyEventListener()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | CellLayout getLayout() { |
| 74 | return mContent; |
| 75 | } |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 76 | |
| 77 | private boolean hasVerticalHotseat() { |
| 78 | return (mIsLandscape && mTransposeLayoutWithOrientation); |
| 79 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 80 | |
| 81 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 82 | int getOrderInHotseat(int x, int y) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 83 | return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 84 | } |
| 85 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 86 | int getCellXFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 87 | return hasVerticalHotseat() ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 88 | } |
| 89 | int getCellYFromOrder(int rank) { |
Winson Chung | 0e721a4 | 2012-08-02 17:40:30 -0700 | [diff] [blame] | 90 | return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 91 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 92 | public boolean isAllAppsButtonRank(int rank) { |
| 93 | return rank == mAllAppsButtonRank; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 94 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 95 | |
| 96 | @Override |
| 97 | protected void onFinishInflate() { |
| 98 | super.onFinishInflate(); |
| 99 | if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX(); |
| 100 | if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY(); |
| 101 | mContent = (CellLayout) findViewById(R.id.layout); |
| 102 | mContent.setGridSize(mCellCountX, mCellCountY); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 103 | mContent.setIsHotseat(true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 104 | |
| 105 | resetLayout(); |
| 106 | } |
| 107 | |
| 108 | void resetLayout() { |
| 109 | mContent.removeAllViewsInLayout(); |
| 110 | |
| 111 | // Add the Apps button |
| 112 | Context context = getContext(); |
| 113 | LayoutInflater inflater = LayoutInflater.from(context); |
| 114 | BubbleTextView allAppsButton = (BubbleTextView) |
| 115 | inflater.inflate(R.layout.application, mContent, false); |
| 116 | allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null, |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 117 | context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null); |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 118 | allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); |
Michael Jurka | 5130e40 | 2011-10-13 04:55:35 -0700 | [diff] [blame] | 119 | allAppsButton.setOnTouchListener(new View.OnTouchListener() { |
| 120 | @Override |
| 121 | public boolean onTouch(View v, MotionEvent event) { |
| 122 | if (mLauncher != null && |
| 123 | (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) { |
| 124 | mLauncher.onTouchDownAllAppsButton(v); |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | }); |
| 129 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 130 | allAppsButton.setOnClickListener(new View.OnClickListener() { |
| 131 | @Override |
| 132 | public void onClick(android.view.View v) { |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 133 | if (mLauncher != null) { |
Michael Jurka | 2a55232 | 2011-10-11 15:22:05 -0700 | [diff] [blame] | 134 | mLauncher.onClickAllAppsButton(v); |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 135 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 136 | } |
| 137 | }); |
| 138 | |
| 139 | // Note: We do this to ensure that the hotseat is always laid out in the orientation of |
| 140 | // the hotseat in order regardless of which orientation they were added |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 141 | int x = getCellXFromOrder(mAllAppsButtonRank); |
| 142 | int y = getCellYFromOrder(mAllAppsButtonRank); |
Adam Cohen | 482ed82 | 2012-03-02 14:15:13 -0800 | [diff] [blame] | 143 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1); |
| 144 | lp.canReorder = false; |
Andrew Flynn | 850d2e7 | 2012-04-26 16:51:20 -0700 | [diff] [blame] | 145 | mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 146 | } |
| 147 | } |