CarrierContentRestriction.java revision 21b919f737006ffd654ac788aeedcaed05f5b367
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 */
17package com.android.mms.model;
18
19import java.util.ArrayList;
20
21import android.content.ContentResolver;
22import android.provider.Settings;
23
24import com.google.android.mms.ContentType;
25import com.android.mms.ContentRestrictionException;
26import com.android.mms.ExceedMessageSizeException;
27import com.android.mms.ResolutionException;
28import com.android.mms.UnsupportContentTypeException;
29
30public class CarrierContentRestriction implements ContentRestriction {
31    public static final int IMAGE_WIDTH_LIMIT  = 640;
32    public static final int IMAGE_HEIGHT_LIMIT = 480;
33    public static final int MESSAGE_SIZE_LIMIT = 300 * 1024;
34
35    private static final ArrayList<String> sSupportedImageTypes;
36    private static final ArrayList<String> sSupportedAudioTypes;
37    private static final ArrayList<String> sSupportedVideoTypes;
38
39    static {
40        sSupportedImageTypes = ContentType.getImageTypes();
41        sSupportedAudioTypes = ContentType.getAudioTypes();
42        sSupportedVideoTypes = ContentType.getVideoTypes();
43    }
44
45    public CarrierContentRestriction() {
46    }
47
48    public void checkMessageSize(int messageSize, int increaseSize, ContentResolver resolver)
49            throws ContentRestrictionException {
50        if ( (messageSize < 0) || (increaseSize < 0) ) {
51            throw new ContentRestrictionException("Negative message size"
52                    + " or increase size");
53        }
54        int messageSizeLimit;
55        try {
56            // Don't cache the max message size. Grab it each time so we'll dynamically
57            // respond to changes made on the server.
58            messageSizeLimit = Integer.parseInt(
59                    Settings.Gservices.getString(resolver,
60                            Settings.Gservices.MMS_MAXIMUM_MESSAGE_SIZE));
61        } catch (java.lang.NumberFormatException e) {
62            messageSizeLimit = MESSAGE_SIZE_LIMIT;
63        }
64        int newSize = messageSize + increaseSize;
65        if ( (newSize < 0) || (newSize > messageSizeLimit) ) {
66            throw new ExceedMessageSizeException("Exceed message size limitation");
67        }
68    }
69
70    public void checkResolution(int width, int height) throws ContentRestrictionException {
71        if ( (width > IMAGE_WIDTH_LIMIT) || (height > IMAGE_HEIGHT_LIMIT) ) {
72            throw new ResolutionException("content resolution exceeds restriction.");
73        }
74    }
75
76    public void checkImageContentType(String contentType)
77            throws ContentRestrictionException {
78        if (null == contentType) {
79            throw new ContentRestrictionException("Null content type to be check");
80        }
81
82        if (!sSupportedImageTypes.contains(contentType)) {
83            throw new UnsupportContentTypeException("Unsupported image content type : "
84                    + contentType);
85        }
86    }
87
88    public void checkAudioContentType(String contentType)
89            throws ContentRestrictionException {
90        if (null == contentType) {
91            throw new ContentRestrictionException("Null content type to be check");
92        }
93
94        if (!sSupportedAudioTypes.contains(contentType)) {
95            throw new UnsupportContentTypeException("Unsupported audio content type : "
96                    + contentType);
97        }
98    }
99
100    public void checkVideoContentType(String contentType)
101            throws ContentRestrictionException {
102        if (null == contentType) {
103            throw new ContentRestrictionException("Null content type to be check");
104        }
105
106        if (!sSupportedVideoTypes.contains(contentType)) {
107            throw new UnsupportContentTypeException("Unsupported video content type : "
108                    + contentType);
109        }
110    }
111}
112