Hall Liu | c47570d | 2018-12-06 17:53:25 -0800 | [diff] [blame] | 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 | |
| 17 | package android.telecom; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.SdkConstant; |
| 21 | import android.annotation.SystemApi; |
| 22 | import android.app.Service; |
| 23 | import android.content.Intent; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.RemoteException; |
| 26 | |
| 27 | import com.android.internal.telecom.IPhoneAccountSuggestionCallback; |
| 28 | import com.android.internal.telecom.IPhoneAccountSuggestionService; |
| 29 | |
| 30 | import java.util.HashMap; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | |
| 34 | /** |
| 35 | * Base class for service that allows system apps to suggest phone accounts for outgoing calls. |
| 36 | * |
| 37 | * Phone account suggestions allow OEMs to intelligently select phone accounts based on knowledge |
| 38 | * about the user's past behavior, carrier billing patterns, or other factors unknown to the AOSP |
| 39 | * Telecom system. |
| 40 | * OEMs who wish to provide a phone account suggestion service on their device should implement this |
| 41 | * service in an app that resides in the /system/priv-app/ directory on their device. For security |
| 42 | * reasons, the service's entry {@code AndroidManifest.xml} file must declare the |
| 43 | * {@link android.Manifest.permission.BIND_PHONE_ACCOUNT_SUGGESTION_SERVICE} permission: |
| 44 | * <pre> |
| 45 | * {@code |
| 46 | * <service android:name="your.package.YourServiceName" |
| 47 | * android:permission="android.permission.BIND_PHONE_ACCOUNT_SUGGESTION_SERVICE"> |
| 48 | * <intent-filter> |
| 49 | * <action android:name="android.telecom.PhoneAccountSuggestionService"/> |
| 50 | * </intent-filter> |
| 51 | * </service> |
| 52 | * } |
| 53 | * </pre> |
| 54 | * Only one system app on each device may implement this service. If multiple system apps implement |
| 55 | * this service, none of them will be queried for suggestions. |
| 56 | * @hide |
| 57 | */ |
| 58 | @SystemApi |
Hall Liu | c47570d | 2018-12-06 17:53:25 -0800 | [diff] [blame] | 59 | public class PhoneAccountSuggestionService extends Service { |
| 60 | /** |
| 61 | * The {@link Intent} that must be declared in the {@code intent-filter} element of the |
| 62 | * service's manifest entry. |
| 63 | */ |
| 64 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 65 | public static final String SERVICE_INTERFACE = "android.telecom.PhoneAccountSuggestionService"; |
| 66 | |
| 67 | private IPhoneAccountSuggestionService mInterface = new IPhoneAccountSuggestionService.Stub() { |
| 68 | @Override |
| 69 | public void onAccountSuggestionRequest(IPhoneAccountSuggestionCallback callback, |
| 70 | String number) { |
| 71 | mCallbackMap.put(number, callback); |
| 72 | PhoneAccountSuggestionService.this.onAccountSuggestionRequest(number); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | private final Map<String, IPhoneAccountSuggestionCallback> mCallbackMap = |
| 77 | new HashMap<>(); |
| 78 | |
| 79 | @Override |
| 80 | public IBinder onBind(Intent intent) { |
| 81 | return mInterface.asBinder(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * The system calls this method during the outgoing call flow if it needs account suggestions. |
| 86 | * |
| 87 | * The implementer of this service must override this method to implement its account suggestion |
| 88 | * logic. After preparing the suggestions, the implementation of the service must call |
| 89 | * {@link #suggestPhoneAccounts(String, List)} to deliver the suggestions back to the system. |
| 90 | * |
| 91 | * Note that the system will suspend the outgoing call process after it calls this method until |
| 92 | * this service calls {@link #suggestPhoneAccounts}. |
| 93 | * |
| 94 | * @param number The phone number to provide suggestions for. |
| 95 | */ |
| 96 | public void onAccountSuggestionRequest(@NonNull String number) {} |
| 97 | |
| 98 | /** |
| 99 | * The implementation of this service calls this method to deliver suggestions to the system. |
| 100 | * |
| 101 | * The implementation of this service must call this method after receiving a call to |
| 102 | * {@link #onAccountSuggestionRequest(String)}. If no suggestions are available, pass an empty |
| 103 | * list as the {@code suggestions} argument. |
| 104 | * |
| 105 | * @param number The phone number to provide suggestions for. |
| 106 | * @param suggestions The list of suggestions. |
| 107 | */ |
| 108 | public final void suggestPhoneAccounts(@NonNull String number, |
| 109 | @NonNull List<PhoneAccountSuggestion> suggestions) { |
| 110 | IPhoneAccountSuggestionCallback callback = mCallbackMap.remove(number); |
| 111 | if (callback == null) { |
| 112 | Log.w(this, "No suggestions requested for the number %s", Log.pii(number)); |
| 113 | return; |
| 114 | } |
| 115 | try { |
| 116 | callback.suggestPhoneAccounts(number, suggestions); |
| 117 | } catch (RemoteException e) { |
| 118 | Log.w(this, "Remote exception calling suggestPhoneAccounts"); |
| 119 | } |
| 120 | } |
| 121 | } |