blob: a8121cc6924f54b53a3410582156401cac564ad2 [file] [log] [blame]
Songchun Fanf5c894f2019-11-29 15:43:58 -08001/*
2 * Copyright (C) 2019 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
17package com.android.server.incremental;
18
19import android.annotation.NonNull;
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -080020import android.content.ComponentName;
Songchun Fanf5c894f2019-11-29 15:43:58 -080021import android.content.Context;
22import android.content.pm.DataLoaderManager;
Alex Buynytskyyea14d192019-12-13 15:42:18 -080023import android.content.pm.DataLoaderParamsParcel;
24import android.content.pm.FileSystemControlParcel;
Songchun Fanf5c894f2019-11-29 15:43:58 -080025import android.content.pm.IDataLoader;
26import android.content.pm.IDataLoaderStatusListener;
27import android.os.Bundle;
28import android.os.RemoteException;
29import android.os.ResultReceiver;
30import android.os.ServiceManager;
31import android.os.ShellCallback;
32import android.os.incremental.IIncrementalManager;
Songchun Fanf5c894f2019-11-29 15:43:58 -080033import android.util.Slog;
34
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080035import com.android.internal.util.DumpUtils;
36
Songchun Fanf5c894f2019-11-29 15:43:58 -080037import java.io.FileDescriptor;
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080038import java.io.PrintWriter;
Songchun Fanf5c894f2019-11-29 15:43:58 -080039
40/**
41 * This service has the following purposes:
42 * 1) Starts the IIncrementalManager binder service.
43 * 1) Starts the native IIncrementalManagerService binder service.
44 * 2) Handles shell commands for "incremental" service.
45 * 3) Handles binder calls from the native IIncrementalManagerService binder service and pass
46 * them to a data loader binder service.
47 */
48
49public class IncrementalManagerService extends IIncrementalManager.Stub {
50 private static final String TAG = "IncrementalManagerService";
51 private static final String BINDER_SERVICE_NAME = "incremental";
52 // DataLoaderManagerService should have been started before us
53 private @NonNull DataLoaderManager mDataLoaderManager;
54 private long mNativeInstance;
55 private final @NonNull Context mContext;
56
57 /**
58 * Starts IIncrementalManager binder service and register to Service Manager.
59 * Starts the native IIncrementalManagerNative binder service.
60 */
61 public static IncrementalManagerService start(Context context) {
62 IncrementalManagerService self = new IncrementalManagerService(context);
63 if (self.mNativeInstance == 0) {
64 return null;
65 }
66 return self;
67 }
68
69 private IncrementalManagerService(Context context) {
70 mContext = context;
71 mDataLoaderManager = mContext.getSystemService(DataLoaderManager.class);
72 ServiceManager.addService(BINDER_SERVICE_NAME, this);
73 // Starts and register IIncrementalManagerNative service
Songchun Fan3c82a302019-11-29 14:23:45 -080074 mNativeInstance = nativeStartService();
Songchun Fanf5c894f2019-11-29 15:43:58 -080075 }
Songchun Fan3c82a302019-11-29 14:23:45 -080076
Alex Buynytskyy18b07a42020-02-03 20:06:00 -080077 @SuppressWarnings("resource")
78 @Override
79 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
80 if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return;
81 nativeDump(mNativeInstance, fd.getInt$());
82 }
83
Songchun Fanf5c894f2019-11-29 15:43:58 -080084 /**
85 * Notifies native IIncrementalManager service that system is ready.
86 */
87 public void systemReady() {
Songchun Fan3c82a302019-11-29 14:23:45 -080088 nativeSystemReady(mNativeInstance);
Songchun Fanf5c894f2019-11-29 15:43:58 -080089 }
90
91 /**
92 * Finds data loader service provider and binds to it. This requires PackageManager.
93 */
94 @Override
Alex Buynytskyyea14d192019-12-13 15:42:18 -080095 public boolean prepareDataLoader(int mountId, FileSystemControlParcel control,
96 DataLoaderParamsParcel params,
Songchun Fanf5c894f2019-11-29 15:43:58 -080097 IDataLoaderStatusListener listener) {
98 Bundle dataLoaderParams = new Bundle();
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -080099 dataLoaderParams.putParcelable("componentName",
100 new ComponentName(params.packageName, params.className));
Songchun Fanf5c894f2019-11-29 15:43:58 -0800101 dataLoaderParams.putParcelable("control", control);
102 dataLoaderParams.putParcelable("params", params);
103 DataLoaderManager dataLoaderManager = mContext.getSystemService(DataLoaderManager.class);
104 if (dataLoaderManager == null) {
105 Slog.e(TAG, "Failed to find data loader manager service");
106 return false;
107 }
108 if (!dataLoaderManager.initializeDataLoader(mountId, dataLoaderParams, listener)) {
109 Slog.e(TAG, "Failed to initialize data loader");
110 return false;
111 }
112 return true;
113 }
114
115
116 @Override
117 public boolean startDataLoader(int mountId) {
118 IDataLoader dataLoader = mDataLoaderManager.getDataLoader(mountId);
119 if (dataLoader == null) {
120 Slog.e(TAG, "Start failed to retrieve data loader for ID=" + mountId);
121 return false;
122 }
123 try {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800124 dataLoader.start();
Songchun Fanf5c894f2019-11-29 15:43:58 -0800125 return true;
126 } catch (RemoteException ex) {
127 return false;
128 }
129 }
130
131 @Override
132 public void destroyDataLoader(int mountId) {
133 IDataLoader dataLoader = mDataLoaderManager.getDataLoader(mountId);
134 if (dataLoader == null) {
135 Slog.e(TAG, "Destroy failed to retrieve data loader for ID=" + mountId);
136 return;
137 }
138 try {
139 dataLoader.destroy();
140 } catch (RemoteException ex) {
141 return;
142 }
143 }
144
Songchun Fanf5c894f2019-11-29 15:43:58 -0800145 @Override
146 public void showHealthBlockedUI(int mountId) {
147 // TODO(b/136132412): implement this
148 }
149
150 @Override
151 public void onShellCommand(@NonNull FileDescriptor in, @NonNull FileDescriptor out,
152 FileDescriptor err, @NonNull String[] args, ShellCallback callback,
153 @NonNull ResultReceiver resultReceiver) {
154 (new IncrementalManagerShellCommand(mContext)).exec(
155 this, in, out, err, args, callback, resultReceiver);
156 }
Songchun Fan3c82a302019-11-29 14:23:45 -0800157
158 private static native long nativeStartService();
159
160 private static native void nativeSystemReady(long nativeInstance);
Alex Buynytskyy18b07a42020-02-03 20:06:00 -0800161
162 private static native void nativeDump(long nativeInstance, int fd);
Songchun Fanf5c894f2019-11-29 15:43:58 -0800163}