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