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 17 package com.android.server.resources; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.content.res.IResourcesManager; 23 import android.os.Binder; 24 import android.os.IBinder; 25 import android.os.ParcelFileDescriptor; 26 import android.os.Process; 27 import android.os.RemoteCallback; 28 import android.os.RemoteException; 29 30 import com.android.server.SystemService; 31 import com.android.server.am.ActivityManagerService; 32 33 import java.io.FileDescriptor; 34 import java.io.PrintWriter; 35 36 /** 37 * A service for managing information about ResourcesManagers 38 */ 39 public class ResourcesManagerService extends SystemService { 40 private ActivityManagerService mActivityManagerService; 41 42 /** 43 * Initializes the system service. 44 * <p> 45 * Subclasses must define a single argument constructor that accepts the context 46 * and passes it to super. 47 * </p> 48 * 49 * @param context The system server context. 50 */ ResourcesManagerService(@onNull Context context)51 public ResourcesManagerService(@NonNull Context context) { 52 super(context); 53 publishBinderService(Context.RESOURCES_SERVICE, mService); 54 } 55 56 @Override onStart()57 public void onStart() { 58 // Intentionally left empty. 59 } 60 61 private final IBinder mService = new IResourcesManager.Stub() { 62 @Override 63 public boolean dumpResources(String process, ParcelFileDescriptor fd, 64 RemoteCallback callback) throws RemoteException { 65 final int callingUid = Binder.getCallingUid(); 66 if (callingUid != Process.ROOT_UID && callingUid != Process.SHELL_UID) { 67 callback.sendResult(null); 68 throw new SecurityException("dump should only be called by shell"); 69 } 70 return mActivityManagerService.dumpResources(process, fd, callback); 71 } 72 73 @Override 74 protected void dump(@NonNull FileDescriptor fd, 75 @NonNull PrintWriter pw, @Nullable String[] args) { 76 try { 77 mActivityManagerService.dumpAllResources(ParcelFileDescriptor.dup(fd), pw); 78 } catch (Exception e) { 79 pw.println("Exception while trying to dump all resources: " + e.getMessage()); 80 e.printStackTrace(pw); 81 } 82 } 83 84 @Override 85 public int handleShellCommand(@NonNull ParcelFileDescriptor in, 86 @NonNull ParcelFileDescriptor out, 87 @NonNull ParcelFileDescriptor err, 88 @NonNull String[] args) { 89 return (new ResourcesManagerShellCommand(this)).exec( 90 this, 91 in.getFileDescriptor(), 92 out.getFileDescriptor(), 93 err.getFileDescriptor(), 94 args); 95 } 96 }; 97 setActivityManagerService( ActivityManagerService activityManagerService)98 public void setActivityManagerService( 99 ActivityManagerService activityManagerService) { 100 mActivityManagerService = activityManagerService; 101 } 102 } 103