public final class

CloseGuardHelper

extends java.lang.Object

 java.lang.Object

↳androidx.camera.core.impl.utils.CloseGuardHelper

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-core', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-core
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-core:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Helper for accessing CloseGuard on API levels that support it.

All operations will be no-ops on non-supported API levels.

Summary

Methods
public voidclose()

Marks this CloseGuard instance as closed to avoid warnings on finalization.

public static CloseGuardHelpercreate()

Returns a CloseGuardHelper which defers to the platform close guard if it is available.

public voidopen(java.lang.String closeMethodName)

Initializes the instance with a warning that the caller should have explicitly called the closeMethodName method instead of relying on finalization.

public voidwarnIfOpen()

Logs a warning if the caller did not properly cleanup by calling an explicit close method before finalization.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static CloseGuardHelper create()

Returns a CloseGuardHelper which defers to the platform close guard if it is available.

public void open(java.lang.String closeMethodName)

Initializes the instance with a warning that the caller should have explicitly called the closeMethodName method instead of relying on finalization.

Parameters:

closeMethodName: non-null name of explicit termination method. Printed by warnIfOpen.

public void close()

Marks this CloseGuard instance as closed to avoid warnings on finalization.

public void warnIfOpen()

Logs a warning if the caller did not properly cleanup by calling an explicit close method before finalization.

Source

/*
 * Copyright 2021 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 androidx.camera.core.impl.utils;

import android.os.Build;
import android.util.CloseGuard;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.util.Preconditions;

/**
 * Helper for accessing CloseGuard on API levels that support it.
 *
 * <p>All operations will be no-ops on non-supported API levels.
 */
public final class CloseGuardHelper {

    private final CloseGuardImpl mImpl;

    private CloseGuardHelper(CloseGuardImpl impl) {
        mImpl = impl;
    }

    /**
     * Returns a {@link CloseGuardHelper} which defers to the platform close guard if it is
     * available.
     */
    @NonNull
    public static CloseGuardHelper create() {
        if (Build.VERSION.SDK_INT >= 30) {
            return new CloseGuardHelper(new CloseGuardApi30Impl());
        }

        return new CloseGuardHelper(new CloseGuardNoOpImpl());
    }

    /**
     * Initializes the instance with a warning that the caller should have explicitly called the
     * {@code closeMethodName} method instead of relying on finalization.
     *
     * @param closeMethodName non-null name of explicit termination method. Printed by
     *                        warnIfOpen.
     * @throws NullPointerException if closeMethodName is null.
     */
    public void open(@NonNull String closeMethodName) {
        mImpl.open(closeMethodName);
    }

    /** Marks this CloseGuard instance as closed to avoid warnings on finalization. */
    public void close() {
        mImpl.close();
    }

    /**
     * Logs a warning if the caller did not properly cleanup by calling an explicit close method
     * before finalization.
     */
    public void warnIfOpen() {
        mImpl.warnIfOpen();
    }

    private interface CloseGuardImpl {
        void open(@NonNull String closeMethodName);
        void close();
        void warnIfOpen();
    }

    @RequiresApi(30)
    static final class CloseGuardApi30Impl implements CloseGuardImpl {

        private final CloseGuard mPlatformImpl = new CloseGuard();

        @Override
        public void open(@NonNull String closeMethodName) {
            mPlatformImpl.open(closeMethodName);
        }

        @Override
        public void close() {
            mPlatformImpl.close();
        }

        @Override
        public void warnIfOpen() {
            mPlatformImpl.warnIfOpen();
        }
    }

    static final class CloseGuardNoOpImpl implements CloseGuardImpl {

        @Override
        public void open(@NonNull String closeMethodName) {
            Preconditions.checkNotNull(closeMethodName, "CloseMethodName must not be null.");
        }

        @Override
        public void close() {
        }

        @Override
        public void warnIfOpen() {
        }
    }
}