1/*
2 * Copyright (C) 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 */
16
17package com.android.storagemanager.utils;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.webkit.MimeTypeMap;
22
23import java.io.File;
24
25/**
26 * IconProvider is a class for getting file icons. It is strongly based upon the DocumentsUI
27 * implementation of IconUtils, but tailored for the Deletion Helper use-case. Useful for testing.
28 */
29public class IconProvider {
30    private Context mContext;
31
32    public IconProvider(Context context) {
33        mContext = context;
34    }
35
36    /**
37     * Returns an icon which represents a file with the given MIME type.
38     *
39     * @param mimeType The MIME type of the file.
40     * @return
41     */
42    public Drawable loadMimeIcon(String mimeType) {
43        return mContext.getContentResolver().getTypeDrawable(mimeType);
44    }
45
46    public static String getMimeType(File file) {
47        String name = file.getName();
48        final int lastDot = name.lastIndexOf('.');
49        if (lastDot >= 0) {
50            final String extension = name.substring(lastDot + 1);
51            final String mimeType =
52                    MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
53            if (mimeType != null) {
54                return mimeType;
55            }
56        }
57        return "application/octet-stream";
58    }
59
60    public static boolean isImageType(File file) {
61        String mimeType = getMimeType(file);
62        return mimeType.startsWith("image/");
63    }
64
65}
66