//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
package com.microsoft.cognitiveservices.speech.util;

import android.content.Context;

/*! \cond INTERNAL */

/**
 * Internal class to hold the application context. This is needed for the SDK to be able to
 * access assets, get files or cache directory location, etc.
 */
public final class ContextHolder {

    /**
     * The singleton instance of the ContextHolder.
     */
    private volatile static ContextHolder instance;

    /**
     * The application context.
     */
    private Context appContext;

    /**
     * Creates and returns the singleton instance of the ContextHolder.
     * @return The singleton instance of the ContextHolder.
     */
    public synchronized static ContextHolder getInstance() {
        if (instance == null) {
            instance = new ContextHolder();
        }
        return instance;
    }

    /**
     * Private constructor.
     */
    private ContextHolder() {
    }

    /**
     * Sets the application context.
     * @param context The context.
     */
    public void setContext(Context context) {
        appContext = (context != null) ? context.getApplicationContext() : null;
    }

    /**
     * Returns the application context.
     * @return The application context.
     */
    public Context getContext() {
        return appContext;
    }
}

/*! \endcond */