1/*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.managedprovisioning.parser;
17
18import static com.android.managedprovisioning.common.StoreUtils.DIR_PROVISIONING_PARAMS_FILE_CACHE;
19
20import android.content.Context;
21import android.net.Uri;
22import android.support.annotation.Nullable;
23
24import com.android.managedprovisioning.common.StoreUtils;
25
26import java.io.File;
27
28/**
29 * parser for {@link EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_ICON_URI} into a local file
30 */
31public class DeviceAdminIconParser {
32    private static final String FILE_PREFIX = "device_admin_icon_";
33    private final Context mContext;
34    private final File mFileIcon;
35
36    public DeviceAdminIconParser(Context context, long provisioningId) {
37        mContext = context;
38        mFileIcon =  new File(new File(mContext.getFilesDir(), DIR_PROVISIONING_PARAMS_FILE_CACHE),
39                FILE_PREFIX + provisioningId);
40    }
41
42    /**
43     * @return absolute path of the local file cache of the icon. Otherwise, return null.
44     */
45    @Nullable
46    public String parse(Uri uri) {
47        if (uri == null) {
48            return null;
49        }
50
51        mFileIcon.getParentFile().mkdirs();
52        boolean success = StoreUtils.copyUriIntoFile(mContext.getContentResolver(), uri, mFileIcon);
53        return success ? mFileIcon.getAbsolutePath() : null;
54    }
55}
56