blob: 38feaa4fddef1806342135d572f3d866d52e97c0 [file] [log] [blame]
Winson Chung3d503fb2011-07-13 17:25:49 -07001/*
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
17package com.android.launcher2;
18
19import android.content.Context;
20import android.content.res.Configuration;
Winson Chung0e721a42012-08-02 17:40:30 -070021import android.content.res.Resources;
Winson Chung3d503fb2011-07-13 17:25:49 -070022import android.content.res.TypedArray;
23import android.util.AttributeSet;
24import android.view.LayoutInflater;
Michael Jurka5130e402011-10-13 04:55:35 -070025import android.view.MotionEvent;
Winson Chung3d503fb2011-07-13 17:25:49 -070026import android.view.View;
27import android.widget.FrameLayout;
28
29import com.android.launcher.R;
30
31public class Hotseat extends FrameLayout {
Michael Jurka3a9fced2012-04-13 14:44:29 -070032 @SuppressWarnings("unused")
Winson Chungf30ad5f2011-08-08 10:55:42 -070033 private static final String TAG = "Hotseat";
Winson Chung3d503fb2011-07-13 17:25:49 -070034
35 private Launcher mLauncher;
36 private CellLayout mContent;
37
38 private int mCellCountX;
39 private int mCellCountY;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080040 private int mAllAppsButtonRank;
Winson Chung0e721a42012-08-02 17:40:30 -070041
42 private boolean mTransposeLayoutWithOrientation;
Winson Chung3d503fb2011-07-13 17:25:49 -070043 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 Chung0e721a42012-08-02 17:40:30 -070058 Resources r = context.getResources();
Winson Chung3d503fb2011-07-13 17:25:49 -070059 mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
60 mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
Winson Chung0e721a42012-08-02 17:40:30 -070061 mAllAppsButtonRank = r.getInteger(R.integer.hotseat_all_apps_index);
62 mTransposeLayoutWithOrientation =
63 r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
Winson Chung3d503fb2011-07-13 17:25:49 -070064 mIsLandscape = context.getResources().getConfiguration().orientation ==
65 Configuration.ORIENTATION_LANDSCAPE;
66 }
67
68 public void setup(Launcher launcher) {
69 mLauncher = launcher;
Adam Cohenac56cff2011-09-28 20:45:37 -070070 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070071 }
72
73 CellLayout getLayout() {
74 return mContent;
75 }
Winson Chung0e721a42012-08-02 17:40:30 -070076
77 private boolean hasVerticalHotseat() {
78 return (mIsLandscape && mTransposeLayoutWithOrientation);
79 }
Winson Chung3d503fb2011-07-13 17:25:49 -070080
81 /* Get the orientation invariant order of the item in the hotseat for persistence. */
82 int getOrderInHotseat(int x, int y) {
Winson Chung0e721a42012-08-02 17:40:30 -070083 return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070084 }
85 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
86 int getCellXFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070087 return hasVerticalHotseat() ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070088 }
89 int getCellYFromOrder(int rank) {
Winson Chung0e721a42012-08-02 17:40:30 -070090 return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070091 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080092 public boolean isAllAppsButtonRank(int rank) {
93 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -070094 }
Winson Chung3d503fb2011-07-13 17:25:49 -070095
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 Flynn0dca1ec2012-02-29 13:33:22 -0800103 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700104
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 Chung90576b52011-09-27 17:45:27 -0700117 context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
Winson Chung4d279d92011-07-21 11:46:32 -0700118 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
Michael Jurka5130e402011-10-13 04:55:35 -0700119 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 Chung3d503fb2011-07-13 17:25:49 -0700130 allAppsButton.setOnClickListener(new View.OnClickListener() {
131 @Override
132 public void onClick(android.view.View v) {
Winson Chung4d279d92011-07-21 11:46:32 -0700133 if (mLauncher != null) {
Michael Jurka2a552322011-10-11 15:22:05 -0700134 mLauncher.onClickAllAppsButton(v);
Winson Chung4d279d92011-07-21 11:46:32 -0700135 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700136 }
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 Flynn0dca1ec2012-02-29 13:33:22 -0800141 int x = getCellXFromOrder(mAllAppsButtonRank);
142 int y = getCellYFromOrder(mAllAppsButtonRank);
Adam Cohen482ed822012-03-02 14:15:13 -0800143 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
144 lp.canReorder = false;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700145 mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700146 }
147}