DrmRights.java revision b7e7bdfe784959ac8615851c2741eb9518a5afcf
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 *
34 */
35public class DrmRights {
36    private byte[] mData;
37    private String mMimeType;
38    private String mAccountId = "_NO_USER";
39    private String mSubscriptionId = "";
40
41    /**
42     * Creates a <code>DrmRights</code> object with the given parameters.
43     *
44     * @param rightsFilePath Path to the file containing rights information.
45     * @param mimeType MIME type.
46     */
47    public DrmRights(String rightsFilePath, String mimeType) {
48        File file = new File(rightsFilePath);
49        instantiate(file, mimeType);
50    }
51
52    /**
53     * Creates a <code>DrmRights</code> object with the given parameters.
54     *
55     * @param rightsFilePath Path to the file containing rights information.
56     * @param mimeType MIME type.
57     * @param accountId Account ID of the user.
58     */
59    public DrmRights(String rightsFilePath, String mimeType, String accountId) {
60        this(rightsFilePath, mimeType);
61
62        if (null != accountId && !accountId.equals("")) {
63            mAccountId = accountId;
64        }
65    }
66
67    /**
68     * Creates a <code>DrmRights</code> object with the given parameters.
69     *
70     * @param rightsFilePath Path to the file containing rights information.
71     * @param mimeType MIME type.
72     * @param accountId Account ID of the user.
73     * @param subscriptionId Subscription ID of the user.
74     */
75    public DrmRights(
76            String rightsFilePath, String mimeType, String accountId, String subscriptionId) {
77        this(rightsFilePath, mimeType);
78
79        if (null != accountId && !accountId.equals("")) {
80            mAccountId = accountId;
81        }
82
83        if (null != subscriptionId && !subscriptionId.equals("")) {
84            mSubscriptionId = subscriptionId;
85        }
86    }
87
88    /**
89     * Creates a <code>DrmRights</code> object with the given parameters.
90     *
91     * @param rightsFile File containing rights information.
92     * @param mimeType MIME type.
93     */
94    public DrmRights(File rightsFile, String mimeType) {
95        instantiate(rightsFile, mimeType);
96    }
97
98    private void instantiate(File rightsFile, String mimeType) {
99        try {
100            mData = DrmUtils.readBytes(rightsFile);
101        } catch (IOException e) {
102            e.printStackTrace();
103        }
104
105        mMimeType = mimeType;
106    }
107
108    /**
109     * Creates a <code>DrmRights</code> object with the given parameters.
110     *
111     * @param data A {@link ProcessedData} object containing rights information.
112     *             data could be null because it's optional for some DRM schemes.
113     * @param mimeType The MIME type.
114     */
115    public DrmRights(ProcessedData data, String mimeType) {
116        if (data != null) {
117            mData = data.getData();
118
119            String accountId = data.getAccountId();
120            if (null != accountId && !accountId.equals("")) {
121                mAccountId = accountId;
122            }
123
124            String subscriptionId = data.getSubscriptionId();
125            if (null != subscriptionId && !subscriptionId.equals("")) {
126                mSubscriptionId = subscriptionId;
127            }
128        }
129
130        mMimeType = mimeType;
131    }
132
133    /**
134     * Retrieves the rights data associated with this <code>DrmRights</code> object.
135     *
136     * @return A <code>byte</code> array representing the rights data.
137     */
138    public byte[] getData() {
139        return mData;
140    }
141
142    /**
143     * Retrieves the MIME type associated with this <code>DrmRights</code> object.
144     *
145     * @return The MIME type.
146     */
147    public String getMimeType() {
148        return mMimeType;
149    }
150
151    /**
152     * Retrieves the account ID associated with this <code>DrmRights</code> object.
153     *
154     * @return The account ID.
155     */
156    public String getAccountId() {
157        return mAccountId;
158    }
159
160    /**
161     * Retrieves the subscription ID associated with this <code>DrmRights</code> object.
162     *
163     * @return The subscription ID.
164     */
165    public String getSubscriptionId() {
166        return mSubscriptionId;
167    }
168
169    /**
170     * Determines whether this instance is valid or not.
171     *
172     * @return True if valid; false if invalid.
173     */
174    /*package*/ boolean isValid() {
175        return (null != mMimeType && !mMimeType.equals("")
176                && null != mData && mData.length > 0);
177    }
178}
179
180