1 /*
2  * Copyright (C) 2011 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  */
17 
18 package com.android.providers.downloads;
19 
20 import android.content.Context;
21 import android.drm.DrmManagerClient;
22 
23 import java.io.File;
24 
25 public class DownloadDrmHelper {
26 
27     /** The MIME type of special DRM files */
28     public static final String MIMETYPE_DRM_MESSAGE = "application/vnd.oma.drm.message";
29 
30     /** The extensions of special DRM files */
31     public static final String EXTENSION_DRM_MESSAGE = ".dm";
32 
33     public static final String EXTENSION_INTERNAL_FWDL = ".fl";
34 
35     /**
36      * Checks if the Media Type needs to be DRM converted
37      *
38      * @param mimetype Media type of the content
39      * @return True if convert is needed else false
40      */
isDrmConvertNeeded(String mimetype)41     public static boolean isDrmConvertNeeded(String mimetype) {
42         return MIMETYPE_DRM_MESSAGE.equals(mimetype);
43     }
44 
45     /**
46      * Modifies the file extension for a DRM Forward Lock file NOTE: This
47      * function shouldn't be called if the file shouldn't be DRM converted
48      */
modifyDrmFwLockFileExtension(String filename)49     public static String modifyDrmFwLockFileExtension(String filename) {
50         if (filename != null) {
51             int extensionIndex;
52             extensionIndex = filename.lastIndexOf(".");
53             if (extensionIndex != -1) {
54                 filename = filename.substring(0, extensionIndex);
55             }
56             filename = filename.concat(EXTENSION_INTERNAL_FWDL);
57         }
58         return filename;
59     }
60 
61     /**
62      * Return the original MIME type of the given file, using the DRM framework
63      * if the file is protected content.
64      */
getOriginalMimeType(Context context, File file, String currentMime)65     public static String getOriginalMimeType(Context context, File file, String currentMime) {
66         final DrmManagerClient client = new DrmManagerClient(context);
67         try {
68             final String rawFile = file.toString();
69             if (client.canHandle(rawFile, null)) {
70                 return client.getOriginalMimeType(rawFile);
71             } else {
72                 return currentMime;
73             }
74         } finally {
75             client.release();
76         }
77     }
78 }
79