summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/compat/CompatUtils.java
blob: 673cb709bad68a06b3aedffbbd015d9a010a14b5 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (C) 2015 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 com.android.dialer.compat;

import android.os.Build;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;

public final class CompatUtils {

  private static final String TAG = CompatUtils.class.getSimpleName();

  /** PrioritizedMimeType is added in API level 23. */
  public static boolean hasPrioritizedMimeType() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M) >= Build.VERSION_CODES.M;
  }

  /**
   * Determines if this version is compatible with multi-SIM and the phone account APIs. Can also
   * force the version to be lower through SdkVersionOverride.
   *
   * @return {@code true} if multi-SIM capability is available, {@code false} otherwise.
   */
  public static boolean isMSIMCompatible() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP)
        >= Build.VERSION_CODES.LOLLIPOP_MR1;
  }

  /**
   * Determines if this version is compatible with video calling. Can also force the version to be
   * lower through SdkVersionOverride.
   *
   * @return {@code true} if video calling is allowed, {@code false} otherwise.
   */
  public static boolean isVideoCompatible() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M;
  }

  /**
   * Determines if this version is capable of using presence checking for video calling. Support for
   * video call presence indication is added in SDK 24.
   *
   * @return {@code true} if video presence checking is allowed, {@code false} otherwise.
   */
  public static boolean isVideoPresenceCompatible() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M) > Build.VERSION_CODES.M;
  }

  /**
   * Determines if this version is compatible with call subject. Can also force the version to be
   * lower through SdkVersionOverride.
   *
   * @return {@code true} if call subject is a feature on this device, {@code false} otherwise.
   */
  public static boolean isCallSubjectCompatible() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M;
  }

  /**
   * Determines if this version is compatible with a default dialer. Can also force the version to
   * be lower through {@link SdkVersionOverride}.
   *
   * @return {@code true} if default dialer is a feature on this device, {@code false} otherwise.
   */
  public static boolean isDefaultDialerCompatible() {
    return isMarshmallowCompatible();
  }

  /**
   * Determines if this version is compatible with Lollipop Mr1-specific APIs. Can also force the
   * version to be lower through SdkVersionOverride.
   *
   * @return {@code true} if runtime sdk is compatible with Lollipop MR1, {@code false} otherwise.
   */
  public static boolean isLollipopMr1Compatible() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP_MR1)
        >= Build.VERSION_CODES.LOLLIPOP_MR1;
  }

  /**
   * Determines if this version is compatible with Marshmallow-specific APIs. Can also force the
   * version to be lower through SdkVersionOverride.
   *
   * @return {@code true} if runtime sdk is compatible with Marshmallow, {@code false} otherwise.
   */
  public static boolean isMarshmallowCompatible() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP) >= Build.VERSION_CODES.M;
  }

  /**
   * Determines if the given class is available. Can be used to check if system apis exist at
   * runtime.
   *
   * @param className the name of the class to look for.
   * @return {@code true} if the given class is available, {@code false} otherwise or if className
   *     is empty.
   */
  public static boolean isClassAvailable(@Nullable String className) {
    if (TextUtils.isEmpty(className)) {
      return false;
    }
    try {
      Class.forName(className);
      return true;
    } catch (ClassNotFoundException e) {
      return false;
    } catch (Throwable t) {
      Log.e(
          TAG,
          "Unexpected exception when checking if class:" + className + " exists at " + "runtime",
          t);
      return false;
    }
  }

  /**
   * Determines if the given class's method is available to call. Can be used to check if system
   * apis exist at runtime.
   *
   * @param className the name of the class to look for
   * @param methodName the name of the method to look for
   * @param parameterTypes the needed parameter types for the method to look for
   * @return {@code true} if the given class is available, {@code false} otherwise or if className
   *     or methodName are empty.
   */
  public static boolean isMethodAvailable(
      @Nullable String className, @Nullable String methodName, Class<?>... parameterTypes) {
    if (TextUtils.isEmpty(className) || TextUtils.isEmpty(methodName)) {
      return false;
    }

    try {
      Class.forName(className).getMethod(methodName, parameterTypes);
      return true;
    } catch (ClassNotFoundException | NoSuchMethodException e) {
      Log.v(TAG, "Could not find method: " + className + "#" + methodName);
      return false;
    } catch (Throwable t) {
      Log.e(
          TAG,
          "Unexpected exception when checking if method: "
              + className
              + "#"
              + methodName
              + " exists at runtime",
          t);
      return false;
    }
  }

  /**
   * Invokes a given class's method using reflection. Can be used to call system apis that exist at
   * runtime but not in the SDK.
   *
   * @param instance The instance of the class to invoke the method on.
   * @param methodName The name of the method to invoke.
   * @param parameterTypes The needed parameter types for the method.
   * @param parameters The parameter values to pass into the method.
   * @return The result of the invocation or {@code null} if instance or methodName are empty, or if
   *     the reflection fails.
   */
  @Nullable
  public static Object invokeMethod(
      @Nullable Object instance,
      @Nullable String methodName,
      Class<?>[] parameterTypes,
      Object[] parameters) {
    if (instance == null || TextUtils.isEmpty(methodName)) {
      return null;
    }

    String className = instance.getClass().getName();
    try {
      return Class.forName(className)
          .getMethod(methodName, parameterTypes)
          .invoke(instance, parameters);
    } catch (ClassNotFoundException
        | NoSuchMethodException
        | IllegalArgumentException
        | IllegalAccessException
        | InvocationTargetException e) {
      Log.v(TAG, "Could not invoke method: " + className + "#" + methodName);
      return null;
    } catch (Throwable t) {
      Log.e(
          TAG,
          "Unexpected exception when invoking method: "
              + className
              + "#"
              + methodName
              + " at runtime",
          t);
      return null;
    }
  }

  /**
   * Determines if this version is compatible with Lollipop-specific APIs. Can also force the
   * version to be lower through SdkVersionOverride.
   *
   * @return {@code true} if call subject is a feature on this device, {@code false} otherwise.
   */
  public static boolean isLollipopCompatible() {
    return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP)
        >= Build.VERSION_CODES.LOLLIPOP;
  }
}