1 /*
2  * Copyright (C) 2023 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.contentprotection;
18 
19 import android.annotation.NonNull;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ParceledListSlice;
24 import android.service.contentcapture.ContentCaptureService;
25 import android.service.contentcapture.IContentProtectionService;
26 import android.util.Slog;
27 import android.view.contentcapture.ContentCaptureEvent;
28 
29 import com.android.internal.infra.ServiceConnector;
30 
31 import java.time.Duration;
32 
33 /**
34  * Connector for the remote content protection service.
35  *
36  * @hide
37  */
38 public class RemoteContentProtectionService
39         extends ServiceConnector.Impl<IContentProtectionService> {
40 
41     private static final String TAG = RemoteContentProtectionService.class.getSimpleName();
42 
43     private static final Duration AUTO_DISCONNECT_TIMEOUT = Duration.ofSeconds(3);
44 
45     @NonNull private final ComponentName mComponentName;
46 
RemoteContentProtectionService( @onNull Context context, @NonNull ComponentName componentName, int userId, boolean bindAllowInstant)47     public RemoteContentProtectionService(
48             @NonNull Context context,
49             @NonNull ComponentName componentName,
50             int userId,
51             boolean bindAllowInstant) {
52         super(
53                 context,
54                 new Intent(ContentCaptureService.PROTECTION_SERVICE_INTERFACE)
55                         .setComponent(componentName),
56                 bindAllowInstant ? Context.BIND_ALLOW_INSTANT : 0,
57                 userId,
58                 IContentProtectionService.Stub::asInterface);
59         mComponentName = componentName;
60     }
61 
62     @Override // from ServiceConnector.Impl
getAutoDisconnectTimeoutMs()63     protected long getAutoDisconnectTimeoutMs() {
64         return AUTO_DISCONNECT_TIMEOUT.toMillis();
65     }
66 
67     @Override // from ServiceConnector.Impl
onServiceConnectionStatusChanged( @onNull IContentProtectionService service, boolean isConnected)68     protected void onServiceConnectionStatusChanged(
69             @NonNull IContentProtectionService service, boolean isConnected) {
70         Slog.i(
71                 TAG,
72                 "Connection status for: "
73                         + mComponentName
74                         + " changed to: "
75                         + (isConnected ? "connected" : "disconnected"));
76     }
77 
onLoginDetected(@onNull ParceledListSlice<ContentCaptureEvent> events)78     public void onLoginDetected(@NonNull ParceledListSlice<ContentCaptureEvent> events) {
79         run(service -> service.onLoginDetected(events));
80     }
81 }
82