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;
18
 
19
import android.os.Handler;
20
 
21
class RequestProgress {
22
    private final Request request;
23
    private final Handler callbackHandler;
24
    private final long threshold;
25
 
26
    private long progress, lastReportedProgress, maxProgress;
27
 
28
    RequestProgress(Handler callbackHandler, Request request) {
29
        this.request = request;
30
        this.callbackHandler = callbackHandler;
31
 
32
        this.threshold = Settings.getOnProgressThreshold();
33
    }
34
 
35
    long getProgress() {
36
        return progress;
37
    }
38
 
39
    long getMaxProgress() {
40
        return maxProgress;
41
    }
42
 
43
    void addProgress(long size) {
44
        progress += size;
45
 
46
        if (progress >= lastReportedProgress + threshold || progress >= maxProgress) {
47
            reportProgress();
48
        }
49
    }
50
 
51
    void addToMax(long size) {
52
        maxProgress += size;
53
    }
54
 
55
    void reportProgress() {
56
        if (progress > lastReportedProgress) {
57
            Request.Callback callback = request.getCallback();
58
            if (maxProgress > 0 && callback instanceof Request.OnProgressCallback) {
59
                // Keep copies to avoid threading issues
60
                final long currentCopy = progress;
61
                final long maxProgressCopy = maxProgress;
62
                final Request.OnProgressCallback callbackCopy = (Request.OnProgressCallback) callback;
63
                if (callbackHandler == null) {
64
                    callbackCopy.onProgress(currentCopy, maxProgressCopy);
65
                }
66
                else {
67
                    callbackHandler.post(new Runnable() {
68
                        @Override
69
                        public void run() {
70
                            callbackCopy.onProgress(currentCopy, maxProgressCopy);
71
                        }
72
                    });
73
                }
74
                lastReportedProgress = progress;
75
            }
76
        }
77
    }
78
}