1/*
2 * Copyright (C) 2015 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 */
16package com.android.messaging.datamodel.media;
17
18import android.content.Context;
19
20import com.android.messaging.util.Assert;
21
22/**
23 * The base ImageRequest descriptor that describes the requirement of the requested image
24 * resource, including the desired size. It holds request info that will be consumed by
25 * ImageRequest instances. Subclasses of ImageRequest are expected to take
26 * more descriptions such as content URI or file path.
27 */
28public abstract class ImageRequestDescriptor extends MediaRequestDescriptor<ImageResource> {
29    /** Desired size for the image (if known). This is used for bitmap downsampling */
30    public final int desiredWidth;
31    public final int desiredHeight;
32
33    /** Source size of the image (if known). This is used so that we don't have to manually decode
34     *  the metrics from the image resource */
35    public final int sourceWidth;
36    public final int sourceHeight;
37
38    /**
39     * A static image resource is required, even if the image format supports animation (like Gif).
40     */
41    public final boolean isStatic;
42
43    /**
44     * The loaded image will be cropped to circular shape.
45     */
46    public final boolean cropToCircle;
47
48    /**
49     * The loaded image will be cropped to circular shape with the background color.
50     */
51    public final int circleBackgroundColor;
52
53    /**
54     * The loaded image will be cropped to circular shape with a stroke color.
55     */
56    public final int circleStrokeColor;
57
58    protected static final char KEY_PART_DELIMITER = '|';
59
60    /**
61     * Creates a new image request with unspecified width and height. In this case, the full
62     * bitmap is loaded and decoded, so unless you are sure that the image will be of
63     * reasonable size, you should consider limiting at least one of the two dimensions
64     * (for example, limiting the image width to the width of the ImageView container).
65     */
66    public ImageRequestDescriptor() {
67        this(ImageRequest.UNSPECIFIED_SIZE, ImageRequest.UNSPECIFIED_SIZE,
68                ImageRequest.UNSPECIFIED_SIZE, ImageRequest.UNSPECIFIED_SIZE, false, false, 0, 0);
69    }
70
71    public ImageRequestDescriptor(final int desiredWidth, final int desiredHeight) {
72        this(desiredWidth, desiredHeight,
73                ImageRequest.UNSPECIFIED_SIZE, ImageRequest.UNSPECIFIED_SIZE, false, false, 0, 0);
74    }
75
76    public ImageRequestDescriptor(final int desiredWidth,
77            final int desiredHeight, final int sourceWidth, final int sourceHeight,
78            final boolean isStatic, final boolean cropToCircle, final int circleBackgroundColor,
79            int circleStrokeColor) {
80        Assert.isTrue(desiredWidth == ImageRequest.UNSPECIFIED_SIZE || desiredWidth > 0);
81        Assert.isTrue(desiredHeight == ImageRequest.UNSPECIFIED_SIZE || desiredHeight > 0);
82        Assert.isTrue(sourceWidth == ImageRequest.UNSPECIFIED_SIZE || sourceWidth > 0);
83        Assert.isTrue(sourceHeight == ImageRequest.UNSPECIFIED_SIZE || sourceHeight > 0);
84        this.desiredWidth = desiredWidth;
85        this.desiredHeight = desiredHeight;
86        this.sourceWidth = sourceWidth;
87        this.sourceHeight = sourceHeight;
88        this.isStatic = isStatic;
89        this.cropToCircle = cropToCircle;
90        this.circleBackgroundColor = circleBackgroundColor;
91        this.circleStrokeColor = circleStrokeColor;
92    }
93
94    public String getKey() {
95        return new StringBuilder()
96                .append(desiredWidth).append(KEY_PART_DELIMITER)
97                .append(desiredHeight).append(KEY_PART_DELIMITER)
98                .append(String.valueOf(cropToCircle)).append(KEY_PART_DELIMITER)
99                .append(String.valueOf(circleBackgroundColor)).append(KEY_PART_DELIMITER)
100                .append(String.valueOf(isStatic)).toString();
101    }
102
103    public boolean isStatic() {
104        return isStatic;
105    }
106
107    @Override
108    public abstract MediaRequest<ImageResource> buildSyncMediaRequest(Context context);
109
110    // Called once source dimensions finally determined upon loading the image
111    public void updateSourceDimensions(final int sourceWidth, final int sourceHeight) {
112    }
113}