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