blob: 5876d433face393c37a664381a3cebec90e29f01 [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
35import java.io.FileDescriptor;
36
37/**
38 * This service has the following purposes:
39 * 1) Starts the IIncrementalManager binder service.
40 * 1) Starts the native IIncrementalManagerService binder service.
41 * 2) Handles shell commands for "incremental" service.
42 * 3) Handles binder calls from the native IIncrementalManagerService binder service and pass
43 * them to a data loader binder service.
44 */
45
46public class IncrementalManagerService extends IIncrementalManager.Stub {
47 private static final String TAG = "IncrementalManagerService";
48 private static final String BINDER_SERVICE_NAME = "incremental";
49 // DataLoaderManagerService should have been started before us
50 private @NonNull DataLoaderManager mDataLoaderManager;
51 private long mNativeInstance;
52 private final @NonNull Context mContext;
53
54 /**
55 * Starts IIncrementalManager binder service and register to Service Manager.
56 * Starts the native IIncrementalManagerNative binder service.
57 */
58 public static IncrementalManagerService start(Context context) {
59 IncrementalManagerService self = new IncrementalManagerService(context);
60 if (self.mNativeInstance == 0) {
61 return null;
62 }
63 return self;
64 }
65
66 private IncrementalManagerService(Context context) {
67 mContext = context;
68 mDataLoaderManager = mContext.getSystemService(DataLoaderManager.class);
69 ServiceManager.addService(BINDER_SERVICE_NAME, this);
70 // Starts and register IIncrementalManagerNative service
Songchun Fan3c82a302019-11-29 14:23:45 -080071 mNativeInstance = nativeStartService();
Songchun Fanf5c894f2019-11-29 15:43:58 -080072 }
Songchun Fan3c82a302019-11-29 14:23:45 -080073
Songchun Fanf5c894f2019-11-29 15:43:58 -080074 /**
75 * Notifies native IIncrementalManager service that system is ready.
76 */
77 public void systemReady() {
Songchun Fan3c82a302019-11-29 14:23:45 -080078 nativeSystemReady(mNativeInstance);
Songchun Fanf5c894f2019-11-29 15:43:58 -080079 }
80
81 /**
82 * Finds data loader service provider and binds to it. This requires PackageManager.
83 */
84 @Override
Alex Buynytskyyea14d192019-12-13 15:42:18 -080085 public boolean prepareDataLoader(int mountId, FileSystemControlParcel control,
86 DataLoaderParamsParcel params,
Songchun Fanf5c894f2019-11-29 15:43:58 -080087 IDataLoaderStatusListener listener) {
88 Bundle dataLoaderParams = new Bundle();
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -080089 dataLoaderParams.putParcelable("componentName",
90 new ComponentName(params.packageName, params.className));
Songchun Fanf5c894f2019-11-29 15:43:58 -080091 dataLoaderParams.putParcelable("control", control);
92 dataLoaderParams.putParcelable("params", params);
93 DataLoaderManager dataLoaderManager = mContext.getSystemService(DataLoaderManager.class);
94 if (dataLoaderManager == null) {
95 Slog.e(TAG, "Failed to find data loader manager service");
96 return false;
97 }
98 if (!dataLoaderManager.initializeDataLoader(mountId, dataLoaderParams, listener)) {
99 Slog.e(TAG, "Failed to initialize data loader");
100 return false;
101 }
102 return true;
103 }
104
105
106 @Override
107 public boolean startDataLoader(int mountId) {
108 IDataLoader dataLoader = mDataLoaderManager.getDataLoader(mountId);
109 if (dataLoader == null) {
110 Slog.e(TAG, "Start failed to retrieve data loader for ID=" + mountId);
111 return false;
112 }
113 try {
Alex Buynytskyy1ecfcec2019-12-17 12:10:41 -0800114 dataLoader.start();
Songchun Fanf5c894f2019-11-29 15:43:58 -0800115 return true;
116 } catch (RemoteException ex) {
117 return false;
118 }
119 }
120
121 @Override
122 public void destroyDataLoader(int mountId) {
123 IDataLoader dataLoader = mDataLoaderManager.getDataLoader(mountId);
124 if (dataLoader == null) {
125 Slog.e(TAG, "Destroy failed to retrieve data loader for ID=" + mountId);
126 return;
127 }
128 try {
129 dataLoader.destroy();
130 } catch (RemoteException ex) {
131 return;
132 }
133 }
134
135 // TODO: remove this
136 @Override
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -0800137 public void newFileForDataLoader(int mountId, byte[] fileId, byte[] metadata) {
Songchun Fanf5c894f2019-11-29 15:43:58 -0800138 IDataLoader dataLoader = mDataLoaderManager.getDataLoader(mountId);
139 if (dataLoader == null) {
140 Slog.e(TAG, "Failed to retrieve data loader for ID=" + mountId);
141 return;
142 }
Songchun Fanf5c894f2019-11-29 15:43:58 -0800143 }
144
145 @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);
Songchun Fanf5c894f2019-11-29 15:43:58 -0800161}