blob: 15d606d8f908a87a56de00fb2619d53a5094e27b [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 {
Michael Jurka3a9fced2012-04-13 14:44:29 -070031 @SuppressWarnings("unused")
Winson Chungf30ad5f2011-08-08 10:55:42 -070032 private static final String TAG = "Hotseat";
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;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080039 private int mAllAppsButtonRank;
Winson Chung3d503fb2011-07-13 17:25:49 -070040 private boolean mIsLandscape;
41
42 public Hotseat(Context context) {
43 this(context, null);
44 }
45
46 public Hotseat(Context context, AttributeSet attrs) {
47 this(context, attrs, 0);
48 }
49
50 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
51 super(context, attrs, defStyle);
52
53 TypedArray a = context.obtainStyledAttributes(attrs,
54 R.styleable.Hotseat, defStyle, 0);
55 mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
56 mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080057 mAllAppsButtonRank = context.getResources().getInteger(R.integer.hotseat_all_apps_index);
Winson Chung3d503fb2011-07-13 17:25:49 -070058 mIsLandscape = context.getResources().getConfiguration().orientation ==
59 Configuration.ORIENTATION_LANDSCAPE;
60 }
61
62 public void setup(Launcher launcher) {
63 mLauncher = launcher;
Adam Cohenac56cff2011-09-28 20:45:37 -070064 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070065 }
66
67 CellLayout getLayout() {
68 return mContent;
69 }
70
71 /* Get the orientation invariant order of the item in the hotseat for persistence. */
72 int getOrderInHotseat(int x, int y) {
73 return mIsLandscape ? (mContent.getCountY() - y - 1) : x;
74 }
75 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
76 int getCellXFromOrder(int rank) {
77 return mIsLandscape ? 0 : rank;
78 }
79 int getCellYFromOrder(int rank) {
80 return mIsLandscape ? (mContent.getCountY() - (rank + 1)) : 0;
81 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080082 public boolean isAllAppsButtonRank(int rank) {
83 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -070084 }
Winson Chung3d503fb2011-07-13 17:25:49 -070085
86 @Override
87 protected void onFinishInflate() {
88 super.onFinishInflate();
89 if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX();
90 if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY();
91 mContent = (CellLayout) findViewById(R.id.layout);
92 mContent.setGridSize(mCellCountX, mCellCountY);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080093 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -070094
95 resetLayout();
96 }
97
98 void resetLayout() {
99 mContent.removeAllViewsInLayout();
100
101 // Add the Apps button
102 Context context = getContext();
103 LayoutInflater inflater = LayoutInflater.from(context);
104 BubbleTextView allAppsButton = (BubbleTextView)
105 inflater.inflate(R.layout.application, mContent, false);
106 allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung90576b52011-09-27 17:45:27 -0700107 context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
Winson Chung4d279d92011-07-21 11:46:32 -0700108 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
Michael Jurka5130e402011-10-13 04:55:35 -0700109 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 Chung3d503fb2011-07-13 17:25:49 -0700120 allAppsButton.setOnClickListener(new View.OnClickListener() {
121 @Override
122 public void onClick(android.view.View v) {
Winson Chung4d279d92011-07-21 11:46:32 -0700123 if (mLauncher != null) {
Michael Jurka2a552322011-10-11 15:22:05 -0700124 mLauncher.onClickAllAppsButton(v);
Winson Chung4d279d92011-07-21 11:46:32 -0700125 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700126 }
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 Flynn0dca1ec2012-02-29 13:33:22 -0800131 int x = getCellXFromOrder(mAllAppsButtonRank);
132 int y = getCellYFromOrder(mAllAppsButtonRank);
Adam Cohen482ed822012-03-02 14:15:13 -0800133 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
134 lp.canReorder = false;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700135 mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700136 }
137}