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 */
16package com.android.contacts.common.compat.telecom;
17
18import android.support.annotation.Nullable;
19import android.telecom.PhoneAccountHandle;
20import android.telecom.TelecomManager;
21import java.lang.reflect.Field;
22
23/** Compatibility class for {@link android.telecom.TelecomManager}. */
24public class TelecomManagerCompat {
25
26  // TODO(mdooley): remove once this is available in android.telecom.Call
27  // a bug
28  public static final String EXTRA_LAST_EMERGENCY_CALLBACK_TIME_MILLIS =
29      "android.telecom.extra.LAST_EMERGENCY_CALLBACK_TIME_MILLIS";
30
31  // Constants from http://cs/android/frameworks/base/telecomm/java/android/telecom/Call.java.
32  public static final String EVENT_REQUEST_HANDOVER = "android.telecom.event.REQUEST_HANDOVER";
33  public static final String EXTRA_HANDOVER_PHONE_ACCOUNT_HANDLE =
34      "android.telecom.extra.HANDOVER_PHONE_ACCOUNT_HANDLE";
35  public static final String EXTRA_HANDOVER_VIDEO_STATE =
36      "android.telecom.extra.HANDOVER_VIDEO_STATE";
37
38  // This is a hidden constant in android.telecom.DisconnectCause. Telecom sets this as a disconnect
39  // reason if it wants us to prompt the user that the video call is not available.
40  // TODO(wangqi): Reference it to constant in android.telecom.DisconnectCause.
41  public static final String REASON_IMS_ACCESS_BLOCKED = "REASON_IMS_ACCESS_BLOCKED";
42
43  /**
44   * Returns the current SIM call manager. Apps must be prepared for this method to return null,
45   * indicating that there currently exists no registered SIM call manager.
46   *
47   * @param telecomManager the {@link TelecomManager} to use to fetch the SIM call manager.
48   * @return The phone account handle of the current sim call manager.
49   */
50  @Nullable
51  public static PhoneAccountHandle getSimCallManager(TelecomManager telecomManager) {
52    if (telecomManager != null) {
53      return telecomManager.getSimCallManager();
54    }
55    return null;
56  }
57
58  /**
59   * Handovers are supported from Android O-DR onward. Since there is no API bump from O to O-DR, we
60   * need to use reflection to check the existence of TelecomManager.EXTRA_IS_HANDOVER in
61   * http://cs/android/frameworks/base/telecomm/java/android/telecom/TelecomManager.java.
62   */
63  public static boolean supportsHandover() {
64    //
65    try {
66      Field field = TelecomManager.class.getDeclaredField("EXTRA_IS_HANDOVER");
67      return "android.telecom.extra.IS_HANDOVER".equals(field.get(null /* obj (static field) */));
68    } catch (Exception e) {
69      // Do nothing
70    }
71    return false;
72  }
73}
74