Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
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
 
17
package com.facebook.internal;
18
 
19
import android.content.Context;
20
import android.net.Uri;
21
 
22
import java.net.URI;
23
import java.net.URISyntaxException;
24
 
25
public class ImageRequest {
26
 
27
    public interface Callback {
28
        /**
29
         * This method should always be called on the UI thread. ImageDownloader makes
30
         * sure to do this when it is responsible for issuing the ImageResponse
31
         * @param response
32
         */
33
        void onCompleted(ImageResponse response);
34
    }
35
 
36
    public static final int UNSPECIFIED_DIMENSION = 0;
37
 
38
    private static final String PROFILEPIC_URL_FORMAT =
39
            "https://graph.facebook.com/%s/picture";
40
    private static final String HEIGHT_PARAM = "height";
41
    private static final String WIDTH_PARAM = "width";
42
    private static final String MIGRATION_PARAM = "migration_overrides";
43
    private static final String MIGRATION_VALUE = "{october_2012:true}";
44
 
45
    private Context context;
46
    private URI imageUri;
47
    private Callback callback;
48
    private boolean allowCachedRedirects;
49
    private Object callerTag;
50
 
51
    public static URI getProfilePictureUrl(
52
            String userId,
53
            int width,
54
            int height)
55
            throws URISyntaxException {
56
 
57
        Validate.notNullOrEmpty(userId, "userId");
58
 
59
        width = Math.max(width, UNSPECIFIED_DIMENSION);
60
        height = Math.max(height, UNSPECIFIED_DIMENSION);
61
 
62
        if (width == UNSPECIFIED_DIMENSION && height == UNSPECIFIED_DIMENSION) {
63
            throw new IllegalArgumentException("Either width or height must be greater than 0");
64
        }
65
 
66
        Uri.Builder builder = new Uri.Builder().encodedPath(String.format(PROFILEPIC_URL_FORMAT, userId));
67
 
68
        if (height != UNSPECIFIED_DIMENSION) {
69
            builder.appendQueryParameter(HEIGHT_PARAM, String.valueOf(height));
70
        }
71
 
72
        if (width != UNSPECIFIED_DIMENSION) {
73
            builder.appendQueryParameter(WIDTH_PARAM, String.valueOf(width));
74
        }
75
 
76
        builder.appendQueryParameter(MIGRATION_PARAM, MIGRATION_VALUE);
77
 
78
        return new URI(builder.toString());
79
    }
80
 
81
    private ImageRequest(Builder builder) {
82
        this.context = builder.context;
83
        this.imageUri = builder.imageUrl;
84
        this.callback = builder.callback;
85
        this.allowCachedRedirects = builder.allowCachedRedirects;
86
        this.callerTag = builder.callerTag == null ? new Object() : builder.callerTag;
87
    }
88
 
89
    public Context getContext() {
90
        return context;
91
    }
92
 
93
    public URI getImageUri() {
94
        return imageUri;
95
    }
96
 
97
    public Callback getCallback() {
98
        return callback;
99
    }
100
 
101
    public boolean isCachedRedirectAllowed() {
102
        return allowCachedRedirects;
103
    }
104
 
105
    public Object getCallerTag() {
106
        return callerTag;
107
    }
108
 
109
    public static class Builder {
110
        // Required
111
        private Context context;
112
        private URI imageUrl;
113
 
114
        // Optional
115
        private Callback callback;
116
        private boolean allowCachedRedirects;
117
        private Object callerTag;
118
 
119
        public Builder(Context context, URI imageUrl) {
120
            Validate.notNull(imageUrl, "imageUrl");
121
            this.context = context;
122
            this.imageUrl = imageUrl;
123
        }
124
 
125
        public Builder setCallback(Callback callback) {
126
            this.callback = callback;
127
            return this;
128
        }
129
 
130
        public Builder setCallerTag(Object callerTag) {
131
            this.callerTag = callerTag;
132
            return this;
133
        }
134
 
135
        public Builder setAllowCachedRedirects(boolean allowCachedRedirects) {
136
            this.allowCachedRedirects = allowCachedRedirects;
137
            return this;
138
        }
139
 
140
        public ImageRequest build() {
141
            return new ImageRequest(this);
142
        }
143
    }
144
}