1 /*
2  * Copyright (C) 2018 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.infra;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.annotation.UserIdInt;
21 
22 import com.android.internal.infra.AbstractRemoteService;
23 
24 import java.io.PrintWriter;
25 
26 /**
27  * A helper class used to resolve the name of the app-provided service a
28  * {@link AbstractRemoteService} binds to.
29  *
30  * @hide
31  */
32 public interface ServiceNameResolver {
33 
34     /**
35      * Listener for name changes.
36      */
37     public interface NameResolverListener {
38 
39         /**
40          * The name change callback.
41          */
onNameResolved(@serIdInt int userId, @Nullable String serviceName, boolean isTemporary)42         void onNameResolved(@UserIdInt int userId, @Nullable String serviceName,
43                 boolean isTemporary);
44     }
45 
46     /**
47      * Sets a callback that is called after the service is
48      * {@link #setTemporaryService(int, String, int) set} or
49      * {@link #resetTemporaryService(int) reset}.
50      *
51      * <p>Typically called after the object is constructed.
52      */
setOnTemporaryServiceNameChangedCallback( @uppressWarnings"unused") @onNull NameResolverListener callback)53     default void setOnTemporaryServiceNameChangedCallback(
54             @SuppressWarnings("unused") @NonNull NameResolverListener callback) {
55         // ignored by default
56     }
57 
58     /**
59      * Gets the default name of the service for the given user.
60      *
61      * <p>Typically implemented by reading a Settings property or framework resource.
62      */
63     @Nullable
getDefaultServiceName(@serIdInt int userId)64     String getDefaultServiceName(@UserIdInt int userId);
65 
66     /**
67      * Gets the current name of the service for the given user
68      *
69      * @return either the temporary name (set by
70      * {@link #setTemporaryService(int, String, int)}, or the
71      * {@link #getDefaultServiceName(int) default name}.
72      */
73     @Nullable
getServiceName(@serIdInt int userId)74     default String getServiceName(@UserIdInt int userId) {
75         return getDefaultServiceName(userId);
76     }
77 
78     /**
79      * Checks whether the current service is temporary for the given user.
80      */
isTemporary(@uppressWarnings"unused") @serIdInt int userId)81     default boolean isTemporary(@SuppressWarnings("unused") @UserIdInt int userId) {
82         return false;
83     }
84 
85     /**
86      * Temporarily sets the service implementation for the given user.
87      *
88      * @param userId user handle
89      * @param componentName name of the new component
90      * @param durationMs how long the change will be valid (the service will be automatically reset
91      *            to the default component after this timeout expires).
92      *
93      * @throws UnsupportedOperationException if not implemented.
94      */
setTemporaryService(@serIdInt int userId, @NonNull String componentName, int durationMs)95     default void setTemporaryService(@UserIdInt int userId, @NonNull String componentName,
96             int durationMs) {
97         throw new UnsupportedOperationException("temporary user not supported");
98     }
99 
100     /**
101      * Resets the temporary service implementation to the default component for the given user.
102      *
103      * @param userId user handle
104      *
105      * @throws UnsupportedOperationException if not implemented.
106      */
resetTemporaryService(@serIdInt int userId)107     default void resetTemporaryService(@UserIdInt int userId) {
108         throw new UnsupportedOperationException("temporary user not supported");
109     }
110 
111     /**
112      * Sets whether the default service should be used when the temporary service is not set.
113      *
114      * <p>Typically used during CTS tests to make sure only the default service doesn't interfere
115      * with the test results.
116      *
117      * @param userId user handle
118      * @param enabled whether the default service should be used when the temporary service is not
119      * set. If the service enabled state is already that value, the command is ignored and this
120      * method return {@code false}.
121      *
122      * @return whether the enabled state changed.
123      * @throws UnsupportedOperationException if not implemented.
124      */
setDefaultServiceEnabled(@serIdInt int userId, boolean enabled)125     default boolean setDefaultServiceEnabled(@UserIdInt int userId, boolean enabled) {
126         throw new UnsupportedOperationException("changing default service not supported");
127     }
128 
129     /**
130      * Checks whether the default service should be used when the temporary service is not set.
131      *
132      * <p>Typically used during CTS tests to make sure only the default service doesn't interfere
133      * with the test results.
134      *
135      * @param userId user handle
136      *
137      * @throws UnsupportedOperationException if not implemented.
138      */
isDefaultServiceEnabled(@serIdInt int userId)139     default boolean isDefaultServiceEnabled(@UserIdInt int userId) {
140         throw new UnsupportedOperationException("checking default service not supported");
141     }
142 
143     /**
144      * Dumps the generic info in just one line (without calling {@code println}.
145      */
146     // TODO(b/117779333): support proto
dumpShort(@onNull PrintWriter pw)147     void dumpShort(@NonNull PrintWriter pw);
148 
149     /**
150      * Dumps the user-specific info in just one line (without calling {@code println}.
151      */
152     // TODO(b/117779333): support proto
dumpShort(@onNull PrintWriter pw, @UserIdInt int userId)153     void dumpShort(@NonNull PrintWriter pw, @UserIdInt int userId);
154 }
155