1/*
2 * Copyright (C) 2016 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
17package com.android.server.telecom.callfiltering;
18
19import android.net.Uri;
20import android.telecom.Log;
21
22import com.android.internal.telephony.CallerInfo;
23import com.android.server.telecom.Call;
24import com.android.server.telecom.CallerInfoLookupHelper;
25import com.android.server.telecom.LogUtils;
26
27import java.util.Objects;
28
29public class DirectToVoicemailCallFilter implements IncomingCallFilter.CallFilter {
30    private final CallerInfoLookupHelper mCallerInfoLookupHelper;
31
32    public DirectToVoicemailCallFilter(CallerInfoLookupHelper callerInfoLookupHelper) {
33        mCallerInfoLookupHelper = callerInfoLookupHelper;
34    }
35
36    @Override
37    public void startFilterLookup(final Call call, CallFilterResultCallback callback) {
38        Log.addEvent(call, LogUtils.Events.DIRECT_TO_VM_INITIATED);
39        final Uri callHandle = call.getHandle();
40
41        mCallerInfoLookupHelper.startLookup(callHandle,
42                new CallerInfoLookupHelper.OnQueryCompleteListener() {
43                    @Override
44                    public void onCallerInfoQueryComplete(Uri handle, CallerInfo info) {
45                        CallFilteringResult result;
46                        if (Objects.equals(callHandle, handle)) {
47                            if (info != null && info.shouldSendToVoicemail) {
48                                result = new CallFilteringResult(
49                                        false, // shouldAllowCall
50                                        true, // shouldReject
51                                        true, // shouldAddToCallLog
52                                        true // shouldShowNotification
53                                );
54                            } else {
55                                result = new CallFilteringResult(
56                                        true, // shouldAllowCall
57                                        false, // shouldReject
58                                        true, // shouldAddToCallLog
59                                        true // shouldShowNotification
60                                );
61                            }
62                            Log.addEvent(call, LogUtils.Events.DIRECT_TO_VM_FINISHED, result);
63                            callback.onCallFilteringComplete(call, result);
64                        } else {
65                            Log.w(this, "CallerInfo lookup returned with a different handle than " +
66                                    "what was passed in. Was %s, should be %s", handle, callHandle);
67                        }
68                    }
69
70                    @Override
71                    public void onContactPhotoQueryComplete(Uri handle, CallerInfo info) {
72                        // ignore
73                    }
74                });
75    }
76}
77