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.drawable.Drawable;
23import android.os.Bundle;
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        implements Stats.LaunchSourceProvider{
32
33    private CellLayout mContent;
34
35    private Launcher mLauncher;
36
37    private int mAllAppsButtonRank;
38
39    private final boolean mHasVerticalHotseat;
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        mLauncher = (Launcher) context;
52        mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
53    }
54
55    CellLayout getLayout() {
56        return mContent;
57    }
58
59    /**
60     * Returns whether there are other icons than the all apps button in the hotseat.
61     */
62    public boolean hasIcons() {
63        return mContent.getShortcutsAndWidgets().getChildCount() > 1;
64    }
65
66    /**
67     * Registers the specified listener on the cell layout of the hotseat.
68     */
69    @Override
70    public void setOnLongClickListener(OnLongClickListener l) {
71        mContent.setOnLongClickListener(l);
72    }
73
74    /* Get the orientation invariant order of the item in the hotseat for persistence. */
75    int getOrderInHotseat(int x, int y) {
76        return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
77    }
78
79    /* Get the orientation specific coordinates given an invariant order in the hotseat. */
80    int getCellXFromOrder(int rank) {
81        return mHasVerticalHotseat ? 0 : rank;
82    }
83
84    int getCellYFromOrder(int rank) {
85        return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
86    }
87
88    public boolean isAllAppsButtonRank(int rank) {
89        return rank == mAllAppsButtonRank;
90    }
91
92    @Override
93    protected void onFinishInflate() {
94        super.onFinishInflate();
95        DeviceProfile grid = mLauncher.getDeviceProfile();
96
97        mAllAppsButtonRank = grid.inv.hotseatAllAppsRank;
98        mContent = (CellLayout) findViewById(R.id.layout);
99        if (grid.isLandscape && !grid.isLargeTablet) {
100            mContent.setGridSize(1, (int) grid.inv.numHotseatIcons);
101        } else {
102            mContent.setGridSize((int) grid.inv.numHotseatIcons, 1);
103        }
104        mContent.setIsHotseat(true);
105
106        resetLayout();
107    }
108
109    void resetLayout() {
110        mContent.removeAllViewsInLayout();
111
112        // Add the Apps button
113        Context context = getContext();
114
115        LayoutInflater inflater = LayoutInflater.from(context);
116        TextView allAppsButton = (TextView)
117                inflater.inflate(R.layout.all_apps_button, mContent, false);
118        Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
119
120        mLauncher.resizeIconDrawable(d);
121        allAppsButton.setCompoundDrawables(null, d, null, null);
122
123        allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
124        allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
125        if (mLauncher != null) {
126            mLauncher.setAllAppsButton(allAppsButton);
127            allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
128            allAppsButton.setOnClickListener(mLauncher);
129            allAppsButton.setOnLongClickListener(mLauncher);
130            allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
131        }
132
133        // Note: We do this to ensure that the hotseat is always laid out in the orientation of
134        // the hotseat in order regardless of which orientation they were added
135        int x = getCellXFromOrder(mAllAppsButtonRank);
136        int y = getCellYFromOrder(mAllAppsButtonRank);
137        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
138        lp.canReorder = false;
139        mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
140    }
141
142    @Override
143    public boolean onInterceptTouchEvent(MotionEvent ev) {
144        // We don't want any clicks to go through to the hotseat unless the workspace is in
145        // the normal state.
146        if (mLauncher.getWorkspace().workspaceInModalState()) {
147            return true;
148        }
149        return false;
150    }
151
152    @Override
153    public void fillInLaunchSourceData(Bundle sourceData) {
154        sourceData.putString(Stats.SOURCE_EXTRA_CONTAINER, Stats.CONTAINER_HOTSEAT);
155    }
156}
157