DrmRights.java revision d074e30ce44b9e33da43b67a4515b8986ca72b26
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 * This is an entity class which wraps the license information which was
24 * retrieved from the online DRM server.
25 *
26 * Caller can instantiate {@link DrmRights} by
27 * invoking {@link DrmRights#DrmRights(ProcessedData, String)}
28 * constructor by using the result of {@link DrmManagerClient#processDrmInfo(DrmInfo)} interface.
29 * Caller can also instantiate {@link DrmRights} using the file path
30 * which contains rights information.
31 *
32 */
33public class DrmRights {
34    private byte[] mData;
35    private String mMimeType;
36    private String mAccountId = "_NO_USER";
37    private String mSubscriptionId = "";
38
39    /**
40     * constructor to create DrmRights object with given parameters
41     *
42     * @param rightsFilePath Path of the file containing rights data
43     * @param mimeType MIME type
44     */
45    public DrmRights(String rightsFilePath, String mimeType) {
46        File file = new File(rightsFilePath);
47        instantiate(file, mimeType);
48    }
49
50    /**
51     * constructor to create DrmRights object with given parameters
52     *
53     * @param rightsFilePath Path of the file containing rights data
54     * @param mimeType MIME type
55     * @param accountId Account Id of the user
56     */
57    public DrmRights(String rightsFilePath, String mimeType, String accountId) {
58        this(rightsFilePath, mimeType);
59
60        if (null != accountId && !accountId.equals("")) {
61            mAccountId = accountId;
62        }
63    }
64
65    /**
66     * constructor to create DrmRights object with given parameters
67     *
68     * @param rightsFilePath Path of the file containing rights data
69     * @param mimeType MIME type
70     * @param accountId Account Id of the user
71     * @param subscriptionId Subscription Id of the user
72     */
73    public DrmRights(
74            String rightsFilePath, String mimeType, String accountId, String subscriptionId) {
75        this(rightsFilePath, mimeType);
76
77        if (null != accountId && !accountId.equals("")) {
78            mAccountId = accountId;
79        }
80
81        if (null != subscriptionId && !subscriptionId.equals("")) {
82            mSubscriptionId = subscriptionId;
83        }
84    }
85
86    /**
87     * constructor to create DrmRights object with given parameters
88     *
89     * @param rightsFile File containing rights data
90     * @param mimeType MIME type
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    }
105
106    /**
107     * constructor to create DrmRights object with given parameters
108     * The user can pass String or binary data<p>
109     * Usage:<p>
110     *        i)  String(e.g. data is instance of String):<br>
111     *            - new DrmRights(data.getBytes(), mimeType)<p>
112     *        ii) Binary data<br>
113     *            - new DrmRights(binaryData[], mimeType)<br>
114     *
115     * @param data Processed data
116     * @param mimeType MIME type
117     */
118    public DrmRights(ProcessedData data, String mimeType) {
119        mData = data.getData();
120
121        String accountId = data.getAccountId();
122        if (null != accountId && !accountId.equals("")) {
123            mAccountId = accountId;
124        }
125
126        String subscriptionId = data.getSubscriptionId();
127        if (null != subscriptionId && !subscriptionId.equals("")) {
128            mSubscriptionId = subscriptionId;
129        }
130
131        mMimeType = mimeType;
132    }
133
134    /**
135     * Returns the rights data associated with this object
136     *
137     * @return Rights data
138     */
139    public byte[] getData() {
140        return mData;
141    }
142
143    /**
144     * Returns the mimetype associated with this object
145     *
146     * @return MIME type
147     */
148    public String getMimeType() {
149        return mMimeType;
150    }
151
152    /**
153     * Returns the account-id associated with this object
154     *
155     * @return Account Id
156     */
157    public String getAccountId() {
158        return mAccountId;
159    }
160
161    /**
162     * Returns the subscription-id associated with this object
163     *
164     * @return Subscription Id
165     */
166    public String getSubscriptionId() {
167        return mSubscriptionId;
168    }
169
170    /**
171     * Returns whether this instance is valid or not
172     *
173     * @return
174     *     true if valid
175     *     false if invalid
176     */
177    /*package*/ boolean isValid() {
178        return (null != mMimeType && !mMimeType.equals("")
179                && null != mData && mData.length > 0);
180    }
181}
182
183