Home | History | Annotate | Download | only in drm
      1 /*
      2  * Copyright (C) 2010 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 package android.drm;
     18 
     19 import java.io.File;
     20 import java.io.IOException;
     21 import java.util.Arrays;
     22 
     23 /**
     24  * An entity class that wraps the license information retrieved from the online DRM server.
     25  *<p>
     26  * A caller can instantiate a {@link DrmRights} object by first invoking the
     27  * {@link DrmManagerClient#processDrmInfo(DrmInfo)} method and then using the resulting
     28  * {@link ProcessedData} object to invoke the {@link DrmRights#DrmRights(ProcessedData, String)}
     29  * constructor.
     30  *<p>
     31  * A caller can also instantiate a {@link DrmRights} object by using the
     32  * {@link DrmRights#DrmRights(String, String)} constructor, which takes a path to a file
     33  * containing rights information instead of a <code>ProcessedData</code>.
     34  *<p>
     35  * Please note that the account id and subscription id is not mandatory by all DRM agents
     36  * or plugins. When account id or subscription id is not required by the specific DRM
     37  * agent or plugin, they can be either null, or an empty string, or any other don't-care
     38  * string value.
     39  *
     40  */
     41 public class DrmRights {
     42     private byte[] mData;
     43     private String mMimeType;
     44     private String mAccountId;
     45     private String mSubscriptionId;
     46 
     47     /**
     48      * Creates a <code>DrmRights</code> object with the given parameters.
     49      *
     50      * @param rightsFilePath Path to the file containing rights information.
     51      * @param mimeType MIME type. Must not be null or an empty string.
     52      */
     53     public DrmRights(String rightsFilePath, String mimeType) {
     54         File file = new File(rightsFilePath);
     55         instantiate(file, mimeType);
     56     }
     57 
     58     /**
     59      * Creates a <code>DrmRights</code> object with the given parameters.
     60      *
     61      * @param rightsFilePath Path to the file containing rights information.
     62      * @param mimeType MIME type. Must not be null or an empty string.
     63      * @param accountId Account ID of the user.
     64      */
     65     public DrmRights(String rightsFilePath, String mimeType, String accountId) {
     66         this(rightsFilePath, mimeType);
     67 
     68         mAccountId = accountId;
     69     }
     70 
     71     /**
     72      * Creates a <code>DrmRights</code> object with the given parameters.
     73      *
     74      * @param rightsFilePath Path to the file containing rights information.
     75      * @param mimeType MIME type. Must not be null or an empty string.
     76      * @param accountId Account ID of the user.
     77      * @param subscriptionId Subscription ID of the user.
     78      */
     79     public DrmRights(
     80             String rightsFilePath, String mimeType, String accountId, String subscriptionId) {
     81         this(rightsFilePath, mimeType);
     82 
     83         mAccountId = accountId;
     84         mSubscriptionId = subscriptionId;
     85     }
     86 
     87     /**
     88      * Creates a <code>DrmRights</code> object with the given parameters.
     89      *
     90      * @param rightsFile File containing rights information.
     91      * @param mimeType MIME type. Must not be null or an empty string.
     92      */
     93     public DrmRights(File rightsFile, String mimeType) {
     94         instantiate(rightsFile, mimeType);
     95     }
     96 
     97     private void instantiate(File rightsFile, String mimeType) {
     98         try {
     99             mData = DrmUtils.readBytes(rightsFile);
    100         } catch (IOException e) {
    101             e.printStackTrace();
    102         }
    103 
    104         mMimeType = mimeType;
    105         if (!isValid()) {
    106             final String msg = "mimeType: " + mMimeType + "," +
    107                                "data: " + Arrays.toString(mData);
    108             throw new IllegalArgumentException(msg);
    109         }
    110     }
    111 
    112     /**
    113      * Creates a <code>DrmRights</code> object with the given parameters.
    114      *
    115      * @param data A {@link ProcessedData} object containing rights information.
    116      *             Must not be null.
    117      * @param mimeType The MIME type. It must not be null or an empty string.
    118      */
    119     public DrmRights(ProcessedData data, String mimeType) {
    120         if (data == null) {
    121             throw new IllegalArgumentException("data is null");
    122         }
    123 
    124         mData = data.getData();
    125         mAccountId = data.getAccountId();
    126         mSubscriptionId = data.getSubscriptionId();
    127         mMimeType = mimeType;
    128 
    129         if (!isValid()) {
    130             final String msg = "mimeType: " + mMimeType + "," +
    131                                "data: " + Arrays.toString(mData);
    132             throw new IllegalArgumentException(msg);
    133         }
    134     }
    135 
    136     /**
    137      * Retrieves the rights data associated with this <code>DrmRights</code> object.
    138      *
    139      * @return A <code>byte</code> array representing the rights data.
    140      */
    141     public byte[] getData() {
    142         return mData;
    143     }
    144 
    145     /**
    146      * Retrieves the MIME type associated with this <code>DrmRights</code> object.
    147      *
    148      * @return The MIME type.
    149      */
    150     public String getMimeType() {
    151         return mMimeType;
    152     }
    153 
    154     /**
    155      * Retrieves the account ID associated with this <code>DrmRights</code> object.
    156      *
    157      * @return The account ID.
    158      */
    159     public String getAccountId() {
    160         return mAccountId;
    161     }
    162 
    163     /**
    164      * Retrieves the subscription ID associated with this <code>DrmRights</code> object.
    165      *
    166      * @return The subscription ID.
    167      */
    168     public String getSubscriptionId() {
    169         return mSubscriptionId;
    170     }
    171 
    172     /**
    173      * Determines whether this instance is valid or not.
    174      *
    175      * @return True if valid; false if invalid.
    176      */
    177     /*package*/ boolean isValid() {
    178         return (null != mMimeType && !mMimeType.equals("")
    179                 && null != mData && mData.length > 0);
    180     }
    181 }
    182 
    183