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 | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 29 | import com.android.launcher3.R; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 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) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame^] | 93 | return false; |
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(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 110 | } |
| 111 | } |