BrowserDownloadAdapter.java revision ed217d91fb3f1a8f4e75ab36ef81d72ef9f4e6d6
1/*
2 * Copyright (C) 2007 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 */
16
17
18package com.android.browser;
19
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.content.res.Resources;
27import android.database.Cursor;
28import android.drm.mobile1.DrmRawContent;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
31import android.provider.Downloads;
32import android.text.format.Formatter;
33import android.view.View;
34import android.widget.ImageView;
35import android.widget.ProgressBar;
36import android.widget.ResourceCursorAdapter;
37import android.widget.TextView;
38
39import java.io.File;
40import java.text.DateFormat;
41import java.util.Date;
42import java.util.List;
43
44/**
45 * This class is used to represent the data for the download list box. The only
46 * real work done by this class is to construct a custom view for the line
47 * items.
48 */
49public class BrowserDownloadAdapter extends ResourceCursorAdapter {
50
51    private int mFilenameColumnId;
52    private int mTitleColumnId;
53    private int mDescColumnId;
54    private int mStatusColumnId;
55    private int mTotalBytesColumnId;
56    private int mCurrentBytesColumnId;
57    private int mMimetypeColumnId;
58    private int mDateColumnId;
59
60    public BrowserDownloadAdapter(Context context, int layout, Cursor c) {
61        super(context, layout, c);
62        mFilenameColumnId = c.getColumnIndexOrThrow(Downloads._DATA);
63        mTitleColumnId = c.getColumnIndexOrThrow(Downloads.TITLE);
64        mDescColumnId = c.getColumnIndexOrThrow(Downloads.DESCRIPTION);
65        mStatusColumnId = c.getColumnIndexOrThrow(Downloads.STATUS);
66        mTotalBytesColumnId = c.getColumnIndexOrThrow(Downloads.TOTAL_BYTES);
67        mCurrentBytesColumnId =
68            c.getColumnIndexOrThrow(Downloads.CURRENT_BYTES);
69        mMimetypeColumnId = c.getColumnIndexOrThrow(Downloads.MIMETYPE);
70        mDateColumnId = c.getColumnIndexOrThrow(Downloads.LAST_MODIFICATION);
71    }
72
73    @Override
74    public void bindView(View view, Context context, Cursor cursor) {
75        Resources r = context.getResources();
76
77        // Retrieve the icon for this download
78        String mimeType = cursor.getString(mMimetypeColumnId);
79        ImageView iv = (ImageView) view.findViewById(R.id.download_icon);
80        if (DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mimeType)) {
81            iv.setImageResource(R.drawable.ic_launcher_drm_file);
82        } else if (mimeType == null) {
83            iv.setVisibility(View.INVISIBLE);
84        } else {
85            Intent intent = new Intent(Intent.ACTION_VIEW);
86            intent.setDataAndType(Uri.fromParts("file", "", null), mimeType);
87            PackageManager pm = context.getPackageManager();
88            List<ResolveInfo> list = pm.queryIntentActivities(intent,
89                    PackageManager.MATCH_DEFAULT_ONLY);
90            if (list.size() > 0) {
91                Drawable icon = list.get(0).activityInfo.loadIcon(pm);
92                iv.setImageDrawable(icon);
93                iv.setVisibility(View.VISIBLE);
94            } else {
95                iv.setVisibility(View.INVISIBLE);
96            }
97        }
98
99        TextView tv = (TextView) view.findViewById(R.id.download_title);
100        String title = cursor.getString(mTitleColumnId);
101        if (title == null) {
102            String fullFilename = cursor.getString(mFilenameColumnId);
103            if (fullFilename == null) {
104                title = r.getString(R.string.download_unknown_filename);
105            } else {
106                // We have a filename, so we can build a title from that
107                title = new File(fullFilename).getName();
108                ContentValues values = new ContentValues();
109                values.put(Downloads.TITLE, title);
110                // assume "_id" is the first column for the cursor
111                context.getContentResolver().update(
112                        ContentUris.withAppendedId(Downloads.CONTENT_URI,
113                        cursor.getLong(0)), values, null, null);
114            }
115        }
116        tv.setText(title);
117
118        tv = (TextView) view.findViewById(R.id.domain);
119        tv.setText(cursor.getString(mDescColumnId));
120
121        long totalBytes = cursor.getLong(mTotalBytesColumnId);
122
123        int status = cursor.getInt(mStatusColumnId);
124        if (Downloads.isStatusCompleted(status)) { // Download stopped
125            View v = view.findViewById(R.id.progress_text);
126            v.setVisibility(View.GONE);
127
128            v = view.findViewById(R.id.download_progress);
129            v.setVisibility(View.GONE);
130
131            tv = (TextView) view.findViewById(R.id.complete_text);
132            tv.setVisibility(View.VISIBLE);
133            if (Downloads.isStatusError(status)) {
134                tv.setText(getErrorText(status));
135            } else {
136                tv.setText(r.getString(R.string.download_success,
137                        Formatter.formatFileSize(mContext, totalBytes)));
138            }
139
140            long time = cursor.getLong(mDateColumnId);
141            Date d = new Date(time);
142            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
143            tv = (TextView) view.findViewById(R.id.complete_date);
144            tv.setVisibility(View.VISIBLE);
145            tv.setText(df.format(d));
146
147        } else { // Download is still running
148            tv = (TextView) view.findViewById(R.id.progress_text);
149            tv.setVisibility(View.VISIBLE);
150
151            View progress = view.findViewById(R.id.download_progress);
152            progress.setVisibility(View.VISIBLE);
153
154            View v = view.findViewById(R.id.complete_date);
155            v.setVisibility(View.GONE);
156
157            v = view.findViewById(R.id.complete_text);
158            v.setVisibility(View.GONE);
159
160            if (status == Downloads.STATUS_PENDING) {
161                tv.setText(r.getText(R.string.download_pending));
162            } else if (status == Downloads.STATUS_PENDING_PAUSED) {
163                tv.setText(r.getText(R.string.download_pending_network));
164            } else {
165                ProgressBar pb = (ProgressBar) progress;
166
167                StringBuilder sb = new StringBuilder();
168                if (status == Downloads.STATUS_RUNNING) {
169                    sb.append(r.getText(R.string.download_running));
170                } else {
171                    sb.append(r.getText(R.string.download_running_paused));
172                }
173                if (totalBytes > 0) {
174                    long currentBytes = cursor.getLong(mCurrentBytesColumnId);
175                    int progressAmount = (int)(currentBytes * 100 / totalBytes);
176                    sb.append(' ');
177                    sb.append(progressAmount);
178                    sb.append("% (");
179                    sb.append(Formatter.formatFileSize(mContext, currentBytes));
180                    sb.append("/");
181                    sb.append(Formatter.formatFileSize(mContext, totalBytes));
182                    sb.append(")");
183                    pb.setProgress(progressAmount);
184                } else {
185                    pb.setIndeterminate(true);
186                }
187                tv.setText(sb.toString());
188            }
189        }
190
191    }
192
193    /**
194     * Provide the resource id for the error string.
195     * @param status status of the download item
196     * @return resource id for the error string.
197     */
198    public static int getErrorText(int status) {
199        switch (status) {
200            case Downloads.STATUS_NOT_ACCEPTABLE:
201                return R.string.download_not_acceptable;
202
203            case Downloads.STATUS_LENGTH_REQUIRED:
204                return R.string.download_length_required;
205
206            case Downloads.STATUS_PRECONDITION_FAILED:
207                return R.string.download_precondition_failed;
208
209            case Downloads.STATUS_CANCELED:
210                return R.string.download_canceled;
211
212            case Downloads.STATUS_FILE_ERROR:
213                return R.string.download_file_error;
214
215            case Downloads.STATUS_BAD_REQUEST:
216            case Downloads.STATUS_UNKNOWN_ERROR:
217            default:
218                return R.string.download_error;
219        }
220    }
221}
222