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