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 android.content.pm;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.RemoteException;
22 
23 /**
24  * Data loader manager takes care of data loaders of different packages. It provides methods to
25  * initialize a data loader binder service (binding and creating it), to return a binder of the data
26  * loader binder service and to destroy a data loader binder service.
27  * @see com.android.server.pm.DataLoaderManagerService
28  * @hide
29  */
30 public class DataLoaderManager {
31     private static final String TAG = "DataLoaderManager";
32     private final IDataLoaderManager mService;
33 
DataLoaderManager(IDataLoaderManager service)34     public DataLoaderManager(IDataLoaderManager service) {
35         mService = service;
36     }
37 
38     /**
39      * Finds a data loader binder service and binds to it. This requires PackageManager.
40      *
41      * @param dataLoaderId ID for the new data loader binder service.
42      * @param params       DataLoaderParamsParcel object that contains data loader params, including
43      *                     its package name, class name, and additional parameters.
44      * @param bindDelayMs  introduce a delay before actual bind in case we want to avoid busylooping
45      * @param listener     Callback for the data loader service to report status back to the
46      *                     caller.
47      * @return false if 1) target ID collides with a data loader that is already bound to data
48      * loader manager; 2) package name is not specified; 3) fails to find data loader package;
49      * or 4) fails to bind to the specified data loader service, otherwise return true.
50      */
bindToDataLoader(int dataLoaderId, @NonNull DataLoaderParamsParcel params, long bindDelayMs, @NonNull IDataLoaderStatusListener listener)51     public boolean bindToDataLoader(int dataLoaderId, @NonNull DataLoaderParamsParcel params,
52             long bindDelayMs, @NonNull IDataLoaderStatusListener listener) {
53         try {
54             return mService.bindToDataLoader(dataLoaderId, params, bindDelayMs, listener);
55         } catch (RemoteException e) {
56             throw e.rethrowFromSystemServer();
57         }
58     }
59 
60     /**
61      * Returns a binder interface of the data loader binder service, given its ID.
62      */
63     @Nullable
getDataLoader(int dataLoaderId)64     public IDataLoader getDataLoader(int dataLoaderId) {
65         try {
66             return mService.getDataLoader(dataLoaderId);
67         } catch (RemoteException e) {
68             throw e.rethrowFromSystemServer();
69         }
70     }
71 
72     /**
73      * Unbinds from a data loader binder service, specified by its ID.
74      * DataLoader will receive destroy notification.
75      */
76     @Nullable
unbindFromDataLoader(int dataLoaderId)77     public void unbindFromDataLoader(int dataLoaderId) {
78         try {
79             mService.unbindFromDataLoader(dataLoaderId);
80         } catch (RemoteException e) {
81             throw e.rethrowFromSystemServer();
82         }
83     }
84 }
85