summaryrefslogtreecommitdiffstats
path: root/src_ui_overrides/com/android/launcher3/uioverrides/OverviewPanel.java
blob: 616e25c2b9a51077773babf965139fb9e4f1d598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.uioverrides;

import static com.android.launcher3.LauncherState.OVERVIEW;
import static com.android.launcher3.WorkspaceStateTransitionAnimation.NO_ANIM_PROPERTY_SETTER;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.launcher3.Insettable;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherState;
import com.android.launcher3.LauncherStateManager;
import com.android.launcher3.LauncherStateManager.AnimationConfig;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.WorkspaceStateTransitionAnimation.AnimatedPropertySetter;
import com.android.launcher3.WorkspaceStateTransitionAnimation.PropertySetter;
import com.android.launcher3.anim.AnimatorSetBuilder;
import com.android.launcher3.anim.Interpolators;
import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
import com.android.launcher3.widget.WidgetsFullSheet;

public class OverviewPanel extends LinearLayout implements Insettable, View.OnClickListener,
        View.OnLongClickListener, LauncherStateManager.StateHandler {

    // Out of 100, the percent of space the overview bar should try and take vertically.
    private static final float OVERVIEW_ICON_ZONE_RATIO = 0.22f;

    private final Launcher mLauncher;

    public OverviewPanel(Context context) {
        this(context, null);
    }

    public OverviewPanel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OverviewPanel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mLauncher = Launcher.getLauncher(context);
        setAlpha(0);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        int visibleChildCount = 3;
        // Attach buttons.
        attachListeners(findViewById(R.id.wallpaper_button));
        attachListeners(findViewById(R.id.widget_button));

        View settingsButton = findViewById(R.id.settings_button);
        if (mLauncher.hasSettings()) {
            attachListeners(settingsButton);
        } else {
            settingsButton.setVisibility(GONE);
            visibleChildCount--;
        }

        // Init UI
        Resources res = getResources();
        int itemWidthPx =
                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
        int spacerWidthPx =
                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);

        int totalItemWidth = visibleChildCount * itemWidthPx;
        int maxWidth = totalItemWidth + (visibleChildCount - 1) * spacerWidthPx;

        getLayoutParams().width = Math.min(mLauncher.getDeviceProfile().availableWidthPx, maxWidth);
        getLayoutParams().height = getButtonBarHeight(mLauncher);
    }

    private void attachListeners(View view) {
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
    }

    @Override
    public void setInsets(Rect insets) {
        ((FrameLayout.LayoutParams) getLayoutParams()).bottomMargin = insets.bottom;
    }

    @Override
    public void onClick(View view) {
        handleViewClick(view, Action.Touch.TAP);
    }

    @Override
    public boolean onLongClick(View view) {
        return handleViewClick(view, Action.Touch.LONGPRESS);
    }

    private boolean handleViewClick(View view, int action) {
        if (mLauncher.getWorkspace().isSwitchingState()) {
            return false;
        }

        final int controlType;
        if (view.getId() == R.id.wallpaper_button) {
            mLauncher.onClickWallpaperPicker(view);
            controlType = ControlType.WALLPAPER_BUTTON;
        } else if (view.getId() == R.id.widget_button) {
            onClickAddWidgetButton();
            controlType = ControlType.WIDGETS_BUTTON;
        } else if (view.getId() == R.id.settings_button) {
            onClickSettingsButton(view);
            controlType = ControlType.SETTINGS_BUTTON;
        } else {
            return false;
        }

        mLauncher.getUserEventDispatcher().logActionOnControl(action, controlType);
        return true;
    }

    /**
     * Event handler for the (Add) Widgets button that appears after a long press
     * on the home screen.
     */
    public void onClickAddWidgetButton() {
        if (getContext().getPackageManager().isSafeMode()) {
            Toast.makeText(mLauncher, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
        } else {
            WidgetsFullSheet.show(mLauncher, true /* animated */);
        }
    }

    /**
     * Event handler for a click on the settings button that appears after a long press
     * on the home screen.
     */
    public void onClickSettingsButton(View v) {
        Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
                .setPackage(getContext().getPackageName());
        intent.setSourceBounds(mLauncher.getViewBounds(v));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(intent, mLauncher.getActivityLaunchOptionsAsBundle(v, false));
    }

    @Override
    public void setState(LauncherState state) {
        setState(state, NO_ANIM_PROPERTY_SETTER);
    }

    @Override
    public void setStateWithAnimation(LauncherState toState,
            AnimatorSetBuilder builder, AnimationConfig config) {
        setState(toState, new AnimatedPropertySetter(config.duration, builder));
    }

    private void setState(LauncherState state, PropertySetter setter) {
        float myAlpha = state == OVERVIEW ? 1 : 0;
        setter.setViewAlpha(this, myAlpha, Interpolators.ACCEL);
    }

    public static int getButtonBarHeight(Launcher launcher) {
        int zoneHeight = (int) (OVERVIEW_ICON_ZONE_RATIO *
                launcher.getDeviceProfile().availableHeightPx);
        Resources res = launcher.getResources();
        int overviewModeMinIconZoneHeightPx =
                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
        int overviewModeMaxIconZoneHeightPx =
                res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
        return Utilities.boundToRange(zoneHeight,
                overviewModeMinIconZoneHeightPx,
                overviewModeMaxIconZoneHeightPx);
    }
}