alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2013 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 31 | package org.sflphone.fragments; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 32 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | import java.util.HashMap; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 35 | import java.util.concurrent.ExecutorService; |
| 36 | import java.util.concurrent.Executors; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 37 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 38 | import org.sflphone.R; |
| 39 | import org.sflphone.adapters.ContactPictureTask; |
| 40 | import org.sflphone.loaders.HistoryLoader; |
| 41 | import org.sflphone.loaders.LoaderConstants; |
| 42 | import org.sflphone.model.HistoryEntry; |
| 43 | import org.sflphone.service.ISipService; |
| 44 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 45 | import android.app.Activity; |
| 46 | import android.app.ListFragment; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 47 | import android.app.LoaderManager.LoaderCallbacks; |
| 48 | import android.content.Loader; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 49 | import android.os.Bundle; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 50 | import android.os.RemoteException; |
| 51 | import android.util.Log; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 52 | import android.view.LayoutInflater; |
| 53 | import android.view.View; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 54 | import android.view.View.OnClickListener; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 55 | import android.view.ViewGroup; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 56 | import android.widget.AdapterView; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 57 | import android.widget.AdapterView.OnItemClickListener; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 58 | import android.widget.BaseAdapter; |
| 59 | import android.widget.Button; |
| 60 | import android.widget.ImageButton; |
| 61 | import android.widget.ImageView; |
| 62 | import android.widget.ListView; |
| 63 | import android.widget.TextView; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 64 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 65 | public class HistoryFragment extends ListFragment implements LoaderCallbacks<ArrayList<HistoryEntry>> { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 66 | |
| 67 | private static final String TAG = HistoryFragment.class.getSimpleName(); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 68 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 69 | HistoryAdapter mAdapter; |
| 70 | private Callbacks mCallbacks = sDummyCallbacks; |
| 71 | /** |
| 72 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 73 | */ |
| 74 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 75 | @Override |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 76 | public void onCallDialed(String to) { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public ISipService getService() { |
| 81 | Log.i(TAG, "Dummy"); |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | }; |
| 86 | |
| 87 | public interface Callbacks { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 88 | public void onCallDialed(String to); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 89 | |
| 90 | public ISipService getService(); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void onAttach(Activity activity) { |
Alexandre Lision | 4a56d43 | 2013-07-19 11:37:53 -0400 | [diff] [blame] | 96 | Log.i(TAG, "Attaching HISTORY"); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 97 | super.onAttach(activity); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 98 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 99 | if (!(activity instanceof Callbacks)) { |
| 100 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 101 | } |
| 102 | |
| 103 | mCallbacks = (Callbacks) activity; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 104 | getLoaderManager().initLoader(LoaderConstants.HISTORY_LOADER, null, this); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onDetach() { |
| 109 | super.onDetach(); |
| 110 | mCallbacks = sDummyCallbacks; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onCreate(Bundle savedInstanceState) { |
| 115 | super.onCreate(savedInstanceState); |
Alexandre Lision | 4a56d43 | 2013-07-19 11:37:53 -0400 | [diff] [blame] | 116 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 117 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 121 | View inflatedView = inflater.inflate(R.layout.frag_history, parent, false); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 122 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 123 | ((ListView) inflatedView.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener() { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 124 | |
| 125 | @Override |
| 126 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
| 127 | mAdapter.getItem(pos); |
| 128 | } |
| 129 | }); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 130 | return inflatedView; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onStart() { |
| 135 | super.onStart(); |
| 136 | Log.w(TAG, "onStart"); |
Alexandre Lision | 4a56d43 | 2013-07-19 11:37:53 -0400 | [diff] [blame] | 137 | getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 138 | } |
| 139 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 140 | public void makeNewCall(int position) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 141 | mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber()); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 142 | } |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 143 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 144 | @Override |
| 145 | public Loader<ArrayList<HistoryEntry>> onCreateLoader(int id, Bundle args) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 146 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 147 | HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService()); |
| 148 | loader.forceLoad(); |
| 149 | return loader; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 150 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onLoadFinished(Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) { |
| 155 | mAdapter = new HistoryAdapter(this, history); |
| 156 | getListView().setAdapter(mAdapter); |
| 157 | mAdapter.notifyDataSetChanged(); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onLoaderReset(Loader<ArrayList<HistoryEntry>> arg0) { |
| 163 | // TODO Auto-generated method stub |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 164 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 165 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 166 | |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 167 | public class HistoryAdapter extends BaseAdapter { |
| 168 | |
| 169 | HistoryFragment mContext; |
| 170 | ArrayList<HistoryEntry> dataset; |
| 171 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
| 172 | |
| 173 | public HistoryAdapter(HistoryFragment activity, ArrayList<HistoryEntry> history) { |
| 174 | mContext = activity; |
| 175 | dataset = history; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public View getView(final int pos, View convertView, ViewGroup arg2) { |
| 180 | View rowView = convertView; |
| 181 | HistoryView entryView = null; |
| 182 | |
| 183 | if (rowView == null) { |
| 184 | // Get a new instance of the row layout view |
| 185 | LayoutInflater inflater = LayoutInflater.from(mContext.getActivity()); |
| 186 | rowView = inflater.inflate(R.layout.item_history, null); |
| 187 | |
| 188 | // Hold the view objects in an object |
| 189 | // so they don't need to be re-fetched |
| 190 | entryView = new HistoryView(); |
| 191 | entryView.photo = (ImageView) rowView.findViewById(R.id.photo); |
| 192 | entryView.displayName = (TextView) rowView.findViewById(R.id.display_name); |
| 193 | entryView.duration = (TextView) rowView.findViewById(R.id.duration); |
| 194 | entryView.date = (TextView) rowView.findViewById(R.id.date_start); |
| 195 | entryView.missed = (TextView) rowView.findViewById(R.id.missed); |
| 196 | entryView.incoming = (TextView) rowView.findViewById(R.id.incomings); |
| 197 | entryView.outgoing = (TextView) rowView.findViewById(R.id.outgoings); |
| 198 | entryView.replay = (Button) rowView.findViewById(R.id.replay); |
| 199 | entryView.call_button = (ImageButton) rowView.findViewById(R.id.action_call); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 200 | rowView.setTag(entryView); |
| 201 | } else { |
| 202 | entryView = (HistoryView) rowView.getTag(); |
| 203 | } |
| 204 | |
| 205 | // Transfer the stock data from the data object |
| 206 | // to the view objects |
| 207 | |
| 208 | // SipCall call = (SipCall) mCallList.values().toArray()[position]; |
| 209 | entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName()); |
| 210 | |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 211 | infos_fetcher.execute(new ContactPictureTask(mContext.getActivity(), entryView.photo, dataset.get(pos).getContact().getId())); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 212 | |
Alexandre Lision | c2bd7b6 | 2013-09-30 10:45:00 -0400 | [diff] [blame] | 213 | |
| 214 | entryView.missed.setText(getString(R.string.hist_missed_calls, dataset.get(pos).getMissed_sum())); |
| 215 | entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum())); |
| 216 | entryView.outgoing.setText(getString(R.string.hist_out_calls, dataset.get(pos).getOutgoing_sum())); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 217 | |
| 218 | if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) { |
| 219 | entryView.replay.setVisibility(View.VISIBLE); |
| 220 | entryView.replay.setTag(R.id.replay, true); |
| 221 | entryView.replay.setOnClickListener(new OnClickListener() { |
| 222 | |
| 223 | @Override |
| 224 | public void onClick(View v) { |
| 225 | try { |
| 226 | if ((Boolean) v.getTag(R.id.replay)) { |
| 227 | mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath()); |
| 228 | v.setTag(R.id.replay, false); |
Alexandre Lision | c2bd7b6 | 2013-09-30 10:45:00 -0400 | [diff] [blame] | 229 | ((Button)v).setText(getString(R.string.hist_replay_button_stop)); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 230 | } else { |
| 231 | mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath()); |
| 232 | v.setTag(R.id.replay, true); |
Alexandre Lision | c2bd7b6 | 2013-09-30 10:45:00 -0400 | [diff] [blame] | 233 | ((Button)v).setText(getString(R.string.hist_replay_button)); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 234 | } |
| 235 | } catch (RemoteException e) { |
| 236 | // TODO Auto-generated catch block |
| 237 | e.printStackTrace(); |
| 238 | } |
| 239 | } |
| 240 | }); |
| 241 | } |
| 242 | |
Alexandre Lision | c2bd7b6 | 2013-09-30 10:45:00 -0400 | [diff] [blame] | 243 | entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate("hh:mm dd/MM/yyyy")); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 244 | entryView.duration.setText(dataset.get(pos).getTotalDuration()); |
Alexandre Lision | 3ee516e | 2013-10-07 17:32:15 -0400 | [diff] [blame] | 245 | entryView.call_button.setOnClickListener(new OnClickListener() { |
| 246 | |
| 247 | @Override |
| 248 | public void onClick(View v) { |
| 249 | mContext.makeNewCall(pos); |
| 250 | |
| 251 | } |
| 252 | }); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 253 | |
| 254 | return rowView; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | /********************* |
| 259 | * ViewHolder Pattern |
| 260 | *********************/ |
| 261 | public class HistoryView { |
| 262 | public ImageView photo; |
| 263 | protected TextView displayName; |
| 264 | protected TextView date; |
| 265 | public TextView duration; |
| 266 | private ImageButton call_button; |
| 267 | private Button replay; |
| 268 | private TextView missed; |
| 269 | private TextView outgoing; |
| 270 | private TextView incoming; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public int getCount() { |
| 275 | |
| 276 | return dataset.size(); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public HistoryEntry getItem(int pos) { |
| 281 | return dataset.get(pos); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public long getItemId(int arg0) { |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | public void clear() { |
| 290 | dataset.clear(); |
| 291 | |
| 292 | } |
| 293 | |
| 294 | public void addAll(ArrayList<HashMap<String, String>> history) { |
| 295 | // dataset.addAll(history); |
| 296 | |
| 297 | } |
| 298 | |
| 299 | } |
| 300 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 301 | } |