Merge "Make Linux terminal option profile aware" into main
This commit is contained in:
@@ -72,6 +72,7 @@ import com.android.settings.development.bluetooth.BluetoothQualityDialogPreferen
|
||||
import com.android.settings.development.bluetooth.BluetoothSampleRateDialogPreferenceController;
|
||||
import com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController;
|
||||
import com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController;
|
||||
import com.android.settings.development.linuxterminal.LinuxTerminalPreferenceController;
|
||||
import com.android.settings.development.qstile.DevelopmentTiles;
|
||||
import com.android.settings.development.storage.SharedDataPreferenceController;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
/*
|
||||
* Copyright 2024 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.settings.development;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.TwoStatePreference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
||||
|
||||
public class LinuxTerminalPreferenceController extends DeveloperOptionsPreferenceController
|
||||
implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
|
||||
private static final String TAG = "LinuxTerminalPrefCtrl";
|
||||
|
||||
private static final String ENABLE_TERMINAL_KEY = "enable_linux_terminal";
|
||||
|
||||
@NonNull
|
||||
private final PackageManager mPackageManager;
|
||||
|
||||
@Nullable
|
||||
private final String mTerminalPackageName;
|
||||
|
||||
public LinuxTerminalPreferenceController(@NonNull Context context) {
|
||||
super(context);
|
||||
mPackageManager = mContext.getPackageManager();
|
||||
|
||||
String packageName = mContext.getString(R.string.config_linux_terminal_app_package_name);
|
||||
mTerminalPackageName =
|
||||
isPackageInstalled(mPackageManager, packageName) ? packageName : null;
|
||||
|
||||
Log.d(TAG, "Terminal app package name=" + packageName + ", isAvailable=" + isAvailable());
|
||||
}
|
||||
|
||||
// Avoid lazy initialization because this may be called before displayPreference().
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
// Returns true only if the terminal app is installed which only happens when the build flag
|
||||
// RELEASE_AVF_SUPPORT_CUSTOM_VM_WITH_PARAVIRTUALIZED_DEVICES is true.
|
||||
// TODO(b/343795511): Add explicitly check for the flag when it's accessible from Java code.
|
||||
return getTerminalPackageName() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public String getPreferenceKey() {
|
||||
return ENABLE_TERMINAL_KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(@NonNull PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference.setEnabled(isAvailable());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(
|
||||
@NonNull Preference preference, @NonNull Object newValue) {
|
||||
String packageName = getTerminalPackageName();
|
||||
if (packageName == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean terminalEnabled = (Boolean) newValue;
|
||||
int state = terminalEnabled
|
||||
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
: PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
|
||||
mPackageManager.setApplicationEnabledSetting(packageName, state, /* flags=*/ 0);
|
||||
((TwoStatePreference) mPreference).setChecked(terminalEnabled);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(@NonNull Preference preference) {
|
||||
String packageName = getTerminalPackageName();
|
||||
if (packageName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isTerminalEnabled = mPackageManager.getApplicationEnabledSetting(packageName)
|
||||
== PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
|
||||
((TwoStatePreference) mPreference).setChecked(isTerminalEnabled);
|
||||
}
|
||||
|
||||
// Can be mocked for testing
|
||||
@VisibleForTesting
|
||||
@Nullable
|
||||
String getTerminalPackageName() {
|
||||
return mTerminalPackageName;
|
||||
}
|
||||
|
||||
private static boolean isPackageInstalled(PackageManager manager, String packageName) {
|
||||
if (TextUtils.isEmpty(packageName)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return manager.getPackageInfo(
|
||||
packageName,
|
||||
PackageManager.MATCH_ALL | PackageManager.MATCH_DISABLED_COMPONENTS) != null;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright 2024 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.settings.development.linuxterminal;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserHandle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.CompoundButton;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.widget.SettingsMainSwitchPreference;
|
||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
|
||||
/** Preference controller for enable/disable toggle of the linux terminal */
|
||||
public class EnableLinuxTerminalPreferenceController extends BasePreferenceController
|
||||
implements CompoundButton.OnCheckedChangeListener, PreferenceControllerMixin {
|
||||
@VisibleForTesting
|
||||
static final int TERMINAL_PACKAGE_NAME_RESID = R.string.config_linux_terminal_app_package_name;
|
||||
|
||||
private static final String TAG = "LinuxTerminalPrefCtrl";
|
||||
|
||||
private static final String ENABLE_TERMINAL_KEY = "enable_linux_terminal";
|
||||
|
||||
@NonNull private final PackageManager mPackageManager;
|
||||
private final boolean mIsPrimaryUser;
|
||||
@Nullable private final String mTerminalPackageName;
|
||||
|
||||
@Nullable private SettingsMainSwitchPreference mPreference;
|
||||
|
||||
public EnableLinuxTerminalPreferenceController(
|
||||
@NonNull Context context, @NonNull Context userAwareContext, int userId) {
|
||||
this(context, userAwareContext, userId == UserHandle.myUserId());
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
EnableLinuxTerminalPreferenceController(
|
||||
@NonNull Context context, @NonNull Context userAwareContext, boolean isPrimaryUser) {
|
||||
super(context, ENABLE_TERMINAL_KEY);
|
||||
|
||||
mPackageManager = userAwareContext.getPackageManager();
|
||||
mIsPrimaryUser = isPrimaryUser;
|
||||
|
||||
String packageName =
|
||||
userAwareContext.getString(R.string.config_linux_terminal_app_package_name);
|
||||
mTerminalPackageName =
|
||||
isPackageInstalled(mPackageManager, packageName) ? packageName : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(@NonNull PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
if (mPreference != null) {
|
||||
mPreference.addOnSwitchChangeListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckedChanged(@NonNull CompoundButton buttonView, boolean isChecked) {
|
||||
if (mTerminalPackageName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
int state =
|
||||
isChecked
|
||||
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
: PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
|
||||
mPackageManager.setApplicationEnabledSetting(mTerminalPackageName, state, /* flags= */ 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("NullAway") // setDisabledByAdmin(EnforcedAdmin) doesn't have @Nullable
|
||||
public void updateState(@NonNull Preference preference) {
|
||||
if (mPreference != preference) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isInstalled = (mTerminalPackageName != null);
|
||||
if (isInstalled) {
|
||||
mPreference.setDisabledByAdmin(/* admin= */ null);
|
||||
mPreference.setEnabled(/* enabled= */ true);
|
||||
boolean terminalEnabled =
|
||||
mPackageManager.getApplicationEnabledSetting(mTerminalPackageName)
|
||||
== PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
|
||||
mPreference.setChecked(terminalEnabled);
|
||||
} else {
|
||||
if (mIsPrimaryUser) {
|
||||
Log.e(TAG, "Terminal app doesn't exist for primary user but UI was shown");
|
||||
mPreference.setDisabledByAdmin(/* admin= */ null);
|
||||
mPreference.setEnabled(/* enabled= */ false);
|
||||
} else {
|
||||
// If admin hasn't enabled the system app, mark it as disabled by admin.
|
||||
mPreference.setDisabledByAdmin(new EnforcedAdmin());
|
||||
// Make it enabled, so clicking it would show error dialog.
|
||||
mPreference.setEnabled(/* enabled= */ true);
|
||||
}
|
||||
mPreference.setChecked(/* checked= */ false);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isPackageInstalled(PackageManager manager, String packageName) {
|
||||
if (TextUtils.isEmpty(packageName)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return manager.getPackageInfo(
|
||||
packageName,
|
||||
PackageManager.MATCH_ALL | PackageManager.MATCH_DISABLED_COMPONENTS)
|
||||
!= null;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2024 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.settings.development.linuxterminal;
|
||||
|
||||
import static android.content.Intent.EXTRA_USER_ID;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.os.UserHandle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.development.DevelopmentSettingsEnabler;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Fragment shown for 'Linux terminal development' preference in developer option. */
|
||||
@SearchIndexable
|
||||
public class LinuxTerminalDashboardFragment extends DashboardFragment {
|
||||
private static final String TAG = "LinuxTerminalFrag";
|
||||
|
||||
private Context mUserAwareContext;
|
||||
|
||||
private int mUserId;
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return SettingsEnums.LINUX_TERMINAL_DASHBOARD;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPreferenceScreenResId() {
|
||||
return R.xml.linux_terminal_settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(@NonNull Context context) {
|
||||
// Initialize mUserId and mUserAwareContext before super.onAttach(),
|
||||
// so createPreferenceControllers() can be called with proper values from super.onAttach().
|
||||
int currentUserId = UserHandle.myUserId();
|
||||
mUserId = getArguments().getInt(EXTRA_USER_ID, currentUserId);
|
||||
mUserAwareContext =
|
||||
(currentUserId == mUserId)
|
||||
? context
|
||||
: context.createContextAsUser(UserHandle.of(mUserId), /* flags= */ 0);
|
||||
|
||||
// Note: This calls createPreferenceControllers() inside.
|
||||
super.onAttach(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public List<AbstractPreferenceController> createPreferenceControllers(
|
||||
@NonNull Context context) {
|
||||
List<AbstractPreferenceController> list = new ArrayList<>();
|
||||
list.add(new EnableLinuxTerminalPreferenceController(context, mUserAwareContext, mUserId));
|
||||
return list;
|
||||
}
|
||||
|
||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider(R.xml.linux_terminal_settings) {
|
||||
|
||||
@Override
|
||||
protected boolean isPageSearchEnabled(Context context) {
|
||||
return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2024 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.settings.development.linuxterminal;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
||||
|
||||
/** Preference controller for Linux terminal option in developers option */
|
||||
public class LinuxTerminalPreferenceController extends DeveloperOptionsPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
@VisibleForTesting
|
||||
static final int TERMINAL_PACKAGE_NAME_RESID = R.string.config_linux_terminal_app_package_name;
|
||||
|
||||
private static final String LINUX_TERMINAL_KEY = "linux_terminal";
|
||||
|
||||
@Nullable private final String mTerminalPackageName;
|
||||
|
||||
public LinuxTerminalPreferenceController(@NonNull Context context) {
|
||||
super(context);
|
||||
String packageName = context.getString(TERMINAL_PACKAGE_NAME_RESID);
|
||||
mTerminalPackageName =
|
||||
isPackageInstalled(context.getPackageManager(), packageName) ? packageName : null;
|
||||
}
|
||||
|
||||
// Avoid lazy initialization because this may be called before displayPreference().
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
// Returns true only if the terminal app is installed which only happens when the build flag
|
||||
// RELEASE_AVF_SUPPORT_CUSTOM_VM_WITH_PARAVIRTUALIZED_DEVICES is true.
|
||||
// TODO(b/343795511): Add explicitly check for the flag when it's accessible from Java code.
|
||||
return mTerminalPackageName != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public String getPreferenceKey() {
|
||||
return LINUX_TERMINAL_KEY;
|
||||
}
|
||||
|
||||
private static boolean isPackageInstalled(PackageManager manager, String packageName) {
|
||||
if (TextUtils.isEmpty(packageName)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return manager.getPackageInfo(
|
||||
packageName,
|
||||
PackageManager.MATCH_ALL | PackageManager.MATCH_DISABLED_COMPONENTS)
|
||||
!= null;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user