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