Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.incremental; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.pm.DataLoaderManager; |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 23 | import android.content.pm.DataLoaderParamsParcel; |
| 24 | import android.content.pm.FileSystemControlParcel; |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 25 | import android.content.pm.IDataLoader; |
| 26 | import android.content.pm.IDataLoaderStatusListener; |
| 27 | import android.os.Bundle; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.ResultReceiver; |
| 30 | import android.os.ServiceManager; |
| 31 | import android.os.ShellCallback; |
| 32 | import android.os.incremental.IIncrementalManager; |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 33 | import android.util.Slog; |
| 34 | |
| 35 | import 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 | |
| 46 | public 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 Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 71 | mNativeInstance = nativeStartService(); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 72 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 73 | |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Notifies native IIncrementalManager service that system is ready. |
| 76 | */ |
| 77 | public void systemReady() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 78 | nativeSystemReady(mNativeInstance); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Finds data loader service provider and binds to it. This requires PackageManager. |
| 83 | */ |
| 84 | @Override |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 85 | public boolean prepareDataLoader(int mountId, FileSystemControlParcel control, |
| 86 | DataLoaderParamsParcel params, |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 87 | IDataLoaderStatusListener listener) { |
| 88 | Bundle dataLoaderParams = new Bundle(); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 89 | dataLoaderParams.putParcelable("componentName", |
| 90 | new ComponentName(params.packageName, params.className)); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 91 | 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 Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 114 | dataLoader.start(); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 115 | 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 Zubrytskyi | 4a25dfb | 2020-01-10 11:53:24 -0800 | [diff] [blame] | 137 | public void newFileForDataLoader(int mountId, byte[] fileId, byte[] metadata) { |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 138 | 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 Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 143 | } |
| 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 Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 157 | |
| 158 | private static native long nativeStartService(); |
| 159 | |
| 160 | private static native void nativeSystemReady(long nativeInstance); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 161 | } |