Loading src/com/android/contacts/common/compat/CompatUtils.java +28 −0 Original line number Diff line number Diff line Loading @@ -16,11 +16,16 @@ package com.android.contacts.common.compat; import android.os.Build; import android.support.annotation.Nullable; import android.util.Log; import com.android.contacts.common.compat.SdkVersionOverride; import com.android.contacts.common.model.CPOWrapper; public final class CompatUtils { private static final String TAG = CompatUtils.class.getSimpleName(); /** * These 4 variables are copied from ContentProviderOperation for compatibility. */ Loading Loading @@ -94,4 +99,27 @@ public final class CompatUtils { 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 null. */ public static boolean isClassAvailable(@Nullable String className) { if (className == null) { 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 exists at runtime", t); return false; } } } tests/src/com/android/contacts/common/compat/CompatUtilsTest.java 0 → 100644 +30 −0 Original line number Diff line number Diff line /* * 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.contacts.common.compat; import android.test.AndroidTestCase; public class CompatUtilsTest extends AndroidTestCase { public void testClassAvailable() { assertTrue(CompatUtils.isClassAvailable(CompatUtils.class.getName())); } public void testClassNotAvailable() { assertFalse(CompatUtils.isClassAvailable("com.android.contacts.common.NonexistantClass")); } } Loading
src/com/android/contacts/common/compat/CompatUtils.java +28 −0 Original line number Diff line number Diff line Loading @@ -16,11 +16,16 @@ package com.android.contacts.common.compat; import android.os.Build; import android.support.annotation.Nullable; import android.util.Log; import com.android.contacts.common.compat.SdkVersionOverride; import com.android.contacts.common.model.CPOWrapper; public final class CompatUtils { private static final String TAG = CompatUtils.class.getSimpleName(); /** * These 4 variables are copied from ContentProviderOperation for compatibility. */ Loading Loading @@ -94,4 +99,27 @@ public final class CompatUtils { 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 null. */ public static boolean isClassAvailable(@Nullable String className) { if (className == null) { 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 exists at runtime", t); return false; } } }
tests/src/com/android/contacts/common/compat/CompatUtilsTest.java 0 → 100644 +30 −0 Original line number Diff line number Diff line /* * 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.contacts.common.compat; import android.test.AndroidTestCase; public class CompatUtilsTest extends AndroidTestCase { public void testClassAvailable() { assertTrue(CompatUtils.isClassAvailable(CompatUtils.class.getName())); } public void testClassNotAvailable() { assertFalse(CompatUtils.isClassAvailable("com.android.contacts.common.NonexistantClass")); } }