alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame^] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 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; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 34 | import java.util.concurrent.ExecutorService; |
| 35 | import java.util.concurrent.Executors; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 36 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 37 | import org.sflphone.R; |
| 38 | import org.sflphone.adapters.ContactPictureTask; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 39 | import org.sflphone.client.DetailHistoryActivity; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 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; |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 46 | import android.content.Context; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 47 | import android.content.Intent; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 48 | import android.os.Bundle; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 49 | import android.os.RemoteException; |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 50 | import android.support.v4.app.ListFragment; |
| 51 | import android.support.v4.app.LoaderManager.LoaderCallbacks; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 52 | import android.util.Log; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 53 | import android.view.LayoutInflater; |
| 54 | import android.view.View; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 55 | import android.view.View.OnClickListener; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 56 | import android.view.ViewGroup; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 57 | import android.widget.AdapterView; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 58 | import android.widget.AdapterView.OnItemClickListener; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 59 | import android.widget.BaseAdapter; |
| 60 | import android.widget.Button; |
| 61 | import android.widget.ImageButton; |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 62 | import android.widget.ListAdapter; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 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; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 71 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 72 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 73 | @Override |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 74 | public void onCallDialed(String to) { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ISipService getService() { |
| 79 | Log.i(TAG, "Dummy"); |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | }; |
| 84 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 85 | public static String ARGS = "Bundle.args"; |
| 86 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 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; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onDetach() { |
| 108 | super.onDetach(); |
| 109 | mCallbacks = sDummyCallbacks; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void onCreate(Bundle savedInstanceState) { |
| 114 | super.onCreate(savedInstanceState); |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 115 | mAdapter = new HistoryAdapter(getActivity(), new ArrayList<HistoryEntry>()); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 116 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 117 | |
| 118 | @Override |
| 119 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 120 | View inflatedView = inflater.inflate(R.layout.frag_history, parent, false); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 121 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 122 | return inflatedView; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onActivityCreated(Bundle savedInstanceState) { |
| 127 | |
| 128 | super.onActivityCreated(savedInstanceState); |
| 129 | getListView().setAdapter(mAdapter); |
| 130 | |
| 131 | getListView().setOnItemClickListener(new OnItemClickListener() { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 132 | |
| 133 | @Override |
| 134 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 135 | |
| 136 | Bundle b = new Bundle(); |
| 137 | b.putParcelable("entry", mAdapter.getItem(pos)); |
| 138 | Intent toStart = new Intent(getActivity(), DetailHistoryActivity.class).putExtra(HistoryFragment.ARGS, b); |
| 139 | startActivity(toStart); |
| 140 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 141 | } |
| 142 | }); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void onStart() { |
| 147 | super.onStart(); |
| 148 | Log.w(TAG, "onStart"); |
Alexandre Lision | 4a56d43 | 2013-07-19 11:37:53 -0400 | [diff] [blame] | 149 | getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 150 | } |
| 151 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 152 | public void makeNewCall(int position) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 153 | mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber()); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 154 | } |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 155 | |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 156 | public class HistoryAdapter extends BaseAdapter implements ListAdapter { |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 157 | |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 158 | Context mContext; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 159 | ArrayList<HistoryEntry> dataset; |
| 160 | private ExecutorService infos_fetcher = Executors.newCachedThreadPool(); |
| 161 | |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 162 | public HistoryAdapter(Context activity, ArrayList<HistoryEntry> history) { |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 163 | mContext = activity; |
| 164 | dataset = history; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public View getView(final int pos, View convertView, ViewGroup arg2) { |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 169 | |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 170 | HistoryView entryView = null; |
| 171 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 172 | if (convertView == null) { |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 173 | // Get a new instance of the row layout view |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 174 | LayoutInflater inflater = LayoutInflater.from(mContext); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 175 | convertView = inflater.inflate(R.layout.item_history, null); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 176 | |
| 177 | // Hold the view objects in an object |
| 178 | // so they don't need to be re-fetched |
| 179 | entryView = new HistoryView(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 180 | entryView.photo = (ImageButton) convertView.findViewById(R.id.photo); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 181 | entryView.displayName = (TextView) convertView.findViewById(R.id.display_name); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 182 | entryView.date = (TextView) convertView.findViewById(R.id.date_start); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 183 | entryView.incoming = (TextView) convertView.findViewById(R.id.incomings); |
| 184 | entryView.outgoing = (TextView) convertView.findViewById(R.id.outgoings); |
| 185 | entryView.replay = (Button) convertView.findViewById(R.id.replay); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 186 | convertView.setTag(entryView); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 187 | } else { |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 188 | entryView = (HistoryView) convertView.getTag(); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Transfer the stock data from the data object |
| 192 | // to the view objects |
| 193 | |
| 194 | // SipCall call = (SipCall) mCallList.values().toArray()[position]; |
| 195 | entryView.displayName.setText(dataset.get(pos).getContact().getmDisplayName()); |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 196 | infos_fetcher.execute(new ContactPictureTask(mContext, entryView.photo, dataset.get(pos).getContact())); |
Alexandre Lision | c2bd7b6 | 2013-09-30 10:45:00 -0400 | [diff] [blame] | 197 | |
Alexandre Lision | c2bd7b6 | 2013-09-30 10:45:00 -0400 | [diff] [blame] | 198 | entryView.incoming.setText(getString(R.string.hist_in_calls, dataset.get(pos).getIncoming_sum())); |
| 199 | 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] | 200 | |
| 201 | if (dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath().length() > 0) { |
| 202 | entryView.replay.setVisibility(View.VISIBLE); |
| 203 | entryView.replay.setTag(R.id.replay, true); |
| 204 | entryView.replay.setOnClickListener(new OnClickListener() { |
| 205 | |
| 206 | @Override |
| 207 | public void onClick(View v) { |
| 208 | try { |
| 209 | if ((Boolean) v.getTag(R.id.replay)) { |
| 210 | mCallbacks.getService().startRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath()); |
| 211 | v.setTag(R.id.replay, false); |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 212 | ((Button) v).setText(getString(R.string.hist_replay_button_stop)); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 213 | } else { |
| 214 | mCallbacks.getService().stopRecordedFilePlayback(dataset.get(pos).getCalls().lastEntry().getValue().getRecordPath()); |
| 215 | v.setTag(R.id.replay, true); |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 216 | ((Button) v).setText(getString(R.string.hist_replay_button)); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 217 | } |
| 218 | } catch (RemoteException e) { |
| 219 | // TODO Auto-generated catch block |
| 220 | e.printStackTrace(); |
| 221 | } |
| 222 | } |
| 223 | }); |
| 224 | } |
| 225 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 226 | entryView.date.setText(dataset.get(pos).getCalls().lastEntry().getValue().getDate()); |
| 227 | entryView.photo.setOnClickListener(new OnClickListener() { |
Alexandre Lision | 3ee516e | 2013-10-07 17:32:15 -0400 | [diff] [blame] | 228 | |
| 229 | @Override |
| 230 | public void onClick(View v) { |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 231 | makeNewCall(pos); |
Alexandre Lision | 3ee516e | 2013-10-07 17:32:15 -0400 | [diff] [blame] | 232 | |
| 233 | } |
| 234 | }); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 235 | |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 236 | return convertView; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 237 | |
| 238 | } |
| 239 | |
| 240 | /********************* |
| 241 | * ViewHolder Pattern |
| 242 | *********************/ |
| 243 | public class HistoryView { |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 244 | public ImageButton photo; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 245 | protected TextView displayName; |
| 246 | protected TextView date; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 247 | private Button replay; |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 248 | private TextView outgoing; |
| 249 | private TextView incoming; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public int getCount() { |
| 254 | |
| 255 | return dataset.size(); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public HistoryEntry getItem(int pos) { |
| 260 | return dataset.get(pos); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public long getItemId(int arg0) { |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | public void clear() { |
| 269 | dataset.clear(); |
| 270 | |
| 271 | } |
| 272 | |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 273 | public void addAll(ArrayList<HistoryEntry> history) { |
| 274 | dataset.addAll(history); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | } |
| 278 | |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 279 | @Override |
| 280 | public android.support.v4.content.Loader<ArrayList<HistoryEntry>> onCreateLoader(int arg0, Bundle arg1) { |
| 281 | HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService()); |
| 282 | loader.forceLoad(); |
| 283 | return loader; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void onLoadFinished(android.support.v4.content.Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) { |
| 288 | mAdapter.clear(); |
| 289 | mAdapter.addAll(history); |
| 290 | mAdapter.notifyDataSetChanged(); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void onLoaderReset(android.support.v4.content.Loader<ArrayList<HistoryEntry>> arg0) { |
| 295 | // TODO Stub de la méthode généré automatiquement |
| 296 | |
| 297 | } |
| 298 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 299 | } |