Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 49344cd7 authored by Brandon Maxwell's avatar Brandon Maxwell Committed by Android (Google) Code Review
Browse files

Merge "Added utility method to check for classes at runtime" into ub-contactsdialer-b-dev

parents 308dfebd 973c0e1a
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -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.
     */
@@ -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;
        }
    }
}
+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"));
    }
}