blob: 4ce5aaa5f5a6a3668aeaf73e011d05330f86fae0 [file] [log] [blame]
Wenyi Wang6fa570f2015-11-24 15:44:11 -08001/*
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;
17
18import android.os.Build;
Brandon Maxwell973c0e12015-12-15 17:09:14 -080019import android.support.annotation.Nullable;
20import android.util.Log;
Wenyi Wang6fa570f2015-11-24 15:44:11 -080021
22import com.android.contacts.common.compat.SdkVersionOverride;
Wenyi Wang93fdd482015-12-07 14:26:54 -080023import com.android.contacts.common.model.CPOWrapper;
Wenyi Wang6fa570f2015-11-24 15:44:11 -080024
25public final class CompatUtils {
Brandon Maxwell973c0e12015-12-15 17:09:14 -080026
27 private static final String TAG = CompatUtils.class.getSimpleName();
28
Wenyi Wang6fa570f2015-11-24 15:44:11 -080029 /**
Wenyi Wang93fdd482015-12-07 14:26:54 -080030 * These 4 variables are copied from ContentProviderOperation for compatibility.
31 */
32 public final static int TYPE_INSERT = 1;
33
34 public final static int TYPE_UPDATE = 2;
35
36 public final static int TYPE_DELETE = 3;
37
38 public final static int TYPE_ASSERT = 4;
39
40 /**
41 * Returns whether the operation in CPOWrapper is of TYPE_INSERT;
42 */
43 public static boolean isInsertCompat(CPOWrapper cpoWrapper) {
44 if (SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M) >= Build.VERSION_CODES.M) {
45 return cpoWrapper.getOperation().isInsert();
46 } else {
47 return (cpoWrapper.getType() == TYPE_INSERT);
48 }
49 }
50
51 /**
Wenyi Wang6fa570f2015-11-24 15:44:11 -080052 * PrioritizedMimeType is added in API level 23.
53 */
54 public static boolean hasPrioritizedMimeType() {
55 return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M)
56 >= Build.VERSION_CODES.M;
57 }
Nancy Cheneab0b822015-11-25 15:44:48 -080058
59 /**
60 * Determines if this version is compatible with multi-SIM and the phone account APIs.
61 * Can also force the version to be lower through SdkVersionOverride.
62 *
63 * @return {@code true} if multi-SIM capability is available, {@code false} otherwise.
64 */
65 public static boolean isMSIMCompatible() {
66 return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP)
67 >= Build.VERSION_CODES.LOLLIPOP_MR1;
68 }
Nancy Chen5fe647d2015-12-08 16:57:48 -080069
70 /**
71 * Determines if this version is compatible with video calling. Can also force the version to be
72 * lower through SdkVersionOverride.
73 *
74 * @return {@code true} if video calling is allowed, {@code false} otherwise.
75 */
76 public static boolean isVideoCompatible() {
77 return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP)
78 >= Build.VERSION_CODES.M;
79 }
80
81 /**
82 * Determines if this version is compatible with call subject. Can also force the version to
83 * be lower through SdkVersionOverride.
84 *
85 * @return {@code true} if call subject is a feature on this device, {@code false} otherwise.
86 */
87 public static boolean isCallSubjectCompatible() {
88 return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP)
89 >= Build.VERSION_CODES.M;
90 }
Nancy Chen04538af2015-12-15 11:58:36 -080091
92 /**
93 * Determines if this version is compatible with Marshmallow-specific APIs. Can also force the
94 * version to be lower through SdkVersionOverride.
95 *
96 * @return {@code true} if call subject is a feature on this device, {@code false} otherwise.
97 */
98 public static boolean isMarshmallowCompatible() {
99 return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP)
100 >= Build.VERSION_CODES.M;
101 }
Brandon Maxwell973c0e12015-12-15 17:09:14 -0800102
103 /**
104 * Determines if the given class is available. Can be used to check if system apis exist at
105 * runtime.
106 *
107 * @param className the name of the class to look for.
108 * @return {@code true} if the given class is available, {@code false} otherwise or if className
109 * is null.
110 */
111 public static boolean isClassAvailable(@Nullable String className) {
112 if (className == null) {
113 return false;
114 }
115 try {
116 Class.forName(className);
117 return true;
118 } catch (ClassNotFoundException e) {
119 return false;
120 } catch (Throwable t) {
121 Log.e(TAG, "Unexpected exception when checking if class exists at runtime", t);
122 return false;
123 }
124 }
Wenyi Wang54ea4b12015-12-16 14:18:59 -0800125
126 /**
127 * Determines if this version is compatible with Lollipop-specific APIs. Can also force the
128 * version to be lower through SdkVersionOverride.
129 *
130 * @return {@code true} if call subject is a feature on this device, {@code false} otherwise.
131 */
132 public static boolean isLollipopCompatible() {
133 return SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.LOLLIPOP)
134 >= Build.VERSION_CODES.LOLLIPOP;
135 }
Wenyi Wang6fa570f2015-11-24 15:44:11 -0800136}