Hotseat.java revision c9acdd51c40c1b397adae6ba62c4acd01914b473
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.launcher3;
18
19import android.content.Context;
20import android.content.res.Configuration;
21import android.content.res.Resources;
22import android.graphics.Rect;
23import android.graphics.drawable.Drawable;
24import android.util.AttributeSet;
25import android.view.LayoutInflater;
26import android.view.MotionEvent;
27import android.widget.FrameLayout;
28import android.widget.TextView;
29
30public class Hotseat extends FrameLayout {
31
32    private CellLayout mContent;
33
34    private Launcher mLauncher;
35
36    private int mAllAppsButtonRank;
37
38    private boolean mTransposeLayoutWithOrientation;
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        Resources r = context.getResources();
53        mTransposeLayoutWithOrientation =
54                r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
55        mIsLandscape = context.getResources().getConfiguration().orientation ==
56            Configuration.ORIENTATION_LANDSCAPE;
57    }
58
59    public void setup(Launcher launcher) {
60        mLauncher = launcher;
61    }
62
63    CellLayout getLayout() {
64        return mContent;
65    }
66
67    /**
68     * Registers the specified listener on the cell layout of the hotseat.
69     */
70    @Override
71    public void setOnLongClickListener(OnLongClickListener l) {
72        mContent.setOnLongClickListener(l);
73    }
74
75    private boolean hasVerticalHotseat() {
76        return (mIsLandscape && mTransposeLayoutWithOrientation);
77    }
78
79    /* Get the orientation invariant order of the item in the hotseat for persistence. */
80    int getOrderInHotseat(int x, int y) {
81        return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
82    }
83
84    /* Get the orientation specific coordinates given an invariant order in the hotseat. */
85    int getCellXFromOrder(int rank) {
86        return hasVerticalHotseat() ? 0 : rank;
87    }
88
89    int getCellYFromOrder(int rank) {
90        return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
91    }
92
93    public int getAllAppsButtonRank() {
94        return mAllAppsButtonRank;
95    }
96
97    public boolean isAllAppsButtonRank(int rank) {
98        return rank == mAllAppsButtonRank;
99    }
100
101    /** This returns the coordinates of an app in a given cell, relative to the DragLayer */
102    Rect getCellCoordinates(int cellX, int cellY) {
103        Rect coords = new Rect();
104        mContent.cellToRect(cellX, cellY, 1, 1, coords);
105        int[] hotseatInParent = new int[2];
106        Utilities.getDescendantCoordRelativeToParent(this, mLauncher.getDragLayer(),
107                hotseatInParent, false);
108        coords.offset(hotseatInParent[0], hotseatInParent[1]);
109
110        // Center the icon
111        int cWidth = mContent.getShortcutsAndWidgets().getCellContentWidth();
112        int cHeight = mContent.getShortcutsAndWidgets().getCellContentHeight();
113        int cellPaddingX = (int) Math.max(0, ((coords.width() - cWidth) / 2f));
114        int cellPaddingY = (int) Math.max(0, ((coords.height() - cHeight) / 2f));
115        coords.offset(cellPaddingX, cellPaddingY);
116
117        return coords;
118    }
119
120    @Override
121    protected void onFinishInflate() {
122        super.onFinishInflate();
123        LauncherAppState app = LauncherAppState.getInstance();
124        DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
125
126        mAllAppsButtonRank = grid.hotseatAllAppsRank;
127        mContent = (CellLayout) findViewById(R.id.layout);
128        if (grid.isLandscape && !grid.isLargeTablet()) {
129            mContent.setGridSize(1, (int) grid.numHotseatIcons);
130        } else {
131            mContent.setGridSize((int) grid.numHotseatIcons, 1);
132        }
133        mContent.setIsHotseat(true);
134
135        resetLayout();
136    }
137
138    void resetLayout() {
139        mContent.removeAllViewsInLayout();
140
141        // Add the Apps button
142        Context context = getContext();
143
144        LayoutInflater inflater = LayoutInflater.from(context);
145        TextView allAppsButton = (TextView)
146                inflater.inflate(R.layout.all_apps_button, mContent, false);
147        Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
148
149        Utilities.resizeIconDrawable(d);
150        allAppsButton.setCompoundDrawables(null, d, null, null);
151
152        allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
153        allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
154        if (mLauncher != null) {
155            allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
156            mLauncher.setAllAppsButton(allAppsButton);
157            allAppsButton.setOnClickListener(mLauncher);
158            allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
159        }
160
161        // Note: We do this to ensure that the hotseat is always laid out in the orientation of
162        // the hotseat in order regardless of which orientation they were added
163        int x = getCellXFromOrder(mAllAppsButtonRank);
164        int y = getCellYFromOrder(mAllAppsButtonRank);
165        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
166        lp.canReorder = false;
167        mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
168    }
169
170    @Override
171    public boolean onInterceptTouchEvent(MotionEvent ev) {
172        // We don't want any clicks to go through to the hotseat unless the workspace is in
173        // the normal state.
174        if (mLauncher.getWorkspace().workspaceInModalState()) {
175            return true;
176        }
177        return false;
178    }
179}
180