Hotseat.java revision 4d279d94ade1c0d455404312b3c9cfde0078c547
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.View;
25import android.widget.FrameLayout;
26
27import com.android.launcher.R;
28
29public class Hotseat extends FrameLayout {
30    static final String TAG = "Hotseat";
31
32    private Launcher mLauncher;
33    private CellLayout mContent;
34
35    private int mCellCountX;
36    private int mCellCountY;
37    private boolean mIsLandscape;
38
39    public Hotseat(Context context) {
40        this(context, null);
41    }
42
43    public Hotseat(Context context, AttributeSet attrs) {
44        this(context, attrs, 0);
45    }
46
47    public Hotseat(Context context, AttributeSet attrs, int defStyle) {
48        super(context, attrs, defStyle);
49
50        TypedArray a = context.obtainStyledAttributes(attrs,
51                R.styleable.Hotseat, defStyle, 0);
52        mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
53        mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
54        mIsLandscape = context.getResources().getConfiguration().orientation ==
55            Configuration.ORIENTATION_LANDSCAPE;
56    }
57
58    public void setup(Launcher launcher) {
59        mLauncher = launcher;
60        setOnKeyListener(new HotseatBubbleTextViewKeyEventListener());
61    }
62
63    CellLayout getLayout() {
64        return mContent;
65    }
66
67    /* Get the orientation invariant order of the item in the hotseat for persistence. */
68    int getOrderInHotseat(int x, int y) {
69        return mIsLandscape ? (mContent.getCountY() - y - 1) : x;
70    }
71    /* Get the orientation specific coordinates given an invariant order in the hotseat. */
72    int getCellXFromOrder(int rank) {
73        return mIsLandscape ? 0 : rank;
74    }
75    int getCellYFromOrder(int rank) {
76        return mIsLandscape ? (mContent.getCountY() - (rank + 1)) : 0;
77    }
78
79    @Override
80    protected void onFinishInflate() {
81        super.onFinishInflate();
82        if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX();
83        if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY();
84        mContent = (CellLayout) findViewById(R.id.layout);
85        mContent.setGridSize(mCellCountX, mCellCountY);
86
87        resetLayout();
88    }
89
90    void resetLayout() {
91        mContent.removeAllViewsInLayout();
92
93        // Add the Apps button
94        Context context = getContext();
95        LayoutInflater inflater = LayoutInflater.from(context);
96        BubbleTextView allAppsButton = (BubbleTextView)
97                inflater.inflate(R.layout.application, mContent, false);
98        allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
99                context.getResources().getDrawable(R.drawable.apps_hotseat_button), null, null);
100        // allAppsButton.setText(context.getString(R.string.all_apps_button_label));
101        allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
102        allAppsButton.setOnClickListener(new View.OnClickListener() {
103            @Override
104            public void onClick(android.view.View v) {
105                if (mLauncher != null) {
106                    mLauncher.showAllApps(true);
107                }
108            }
109        });
110
111        // Note: We do this to ensure that the hotseat is always laid out in the orientation of
112        // the hotseat in order regardless of which orientation they were added
113        int x = getCellXFromOrder(0);
114        int y = getCellYFromOrder(0);
115        mContent.addViewToCellLayout(allAppsButton, -1, 0, new CellLayout.LayoutParams(x,y,1,1),
116                true);
117    }
118}
119