| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.dataloader; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.RequiresPermission; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.app.Service; |
| 24 |
import android.content.Intent; |
| 25 |
import android.content.pm.DataLoaderParams; |
| 26 |
import android.content.pm.DataLoaderParamsParcel; |
| 27 |
import android.content.pm.FileSystemControlParcel; |
| 28 |
import android.content.pm.IDataLoader; |
| 29 |
import android.content.pm.IDataLoaderStatusListener; |
| 30 |
import android.content.pm.InstallationFile; |
| 31 |
import android.content.pm.InstallationFileParcel; |
| 32 |
import android.os.IBinder; |
| 33 |
import android.os.ParcelFileDescriptor; |
| 34 |
import android.util.ExceptionUtils; |
| 35 |
import android.util.Slog; |
| 36 |
|
| 37 |
import libcore.io.IoUtils; |
| 38 |
|
| 39 |
import java.io.IOException; |
| 40 |
import java.util.Collection; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@SystemApi |
| 56 |
public abstract class DataLoaderService extends Service { |
| 57 |
private static final String TAG = "DataLoaderService"; |
| 58 |
private final DataLoaderBinderService mBinder = new DataLoaderBinderService(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@SystemApi |
| 65 |
public interface DataLoader { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
boolean onCreate(@NonNull DataLoaderParams dataLoaderParams, |
| 79 |
@NonNull FileSystemConnector connector); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
boolean onPrepareImage(@NonNull Collection<InstallationFile> addedFiles, |
| 115 |
@NonNull Collection<String> removedFiles); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
@SystemApi |
| 124 |
public @Nullable DataLoader onCreateDataLoader(@NonNull DataLoaderParams dataLoaderParams) { |
| 125 |
return null; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public final @NonNull IBinder onBind(@NonNull Intent intent) { |
| 132 |
return (IBinder) mBinder; |
| 133 |
} |
| 134 |
|
| 135 |
private class DataLoaderBinderService extends IDataLoader.Stub { |
| 136 |
@Override |
| 137 |
public void create(int id, @NonNull DataLoaderParamsParcel params, |
| 138 |
@NonNull FileSystemControlParcel control, |
| 139 |
@NonNull IDataLoaderStatusListener listener) |
| 140 |
throws RuntimeException { |
| 141 |
try { |
| 142 |
nativeCreateDataLoader(id, control, params, listener); |
| 143 |
} catch (Exception ex) { |
| 144 |
Slog.e(TAG, "Failed to create native loader for " + id, ex); |
| 145 |
destroy(id); |
| 146 |
throw new RuntimeException(ex); |
| 147 |
} finally { |
| 148 |
if (control.incremental != null) { |
| 149 |
IoUtils.closeQuietly(control.incremental.cmd); |
| 150 |
IoUtils.closeQuietly(control.incremental.pendingReads); |
| 151 |
IoUtils.closeQuietly(control.incremental.log); |
| 152 |
IoUtils.closeQuietly(control.incremental.blocksWritten); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void start(int id) { |
| 159 |
if (!nativeStartDataLoader(id)) { |
| 160 |
Slog.e(TAG, "Failed to start loader: " + id); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public void stop(int id) { |
| 166 |
if (!nativeStopDataLoader(id)) { |
| 167 |
Slog.w(TAG, "Failed to stop loader: " + id); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void destroy(int id) { |
| 173 |
if (!nativeDestroyDataLoader(id)) { |
| 174 |
Slog.w(TAG, "Failed to destroy loader: " + id); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void prepareImage(int id, InstallationFileParcel[] addedFiles, |
| 180 |
String[] removedFiles) { |
| 181 |
if (!nativePrepareImage(id, addedFiles, removedFiles)) { |
| 182 |
Slog.w(TAG, "Failed to prepare image for data loader: " + id); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@SystemApi |
| 193 |
public static final class FileSystemConnector { |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
FileSystemConnector(long nativeInstance) { |
| 200 |
mNativeInstance = nativeInstance; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
@RequiresPermission(android.Manifest.permission.INSTALL_PACKAGES) |
| 218 |
public void writeData(@NonNull String name, long offsetBytes, long lengthBytes, |
| 219 |
@NonNull ParcelFileDescriptor incomingFd) throws IOException { |
| 220 |
try { |
| 221 |
nativeWriteData(mNativeInstance, name, offsetBytes, lengthBytes, incomingFd); |
| 222 |
} catch (RuntimeException e) { |
| 223 |
ExceptionUtils.maybeUnwrapIOException(e); |
| 224 |
throw e; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
private final long mNativeInstance; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
private native boolean nativeCreateDataLoader(int storageId, |
| 233 |
@NonNull FileSystemControlParcel control, |
| 234 |
@NonNull DataLoaderParamsParcel params, |
| 235 |
IDataLoaderStatusListener listener); |
| 236 |
|
| 237 |
private native boolean nativeStartDataLoader(int storageId); |
| 238 |
|
| 239 |
private native boolean nativeStopDataLoader(int storageId); |
| 240 |
|
| 241 |
private native boolean nativeDestroyDataLoader(int storageId); |
| 242 |
|
| 243 |
private native boolean nativePrepareImage(int storageId, |
| 244 |
InstallationFileParcel[] addedFiles, String[] removedFiles); |
| 245 |
|
| 246 |
private static native void nativeWriteData(long nativeInstance, String name, long offsetBytes, |
| 247 |
long lengthBytes, ParcelFileDescriptor incomingFd); |
| 248 |
|
| 249 |
} |