2018-01-19 11:24:32 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 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.
|
|
|
|
|
*/
|
2018-03-07 19:58:07 -08:00
|
|
|
package com.android.launcher3.views;
|
2018-01-19 11:24:32 -08:00
|
|
|
|
2020-06-06 14:44:27 -07:00
|
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.IGNORE;
|
2020-05-28 23:36:14 -07:00
|
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_SETTINGS_BUTTON_TAP_OR_LONGPRESS;
|
|
|
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_WIDGETSTRAY_BUTTON_TAP_OR_LONGPRESS;
|
2018-03-30 17:10:13 -07:00
|
|
|
|
2018-01-19 11:24:32 -08:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.graphics.Rect;
|
2018-03-30 17:10:13 -07:00
|
|
|
import android.graphics.RectF;
|
2021-04-01 16:04:44 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2018-03-30 17:10:13 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.ArrayMap;
|
2018-01-19 11:24:32 -08:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.View.OnClickListener;
|
2018-03-07 19:58:07 -08:00
|
|
|
import android.view.View.OnLongClickListener;
|
2018-01-19 11:24:32 -08:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
2019-08-29 23:11:30 -07:00
|
|
|
import androidx.annotation.Nullable;
|
2021-04-01 16:04:44 -07:00
|
|
|
import androidx.core.content.ContextCompat;
|
2019-08-29 23:11:30 -07:00
|
|
|
|
2021-10-25 15:19:38 -05:00
|
|
|
import com.android.launcher3.AbstractFloatingView;
|
2018-01-19 11:24:32 -08:00
|
|
|
import com.android.launcher3.Launcher;
|
2020-06-06 14:44:27 -07:00
|
|
|
import com.android.launcher3.LauncherSettings;
|
2018-01-19 11:24:32 -08:00
|
|
|
import com.android.launcher3.R;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
2020-05-28 23:36:14 -07:00
|
|
|
import com.android.launcher3.logging.StatsLogManager.EventEnum;
|
2019-09-11 16:51:50 -07:00
|
|
|
import com.android.launcher3.model.WidgetsModel;
|
2020-06-06 14:44:27 -07:00
|
|
|
import com.android.launcher3.model.data.WorkspaceItemInfo;
|
2018-03-30 17:10:13 -07:00
|
|
|
import com.android.launcher3.popup.ArrowPopup;
|
|
|
|
|
import com.android.launcher3.shortcuts.DeepShortcutView;
|
2020-04-15 12:43:01 -07:00
|
|
|
import com.android.launcher3.testing.TestLogging;
|
2022-07-26 13:54:31 -07:00
|
|
|
import com.android.launcher3.testing.shared.TestProtocol;
|
2022-11-04 16:43:42 -07:00
|
|
|
import com.android.launcher3.util.PackageManagerHelper;
|
2021-02-08 17:18:25 +00:00
|
|
|
import com.android.launcher3.widget.picker.WidgetsFullSheet;
|
2018-01-19 11:24:32 -08:00
|
|
|
|
2018-03-30 17:10:13 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2018-01-19 11:24:32 -08:00
|
|
|
/**
|
|
|
|
|
* Popup shown on long pressing an empty space in launcher
|
|
|
|
|
*/
|
2021-09-23 19:56:24 -04:00
|
|
|
public class OptionsPopupView extends ArrowPopup<Launcher>
|
2018-03-07 19:58:07 -08:00
|
|
|
implements OnClickListener, OnLongClickListener {
|
2018-01-19 11:24:32 -08:00
|
|
|
|
2022-11-04 16:43:42 -07:00
|
|
|
// An intent extra to indicate the horizontal scroll of the wallpaper.
|
|
|
|
|
private static final String EXTRA_WALLPAPER_OFFSET = "com.android.launcher3.WALLPAPER_OFFSET";
|
|
|
|
|
private static final String EXTRA_WALLPAPER_FLAVOR = "com.android.launcher3.WALLPAPER_FLAVOR";
|
|
|
|
|
// An intent extra to indicate the launch source by launcher.
|
|
|
|
|
private static final String EXTRA_WALLPAPER_LAUNCH_SOURCE =
|
|
|
|
|
"com.android.wallpaper.LAUNCH_SOURCE";
|
|
|
|
|
|
2018-03-30 17:10:13 -07:00
|
|
|
private final ArrayMap<View, OptionItem> mItemMap = new ArrayMap<>();
|
|
|
|
|
private RectF mTargetRect;
|
2021-04-01 16:04:44 -07:00
|
|
|
private boolean mShouldAddArrow;
|
2018-01-19 11:24:32 -08:00
|
|
|
|
|
|
|
|
public OptionsPopupView(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionsPopupView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 19:56:24 -04:00
|
|
|
public void setTargetRect(RectF targetRect) {
|
|
|
|
|
mTargetRect = targetRect;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 11:24:32 -08:00
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
2020-05-28 23:36:14 -07:00
|
|
|
handleViewClick(view);
|
2018-03-07 19:58:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View view) {
|
2020-05-28 23:36:14 -07:00
|
|
|
return handleViewClick(view);
|
2018-03-07 19:58:07 -08:00
|
|
|
}
|
|
|
|
|
|
2020-05-28 23:36:14 -07:00
|
|
|
private boolean handleViewClick(View view) {
|
2018-03-30 17:10:13 -07:00
|
|
|
OptionItem item = mItemMap.get(view);
|
|
|
|
|
if (item == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-07-20 12:38:01 -07:00
|
|
|
if (item.eventId.getId() > 0) {
|
2021-09-23 19:56:24 -04:00
|
|
|
mActivityContext.getStatsLogManager().logger().log(item.eventId);
|
2018-03-30 17:10:13 -07:00
|
|
|
}
|
2020-07-20 12:38:01 -07:00
|
|
|
if (item.clickListener.onLongClick(view)) {
|
2018-01-19 11:24:32 -08:00
|
|
|
close(true);
|
2018-03-07 19:58:07 -08:00
|
|
|
return true;
|
2018-01-19 11:24:32 -08:00
|
|
|
}
|
2018-03-07 19:58:07 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 11:24:32 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
|
|
|
|
|
if (ev.getAction() != MotionEvent.ACTION_DOWN) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-03 15:53:39 -07:00
|
|
|
if (getPopupContainer().isEventOverView(this, ev)) {
|
2018-01-19 11:24:32 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
close(true);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean isOfType(int type) {
|
|
|
|
|
return (type & TYPE_OPTIONS_POPUP) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 16:04:44 -07:00
|
|
|
public void setShouldAddArrow(boolean shouldAddArrow) {
|
|
|
|
|
mShouldAddArrow = shouldAddArrow;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 15:30:41 -05:00
|
|
|
@Override
|
|
|
|
|
protected boolean shouldAddArrow() {
|
2021-04-01 16:04:44 -07:00
|
|
|
return mShouldAddArrow;
|
2021-03-12 15:30:41 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-30 17:10:13 -07:00
|
|
|
@Override
|
|
|
|
|
protected void getTargetObjectLocation(Rect outPos) {
|
|
|
|
|
mTargetRect.roundOut(outPos);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 22:48:37 -08:00
|
|
|
public static OptionsPopupView show(ActivityContext launcher, RectF targetRect,
|
2022-11-18 16:43:03 -08:00
|
|
|
List<OptionItem> items, boolean shouldAddArrow) {
|
2021-06-16 10:35:14 -07:00
|
|
|
return show(launcher, targetRect, items, shouldAddArrow, 0 /* width */);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 22:48:37 -08:00
|
|
|
public static OptionsPopupView show(ActivityContext launcher, RectF targetRect,
|
2022-11-18 16:43:03 -08:00
|
|
|
List<OptionItem> items, boolean shouldAddArrow, int width) {
|
2018-03-30 17:10:13 -07:00
|
|
|
OptionsPopupView popup = (OptionsPopupView) launcher.getLayoutInflater()
|
|
|
|
|
.inflate(R.layout.longpress_options_menu, launcher.getDragLayer(), false);
|
|
|
|
|
popup.mTargetRect = targetRect;
|
2021-04-01 16:04:44 -07:00
|
|
|
popup.setShouldAddArrow(shouldAddArrow);
|
2018-03-30 17:10:13 -07:00
|
|
|
|
|
|
|
|
for (OptionItem item : items) {
|
2020-01-07 13:07:55 -08:00
|
|
|
DeepShortcutView view =
|
|
|
|
|
(DeepShortcutView) popup.inflateAndAdd(R.layout.system_shortcut, popup);
|
2021-06-16 10:35:14 -07:00
|
|
|
if (width > 0) {
|
|
|
|
|
view.getLayoutParams().width = width;
|
|
|
|
|
}
|
2021-04-01 16:04:44 -07:00
|
|
|
view.getIconView().setBackgroundDrawable(item.icon);
|
|
|
|
|
view.getBubbleText().setText(item.label);
|
2018-03-30 17:10:13 -07:00
|
|
|
view.setOnClickListener(popup);
|
|
|
|
|
view.setOnLongClickListener(popup);
|
|
|
|
|
popup.mItemMap.put(view, item);
|
2018-01-19 11:24:32 -08:00
|
|
|
}
|
2021-06-01 21:26:10 -07:00
|
|
|
|
2020-10-23 09:26:44 -07:00
|
|
|
popup.show();
|
2021-02-09 22:50:02 -08:00
|
|
|
return popup;
|
2018-01-19 11:24:32 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-20 12:38:01 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the list of supported actions
|
|
|
|
|
*/
|
|
|
|
|
public static ArrayList<OptionItem> getOptions(Launcher launcher) {
|
2018-03-30 17:10:13 -07:00
|
|
|
ArrayList<OptionItem> options = new ArrayList<>();
|
2022-11-04 16:43:42 -07:00
|
|
|
boolean styleWallpaperExists = styleWallpapersExists(launcher);
|
|
|
|
|
int resString = styleWallpaperExists
|
|
|
|
|
? R.string.styles_wallpaper_button_text : R.string.wallpaper_button_text;
|
|
|
|
|
int resDrawable = styleWallpaperExists
|
|
|
|
|
? R.drawable.ic_palette : R.drawable.ic_wallpaper;
|
2021-04-01 16:04:44 -07:00
|
|
|
options.add(new OptionItem(launcher,
|
|
|
|
|
resString,
|
|
|
|
|
resDrawable,
|
2020-06-06 14:44:27 -07:00
|
|
|
IGNORE,
|
2020-05-28 23:36:14 -07:00
|
|
|
OptionsPopupView::startWallpaperPicker));
|
2022-02-15 13:31:40 -08:00
|
|
|
if (!WidgetsModel.GO_DISABLE_WIDGETS) {
|
|
|
|
|
options.add(new OptionItem(launcher,
|
|
|
|
|
R.string.widget_button_text,
|
|
|
|
|
R.drawable.ic_widget,
|
|
|
|
|
LAUNCHER_WIDGETSTRAY_BUTTON_TAP_OR_LONGPRESS,
|
|
|
|
|
OptionsPopupView::onWidgetsClicked));
|
|
|
|
|
}
|
|
|
|
|
options.add(new OptionItem(launcher,
|
|
|
|
|
R.string.settings_button_text,
|
|
|
|
|
R.drawable.ic_setting,
|
|
|
|
|
LAUNCHER_SETTINGS_BUTTON_TAP_OR_LONGPRESS,
|
|
|
|
|
OptionsPopupView::startSettings));
|
2020-07-20 12:38:01 -07:00
|
|
|
return options;
|
2018-01-19 11:24:32 -08:00
|
|
|
}
|
2018-03-07 19:58:07 -08:00
|
|
|
|
2020-07-20 12:38:01 -07:00
|
|
|
private static boolean onWidgetsClicked(View view) {
|
2019-08-29 23:11:30 -07:00
|
|
|
return openWidgets(Launcher.getLauncher(view.getContext())) != null;
|
2018-06-12 13:44:43 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 23:11:30 -07:00
|
|
|
/** Returns WidgetsFullSheet that was opened, or null if nothing was opened. */
|
|
|
|
|
@Nullable
|
|
|
|
|
public static WidgetsFullSheet openWidgets(Launcher launcher) {
|
2018-03-07 19:58:07 -08:00
|
|
|
if (launcher.getPackageManager().isSafeMode()) {
|
|
|
|
|
Toast.makeText(launcher, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
|
2019-08-29 23:11:30 -07:00
|
|
|
return null;
|
2018-03-07 19:58:07 -08:00
|
|
|
} else {
|
2021-10-25 15:19:38 -05:00
|
|
|
AbstractFloatingView floatingView = AbstractFloatingView.getTopOpenViewWithType(
|
|
|
|
|
launcher, TYPE_WIDGETS_FULL_SHEET);
|
|
|
|
|
if (floatingView != null) {
|
|
|
|
|
return (WidgetsFullSheet) floatingView;
|
|
|
|
|
}
|
2019-08-29 23:11:30 -07:00
|
|
|
return WidgetsFullSheet.show(launcher, true /* animated */);
|
2018-03-07 19:58:07 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 12:38:01 -07:00
|
|
|
private static boolean startSettings(View view) {
|
2020-04-15 12:43:01 -07:00
|
|
|
TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: startSettings");
|
2018-03-30 17:10:13 -07:00
|
|
|
Launcher launcher = Launcher.getLauncher(view.getContext());
|
2018-03-07 19:58:07 -08:00
|
|
|
launcher.startActivity(new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
|
|
|
|
|
.setPackage(launcher.getPackageName())
|
2022-03-16 11:29:47 -07:00
|
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
|
2018-03-30 17:10:13 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Event handler for the wallpaper picker button that appears after a long press
|
|
|
|
|
* on the home screen.
|
|
|
|
|
*/
|
2020-07-20 12:38:01 -07:00
|
|
|
private static boolean startWallpaperPicker(View v) {
|
2018-03-30 17:10:13 -07:00
|
|
|
Launcher launcher = Launcher.getLauncher(v.getContext());
|
|
|
|
|
if (!Utilities.isWallpaperAllowed(launcher)) {
|
2021-12-20 16:47:38 +00:00
|
|
|
String message = launcher.getStringCache() != null
|
|
|
|
|
? launcher.getStringCache().disabledByAdminMessage
|
|
|
|
|
: launcher.getString(R.string.msg_disabled_by_admin);
|
|
|
|
|
Toast.makeText(launcher, message, Toast.LENGTH_SHORT).show();
|
2018-03-30 17:10:13 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
|
2019-01-31 12:37:41 -08:00
|
|
|
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
2018-03-30 17:10:13 -07:00
|
|
|
.putExtra(EXTRA_WALLPAPER_OFFSET,
|
2020-10-23 09:26:44 -07:00
|
|
|
launcher.getWorkspace().getWallpaperOffsetForCenterPage())
|
|
|
|
|
.putExtra(EXTRA_WALLPAPER_LAUNCH_SOURCE, "app_launched_launcher");
|
2022-11-04 16:43:42 -07:00
|
|
|
if (!styleWallpapersExists(launcher)) {
|
2019-02-07 10:33:55 -08:00
|
|
|
intent.putExtra(EXTRA_WALLPAPER_FLAVOR, "wallpaper_only");
|
2019-04-11 18:30:32 -04:00
|
|
|
} else {
|
|
|
|
|
intent.putExtra(EXTRA_WALLPAPER_FLAVOR, "focus_wallpaper");
|
2019-02-07 10:33:55 -08:00
|
|
|
}
|
2018-03-30 17:10:13 -07:00
|
|
|
String pickerPackage = launcher.getString(R.string.wallpaper_picker_package);
|
|
|
|
|
if (!TextUtils.isEmpty(pickerPackage)) {
|
|
|
|
|
intent.setPackage(pickerPackage);
|
|
|
|
|
}
|
2020-08-03 12:54:21 -07:00
|
|
|
return launcher.startActivitySafely(v, intent, placeholderInfo(intent));
|
2020-06-06 14:44:27 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 12:54:21 -07:00
|
|
|
static WorkspaceItemInfo placeholderInfo(Intent intent) {
|
|
|
|
|
WorkspaceItemInfo placeholderInfo = new WorkspaceItemInfo();
|
|
|
|
|
placeholderInfo.intent = intent;
|
|
|
|
|
placeholderInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
|
|
|
|
|
placeholderInfo.container = LauncherSettings.Favorites.CONTAINER_SETTINGS;
|
|
|
|
|
return placeholderInfo;
|
2018-03-30 17:10:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class OptionItem {
|
|
|
|
|
|
2021-04-01 16:04:44 -07:00
|
|
|
// Used to create AccessibilityNodeInfo in AccessibilityActionsView.java.
|
2020-07-20 12:38:01 -07:00
|
|
|
public final int labelRes;
|
2021-04-01 16:04:44 -07:00
|
|
|
|
|
|
|
|
public final CharSequence label;
|
|
|
|
|
public final Drawable icon;
|
2020-07-20 12:38:01 -07:00
|
|
|
public final EventEnum eventId;
|
|
|
|
|
public final OnLongClickListener clickListener;
|
2018-03-30 17:10:13 -07:00
|
|
|
|
2021-04-01 16:04:44 -07:00
|
|
|
public OptionItem(Context context, int labelRes, int iconRes, EventEnum eventId,
|
2021-10-25 15:19:38 -05:00
|
|
|
OnLongClickListener clickListener) {
|
2020-07-20 12:38:01 -07:00
|
|
|
this.labelRes = labelRes;
|
2021-04-01 16:04:44 -07:00
|
|
|
this.label = context.getText(labelRes);
|
|
|
|
|
this.icon = ContextCompat.getDrawable(context, iconRes);
|
|
|
|
|
this.eventId = eventId;
|
|
|
|
|
this.clickListener = clickListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionItem(CharSequence label, Drawable icon, EventEnum eventId,
|
2021-10-25 15:19:38 -05:00
|
|
|
OnLongClickListener clickListener) {
|
2021-04-01 16:04:44 -07:00
|
|
|
this.labelRes = 0;
|
|
|
|
|
this.label = label;
|
|
|
|
|
this.icon = icon;
|
2020-07-20 12:38:01 -07:00
|
|
|
this.eventId = eventId;
|
|
|
|
|
this.clickListener = clickListener;
|
2018-03-30 17:10:13 -07:00
|
|
|
}
|
2018-03-07 19:58:07 -08:00
|
|
|
}
|
2022-11-04 16:43:42 -07:00
|
|
|
|
|
|
|
|
private static boolean styleWallpapersExists(Context context) {
|
|
|
|
|
return context.getPackageManager().resolveActivity(
|
|
|
|
|
PackageManagerHelper.getStyleWallpapersIntent(context), 0) != null;
|
|
|
|
|
}
|
2018-01-19 11:24:32 -08:00
|
|
|
}
|