Initial implementation of 'cmd device_policy'.
For now it only provides is-operation-safe, but in the long term it will replace `dpm` Test: adb shell cmd device_policy is-operation-safe 1 Bug: 172376923 Bug: 173381875 Change-Id: I730d9a098ba83f5813ad9f5370c680c2c89b0230
This commit is contained in:
@@ -218,8 +218,10 @@ import android.os.Process;
|
||||
import android.os.RecoverySystem;
|
||||
import android.os.RemoteCallback;
|
||||
import android.os.RemoteException;
|
||||
import android.os.ResultReceiver;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.ServiceSpecificException;
|
||||
import android.os.ShellCallback;
|
||||
import android.os.SystemClock;
|
||||
import android.os.SystemProperties;
|
||||
import android.os.UserHandle;
|
||||
@@ -8727,6 +8729,14 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
|
||||
pw.decreaseIndent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
|
||||
String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
|
||||
new DevicePolicyManagerServiceShellCommand(DevicePolicyManagerService.this).exec(
|
||||
this, in, out, err, args, callback, resultReceiver);
|
||||
|
||||
}
|
||||
|
||||
private String getEncryptionStatusName(int encryptionStatus) {
|
||||
switch (encryptionStatus) {
|
||||
case DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE:
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.server.devicepolicy;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.os.ShellCommand;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Objects;
|
||||
|
||||
final class DevicePolicyManagerServiceShellCommand extends ShellCommand {
|
||||
|
||||
private static final String CMD_IS_SAFE_OPERATION = "is-operation-safe";
|
||||
|
||||
private final DevicePolicyManagerService mService;
|
||||
|
||||
DevicePolicyManagerServiceShellCommand(DevicePolicyManagerService service) {
|
||||
mService = Objects.requireNonNull(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHelp() {
|
||||
try (PrintWriter pw = getOutPrintWriter();) {
|
||||
pw.printf("DevicePolicyManager Service (device_policy) commands:\n\n");
|
||||
showHelp(pw);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onCommand(String cmd) {
|
||||
if (cmd == null) {
|
||||
return handleDefaultCommands(cmd);
|
||||
}
|
||||
try (PrintWriter pw = getOutPrintWriter();) {
|
||||
switch (cmd) {
|
||||
case CMD_IS_SAFE_OPERATION:
|
||||
return runIsSafeOperation(pw);
|
||||
default:
|
||||
return onInvalidCommand(pw, cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int onInvalidCommand(PrintWriter pw, String cmd) {
|
||||
if (super.handleDefaultCommands(cmd) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
pw.println("Usage: ");
|
||||
showHelp(pw);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
private void showHelp(PrintWriter pw) {
|
||||
pw.printf(" help\n");
|
||||
pw.printf(" Prints this help text.\n\n");
|
||||
pw.printf(" %s <OPERATION_ID>\n", CMD_IS_SAFE_OPERATION);
|
||||
pw.printf(" Checks if the give operation is safe \n\n");
|
||||
}
|
||||
|
||||
private int runIsSafeOperation(PrintWriter pw) {
|
||||
int operation = Integer.parseInt(getNextArgRequired());
|
||||
boolean safe = mService.canExecute(operation);
|
||||
pw.printf("Operation %s is %s\n", DevicePolicyManager.operationToString(operation),
|
||||
safe ? "SAFE" : "UNSAFE");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user