1 /*
2  * Copyright (C) 2012 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.google.android.mms.util;
19 
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.Context;
22 import android.drm.DrmManagerClient;
23 import android.util.Log;
24 
25 public class DownloadDrmHelper {
26     private static final String TAG = "DownloadDrmHelper";
27 
28     /** The MIME type of special DRM files */
29     public static final String MIMETYPE_DRM_MESSAGE = "application/vnd.oma.drm.message";
30 
31     /** The extensions of special DRM files */
32     public static final String EXTENSION_DRM_MESSAGE = ".dm";
33 
34     public static final String EXTENSION_INTERNAL_FWDL = ".fl";
35 
36     /**
37      * Checks if the Media Type is a DRM Media Type
38      *
39      * @param drmManagerClient A DrmManagerClient
40      * @param mimetype Media Type to check
41      * @return True if the Media Type is DRM else false
42      */
isDrmMimeType(Context context, String mimetype)43     public static boolean isDrmMimeType(Context context, String mimetype) {
44         boolean result = false;
45         if (context != null) {
46             try {
47                 DrmManagerClient drmClient = new DrmManagerClient(context);
48                 if (drmClient != null && mimetype != null && mimetype.length() > 0) {
49                     result = drmClient.canHandle("", mimetype);
50                 }
51             } catch (IllegalArgumentException e) {
52                 Log.w(TAG,
53                         "DrmManagerClient instance could not be created, context is Illegal.");
54             } catch (IllegalStateException e) {
55                 Log.w(TAG, "DrmManagerClient didn't initialize properly.");
56             }
57         }
58         return result;
59     }
60 
61     /**
62      * Checks if the Media Type needs to be DRM converted
63      *
64      * @param mimetype Media type of the content
65      * @return True if convert is needed else false
66      */
67     @UnsupportedAppUsage
isDrmConvertNeeded(String mimetype)68     public static boolean isDrmConvertNeeded(String mimetype) {
69         return MIMETYPE_DRM_MESSAGE.equals(mimetype);
70     }
71 
72     /**
73      * Modifies the file extension for a DRM Forward Lock file NOTE: This
74      * function shouldn't be called if the file shouldn't be DRM converted
75      */
76     @UnsupportedAppUsage
modifyDrmFwLockFileExtension(String filename)77     public static String modifyDrmFwLockFileExtension(String filename) {
78         if (filename != null) {
79             int extensionIndex;
80             extensionIndex = filename.lastIndexOf(".");
81             if (extensionIndex != -1) {
82                 filename = filename.substring(0, extensionIndex);
83             }
84             filename = filename.concat(EXTENSION_INTERNAL_FWDL);
85         }
86         return filename;
87     }
88 
89     /**
90      * Gets the original mime type of DRM protected content.
91      *
92      * @param context The context
93      * @param path Path to the file
94      * @param containingMime The current mime type of the file i.e. the
95      *            containing mime type
96      * @return The original mime type of the file if DRM protected else the
97      *         currentMime
98      */
getOriginalMimeType(Context context, String path, String containingMime)99     public static String getOriginalMimeType(Context context, String path, String containingMime) {
100         String result = containingMime;
101         DrmManagerClient drmClient = new DrmManagerClient(context);
102         try {
103             if (drmClient.canHandle(path, null)) {
104                 result = drmClient.getOriginalMimeType(path);
105             }
106         } catch (IllegalArgumentException ex) {
107             Log.w(TAG,
108                     "Can't get original mime type since path is null or empty string.");
109         } catch (IllegalStateException ex) {
110             Log.w(TAG, "DrmManagerClient didn't initialize properly.");
111         }
112         return result;
113     }
114 }
115