blob: 3ce101414a0620e82ad470d351bcbe961209fa96 [file] [log] [blame]
Sunny Goyal16764582017-11-06 16:00:34 -08001/*
2 * Copyright (C) 2017 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 */
16package com.android.launcher3.uioverrides;
17
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080018import static com.android.launcher3.WorkspaceStateTransitionAnimation.NO_ANIM_PROPERTY_SETTER;
19
20import android.animation.AnimatorSet;
Sunny Goyal16764582017-11-06 16:00:34 -080021import android.content.Context;
22import android.content.Intent;
23import android.content.res.Resources;
24import android.graphics.Rect;
25import android.util.AttributeSet;
Sunny Goyal16764582017-11-06 16:00:34 -080026import android.view.View;
27import android.widget.FrameLayout;
28import android.widget.LinearLayout;
29import android.widget.Toast;
30
31import com.android.launcher3.Insettable;
32import com.android.launcher3.Launcher;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080033import com.android.launcher3.LauncherState;
34import com.android.launcher3.LauncherStateManager;
35import com.android.launcher3.LauncherStateManager.AnimationConfig;
Sunny Goyal16764582017-11-06 16:00:34 -080036import com.android.launcher3.R;
37import com.android.launcher3.Utilities;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080038import com.android.launcher3.WorkspaceStateTransitionAnimation.AnimatedPropertySetter;
39import com.android.launcher3.WorkspaceStateTransitionAnimation.PropertySetter;
40import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyal16764582017-11-06 16:00:34 -080041import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
42import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
43import com.android.launcher3.widget.WidgetsFullSheet;
44
45public class OverviewPanel extends LinearLayout implements Insettable, View.OnClickListener,
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080046 View.OnLongClickListener, LauncherStateManager.StateHandler {
Sunny Goyal16764582017-11-06 16:00:34 -080047
48 // Out of 100, the percent of space the overview bar should try and take vertically.
49 private static final float OVERVIEW_ICON_ZONE_RATIO = 0.22f;
50
51 private final Launcher mLauncher;
52
53 public OverviewPanel(Context context) {
54 this(context, null);
55 }
56
57 public OverviewPanel(Context context, AttributeSet attrs) {
58 this(context, attrs, 0);
59 }
60
61 public OverviewPanel(Context context, AttributeSet attrs, int defStyleAttr) {
62 super(context, attrs, defStyleAttr);
63 mLauncher = Launcher.getLauncher(context);
64 setAlpha(0);
65 }
66
67 @Override
68 protected void onFinishInflate() {
69 super.onFinishInflate();
70
71 int visibleChildCount = 3;
72 // Attach buttons.
73 attachListeners(findViewById(R.id.wallpaper_button));
74 attachListeners(findViewById(R.id.widget_button));
75
76 View settingsButton = findViewById(R.id.settings_button);
77 if (mLauncher.hasSettings()) {
78 attachListeners(settingsButton);
79 } else {
80 settingsButton.setVisibility(GONE);
81 visibleChildCount--;
82 }
83
84 // Init UI
85 Resources res = getResources();
86 int itemWidthPx =
87 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
88 int spacerWidthPx =
89 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
90
91 int totalItemWidth = visibleChildCount * itemWidthPx;
92 int maxWidth = totalItemWidth + (visibleChildCount - 1) * spacerWidthPx;
93
94 getLayoutParams().width = Math.min(mLauncher.getDeviceProfile().availableWidthPx, maxWidth);
95 getLayoutParams().height = getButtonBarHeight(mLauncher);
96 }
97
98 private void attachListeners(View view) {
99 view.setOnClickListener(this);
100 view.setOnLongClickListener(this);
101 }
102
103 @Override
104 public void setInsets(Rect insets) {
105 ((FrameLayout.LayoutParams) getLayoutParams()).bottomMargin = insets.bottom;
106 }
107
108 @Override
109 public void onClick(View view) {
110 handleViewClick(view, Action.Touch.TAP);
111 }
112
113 @Override
114 public boolean onLongClick(View view) {
115 return handleViewClick(view, Action.Touch.LONGPRESS);
116 }
117
118 private boolean handleViewClick(View view, int action) {
119 if (mLauncher.getWorkspace().isSwitchingState()) {
120 return false;
121 }
122
123 final int controlType;
124 if (view.getId() == R.id.wallpaper_button) {
125 mLauncher.onClickWallpaperPicker(view);
126 controlType = ControlType.WALLPAPER_BUTTON;
127 } else if (view.getId() == R.id.widget_button) {
128 onClickAddWidgetButton();
129 controlType = ControlType.WIDGETS_BUTTON;
130 } else if (view.getId() == R.id.settings_button) {
131 onClickSettingsButton(view);
132 controlType = ControlType.SETTINGS_BUTTON;
133 } else {
134 return false;
135 }
136
137 mLauncher.getUserEventDispatcher().logActionOnControl(action, controlType);
138 return true;
139 }
140
141 /**
142 * Event handler for the (Add) Widgets button that appears after a long press
143 * on the home screen.
144 */
145 public void onClickAddWidgetButton() {
146 if (getContext().getPackageManager().isSafeMode()) {
147 Toast.makeText(mLauncher, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
148 } else {
149 WidgetsFullSheet.show(mLauncher, true /* animated */);
150 }
151 }
152
153 /**
154 * Event handler for a click on the settings button that appears after a long press
155 * on the home screen.
156 */
157 public void onClickSettingsButton(View v) {
158 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
159 .setPackage(getContext().getPackageName());
160 intent.setSourceBounds(mLauncher.getViewBounds(v));
161 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
162 getContext().startActivity(intent, mLauncher.getActivityLaunchOptions(v));
163 }
164
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800165 @Override
166 public void setState(LauncherState state) {
167 setState(state, NO_ANIM_PROPERTY_SETTER);
168 }
169
170 @Override
171 public void setStateWithAnimation(LauncherState toState, AnimationLayerSet layerViews,
172 AnimatorSet anim, AnimationConfig config) {
173 setState(toState, new AnimatedPropertySetter(config.duration, layerViews, anim));
174 }
175
176 private void setState(LauncherState state, PropertySetter setter) {
177 boolean isOverview = state == LauncherState.OVERVIEW;
178 float finalHotseatAlpha = isOverview ? 0 : 1;
179
180 setter.setViewAlpha(null, this, isOverview ? 1 : 0);
181 setter.setViewAlpha(
182 mLauncher.getWorkspace().createHotseatAlphaAnimator(finalHotseatAlpha),
183 mLauncher.getHotseat(), finalHotseatAlpha);
184 }
Sunny Goyal16764582017-11-06 16:00:34 -0800185
186 public static int getButtonBarHeight(Launcher launcher) {
187 int zoneHeight = (int) (OVERVIEW_ICON_ZONE_RATIO *
Sunny Goyalc4fa8c32017-11-07 12:23:58 -0800188 launcher.getDeviceProfile().availableHeightPx);
Sunny Goyal16764582017-11-06 16:00:34 -0800189 Resources res = launcher.getResources();
190 int overviewModeMinIconZoneHeightPx =
191 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
192 int overviewModeMaxIconZoneHeightPx =
193 res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
194 return Utilities.boundToRange(zoneHeight,
195 overviewModeMinIconZoneHeightPx,
196 overviewModeMaxIconZoneHeightPx);
197 }
198}