summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/BrowserDownloadAdapter.java
blob: 16cb98289ade735e480b96388d119ea2cea578aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 
package com.android.browser;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.database.Cursor;
import android.drm.mobile1.DrmRawContent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.Downloads;
import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

import java.io.File;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * This class is used to represent the data for the download list box. The only 
 * real work done by this class is to construct a custom view for the line
 * items.
 */
public class BrowserDownloadAdapter extends ResourceCursorAdapter {
    
    private int mFilenameColumnId;
    private int mTitleColumnId;
    private int mDescColumnId;
    private int mStatusColumnId;
    private int mTotalBytesColumnId;
    private int mCurrentBytesColumnId;
    private int mMimetypeColumnId;
    private int mDateColumnId;

    public BrowserDownloadAdapter(Context context, int layout, Cursor c) {
        super(context, layout, c);
        mFilenameColumnId = c.getColumnIndexOrThrow(Downloads._DATA);
        mTitleColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_TITLE);
        mDescColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_DESCRIPTION);
        mStatusColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_STATUS);
        mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_TOTAL_BYTES);
        mCurrentBytesColumnId = 
            c.getColumnIndexOrThrow(Downloads.COLUMN_CURRENT_BYTES);
        mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_MIME_TYPE);
        mDateColumnId = c.getColumnIndexOrThrow(Downloads.COLUMN_LAST_MODIFICATION);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Resources r = context.getResources();
        
        // Retrieve the icon for this download
        String mimeType = cursor.getString(mMimetypeColumnId);
        ImageView iv = (ImageView) view.findViewById(R.id.download_icon);
        if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) {
            iv.setImageResource(R.drawable.ic_launcher_drm_file);
        } else if (mimeType == null) {
            iv.setVisibility(View.INVISIBLE);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
            PackageManager pm = context.getPackageManager();
            List<ResolveInfo> list = pm.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            if (list.size() > 0) {
                Drawable icon = list.get(0).activityInfo.loadIcon(pm);
                iv.setImageDrawable(icon);
                iv.setVisibility(View.VISIBLE);
            } else {
                iv.setVisibility(View.INVISIBLE);
            }
        }
        
        TextView tv = (TextView) view.findViewById(R.id.download_title);
        String title = cursor.getString(mTitleColumnId);
        if (title == null) {
            String fullFilename = cursor.getString(mFilenameColumnId);
            if (fullFilename == null) {
                title = r.getString(R.string.download_unknown_filename);
            } else {
                // We have a filename, so we can build a title from that
                title = new File(fullFilename).getName();
                ContentValues values = new ContentValues();
                values.put(Downloads.COLUMN_TITLE, title);
                // assume "_id" is the first column for the cursor 
                context.getContentResolver().update(
                        ContentUris.withAppendedId(Downloads.CONTENT_URI,
                        cursor.getLong(0)), values, null, null);
            }
        }
        tv.setText(title);
        
        tv = (TextView) view.findViewById(R.id.domain);
        tv.setText(cursor.getString(mDescColumnId));
        
        long totalBytes = cursor.getLong(mTotalBytesColumnId);
        
        int status = cursor.getInt(mStatusColumnId);
        if (Downloads.isStatusCompleted(status)) { // Download stopped
            View v = view.findViewById(R.id.progress_text);
            v.setVisibility(View.GONE);

            v = view.findViewById(R.id.download_progress);
            v.setVisibility(View.GONE);

            tv = (TextView) view.findViewById(R.id.complete_text);
            tv.setVisibility(View.VISIBLE);
            if (Downloads.isStatusError(status)) {
                tv.setText(getErrorText(status));
            } else {
                tv.setText(r.getString(R.string.download_success, 
                        Formatter.formatFileSize(mContext, totalBytes)));
            }
            
            long time = cursor.getLong(mDateColumnId);
            Date d = new Date(time);
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
            tv = (TextView) view.findViewById(R.id.complete_date);
            tv.setVisibility(View.VISIBLE);
            tv.setText(df.format(d));
            
        } else { // Download is still running
            tv = (TextView) view.findViewById(R.id.progress_text);
            tv.setVisibility(View.VISIBLE);

            View progress = view.findViewById(R.id.download_progress);
            progress.setVisibility(View.VISIBLE);
            
            View v = view.findViewById(R.id.complete_date);
            v.setVisibility(View.GONE);

            v = view.findViewById(R.id.complete_text);
            v.setVisibility(View.GONE);
            
            if (status == Downloads.STATUS_PENDING) {
                tv.setText(r.getText(R.string.download_pending));
            } else if (status == Downloads.STATUS_PENDING_PAUSED) {
                tv.setText(r.getText(R.string.download_pending_network));
            } else {
                ProgressBar pb = (ProgressBar) progress;

                StringBuilder sb = new StringBuilder();
                if (status == Downloads.STATUS_RUNNING) {
                    sb.append(r.getText(R.string.download_running));
                } else {
                    sb.append(r.getText(R.string.download_running_paused));
                }
                if (totalBytes > 0) {
                    long currentBytes = cursor.getLong(mCurrentBytesColumnId); 
                    int progressAmount = (int)(currentBytes * 100 / totalBytes);
                    sb.append(' ');
                    sb.append(progressAmount);
                    sb.append("% (");
                    sb.append(Formatter.formatFileSize(mContext, currentBytes));
                    sb.append("/");
                    sb.append(Formatter.formatFileSize(mContext, totalBytes));
                    sb.append(")");
                    pb.setIndeterminate(false);
                    pb.setProgress(progressAmount);
                } else {
                    pb.setIndeterminate(true);
                }
                tv.setText(sb.toString()); 
            }
        }
        
    }
    
    /**
     * Provide the resource id for the error string.
     * @param status status of the download item
     * @return resource id for the error string.
     */
    public static int getErrorText(int status) {
        switch (status) {
            case Downloads.STATUS_NOT_ACCEPTABLE:
                return R.string.download_not_acceptable;
                
            case Downloads.STATUS_LENGTH_REQUIRED:
                return R.string.download_length_required;
                
            case Downloads.STATUS_PRECONDITION_FAILED:
                return R.string.download_precondition_failed;
                
            case Downloads.STATUS_CANCELED:
                return R.string.download_canceled;

            case Downloads.STATUS_FILE_ERROR:
                return R.string.download_file_error;
                
            case Downloads.STATUS_BAD_REQUEST:
            case Downloads.STATUS_UNKNOWN_ERROR:
            default:
                return R.string.download_error;
        }
    }
}