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 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 default list of names of the services for the given user. 68 * 69 * <p>Typically implemented by reading a Settings property or framework resource. 70 */ 71 @Nullable getDefaultServiceNameList(@serIdInt int userId)72 default String[] getDefaultServiceNameList(@UserIdInt int userId) { 73 if (isConfiguredInMultipleMode()) { 74 throw new UnsupportedOperationException("getting default service list not supported"); 75 } else { 76 return new String[] { getDefaultServiceName(userId) }; 77 } 78 } 79 80 /** 81 * Returns whether the resolver is configured to connect to multiple backend services. 82 * The default return type is false. 83 * 84 * <p>Typically implemented by reading a Settings property or framework resource. 85 */ isConfiguredInMultipleMode()86 default boolean isConfiguredInMultipleMode() { 87 return false; 88 } 89 90 /** 91 * Gets the current name of the service for the given user 92 * 93 * @return either the temporary name (set by 94 * {@link #setTemporaryService(int, String, int)}, or the 95 * {@link #getDefaultServiceName(int) default name}. 96 */ 97 @Nullable getServiceName(@serIdInt int userId)98 default String getServiceName(@UserIdInt int userId) { 99 return getDefaultServiceName(userId); 100 } 101 102 /** 103 * Gets the current name of the service for the given user 104 * 105 * @return either the temporary name (set by 106 * {@link #setTemporaryService(int, String, int)}, or the 107 * {@link #getDefaultServiceName(int) default name}. 108 */ 109 @Nullable getServiceNameList(@serIdInt int userId)110 default String[] getServiceNameList(@UserIdInt int userId) { 111 return getDefaultServiceNameList(userId); 112 } 113 114 /** 115 * Checks whether the current service is temporary for the given user. 116 */ isTemporary(@uppressWarnings"unused") @serIdInt int userId)117 default boolean isTemporary(@SuppressWarnings("unused") @UserIdInt int userId) { 118 return false; 119 } 120 121 /** 122 * Temporarily sets the service implementation for the given user. 123 * 124 * @param userId user handle 125 * @param componentName name of the new component 126 * @param durationMs how long the change will be valid (the service will be automatically 127 * reset 128 * to the default component after this timeout expires). 129 * @throws UnsupportedOperationException if not implemented. 130 */ setTemporaryService(@serIdInt int userId, @NonNull String componentName, int durationMs)131 default void setTemporaryService(@UserIdInt int userId, @NonNull String componentName, 132 int durationMs) { 133 throw new UnsupportedOperationException("temporary user not supported"); 134 } 135 136 /** 137 * Temporarily sets the service implementation for the given user. 138 * 139 * @param userId user handle 140 * @param componentNames list of the names of the new component 141 * @param durationMs how long the change will be valid (the service will be automatically 142 * reset 143 * to the default component after this timeout expires). 144 * @throws UnsupportedOperationException if not implemented. 145 */ setTemporaryServices(@serIdInt int userId, @NonNull String[] componentNames, int durationMs)146 default void setTemporaryServices(@UserIdInt int userId, @NonNull String[] componentNames, 147 int durationMs) { 148 throw new UnsupportedOperationException("temporary user not supported"); 149 } 150 151 /** 152 * Resets the temporary service implementation to the default component for the given user. 153 * 154 * @param userId user handle 155 * @throws UnsupportedOperationException if not implemented. 156 */ resetTemporaryService(@serIdInt int userId)157 default void resetTemporaryService(@UserIdInt int userId) { 158 throw new UnsupportedOperationException("temporary user not supported"); 159 } 160 161 /** 162 * Sets whether the default service should be used when the temporary service is not set. 163 * 164 * <p>Typically used during CTS tests to make sure only the default service doesn't interfere 165 * with the test results. 166 * 167 * @param userId user handle 168 * @param enabled whether the default service should be used when the temporary service is not 169 * set. If the service enabled state is already that value, the command is 170 * ignored and this 171 * method return {@code false}. 172 * @return whether the enabled state changed. 173 * @throws UnsupportedOperationException if not implemented. 174 */ setDefaultServiceEnabled(@serIdInt int userId, boolean enabled)175 default boolean setDefaultServiceEnabled(@UserIdInt int userId, boolean enabled) { 176 throw new UnsupportedOperationException("changing default service not supported"); 177 } 178 179 /** 180 * Checks whether the default service should be used when the temporary service is not set. 181 * 182 * <p>Typically used during CTS tests to make sure only the default service doesn't interfere 183 * with the test results. 184 * 185 * @param userId user handle 186 * @throws UnsupportedOperationException if not implemented. 187 */ isDefaultServiceEnabled(@serIdInt int userId)188 default boolean isDefaultServiceEnabled(@UserIdInt int userId) { 189 throw new UnsupportedOperationException("checking default service not supported"); 190 } 191 192 /** 193 * Dumps the generic info in just one line (without calling {@code println}. 194 */ 195 // TODO(b/117779333): support proto dumpShort(@onNull PrintWriter pw)196 void dumpShort(@NonNull PrintWriter pw); 197 198 /** 199 * Dumps the user-specific info in just one line (without calling {@code println}. 200 */ 201 // TODO(b/117779333): support proto dumpShort(@onNull PrintWriter pw, @UserIdInt int userId)202 void dumpShort(@NonNull PrintWriter pw, @UserIdInt int userId); 203 } 204