blob: f7fa38007255fae87e9890dbe3c5b72089124edb [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;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
Michael Jurka5130e402011-10-13 04:55:35 -070024import android.view.MotionEvent;
Winson Chung3d503fb2011-07-13 17:25:49 -070025import android.view.View;
26import android.widget.FrameLayout;
27
28import com.android.launcher.R;
29
30public class Hotseat extends FrameLayout {
Winson Chungf30ad5f2011-08-08 10:55:42 -070031 private static final String TAG = "Hotseat";
32 private static final int sAllAppsButtonRank = 2; // In the middle of the dock
Winson Chung3d503fb2011-07-13 17:25:49 -070033
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 Cohenac56cff2011-09-28 20:45:37 -070062 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070063 }
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 Chungf30ad5f2011-08-08 10:55:42 -070080 public static boolean isAllAppsButtonRank(int rank) {
81 return rank == sAllAppsButtonRank;
82 }
Winson Chung3d503fb2011-07-13 17:25:49 -070083
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 Chung90576b52011-09-27 17:45:27 -0700104 context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
Winson Chung4d279d92011-07-21 11:46:32 -0700105 // allAppsButton.setText(context.getString(R.string.all_apps_button_label));
106 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
Michael Jurka5130e402011-10-13 04:55:35 -0700107 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 Chung3d503fb2011-07-13 17:25:49 -0700118 allAppsButton.setOnClickListener(new View.OnClickListener() {
119 @Override
120 public void onClick(android.view.View v) {
Winson Chung4d279d92011-07-21 11:46:32 -0700121 if (mLauncher != null) {
Michael Jurka2a552322011-10-11 15:22:05 -0700122 mLauncher.onClickAllAppsButton(v);
Winson Chung4d279d92011-07-21 11:46:32 -0700123 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700124 }
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 Chungf30ad5f2011-08-08 10:55:42 -0700129 int x = getCellXFromOrder(sAllAppsButtonRank);
130 int y = getCellYFromOrder(sAllAppsButtonRank);
Winson Chung3d503fb2011-07-13 17:25:49 -0700131 mContent.addViewToCellLayout(allAppsButton, -1, 0, new CellLayout.LayoutParams(x,y,1,1),
132 true);
133 }
134}