blob: add62c004a95a4f198cde90c45e24dc51ba1bfe1 [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";
Winson Chung3d503fb2011-07-13 17:25:49 -070032
33 private Launcher mLauncher;
34 private CellLayout mContent;
35
36 private int mCellCountX;
37 private int mCellCountY;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080038 private int mAllAppsButtonRank;
Winson Chung3d503fb2011-07-13 17:25:49 -070039 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 Flynn0dca1ec2012-02-29 13:33:22 -080056 mAllAppsButtonRank = context.getResources().getInteger(R.integer.hotseat_all_apps_index);
Winson Chung3d503fb2011-07-13 17:25:49 -070057 mIsLandscape = context.getResources().getConfiguration().orientation ==
58 Configuration.ORIENTATION_LANDSCAPE;
59 }
60
61 public void setup(Launcher launcher) {
62 mLauncher = launcher;
Adam Cohenac56cff2011-09-28 20:45:37 -070063 setOnKeyListener(new HotseatIconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -070064 }
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 Flynn0dca1ec2012-02-29 13:33:22 -080081 public boolean isAllAppsButtonRank(int rank) {
82 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -070083 }
Winson Chung3d503fb2011-07-13 17:25:49 -070084
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 Flynn0dca1ec2012-02-29 13:33:22 -080092 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -070093
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 Chung90576b52011-09-27 17:45:27 -0700106 context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
Winson Chung4d279d92011-07-21 11:46:32 -0700107 // allAppsButton.setText(context.getString(R.string.all_apps_button_label));
108 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);
Winson Chung3d503fb2011-07-13 17:25:49 -0700133 mContent.addViewToCellLayout(allAppsButton, -1, 0, new CellLayout.LayoutParams(x,y,1,1),
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800134 true, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700135 }
136}