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.util.ArrayList;
     20 import java.util.Iterator;
     21 
     22 /**
     23  * An entity class that wraps the capability of each DRM plug-in (agent),
     24  * such as the MIME type and file suffix the DRM plug-in can handle.
     25  *<p>
     26  * Plug-in developers can expose the capability of their plug-in by passing an instance of this
     27  * class to an application.
     28  *
     29  */
     30 public class DrmSupportInfo {
     31     private final ArrayList<String> mFileSuffixList = new ArrayList<String>();
     32     private final ArrayList<String> mMimeTypeList = new ArrayList<String>();
     33     private String mDescription = "";
     34 
     35     /**
     36      * Adds the specified MIME type to the list of MIME types this DRM plug-in supports.
     37      *
     38      * @param mimeType MIME type that can be handles by this DRM plug-in.
     39      * Must not be null or an empty string.
     40      */
     41     public void addMimeType(String mimeType) {
     42         if (mimeType == null) {
     43             throw new IllegalArgumentException("mimeType is null");
     44         }
     45         if (mimeType == "") {
     46             throw new IllegalArgumentException("mimeType is an empty string");
     47         }
     48 
     49         mMimeTypeList.add(mimeType);
     50     }
     51 
     52     /**
     53      * Adds the specified file suffix to the list of file suffixes this DRM plug-in supports.
     54      *
     55      * @param fileSuffix File suffix that can be handled by this DRM plug-in.
     56      * it could be null but not an empty string. When it is null, it indicates
     57      * that some DRM content comes with no file suffix.
     58      */
     59     public void addFileSuffix(String fileSuffix) {
     60         if (fileSuffix == "") {
     61             throw new IllegalArgumentException("fileSuffix is an empty string");
     62         }
     63 
     64         mFileSuffixList.add(fileSuffix);
     65     }
     66 
     67     /**
     68      * Retrieves an iterator object that you can use to iterate over the MIME types that
     69      * this DRM plug-in supports.
     70      *
     71      * @return The iterator object
     72      */
     73     public Iterator<String> getMimeTypeIterator() {
     74         return mMimeTypeList.iterator();
     75     }
     76 
     77     /**
     78      * Retrieves an iterator object that you can use to iterate over the file suffixes that
     79      * this DRM plug-in supports.
     80      *
     81      * @return The iterator object.
     82      */
     83     public Iterator<String> getFileSuffixIterator() {
     84         return mFileSuffixList.iterator();
     85     }
     86 
     87     /**
     88      * Sets a description for the DRM plug-in (agent).
     89      *
     90      * @param description Unique description of plug-in. Must not be null
     91      * or an empty string.
     92      */
     93     public void setDescription(String description) {
     94         if (description == null) {
     95             throw new IllegalArgumentException("description is null");
     96         }
     97         if (description == "") {
     98             throw new IllegalArgumentException("description is an empty string");
     99         }
    100 
    101         mDescription = description;
    102     }
    103 
    104     /**
    105      * Retrieves the DRM plug-in (agent) description.
    106      *
    107      * @return The plug-in description.
    108      * @deprecated The method name is mis-spelled, and it is replaced by
    109      * {@link #getDescription()}.
    110      */
    111     public String getDescriprition() {
    112         return mDescription;
    113     }
    114 
    115     /**
    116      * Retrieves the DRM plug-in (agent) description. Even if null or an empty
    117      * string is not allowed in {@link #setDescription(String)}, if
    118      * {@link #setDescription(String)} is not called, description returned
    119      * from this method is an empty string.
    120      *
    121      * @return The plug-in description.
    122      */
    123     public String getDescription() {
    124         return mDescription;
    125     }
    126 
    127     /**
    128      * Overridden hash code implementation.
    129      *
    130      * @return The hash code value.
    131      */
    132     public int hashCode() {
    133         return mFileSuffixList.hashCode() + mMimeTypeList.hashCode() + mDescription.hashCode();
    134     }
    135 
    136     /**
    137      * Overridden <code>equals</code> implementation. Two DrmSupportInfo objects
    138      * are considered being equal if they support exactly the same set of mime
    139      * types, file suffixes, and has exactly the same description.
    140      *
    141      * @param object The object to be compared.
    142      * @return True if equal; false if not equal.
    143      */
    144     public boolean equals(Object object) {
    145         if (object instanceof DrmSupportInfo) {
    146             DrmSupportInfo info = (DrmSupportInfo) object;
    147             return mFileSuffixList.equals(info.mFileSuffixList) &&
    148                    mMimeTypeList.equals(info.mMimeTypeList) &&
    149                    mDescription.equals(info.mDescription);
    150         }
    151         return false;
    152     }
    153 
    154     /**
    155      * Determines whether a given MIME type is supported.
    156      *
    157      * @param mimeType MIME type.
    158      * @return True if Mime type is supported; false if MIME type is not supported.
    159      * Null or empty string is not a supported mimeType.
    160      */
    161     /* package */ boolean isSupportedMimeType(String mimeType) {
    162         if (null != mimeType && !mimeType.equals("")) {
    163             for (int i = 0; i < mMimeTypeList.size(); i++) {
    164                 String completeMimeType = mMimeTypeList.get(i);
    165 
    166                 // The reason that equals() is not used is that sometimes,
    167                 // content distributor might just append something to
    168                 // the basic MIME type. startsWith() is used to avoid
    169                 // frequent update of DRM agent.
    170                 if (completeMimeType.startsWith(mimeType)) {
    171                     return true;
    172                 }
    173             }
    174         }
    175         return false;
    176     }
    177 
    178     /**
    179      * Determines whether a given file suffix is supported.
    180      *
    181      * @param fileSuffix File suffix.
    182      * @return True if file suffix is supported; false if file suffix is not supported.
    183      */
    184     /* package */ boolean isSupportedFileSuffix(String fileSuffix) {
    185         return mFileSuffixList.contains(fileSuffix);
    186     }
    187 }
    188 
    189