summaryrefslogtreecommitdiff
path: root/telecomm/java/android/telecom/PhoneAccountSuggestionService.java
blob: 8a91b9e9ee8158ce87fe1d6b6bd4d1e292e1d195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.telecom;

import android.annotation.NonNull;
import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.android.internal.telecom.IPhoneAccountSuggestionCallback;
import com.android.internal.telecom.IPhoneAccountSuggestionService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Base class for service that allows system apps to suggest phone accounts for outgoing calls.
 *
 * Phone account suggestions allow OEMs to intelligently select phone accounts based on knowledge
 * about the user's past behavior, carrier billing patterns, or other factors unknown to the AOSP
 * Telecom system.
 * OEMs who wish to provide a phone account suggestion service on their device should implement this
 * service in an app that resides in the /system/priv-app/ directory on their device. For security
 * reasons, the service's entry {@code AndroidManifest.xml} file must declare the
 * {@link android.Manifest.permission.BIND_PHONE_ACCOUNT_SUGGESTION_SERVICE} permission:
 * <pre>
 * {@code
 * <service android:name="your.package.YourServiceName"
 *          android:permission="android.permission.BIND_PHONE_ACCOUNT_SUGGESTION_SERVICE">
 *      <intent-filter>
 *          <action android:name="android.telecom.PhoneAccountSuggestionService"/>
 *      </intent-filter>
 * </service>
 * }
 * </pre>
 * Only one system app on each device may implement this service. If multiple system apps implement
 * this service, none of them will be queried for suggestions.
 * @hide
 */
@SystemApi
public class PhoneAccountSuggestionService extends Service {
    /**
     * The {@link Intent} that must be declared in the {@code intent-filter} element of the
     * service's manifest entry.
     */
    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
    public static final String SERVICE_INTERFACE = "android.telecom.PhoneAccountSuggestionService";

    private IPhoneAccountSuggestionService mInterface = new IPhoneAccountSuggestionService.Stub() {
        @Override
        public void onAccountSuggestionRequest(IPhoneAccountSuggestionCallback callback,
                String number) {
            mCallbackMap.put(number, callback);
            PhoneAccountSuggestionService.this.onAccountSuggestionRequest(number);
        }
    };

    private final Map<String, IPhoneAccountSuggestionCallback> mCallbackMap =
            new HashMap<>();

    @Override
    public IBinder onBind(Intent intent) {
        return mInterface.asBinder();
    }

    /**
     * The system calls this method during the outgoing call flow if it needs account suggestions.
     *
     * The implementer of this service must override this method to implement its account suggestion
     * logic. After preparing the suggestions, the implementation of the service must call
     * {@link #suggestPhoneAccounts(String, List)} to deliver the suggestions back to the system.
     *
     * Note that the system will suspend the outgoing call process after it calls this method until
     * this service calls {@link #suggestPhoneAccounts}.
     *
     * @param number The phone number to provide suggestions for.
     */
    public void onAccountSuggestionRequest(@NonNull String number) {}

    /**
     * The implementation of this service calls this method to deliver suggestions to the system.
     *
     * The implementation of this service must call this method after receiving a call to
     * {@link #onAccountSuggestionRequest(String)}. If no suggestions are available, pass an empty
     * list as the {@code suggestions} argument.
     *
     * @param number The phone number to provide suggestions for.
     * @param suggestions The list of suggestions.
     */
    public final void suggestPhoneAccounts(@NonNull String number,
            @NonNull List<PhoneAccountSuggestion> suggestions) {
        IPhoneAccountSuggestionCallback callback = mCallbackMap.remove(number);
        if (callback == null) {
            Log.w(this, "No suggestions requested for the number %s", Log.pii(number));
            return;
        }
        try {
            callback.suggestPhoneAccounts(number, suggestions);
        } catch (RemoteException e) {
            Log.w(this, "Remote exception calling suggestPhoneAccounts");
        }
    }
}