1 /*
2  * Copyright 2021 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 androidx.work.multiprocess.parcelable;
18 
19 import android.annotation.SuppressLint;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import androidx.annotation.RestrictTo;
24 import androidx.work.WorkerParameters;
25 
26 import org.jspecify.annotations.NonNull;
27 
28 /**
29  * Everything you need to run a {@link androidx.work.multiprocess.RemoteListenableWorker}.
30  *
31  */
32 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
33 @SuppressLint("BanParcelableUsage")
34 public class ParcelableRemoteWorkRequest implements Parcelable {
35     // We are holding on to parcelables here instead of the actual deserialized representation.
36     // This is because, to create an instance of WorkerParameters we need the application context
37     // using which we can determine the configuration, taskExecutor to use etc.
38 
39     private final String mWorkerClassName;
40     private final ParcelableWorkerParameters mParcelableWorkerParameters;
41 
ParcelableRemoteWorkRequest( @onNull String workerClassName, @NonNull WorkerParameters workerParameters)42     public ParcelableRemoteWorkRequest(
43             @NonNull String workerClassName,
44             @NonNull WorkerParameters workerParameters) {
45 
46         mWorkerClassName = workerClassName;
47         mParcelableWorkerParameters = new ParcelableWorkerParameters(workerParameters);
48     }
49 
ParcelableRemoteWorkRequest(@onNull Parcel in)50     protected ParcelableRemoteWorkRequest(@NonNull Parcel in) {
51         // workerClassName
52         mWorkerClassName = in.readString();
53         // parcelableWorkerParameters
54         mParcelableWorkerParameters = new ParcelableWorkerParameters(in);
55     }
56 
57     public static final Creator<ParcelableRemoteWorkRequest> CREATOR =
58             new Creator<ParcelableRemoteWorkRequest>() {
59                 @Override
60                 public ParcelableRemoteWorkRequest createFromParcel(Parcel in) {
61                     return new ParcelableRemoteWorkRequest(in);
62                 }
63 
64                 @Override
65                 public ParcelableRemoteWorkRequest[] newArray(int size) {
66                     return new ParcelableRemoteWorkRequest[size];
67                 }
68             };
69 
70     @Override
describeContents()71     public int describeContents() {
72         return 0;
73     }
74 
75     @Override
writeToParcel(@onNull Parcel parcel, int flags)76     public void writeToParcel(@NonNull Parcel parcel, int flags) {
77         // workerClassName
78         parcel.writeString(mWorkerClassName);
79         // parcelableWorkerParameters
80         mParcelableWorkerParameters.writeToParcel(parcel, flags);
81     }
82 
getWorkerClassName()83     public @NonNull String getWorkerClassName() {
84         return mWorkerClassName;
85     }
86 
getParcelableWorkerParameters()87     public @NonNull ParcelableWorkerParameters getParcelableWorkerParameters() {
88         return mParcelableWorkerParameters;
89     }
90 }
91