Hotseat.java revision 850d2e718560cb12ae73292e9d39f21a93d3c2c1
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
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;
24import android.view.MotionEvent;
25import android.view.View;
26import android.widget.FrameLayout;
27
28import com.android.launcher.R;
29
30public class Hotseat extends FrameLayout {
31    @SuppressWarnings("unused")
32    private static final String TAG = "Hotseat";
33
34    private Launcher mLauncher;
35    private CellLayout mContent;
36
37    private int mCellCountX;
38    private int mCellCountY;
39    private int mAllAppsButtonRank;
40    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);
57        mAllAppsButtonRank = context.getResources().getInteger(R.integer.hotseat_all_apps_index);
58        mIsLandscape = context.getResources().getConfiguration().orientation ==
59            Configuration.ORIENTATION_LANDSCAPE;
60    }
61
62    public void setup(Launcher launcher) {
63        mLauncher = launcher;
64        setOnKeyListener(new HotseatIconKeyEventListener());
65    }
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    }
82    public boolean isAllAppsButtonRank(int rank) {
83        return rank == mAllAppsButtonRank;
84    }
85
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);
93        mContent.setIsHotseat(true);
94
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,
107                context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
108        allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
109        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
120        allAppsButton.setOnClickListener(new View.OnClickListener() {
121            @Override
122            public void onClick(android.view.View v) {
123                if (mLauncher != null) {
124                    mLauncher.onClickAllAppsButton(v);
125                }
126            }
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
131        int x = getCellXFromOrder(mAllAppsButtonRank);
132        int y = getCellYFromOrder(mAllAppsButtonRank);
133        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
134        lp.canReorder = false;
135        mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
136    }
137}
138