2018-10-05 15:11:21 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
|
*
|
2018-10-24 16:20:37 -07:00
|
|
|
* 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
|
2018-10-05 15:11:21 -07:00
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
2018-10-24 16:20:37 -07:00
|
|
|
* 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-10-05 15:11:21 -07:00
|
|
|
*/
|
2018-10-24 16:20:37 -07:00
|
|
|
package com.android.launcher3.settings;
|
2018-10-05 15:11:21 -07:00
|
|
|
|
2019-11-08 13:43:58 -08:00
|
|
|
import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
|
|
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
import static com.android.launcher3.uioverrides.plugins.PluginManagerWrapper.PLUGIN_CHANGED;
|
|
|
|
|
import static com.android.launcher3.uioverrides.plugins.PluginManagerWrapper.pluginEnabledKey;
|
2018-10-05 15:11:21 -07:00
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
import android.annotation.TargetApi;
|
2018-10-05 15:11:21 -07:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
2019-11-08 13:43:58 -08:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2018-10-05 15:11:21 -07:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.content.pm.ResolveInfo;
|
2019-11-08 13:43:58 -08:00
|
|
|
import android.content.pm.ServiceInfo;
|
2018-10-05 15:11:21 -07:00
|
|
|
import android.net.Uri;
|
2018-10-24 16:20:37 -07:00
|
|
|
import android.os.Build;
|
2018-10-05 15:11:21 -07:00
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.provider.Settings;
|
|
|
|
|
import android.util.ArrayMap;
|
2019-11-08 13:43:58 -08:00
|
|
|
import android.util.Pair;
|
2018-10-24 16:20:37 -07:00
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
|
import android.view.MenuItem;
|
2018-10-05 15:11:21 -07:00
|
|
|
import android.view.View;
|
|
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
import androidx.preference.Preference;
|
|
|
|
|
import androidx.preference.PreferenceCategory;
|
|
|
|
|
import androidx.preference.PreferenceDataStore;
|
2019-12-09 14:36:18 -08:00
|
|
|
import androidx.preference.PreferenceFragmentCompat;
|
2018-10-24 16:20:37 -07:00
|
|
|
import androidx.preference.PreferenceScreen;
|
|
|
|
|
import androidx.preference.PreferenceViewHolder;
|
|
|
|
|
import androidx.preference.SwitchPreference;
|
|
|
|
|
|
2019-11-08 13:43:58 -08:00
|
|
|
import com.android.launcher3.R;
|
|
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
|
|
|
|
import com.android.launcher3.config.FlagTogglerPrefUi;
|
|
|
|
|
import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
2018-10-05 15:11:21 -07:00
|
|
|
/**
|
2018-10-24 16:20:37 -07:00
|
|
|
* Dev-build only UI allowing developers to toggle flag settings and plugins.
|
|
|
|
|
* See {@link FeatureFlags}.
|
2018-10-05 15:11:21 -07:00
|
|
|
*/
|
2018-10-24 16:20:37 -07:00
|
|
|
@TargetApi(Build.VERSION_CODES.O)
|
2019-12-09 14:36:18 -08:00
|
|
|
public class DeveloperOptionsFragment extends PreferenceFragmentCompat {
|
2018-10-05 15:11:21 -07:00
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
private static final String ACTION_PLUGIN_SETTINGS = "com.android.systemui.action.PLUGIN_SETTINGS";
|
2018-10-05 15:11:21 -07:00
|
|
|
private static final String PLUGIN_PERMISSION = "com.android.systemui.permission.PLUGIN";
|
|
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
private final BroadcastReceiver mPluginReceiver = new BroadcastReceiver() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
loadPluginPrefs();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private PreferenceScreen mPreferenceScreen;
|
|
|
|
|
|
|
|
|
|
private PreferenceCategory mPluginsCategory;
|
|
|
|
|
private FlagTogglerPrefUi mFlagTogglerPrefUi;
|
2018-10-05 15:11:21 -07:00
|
|
|
|
|
|
|
|
@Override
|
2018-10-24 16:20:37 -07:00
|
|
|
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
2018-10-05 15:11:21 -07:00
|
|
|
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
|
|
|
|
|
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
|
|
|
|
|
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
|
|
|
|
filter.addDataScheme("package");
|
2018-10-24 16:20:37 -07:00
|
|
|
getContext().registerReceiver(mPluginReceiver, filter);
|
|
|
|
|
getContext().registerReceiver(mPluginReceiver,
|
|
|
|
|
new IntentFilter(Intent.ACTION_USER_UNLOCKED));
|
|
|
|
|
|
|
|
|
|
mPreferenceScreen = getPreferenceManager().createPreferenceScreen(getContext());
|
|
|
|
|
setPreferenceScreen(mPreferenceScreen);
|
2018-10-05 15:11:21 -07:00
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
initFlags();
|
|
|
|
|
loadPluginPrefs();
|
2020-02-19 23:48:21 +00:00
|
|
|
maybeAddSandboxCategory();
|
2018-10-05 15:11:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
super.onDestroy();
|
2018-10-24 16:20:37 -07:00
|
|
|
getContext().unregisterReceiver(mPluginReceiver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PreferenceCategory newCategory(String title) {
|
|
|
|
|
PreferenceCategory category = new PreferenceCategory(getContext());
|
|
|
|
|
category.setOrder(Preference.DEFAULT_ORDER);
|
|
|
|
|
category.setTitle(title);
|
|
|
|
|
mPreferenceScreen.addPreference(category);
|
|
|
|
|
return category;
|
2018-10-05 15:11:21 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
private void initFlags() {
|
|
|
|
|
if (!FeatureFlags.showFlagTogglerUi(getContext())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mFlagTogglerPrefUi = new FlagTogglerPrefUi(this);
|
|
|
|
|
mFlagTogglerPrefUi.applyTo(newCategory("Feature flags"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
|
if (mFlagTogglerPrefUi != null) {
|
|
|
|
|
mFlagTogglerPrefUi.onCreateOptionsMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
if (mFlagTogglerPrefUi != null) {
|
|
|
|
|
mFlagTogglerPrefUi.onOptionsItemSelected(item);
|
|
|
|
|
}
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onStop() {
|
|
|
|
|
if (mFlagTogglerPrefUi != null) {
|
|
|
|
|
mFlagTogglerPrefUi.onStop();
|
|
|
|
|
}
|
|
|
|
|
super.onStop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadPluginPrefs() {
|
|
|
|
|
if (mPluginsCategory != null) {
|
|
|
|
|
mPreferenceScreen.removePreference(mPluginsCategory);
|
|
|
|
|
}
|
|
|
|
|
if (!PluginManagerWrapper.hasPlugins(getActivity())) {
|
|
|
|
|
mPluginsCategory = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mPluginsCategory = newCategory("Plugins");
|
|
|
|
|
|
|
|
|
|
PluginManagerWrapper manager = PluginManagerWrapper.INSTANCE.get(getContext());
|
2018-10-05 15:11:21 -07:00
|
|
|
Context prefContext = getContext();
|
|
|
|
|
PackageManager pm = getContext().getPackageManager();
|
|
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
Set<String> pluginActions = manager.getPluginActions();
|
2019-11-08 13:43:58 -08:00
|
|
|
|
|
|
|
|
ArrayMap<Pair<String, String>, ArrayList<Pair<String, ServiceInfo>>> plugins =
|
|
|
|
|
new ArrayMap<>();
|
|
|
|
|
|
|
|
|
|
Set<String> pluginPermissionApps = pm.getPackagesHoldingPermissions(
|
|
|
|
|
new String[]{PLUGIN_PERMISSION}, MATCH_DISABLED_COMPONENTS)
|
|
|
|
|
.stream()
|
|
|
|
|
.map(pi -> pi.packageName)
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
|
2018-10-05 15:11:21 -07:00
|
|
|
for (String action : pluginActions) {
|
|
|
|
|
String name = toName(action);
|
|
|
|
|
List<ResolveInfo> result = pm.queryIntentServices(
|
2019-11-08 13:43:58 -08:00
|
|
|
new Intent(action), MATCH_DISABLED_COMPONENTS);
|
2018-10-05 15:11:21 -07:00
|
|
|
for (ResolveInfo info : result) {
|
|
|
|
|
String packageName = info.serviceInfo.packageName;
|
2019-11-08 13:43:58 -08:00
|
|
|
if (!pluginPermissionApps.contains(packageName)) {
|
|
|
|
|
continue;
|
2018-10-05 15:11:21 -07:00
|
|
|
}
|
2019-11-08 13:43:58 -08:00
|
|
|
|
|
|
|
|
Pair<String, String> key = Pair.create(packageName, info.serviceInfo.processName);
|
|
|
|
|
if (!plugins.containsKey(key)) {
|
|
|
|
|
plugins.put(key, new ArrayList<>());
|
|
|
|
|
}
|
|
|
|
|
plugins.get(key).add(Pair.create(name, info.serviceInfo));
|
2018-10-05 15:11:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 13:43:58 -08:00
|
|
|
PreferenceDataStore enabler = manager.getPluginEnabler();
|
|
|
|
|
plugins.forEach((key, si) -> {
|
|
|
|
|
String packageName = key.first;
|
|
|
|
|
List<ComponentName> componentNames = si.stream()
|
|
|
|
|
.map(p -> new ComponentName(packageName, p.second.name))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
if (!componentNames.isEmpty()) {
|
|
|
|
|
SwitchPreference pref = new PluginPreference(
|
|
|
|
|
prefContext, si.get(0).second.applicationInfo, enabler, componentNames);
|
|
|
|
|
pref.setSummary("Plugins: "
|
|
|
|
|
+ si.stream().map(p -> p.first).collect(Collectors.joining(", ")));
|
|
|
|
|
mPluginsCategory.addPreference(pref);
|
2018-10-05 15:11:21 -07:00
|
|
|
}
|
2019-11-08 13:43:58 -08:00
|
|
|
});
|
2018-10-05 15:11:21 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 23:48:21 +00:00
|
|
|
private void maybeAddSandboxCategory() {
|
|
|
|
|
Context context = getContext();
|
|
|
|
|
if (context == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Intent launchSandboxIntent =
|
|
|
|
|
new Intent("com.android.quickstep.action.GESTURE_SANDBOX")
|
|
|
|
|
.setPackage(context.getPackageName())
|
|
|
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
if (launchSandboxIntent.resolveActivity(context.getPackageManager()) == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-15 20:25:32 +00:00
|
|
|
PreferenceCategory sandboxCategory = newCategory("Gesture Navigation Sandbox");
|
|
|
|
|
sandboxCategory.setSummary("Learn and practice navigation gestures");
|
|
|
|
|
Preference launchBackTutorialPreference = new Preference(context);
|
|
|
|
|
launchBackTutorialPreference.setKey("launchBackTutorial");
|
|
|
|
|
launchBackTutorialPreference.setTitle("Launch Back Tutorial");
|
|
|
|
|
launchBackTutorialPreference.setSummary("Learn how to use the Back gesture");
|
|
|
|
|
launchBackTutorialPreference.setOnPreferenceClickListener(preference -> {
|
|
|
|
|
startActivity(launchSandboxIntent.putExtra(
|
|
|
|
|
"tutorial_type", "RIGHT_EDGE_BACK_NAVIGATION"));
|
2020-02-19 23:48:21 +00:00
|
|
|
return true;
|
|
|
|
|
});
|
2020-04-15 20:25:32 +00:00
|
|
|
sandboxCategory.addPreference(launchBackTutorialPreference);
|
|
|
|
|
Preference launchHomeTutorialPreference = new Preference(context);
|
|
|
|
|
launchHomeTutorialPreference.setKey("launchHomeTutorial");
|
|
|
|
|
launchHomeTutorialPreference.setTitle("Launch Home Tutorial");
|
|
|
|
|
launchHomeTutorialPreference.setSummary("Learn how to use the Home gesture");
|
|
|
|
|
launchHomeTutorialPreference.setOnPreferenceClickListener(preference -> {
|
|
|
|
|
startActivity(launchSandboxIntent.putExtra("tutorial_type", "HOME_NAVIGATION"));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
sandboxCategory.addPreference(launchHomeTutorialPreference);
|
2020-02-19 23:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-05 15:11:21 -07:00
|
|
|
private String toName(String action) {
|
2019-11-08 13:43:58 -08:00
|
|
|
String str = action.replace("com.android.systemui.action.PLUGIN_", "")
|
|
|
|
|
.replace("com.android.launcher3.action.PLUGIN_", "");
|
2018-10-05 15:11:21 -07:00
|
|
|
StringBuilder b = new StringBuilder();
|
|
|
|
|
for (String s : str.split("_")) {
|
|
|
|
|
if (b.length() != 0) {
|
|
|
|
|
b.append(' ');
|
|
|
|
|
}
|
|
|
|
|
b.append(s.substring(0, 1));
|
|
|
|
|
b.append(s.substring(1).toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
return b.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class PluginPreference extends SwitchPreference {
|
|
|
|
|
private final boolean mHasSettings;
|
2018-10-24 16:20:37 -07:00
|
|
|
private final PreferenceDataStore mPluginEnabler;
|
2019-11-08 13:43:58 -08:00
|
|
|
private final String mPackageName;
|
|
|
|
|
private final List<ComponentName> mComponentNames;
|
2018-10-05 15:11:21 -07:00
|
|
|
|
2019-11-08 13:43:58 -08:00
|
|
|
PluginPreference(Context prefContext, ApplicationInfo info,
|
|
|
|
|
PreferenceDataStore pluginEnabler, List<ComponentName> componentNames) {
|
2018-10-05 15:11:21 -07:00
|
|
|
super(prefContext);
|
|
|
|
|
PackageManager pm = prefContext.getPackageManager();
|
|
|
|
|
mHasSettings = pm.resolveActivity(new Intent(ACTION_PLUGIN_SETTINGS)
|
|
|
|
|
.setPackage(info.packageName), 0) != null;
|
2019-11-08 13:43:58 -08:00
|
|
|
mPackageName = info.packageName;
|
|
|
|
|
mComponentNames = componentNames;
|
2018-10-05 15:11:21 -07:00
|
|
|
mPluginEnabler = pluginEnabler;
|
2019-11-08 13:43:58 -08:00
|
|
|
setTitle(info.loadLabel(pm));
|
2018-10-05 15:11:21 -07:00
|
|
|
setChecked(isPluginEnabled());
|
|
|
|
|
setWidgetLayoutResource(R.layout.switch_preference_with_settings);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 16:20:37 -07:00
|
|
|
private boolean isEnabled(ComponentName cn) {
|
|
|
|
|
return mPluginEnabler.getBoolean(pluginEnabledKey(cn), true);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 15:11:21 -07:00
|
|
|
private boolean isPluginEnabled() {
|
2019-11-08 13:43:58 -08:00
|
|
|
for (ComponentName componentName : mComponentNames) {
|
2018-10-24 16:20:37 -07:00
|
|
|
if (!isEnabled(componentName)) {
|
2018-10-05 15:11:21 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean persistBoolean(boolean isEnabled) {
|
|
|
|
|
boolean shouldSendBroadcast = false;
|
2019-11-08 13:43:58 -08:00
|
|
|
for (ComponentName componentName : mComponentNames) {
|
2018-10-24 16:20:37 -07:00
|
|
|
if (isEnabled(componentName) != isEnabled) {
|
|
|
|
|
mPluginEnabler.putBoolean(pluginEnabledKey(componentName), isEnabled);
|
2018-10-05 15:11:21 -07:00
|
|
|
shouldSendBroadcast = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (shouldSendBroadcast) {
|
2019-11-08 13:43:58 -08:00
|
|
|
final String pkg = mPackageName;
|
2018-10-24 16:20:37 -07:00
|
|
|
final Intent intent = new Intent(PLUGIN_CHANGED,
|
2018-10-05 15:11:21 -07:00
|
|
|
pkg != null ? Uri.fromParts("package", pkg, null) : null);
|
|
|
|
|
getContext().sendBroadcast(intent);
|
|
|
|
|
}
|
|
|
|
|
setChecked(isEnabled);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-10-24 16:20:37 -07:00
|
|
|
public void onBindViewHolder(PreferenceViewHolder holder) {
|
|
|
|
|
super.onBindViewHolder(holder);
|
|
|
|
|
holder.findViewById(R.id.settings).setVisibility(mHasSettings ? View.VISIBLE
|
2018-10-05 15:11:21 -07:00
|
|
|
: View.GONE);
|
2018-10-24 16:20:37 -07:00
|
|
|
holder.findViewById(R.id.divider).setVisibility(mHasSettings ? View.VISIBLE
|
2018-10-05 15:11:21 -07:00
|
|
|
: View.GONE);
|
2018-10-24 16:20:37 -07:00
|
|
|
holder.findViewById(R.id.settings).setOnClickListener(v -> {
|
2018-10-05 15:11:21 -07:00
|
|
|
ResolveInfo result = v.getContext().getPackageManager().resolveActivity(
|
2019-11-08 13:43:58 -08:00
|
|
|
new Intent(ACTION_PLUGIN_SETTINGS).setPackage(mPackageName), 0);
|
2018-10-05 15:11:21 -07:00
|
|
|
if (result != null) {
|
|
|
|
|
v.getContext().startActivity(new Intent().setComponent(
|
|
|
|
|
new ComponentName(result.activityInfo.packageName,
|
|
|
|
|
result.activityInfo.name)));
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-10-24 16:20:37 -07:00
|
|
|
holder.itemView.setOnLongClickListener(v -> {
|
2018-10-05 15:11:21 -07:00
|
|
|
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
2019-11-08 13:43:58 -08:00
|
|
|
intent.setData(Uri.fromParts("package", mPackageName, null));
|
2018-10-05 15:11:21 -07:00
|
|
|
getContext().startActivity(intent);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|