Wenyi Wang | 6fa570f | 2015-11-24 15:44:11 -0800 | [diff] [blame] | 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 | package com.android.contacts.common.compat; |
| 17 | |
| 18 | import android.os.Build; |
Brandon Maxwell | 973c0e1 | 2015-12-15 17:09:14 -0800 | [diff] [blame] | 19 | import android.support.annotation.Nullable; |
| 20 | import android.util.Log; |
Wenyi Wang | 6fa570f | 2015-11-24 15:44:11 -0800 | [diff] [blame] | 21 | |
| 22 | import com.android.contacts.common.compat.SdkVersionOverride; |
Wenyi Wang | 93fdd48 | 2015-12-07 14:26:54 -0800 | [diff] [blame] | 23 | import com.android.contacts.common.model.CPOWrapper; |
Wenyi Wang | 6fa570f | 2015-11-24 15:44:11 -0800 | [diff] [blame] | 24 | |
| 25 | public final class CompatUtils { |
Brandon Maxwell | 973c0e1 | 2015-12-15 17:09:14 -0800 | [diff] [blame] | 26 | |
| 27 | private static final String TAG = CompatUtils.class.getSimpleName(); |
| 28 | |
Wenyi Wang | 6fa570f | 2015-11-24 15:44:11 -0800 | [diff] [blame] | 29 | /** |
Wenyi Wang | 93fdd48 | 2015-12-07 14:26:54 -0800 | [diff] [blame] | 30 | * 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 Wang | 6fa570f | 2015-11-24 15:44:11 -0800 | [diff] [blame] | 52 | * 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 Chen | eab0b82 | 2015-11-25 15:44:48 -0800 | [diff] [blame] | 58 | |
| 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 Chen | 5fe647d | 2015-12-08 16:57:48 -0800 | [diff] [blame] | 69 | |
| 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 Chen | 04538af | 2015-12-15 11:58:36 -0800 | [diff] [blame] | 91 | |
| 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 Maxwell | 973c0e1 | 2015-12-15 17:09:14 -0800 | [diff] [blame] | 102 | |
| 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 Wang | 54ea4b1 | 2015-12-16 14:18:59 -0800 | [diff] [blame^] | 125 | |
| 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 Wang | 6fa570f | 2015-11-24 15:44:11 -0800 | [diff] [blame] | 136 | } |