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