1/*
2 * Copyright (C) 2007 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.mobile1;
18
19/**
20 * This class provides interfaces to access the DRM rights.
21 */
22public class DrmRights {
23    /**
24     * The DRM permission of play.
25     */
26    public static final int DRM_PERMISSION_PLAY = 1;
27
28    /**
29     * The DRM permission of display.
30     */
31    public static final int DRM_PERMISSION_DISPLAY = 2;
32
33    /**
34     * The DRM permission of execute.
35     */
36    public static final int DRM_PERMISSION_EXECUTE = 3;
37
38    /**
39     * The DRM permission of print.
40     */
41    public static final int DRM_PERMISSION_PRINT = 4;
42
43    /**
44     * Successful operation.
45     */
46    private static final int JNI_DRM_SUCCESS = 0;
47
48    /**
49     * General failure.
50     */
51    private static final int JNI_DRM_FAILURE = -1;
52
53    /**
54     * The uid of this rights object.
55     */
56    private String roId = "";
57
58
59    /**
60     * Construct the DrmRights.
61     */
62    public DrmRights() {
63    }
64
65    /**
66     * Get the constraint of the given permission on this rights object.
67     *
68     * @param permission    the given permission.
69     *
70     * @return a DrmConstraint instance.
71     */
72    public DrmConstraintInfo getConstraint(int permission) {
73        DrmConstraintInfo c = new DrmConstraintInfo();
74
75        /* call native method to get latest constraint information */
76        int res = nativeGetConstraintInfo(permission, c);
77
78        if (JNI_DRM_FAILURE == res)
79            return null;
80
81        return c;
82    }
83
84    /**
85     * Consume the rights of the given permission.
86     *
87     * @param permission    the given permission.
88     *
89     * @return true if consume success.
90     *         false if consume failure.
91     */
92    public boolean consumeRights(int permission) {
93        /* call native method to consume and update rights */
94        int res = nativeConsumeRights(permission);
95
96        if (JNI_DRM_FAILURE == res)
97            return false;
98
99        return true;
100    }
101
102
103    /**
104     * native method: get the constraint information of the given permission.
105     *
106     * @param permission    the given permission.
107     * @param constraint    the instance of constraint.
108     *
109     * @return #JNI_DRM_SUCCESS if succeed.
110     *         #JNI_DRM_FAILURE if fail.
111     */
112    private native int nativeGetConstraintInfo(int permission, DrmConstraintInfo constraint);
113
114    /**
115     * native method: consume the rights of the given permission.
116     *
117     * @param permission    the given permission.
118     *
119     * @return #JNI_DRM_SUCCESS if succeed.
120     *         #JNI_DRM_FAILURE if fail.
121     */
122    private native int nativeConsumeRights(int permission);
123
124
125    /**
126     * Load the shared library to link the native methods.
127     */
128    static {
129        try {
130            System.loadLibrary("drm1_jni");
131        }
132        catch (UnsatisfiedLinkError ule) {
133            System.err.println("WARNING: Could not load libdrm1_jni.so");
134        }
135    }
136}
137