102c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root/*
202c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * Copyright (C) 2010 The Android Open Source Project
302c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root *
402c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * Licensed under the Apache License, Version 2.0 (the "License");
502c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * you may not use this file except in compliance with the License.
602c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * You may obtain a copy of the License at
702c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root *
802c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root *      http://www.apache.org/licenses/LICENSE-2.0
902c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root *
1002c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * Unless required by applicable law or agreed to in writing, software
1102c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * distributed under the License is distributed on an "AS IS" BASIS,
1202c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1302c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * See the License for the specific language governing permissions and
1402c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root * limitations under the License.
1502c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root */
1602c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root
1702c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Rootpackage android.content.res;
1802c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root
1905105f7abe02b2dff91d6260b3628c8b97816babKenny Rootimport java.io.File;
2005105f7abe02b2dff91d6260b3628c8b97816babKenny Rootimport java.io.IOException;
2105105f7abe02b2dff91d6260b3628c8b97816babKenny Root
2202c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root/**
2305105f7abe02b2dff91d6260b3628c8b97816babKenny Root * Class to scan Opaque Binary Blob (OBB) files. Use this to get information
2405105f7abe02b2dff91d6260b3628c8b97816babKenny Root * about an OBB file for use in a program via {@link ObbInfo}.
2502c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root */
2602c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Rootpublic class ObbScanner {
2702c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root    // Don't allow others to instantiate this class
2802c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root    private ObbScanner() {}
2902c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root
3005105f7abe02b2dff91d6260b3628c8b97816babKenny Root    /**
3105105f7abe02b2dff91d6260b3628c8b97816babKenny Root     * Scan a file for OBB information.
3205105f7abe02b2dff91d6260b3628c8b97816babKenny Root     *
3305105f7abe02b2dff91d6260b3628c8b97816babKenny Root     * @param filePath path to the OBB file to be scanned.
3405105f7abe02b2dff91d6260b3628c8b97816babKenny Root     * @return ObbInfo object information corresponding to the file path
3505105f7abe02b2dff91d6260b3628c8b97816babKenny Root     * @throws IllegalArgumentException if the OBB file couldn't be found
3605105f7abe02b2dff91d6260b3628c8b97816babKenny Root     * @throws IOException if the OBB file couldn't be read
3705105f7abe02b2dff91d6260b3628c8b97816babKenny Root     */
3805105f7abe02b2dff91d6260b3628c8b97816babKenny Root    public static ObbInfo getObbInfo(String filePath) throws IOException {
3902c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root        if (filePath == null) {
4005105f7abe02b2dff91d6260b3628c8b97816babKenny Root            throw new IllegalArgumentException("file path cannot be null");
4102c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root        }
4202c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root
4305105f7abe02b2dff91d6260b3628c8b97816babKenny Root        final File obbFile = new File(filePath);
4405105f7abe02b2dff91d6260b3628c8b97816babKenny Root        if (!obbFile.exists()) {
45f1121dc1d35c7e8c317c278aad0dd4ad1358d870Kenny Root            throw new IllegalArgumentException("OBB file does not exist: " + filePath);
4602c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root        }
4705105f7abe02b2dff91d6260b3628c8b97816babKenny Root
4838cf8867a8d3e8d5159abd0bd0e6a3b0b8348b94Kenny Root        /*
4938cf8867a8d3e8d5159abd0bd0e6a3b0b8348b94Kenny Root         * XXX This will fail to find the real canonical path if bind mounts are
5038cf8867a8d3e8d5159abd0bd0e6a3b0b8348b94Kenny Root         * used, but we don't use any bind mounts right now.
5138cf8867a8d3e8d5159abd0bd0e6a3b0b8348b94Kenny Root         */
5205105f7abe02b2dff91d6260b3628c8b97816babKenny Root        final String canonicalFilePath = obbFile.getCanonicalPath();
5305105f7abe02b2dff91d6260b3628c8b97816babKenny Root
5405105f7abe02b2dff91d6260b3628c8b97816babKenny Root        ObbInfo obbInfo = new ObbInfo();
5538cf8867a8d3e8d5159abd0bd0e6a3b0b8348b94Kenny Root        obbInfo.filename = canonicalFilePath;
5605105f7abe02b2dff91d6260b3628c8b97816babKenny Root        getObbInfo_native(canonicalFilePath, obbInfo);
5705105f7abe02b2dff91d6260b3628c8b97816babKenny Root
5802c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root        return obbInfo;
5902c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root    }
6002c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root
6105105f7abe02b2dff91d6260b3628c8b97816babKenny Root    private native static void getObbInfo_native(String filePath, ObbInfo obbInfo)
6205105f7abe02b2dff91d6260b3628c8b97816babKenny Root            throws IOException;
6302c8730c1bf19daf48bec8c6995df676a00a73b1Kenny Root}
64