1/*
2 * Copyright (c) 2008-2009, Motorola, Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Motorola, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.android.bluetooth.opp;
34
35import com.android.bluetooth.R;
36
37import android.bluetooth.BluetoothAdapter;
38import android.bluetooth.BluetoothDevice;
39import android.content.Context;
40import android.content.res.Resources;
41import android.database.Cursor;
42import android.text.format.DateUtils;
43import android.text.format.DateFormat;
44import android.text.format.Formatter;
45import android.view.View;
46import android.widget.ImageView;
47import android.widget.ResourceCursorAdapter;
48import android.widget.TextView;
49
50import java.util.Date;
51
52/**
53 * This class is used to represent the data for the transfer history list box.
54 * The only real work done by this class is to construct a custom view for the
55 * line items.
56 */
57public class BluetoothOppTransferAdapter extends ResourceCursorAdapter {
58    private Context mContext;
59
60    public BluetoothOppTransferAdapter(Context context, int layout, Cursor c) {
61        super(context, layout, c);
62        mContext = context;
63    }
64
65    @Override
66    public void bindView(View view, Context context, Cursor cursor) {
67        Resources r = context.getResources();
68
69        // Retrieve the icon for this transfer
70        ImageView iv = (ImageView)view.findViewById(R.id.transfer_icon);
71        int status = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare.STATUS));
72        int dir = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare.DIRECTION));
73        if (BluetoothShare.isStatusError(status)) {
74            iv.setImageResource(android.R.drawable.stat_notify_error);
75        } else {
76            if (dir == BluetoothShare.DIRECTION_OUTBOUND) {
77                iv.setImageResource(android.R.drawable.stat_sys_upload_done);
78            } else {
79                iv.setImageResource(android.R.drawable.stat_sys_download_done);
80            }
81        }
82
83        // Set title
84        TextView tv = (TextView)view.findViewById(R.id.transfer_title);
85        String title = cursor.getString(
86                cursor.getColumnIndexOrThrow(BluetoothShare.FILENAME_HINT));
87        if (title == null) {
88            title = mContext.getString(R.string.unknown_file);
89        }
90        tv.setText(title);
91
92        // target device
93        tv = (TextView)view.findViewById(R.id.targetdevice);
94        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
95        int destinationColumnId = cursor.getColumnIndexOrThrow(BluetoothShare.DESTINATION);
96        BluetoothDevice remoteDevice = adapter.getRemoteDevice(cursor
97                .getString(destinationColumnId));
98        String deviceName = BluetoothOppManager.getInstance(context).getDeviceName(remoteDevice);
99        tv.setText(deviceName);
100
101        // complete text and complete date
102        long totalBytes = cursor.getLong(cursor.getColumnIndexOrThrow(BluetoothShare.TOTAL_BYTES));
103        if (BluetoothShare.isStatusCompleted(status)) {
104            tv = (TextView)view.findViewById(R.id.complete_text);
105            tv.setVisibility(View.VISIBLE);
106            if (BluetoothShare.isStatusError(status)) {
107                tv.setText(BluetoothOppUtility.getStatusDescription(mContext, status, deviceName));
108            } else {
109                String completeText;
110                if (dir == BluetoothShare.DIRECTION_INBOUND) {
111                    completeText = r.getString(R.string.download_success, Formatter.formatFileSize(
112                            mContext, totalBytes));
113                } else {
114                    completeText = r.getString(R.string.upload_success, Formatter.formatFileSize(
115                            mContext, totalBytes));
116                }
117                tv.setText(completeText);
118            }
119
120            int dateColumnId = cursor.getColumnIndexOrThrow(BluetoothShare.TIMESTAMP);
121            long time = cursor.getLong(dateColumnId);
122            Date d = new Date(time);
123            CharSequence str = DateUtils.isToday(time) ? DateFormat.getTimeFormat(mContext).format(
124                    d) : DateFormat.getDateFormat(mContext).format(d);
125            tv = (TextView)view.findViewById(R.id.complete_date);
126            tv.setVisibility(View.VISIBLE);
127            tv.setText(str);
128        }
129    }
130}
131