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 17 package com.android.launcher2; 18 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.content.res.TypedArray; 22 import android.util.AttributeSet; 23 import android.view.LayoutInflater; 24 import android.view.MotionEvent; 25 import android.view.View; 26 import android.widget.FrameLayout; 27 28 import com.android.launcher.R; 29 30 public 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 Hotseat(Context context)42 public Hotseat(Context context) { 43 this(context, null); 44 } 45 Hotseat(Context context, AttributeSet attrs)46 public Hotseat(Context context, AttributeSet attrs) { 47 this(context, attrs, 0); 48 } 49 Hotseat(Context context, AttributeSet attrs, int defStyle)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 setup(Launcher launcher)62 public void setup(Launcher launcher) { 63 mLauncher = launcher; 64 setOnKeyListener(new HotseatIconKeyEventListener()); 65 } 66 getLayout()67 CellLayout getLayout() { 68 return mContent; 69 } 70 71 /* Get the orientation invariant order of the item in the hotseat for persistence. */ getOrderInHotseat(int x, int y)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. */ getCellXFromOrder(int rank)76 int getCellXFromOrder(int rank) { 77 return mIsLandscape ? 0 : rank; 78 } getCellYFromOrder(int rank)79 int getCellYFromOrder(int rank) { 80 return mIsLandscape ? (mContent.getCountY() - (rank + 1)) : 0; 81 } isAllAppsButtonRank(int rank)82 public boolean isAllAppsButtonRank(int rank) { 83 return rank == mAllAppsButtonRank; 84 } 85 86 @Override onFinishInflate()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 resetLayout()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