1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.car.testdpc; 17 18 import android.app.admin.DeviceAdminService; 19 import android.app.admin.DevicePolicyManager; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.os.Process; 23 import android.util.Log; 24 25 import java.io.FileDescriptor; 26 import java.io.PrintWriter; 27 import java.util.Arrays; 28 import java.util.Set; 29 30 /** 31 * Service that handles calls to Device Policy Manager from shell. 32 * 33 * <p>This service handles binding of remote device policy managers to the current user 34 * by checking if current user is device or profile owner. If it is either then it accordingly 35 * binds and assigns all affiliated users to the appropriate {@code DevicePolicyManagerInterface} 36 * implementation ({@code RemoteDevicePolicyManager}/{@code LocalDevicePolicyManager}) 37 */ 38 39 public final class DpcService extends DeviceAdminService { 40 41 private static final String TAG = DpcService.class.getSimpleName(); 42 private static final Set<String> AFFILIATION_IDS = Set.of("42"); 43 44 private ComponentName mAdmin; 45 private Context mContext; 46 private DpcFactory mDpcFactory; 47 private DevicePolicyManager mDpm; 48 49 @Override onCreate()50 public void onCreate() { 51 super.onCreate(); 52 53 Log.d(TAG, "Service created (on user " + Process.myUserHandle() + ")"); 54 55 mContext = getApplicationContext(); 56 mAdmin = DpcReceiver.getComponentName(mContext); 57 mDpm = mContext.getSystemService(DevicePolicyManager.class); 58 59 mDpm.setAffiliationIds(mAdmin, AFFILIATION_IDS); 60 Log.i(TAG, "setAffiliationIds(" + mAdmin.flattenToShortString() + ", " 61 + AFFILIATION_IDS + ")"); 62 63 mDpcFactory = new DpcFactory(mContext); 64 } 65 66 @Override dump(FileDescriptor fd, PrintWriter writer, String[] args)67 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 68 Log.d(TAG, "dump(): " + Arrays.toString(args)); 69 printInternalState(writer); 70 if (args != null && args.length > 0) { 71 switch (args[0]) { 72 case "cmd": 73 String[] cmdArgs = new String[args.length - 1]; 74 System.arraycopy(args, 1, cmdArgs, 0, args.length - 1); 75 new DpcShellCommand(this, mDpcFactory, writer, cmdArgs).run(); 76 return; 77 default: 78 break; 79 } 80 } 81 } 82 printInternalState(PrintWriter writer)83 private void printInternalState(PrintWriter writer) { 84 writer.printf("Current User: %s\n", Process.myUserHandle()); 85 writer.printf("Admin Component Name: %s\n", mAdmin.flattenToShortString()); 86 writer.printf("isProfileOwner()? %b\n", mDpm.isProfileOwnerApp(mContext.getPackageName())); 87 writer.printf("isDeviceOwner()? %b\n", mDpm.isDeviceOwnerApp(mContext.getPackageName())); 88 writer.printf("AFFILIATION_IDS=%s\n\n", AFFILIATION_IDS); 89 } 90 } 91