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