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 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 35 | import com.android.internal.util.DumpUtils; |
| 36 | |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 37 | import java.io.FileDescriptor; |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 38 | import java.io.PrintWriter; |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 39 | |
| 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 | |
| 49 | public 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 Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 74 | mNativeInstance = nativeStartService(); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 75 | } |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 76 | |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 77 | @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 Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Notifies native IIncrementalManager service that system is ready. |
| 86 | */ |
| 87 | public void systemReady() { |
Songchun Fan | 3c82a30 | 2019-11-29 14:23:45 -0800 | [diff] [blame] | 88 | nativeSystemReady(mNativeInstance); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Finds data loader service provider and binds to it. This requires PackageManager. |
| 93 | */ |
| 94 | @Override |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 95 | public boolean prepareDataLoader(int mountId, FileSystemControlParcel control, |
| 96 | DataLoaderParamsParcel params, |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 97 | IDataLoaderStatusListener listener) { |
| 98 | Bundle dataLoaderParams = new Bundle(); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 99 | dataLoaderParams.putParcelable("componentName", |
| 100 | new ComponentName(params.packageName, params.className)); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 101 | 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 Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 124 | dataLoader.start(); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 125 | 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 Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 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); |
Alex Buynytskyy | 18b07a4 | 2020-02-03 20:06:00 -0800 | [diff] [blame] | 161 | |
| 162 | private static native void nativeDump(long nativeInstance, int fd); |
Songchun Fan | f5c894f | 2019-11-29 15:43:58 -0800 | [diff] [blame] | 163 | } |