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