1/*
2 * Copyright (C) 2017 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.voicemail.impl;
18
19import android.app.PendingIntent;
20import android.content.Context;
21import android.telecom.PhoneAccountHandle;
22import android.telephony.TelephonyManager;
23import android.telephony.VisualVoicemailService;
24import android.telephony.VisualVoicemailSmsFilterSettings;
25import com.android.dialer.common.LogUtil;
26import java.lang.reflect.Method;
27
28/** Handles {@link TelephonyManager} API changes in experimental SDK */
29public class TelephonyMangerCompat {
30  /** Moved from VisualVoicemailService to TelephonyManager */
31  public static String sendVisualVoicemailSms(
32      Context context,
33      PhoneAccountHandle phoneAccountHandle,
34      String number,
35      int port,
36      String text,
37      PendingIntent sentIntent) {
38    try {
39      Method method =
40          TelephonyManager.class.getMethod(
41              "sendVisualVoicemailSms", String.class, int.class, String.class, PendingIntent.class);
42      try {
43        LogUtil.i("TelephonyMangerCompat.sendVisualVoicemailSms", "using TelephonyManager");
44        TelephonyManager telephonyManager =
45            context
46                .getSystemService(TelephonyManager.class)
47                .createForPhoneAccountHandle(phoneAccountHandle);
48        return (String) method.invoke(telephonyManager, number, port, text, sentIntent);
49      } catch (ReflectiveOperationException e) {
50        throw new RuntimeException(e);
51      }
52    } catch (NoSuchMethodException e) {
53      // Do nothing, try the next version.
54    }
55
56    try {
57      LogUtil.i("TelephonyMangerCompat.sendVisualVoicemailSms", "using VisualVoicemailService");
58      Method method =
59          VisualVoicemailService.class.getMethod(
60              "sendVisualVoicemailSms",
61              Context.class,
62              PhoneAccountHandle.class,
63              String.class,
64              short.class,
65              String.class,
66              PendingIntent.class);
67      return (String)
68          method.invoke(null, context, phoneAccountHandle, number, (short) port, text, sentIntent);
69
70    } catch (ReflectiveOperationException e) {
71      throw new RuntimeException(e);
72    }
73  }
74
75  /** Moved from VisualVoicemailService to TelephonyManager */
76  public static String setVisualVoicemailSmsFilterSettings(
77      Context context,
78      PhoneAccountHandle phoneAccountHandle,
79      VisualVoicemailSmsFilterSettings settings) {
80    try {
81      Method method =
82          TelephonyManager.class.getMethod(
83              "setVisualVoicemailSmsFilterSettings", VisualVoicemailSmsFilterSettings.class);
84      try {
85        LogUtil.i(
86            "TelephonyMangerCompat.setVisualVoicemailSmsFilterSettings", "using TelephonyManager");
87        TelephonyManager telephonyManager =
88            context
89                .getSystemService(TelephonyManager.class)
90                .createForPhoneAccountHandle(phoneAccountHandle);
91        return (String) method.invoke(telephonyManager, settings);
92      } catch (ReflectiveOperationException e) {
93        throw new RuntimeException(e);
94      }
95    } catch (NoSuchMethodException e) {
96      // Do nothing, try the next version.
97    }
98
99    try {
100      LogUtil.i(
101          "TelephonyMangerCompat.setVisualVoicemailSmsFilterSettings",
102          "using VisualVoicemailService");
103      Method method =
104          VisualVoicemailService.class.getMethod(
105              "setSmsFilterSettings",
106              Context.class,
107              PhoneAccountHandle.class,
108              VisualVoicemailSmsFilterSettings.class);
109      return (String) method.invoke(null, context, phoneAccountHandle, settings);
110
111    } catch (ReflectiveOperationException e) {
112      throw new RuntimeException(e);
113    }
114  }
115}
116