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