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.Resources;
22import android.content.res.TypedArray;
23import android.util.AttributeSet;
24import android.view.LayoutInflater;
25import android.view.MotionEvent;
26import android.view.View;
27import android.widget.FrameLayout;
28
29import com.android.launcher.R;
30
31public class Hotseat extends FrameLayout {
32    @SuppressWarnings("unused")
33    private static final String TAG = "Hotseat";
34
35    private Launcher mLauncher;
36    private CellLayout mContent;
37
38    private int mCellCountX;
39    private int mCellCountY;
40    private int mAllAppsButtonRank;
41
42    private boolean mTransposeLayoutWithOrientation;
43    private boolean mIsLandscape;
44
45    public Hotseat(Context context) {
46        this(context, null);
47    }
48
49    public Hotseat(Context context, AttributeSet attrs) {
50        this(context, attrs, 0);
51    }
52
53    public Hotseat(Context context, AttributeSet attrs, int defStyle) {
54        super(context, attrs, defStyle);
55
56        TypedArray a = context.obtainStyledAttributes(attrs,
57                R.styleable.Hotseat, defStyle, 0);
58        Resources r = context.getResources();
59        mCellCountX = a.getInt(R.styleable.Hotseat_cellCountX, -1);
60        mCellCountY = a.getInt(R.styleable.Hotseat_cellCountY, -1);
61        mAllAppsButtonRank = r.getInteger(R.integer.hotseat_all_apps_index);
62        mTransposeLayoutWithOrientation =
63                r.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
64        mIsLandscape = context.getResources().getConfiguration().orientation ==
65            Configuration.ORIENTATION_LANDSCAPE;
66    }
67
68    public void setup(Launcher launcher) {
69        mLauncher = launcher;
70        setOnKeyListener(new HotseatIconKeyEventListener());
71    }
72
73    CellLayout getLayout() {
74        return mContent;
75    }
76
77    private boolean hasVerticalHotseat() {
78        return (mIsLandscape && mTransposeLayoutWithOrientation);
79    }
80
81    /* Get the orientation invariant order of the item in the hotseat for persistence. */
82    int getOrderInHotseat(int x, int y) {
83        return hasVerticalHotseat() ? (mContent.getCountY() - y - 1) : x;
84    }
85    /* Get the orientation specific coordinates given an invariant order in the hotseat. */
86    int getCellXFromOrder(int rank) {
87        return hasVerticalHotseat() ? 0 : rank;
88    }
89    int getCellYFromOrder(int rank) {
90        return hasVerticalHotseat() ? (mContent.getCountY() - (rank + 1)) : 0;
91    }
92    public boolean isAllAppsButtonRank(int rank) {
93        return rank == mAllAppsButtonRank;
94    }
95
96    @Override
97    protected void onFinishInflate() {
98        super.onFinishInflate();
99        if (mCellCountX < 0) mCellCountX = LauncherModel.getCellCountX();
100        if (mCellCountY < 0) mCellCountY = LauncherModel.getCellCountY();
101        mContent = (CellLayout) findViewById(R.id.layout);
102        mContent.setGridSize(mCellCountX, mCellCountY);
103        mContent.setIsHotseat(true);
104
105        resetLayout();
106    }
107
108    void resetLayout() {
109        mContent.removeAllViewsInLayout();
110
111        // Add the Apps button
112        Context context = getContext();
113        LayoutInflater inflater = LayoutInflater.from(context);
114        BubbleTextView allAppsButton = (BubbleTextView)
115                inflater.inflate(R.layout.application, mContent, false);
116        allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
117                context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
118        allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
119        allAppsButton.setOnTouchListener(new View.OnTouchListener() {
120            @Override
121            public boolean onTouch(View v, MotionEvent event) {
122                if (mLauncher != null &&
123                    (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
124                    mLauncher.onTouchDownAllAppsButton(v);
125                }
126                return false;
127            }
128        });
129
130        allAppsButton.setOnClickListener(new View.OnClickListener() {
131            @Override
132            public void onClick(android.view.View v) {
133                if (mLauncher != null) {
134                    mLauncher.onClickAllAppsButton(v);
135                }
136            }
137        });
138
139        // Note: We do this to ensure that the hotseat is always laid out in the orientation of
140        // the hotseat in order regardless of which orientation they were added
141        int x = getCellXFromOrder(mAllAppsButtonRank);
142        int y = getCellYFromOrder(mAllAppsButtonRank);
143        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
144        lp.canReorder = false;
145        mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
146    }
147}
148