1 /*
2  * Copyright (C) 2020 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 package com.android.server.searchui;
17 
18 import android.annotation.NonNull;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.os.IBinder;
22 import android.service.search.ISearchUiService;
23 import android.text.format.DateUtils;
24 
25 import com.android.internal.infra.AbstractMultiplePendingRequestsRemoteService;
26 
27 
28 /**
29  * Proxy to the {@link android.service.searchui.SearchUiService} implementation in another
30  * process.
31  */
32 public class RemoteSearchUiService extends
33         AbstractMultiplePendingRequestsRemoteService<RemoteSearchUiService,
34                 ISearchUiService> {
35 
36     private static final String TAG = "RemoteSearchUiService";
37 
38     private static final long TIMEOUT_REMOTE_REQUEST_MILLIS = 2 * DateUtils.SECOND_IN_MILLIS;
39 
40     private final RemoteSearchUiServiceCallbacks mCallback;
41 
RemoteSearchUiService(Context context, String serviceInterface, ComponentName componentName, int userId, RemoteSearchUiServiceCallbacks callback, boolean bindInstantServiceAllowed, boolean verbose)42     public RemoteSearchUiService(Context context, String serviceInterface,
43             ComponentName componentName, int userId,
44             RemoteSearchUiServiceCallbacks callback, boolean bindInstantServiceAllowed,
45             boolean verbose) {
46         super(context, serviceInterface, componentName, userId, callback,
47                 context.getMainThreadHandler(),
48                 bindInstantServiceAllowed ? Context.BIND_ALLOW_INSTANT : 0,
49                 verbose, /* initialCapacity= */ 1);
50         mCallback = callback;
51     }
52 
53     @Override
getServiceInterface(IBinder service)54     protected ISearchUiService getServiceInterface(IBinder service) {
55         return ISearchUiService.Stub.asInterface(service);
56     }
57 
58     @Override
getTimeoutIdleBindMillis()59     protected long getTimeoutIdleBindMillis() {
60         return PERMANENT_BOUND_TIMEOUT_MS;
61     }
62 
63     @Override
getRemoteRequestMillis()64     protected long getRemoteRequestMillis() {
65         return TIMEOUT_REMOTE_REQUEST_MILLIS;
66     }
67 
68     /**
69      * Schedules a request to bind to the remote service.
70      */
reconnect()71     public void reconnect() {
72         super.scheduleBind();
73     }
74 
75     /**
76      * Schedule async request on remote service.
77      */
scheduleOnResolvedService(@onNull AsyncRequest<ISearchUiService> request)78     public void scheduleOnResolvedService(@NonNull AsyncRequest<ISearchUiService> request) {
79         scheduleAsyncRequest(request);
80     }
81 
82     /**
83      * Execute async request on remote service immediately instead of sending it to Handler queue.
84      */
executeOnResolvedService(@onNull AsyncRequest<ISearchUiService> request)85     public void executeOnResolvedService(@NonNull AsyncRequest<ISearchUiService> request) {
86         executeAsyncRequest(request);
87     }
88 
89     /**
90      * Failure callback
91      */
92     public interface RemoteSearchUiServiceCallbacks
93             extends VultureCallback<RemoteSearchUiService> {
94 
95         /**
96          * Notifies a the failure or timeout of a remote call.
97          */
onFailureOrTimeout(boolean timedOut)98         void onFailureOrTimeout(boolean timedOut);
99 
100         /**
101          * Notifies change in connected state of the remote service.
102          */
onConnectedStateChanged(boolean connected)103         void onConnectedStateChanged(boolean connected);
104     }
105 
106     @Override // from AbstractRemoteService
handleOnConnectedStateChanged(boolean connected)107     protected void handleOnConnectedStateChanged(boolean connected) {
108         if (mCallback != null) {
109             mCallback.onConnectedStateChanged(connected);
110         }
111     }
112 }
113