1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.media;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.action.UpdateMessagePartSizeAction;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.MessagePartData;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ImageUtils;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Image descriptor attached to a message part.
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Once image size is determined during loading this descriptor will update the db if necessary.
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class MessagePartImageRequestDescriptor extends UriImageRequestDescriptor {
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final String mMessagePartId;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Creates a new image request for a message part.
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public MessagePartImageRequestDescriptor(final MessagePartData messagePart,
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int desiredWidth, final int desiredHeight, boolean isStatic) {
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Pull image parameters out of the MessagePart record
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        this(messagePart.getPartId(), messagePart.getContentUri(), desiredWidth, desiredHeight,
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                messagePart.getWidth(), messagePart.getHeight(), isStatic);
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected MessagePartImageRequestDescriptor(final String messagePartId, final Uri contentUri,
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int desiredWidth, final int desiredHeight, final int sourceWidth,
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int sourceHeight, boolean isStatic) {
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super(contentUri, desiredWidth, desiredHeight, sourceWidth, sourceHeight,
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                true /* allowCompression */, isStatic, false /* cropToCircle */,
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                ImageUtils.DEFAULT_CIRCLE_BACKGROUND_COLOR /* circleBackgroundColor */,
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                ImageUtils.DEFAULT_CIRCLE_STROKE_COLOR /* circleStrokeColor */);
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMessagePartId = messagePartId;
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void updateSourceDimensions(final int updatedWidth, final int updatedHeight) {
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // If the dimensions of the image do not match then queue a DB update with new size.
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Don't update if we don't have a part id, which happens if this part is loaded as
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // draft through actions such as share intent/message forwarding.
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mMessagePartId != null &&
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                updatedWidth != MessagePartData.UNSPECIFIED_SIZE &&
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                updatedHeight != MessagePartData.UNSPECIFIED_SIZE &&
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                updatedWidth != sourceWidth && updatedHeight != sourceHeight) {
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            UpdateMessagePartSizeAction.updateSize(mMessagePartId, updatedWidth, updatedHeight);
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
65