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