1/*
2 * Copyright (C) 2008 Esmertec AG.
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mms.drm;
19
20import android.drm.DrmManagerClient;
21import android.drm.DrmStore;
22import android.net.Uri;
23import android.util.Log;
24
25import com.android.mms.MmsApp;
26
27public class DrmUtils {
28    private static final String TAG = "DrmUtils";
29
30    /** The MIME type of special DRM files */
31    private static final String EXTENSION_ANDROID_FWDL = ".fl";
32
33    private DrmUtils() {
34    }
35
36    public static String getConvertExtension(String mimetype) {
37        return EXTENSION_ANDROID_FWDL;
38    }
39
40    public static boolean isDrmType(String mimeType) {
41        boolean result = false;
42        DrmManagerClient drmManagerClient = MmsApp.getApplication().getDrmManagerClient();
43        if (drmManagerClient != null) {
44            try {
45                if (drmManagerClient.canHandle("", mimeType)) {
46                    result = true;
47                }
48            } catch (IllegalArgumentException ex) {
49                Log.w(TAG, "canHandle called with wrong parameters");
50            } catch (IllegalStateException ex) {
51                Log.w(TAG, "DrmManagerClient didn't initialize properly");
52            }
53        }
54        return result;
55    }
56
57    /**
58     * Check if content may be forwarded according to DRM
59     *
60     * @param uri Uri to content
61     * @return true if the content may be forwarded
62     */
63    public static boolean haveRightsForAction(Uri uri, int action) {
64        DrmManagerClient drmManagerClient = MmsApp.getApplication().getDrmManagerClient();
65
66        try {
67            // first check if the URI is registered as DRM in DRM-framework
68            if (drmManagerClient.canHandle(uri.toString(), null)) {
69                int check = drmManagerClient.checkRightsStatus(uri.toString(), action);
70                return (check == DrmStore.RightsStatus.RIGHTS_VALID);
71            }
72        } catch (Exception e) {
73            // Ignore exception and assume it is OK to forward file.
74        } finally {
75        }
76        return true;
77    }
78}
79