mediapipe/mediapipe/util/android/asset_manager_util.h
MediaPipe Team 350fbb2100 Project import generated by Copybara.
GitOrigin-RevId: d073f8e21be2fcc0e503cb97c6695078b6b75310
2021-02-27 03:30:05 -05:00

112 lines
4.3 KiB
C++

// Copyright 2019 The MediaPipe Authors.
//
// 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.
#ifndef MEDIAPIPE_ANDROID_UTIL_ASSET_MANAGER_UTIL_H_
#define MEDIAPIPE_ANDROID_UTIL_ASSET_MANAGER_UTIL_H_
#include <string>
#include <vector>
#ifdef __ANDROID__
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <jni.h>
#include "mediapipe/framework/port/canonical_errors.h"
#include "mediapipe/framework/port/singleton.h"
#include "mediapipe/framework/port/status.h"
#include "mediapipe/framework/port/statusor.h"
namespace mediapipe {
// Thin wrapper over AAssetManager provided by JNI. This class is meant to be
// used as a singleton.
// Usage: Call InitializeFromActivity from a JNI function that has access to the
// Java activity in the Android application. This initializes the asset manager
// and now files bundled in the assets folder can be read using ReadFile().
class AssetManager {
public:
AssetManager(const AssetManager&) = delete;
AssetManager& operator=(const AssetManager&) = delete;
// Returns the asset manager if it has been set by a call to
// InitializeFromActivity, otherwise returns nullptr.
AAssetManager* GetAssetManager();
// Returns true if AAssetManager was successfully initialized.
bool InitializeFromAssetManager(JNIEnv* env, jobject local_asset_manager,
const std::string& cache_dir_path);
// Returns true if AAssetManager was successfully initialized.
// cache_dir_path should be set to activity.getCacheDir().getAbsolutePath().
// We could get it from the activity, but we have the Java layer pass it
// directly for convenience.
bool InitializeFromActivity(JNIEnv* env, jobject activity,
const std::string& cache_dir_path);
// Returns true if AAssetManager was successfully initialized.
ABSL_DEPRECATED("Use InitializeFromActivity instead.")
bool InitializeFromAssetManager(JNIEnv* env, jobject local_asset_manager);
// Returns true if AAssetManager was successfully initialized.
// cache_dir_path should be set to context.getCacheDir().getAbsolutePath().
// We could get it from the context, but we have the Java layer pass it
// directly for convenience.
bool InitializeFromContext(JNIEnv* env, jobject context,
const std::string& cache_dir_path);
// Checks if a file exists. Returns true on success, false otherwise. If it
// does exist, then 'is_dir' will be set to indicate whether the file is a
// directory.
bool FileExists(const std::string& filename, bool* is_dir = nullptr);
// Reads a file into output. Returns true on success, false otherwise.
bool ReadFile(const std::string& filename, std::string* output);
// Reads the raw bytes referred to by the supplied content URI. Returns true
// on success, false otherwise.
absl::Status ReadContentUri(const std::string& content_uri,
std::string* output);
// Returns the path to the Android cache directory. Will be empty if
// InitializeFromActivity has not been called.
const std::string& GetCacheDirPath();
// Caches the contents of the given asset as a file, and returns a path to
// that file. This can be used to pass an asset to APIs that require a path
// to a filesystem file.
absl::StatusOr<std::string> CachedFileFromAsset(
const std::string& asset_path);
private:
// Private constructor since this class is meant to be a singleton.
AssetManager() = default;
// Pointer to asset manager from JNI.
AAssetManager* asset_manager_ = nullptr;
// The context from which assets should be loaded.
jobject context_;
// Path to the Android cache directory for our context.
std::string cache_dir_path_;
friend class Singleton<AssetManager>;
};
} // namespace mediapipe
#endif // __ANDROID__
#endif // MEDIAPIPE_ANDROID_UTIL_ASSET_MANAGER_UTIL_H_