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 
33 package com.android.bluetooth.opp;
34 
35 import android.bluetooth.BluetoothAdapter;
36 import android.bluetooth.BluetoothDevice;
37 import android.content.Context;
38 import android.content.res.Resources;
39 import android.database.Cursor;
40 import android.text.format.DateFormat;
41 import android.text.format.DateUtils;
42 import android.text.format.Formatter;
43 import android.view.View;
44 import android.widget.ImageView;
45 import android.widget.ResourceCursorAdapter;
46 import android.widget.TextView;
47 
48 import com.android.bluetooth.R;
49 
50 import 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  */
57 public class BluetoothOppTransferAdapter extends ResourceCursorAdapter {
58     private Context mContext;
59 
BluetoothOppTransferAdapter(Context context, int layout, Cursor c)60     public BluetoothOppTransferAdapter(Context context, int layout, Cursor c) {
61         super(context, layout, c, true /* autoRequery */ );
62         mContext = context;
63     }
64 
65     @Override
bindView(View view, Context context, Cursor cursor)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(cursor.getColumnIndexOrThrow(BluetoothShare.FILENAME_HINT));
86         if (title == null) {
87             title = mContext.getString(R.string.unknown_file);
88         }
89         tv.setText(title);
90 
91         // target device
92         tv = (TextView) view.findViewById(R.id.targetdevice);
93         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
94         int destinationColumnId = cursor.getColumnIndexOrThrow(BluetoothShare.DESTINATION);
95         BluetoothDevice remoteDevice =
96                 adapter.getRemoteDevice(cursor.getString(destinationColumnId));
97         String deviceName = BluetoothOppManager.getInstance(context).getDeviceName(remoteDevice);
98         tv.setText(deviceName);
99 
100         // complete text and complete date
101         long totalBytes = cursor.getLong(cursor.getColumnIndexOrThrow(BluetoothShare.TOTAL_BYTES));
102         if (BluetoothShare.isStatusCompleted(status)) {
103             tv = (TextView) view.findViewById(R.id.complete_text);
104             tv.setVisibility(View.VISIBLE);
105             if (BluetoothShare.isStatusError(status)) {
106                 tv.setText(BluetoothOppUtility.getStatusDescription(mContext, status, deviceName));
107             } else {
108                 String completeText;
109                 if (dir == BluetoothShare.DIRECTION_INBOUND) {
110                     completeText = r.getString(R.string.download_success,
111                             Formatter.formatFileSize(mContext, totalBytes));
112                 } else {
113                     completeText = r.getString(R.string.upload_success,
114                             Formatter.formatFileSize(mContext, totalBytes));
115                 }
116                 tv.setText(completeText);
117             }
118 
119             int dateColumnId = cursor.getColumnIndexOrThrow(BluetoothShare.TIMESTAMP);
120             long time = cursor.getLong(dateColumnId);
121             Date d = new Date(time);
122             CharSequence str =
123                     DateUtils.isToday(time) ? DateFormat.getTimeFormat(mContext).format(d)
124                             : 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