1/*
2 * Copyright (C) 2015 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.services.telephony;
18
19import android.os.AsyncResult;
20import android.os.Handler;
21import android.os.Message;
22import android.telecom.PhoneAccount;
23import android.telecom.PhoneAccountHandle;
24import android.telecom.TelecomManager;
25
26import com.android.internal.telephony.Phone;
27import com.android.internal.util.Preconditions;
28import com.android.phone.PhoneUtils;
29
30/**
31 * Listens to phone's capabilities changed event and notifies Telecomm. One instance of these exists
32 * for each of the telephony-based call services.
33 */
34final class PstnPhoneCapabilitiesNotifier {
35    private static final int EVENT_VIDEO_CAPABILITIES_CHANGED = 1;
36
37    /**
38     * Listener called when video capabilities have changed.
39     */
40    public interface Listener {
41        public void onVideoCapabilitiesChanged(boolean isVideoCapable);
42    }
43
44    private final Phone mPhone;
45    private final Listener mListener;
46
47    private final Handler mHandler = new Handler() {
48        @Override
49        public void handleMessage(Message msg) {
50            switch (msg.what) {
51                case EVENT_VIDEO_CAPABILITIES_CHANGED:
52                    handleVideoCapabilitesChanged((AsyncResult) msg.obj);
53                    break;
54                default:
55                    break;
56            }
57        }
58    };
59
60    /*package*/
61    PstnPhoneCapabilitiesNotifier(Phone phone, Listener listener) {
62        Preconditions.checkNotNull(phone);
63
64        mPhone = phone;
65        mListener = listener;
66
67        registerForNotifications();
68    }
69
70    /*package*/
71    void teardown() {
72        unregisterForNotifications();
73    }
74
75    private void registerForNotifications() {
76        if (mPhone != null) {
77            Log.d(this, "Registering: " + mPhone);
78            mPhone.registerForVideoCapabilityChanged(mHandler, EVENT_VIDEO_CAPABILITIES_CHANGED,
79                    null);
80        }
81    }
82
83    private void unregisterForNotifications() {
84        if (mPhone != null) {
85            Log.d(this, "Unregistering: " + mPhone);
86            mPhone.unregisterForVideoCapabilityChanged(mHandler);
87        }
88    }
89
90    private void handleVideoCapabilitesChanged(AsyncResult ar) {
91        try {
92            boolean isVideoCapable = (Boolean) ar.result;
93            Log.i(this, "handleVideoCapabilitesChanged. Video capability - " + isVideoCapable);
94            mListener.onVideoCapabilitiesChanged(isVideoCapable);
95        } catch (Exception e) {
96            Log.w(this, "handleVideoCapabilitesChanged. Exception=" + e);
97        }
98    }
99
100    private int newCapabilities(int capabilities, int capability, boolean set) {
101        if (set) {
102            capabilities |= capability;
103        } else {
104            capabilities &= ~capability;
105        }
106        return capabilities;
107    }
108}
109