Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [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 | */ |
| 31 | |
| 32 | package org.sflphone.fragments; |
| 33 | |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 34 | import java.io.InvalidObjectException; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 35 | import java.util.ArrayList; |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 36 | import java.util.HashMap; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 37 | import java.util.NavigableMap; |
| 38 | |
| 39 | import org.sflphone.R; |
| 40 | import org.sflphone.adapters.ContactPictureTask; |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 41 | import org.sflphone.model.Account; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 42 | import org.sflphone.model.HistoryEntry; |
| 43 | import org.sflphone.model.HistoryEntry.HistoryCall; |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 44 | import org.sflphone.model.SipCall; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 45 | import org.sflphone.service.ISipService; |
| 46 | import org.sflphone.views.parallaxscrollview.AnotherView; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 47 | |
| 48 | import android.app.Activity; |
| 49 | import android.app.Fragment; |
| 50 | import android.content.Context; |
| 51 | import android.os.Bundle; |
| 52 | import android.os.RemoteException; |
| 53 | import android.view.LayoutInflater; |
| 54 | import android.view.View; |
| 55 | import android.view.View.MeasureSpec; |
| 56 | import android.view.View.OnClickListener; |
| 57 | import android.view.ViewGroup; |
| 58 | import android.widget.BaseAdapter; |
| 59 | import android.widget.Button; |
| 60 | import android.widget.ImageView; |
| 61 | import android.widget.LinearLayout; |
| 62 | import android.widget.ListAdapter; |
| 63 | import android.widget.ListView; |
| 64 | import android.widget.RelativeLayout; |
| 65 | import android.widget.TextView; |
| 66 | |
| 67 | public class DetailsHistoryEntryFragment extends Fragment { |
| 68 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 69 | DetailHistoryAdapter mAdapter; |
| 70 | HistoryEntry toDisplay; |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame^] | 71 | @SuppressWarnings("unused") |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 72 | private static final String TAG = DetailsHistoryEntryFragment.class.getSimpleName(); |
| 73 | ContactPictureTask tasker; |
| 74 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 75 | private ListView lvMain; |
| 76 | private LinearLayout llMain, llMainHolder; |
| 77 | private AnotherView anotherView; |
| 78 | private RelativeLayout iv; |
| 79 | |
| 80 | private Callbacks mCallbacks = sDummyCallbacks; |
| 81 | |
| 82 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 83 | |
| 84 | @Override |
| 85 | public ISipService getService() { |
| 86 | return null; |
| 87 | } |
| 88 | |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 89 | @Override |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 90 | public void onCall(SipCall call) { |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | public interface Callbacks { |
| 96 | |
| 97 | public ISipService getService(); |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 98 | |
| 99 | public void onCall(SipCall call); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 100 | |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onAttach(Activity activity) { |
| 105 | super.onAttach(activity); |
| 106 | |
| 107 | if (!(activity instanceof Callbacks)) { |
| 108 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 109 | } |
| 110 | |
| 111 | mCallbacks = (Callbacks) activity; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onDetach() { |
| 116 | super.onDetach(); |
| 117 | mCallbacks = sDummyCallbacks; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onCreate(Bundle savedInstanceState) { |
| 122 | super.onCreate(savedInstanceState); |
| 123 | |
| 124 | toDisplay = (HistoryEntry) getArguments().get("entry"); |
| 125 | mAdapter = new DetailHistoryAdapter(toDisplay.getCalls(), getActivity()); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 130 | View inflatedView = inflater.inflate(R.layout.frag_history_detail, parent, false); |
| 131 | |
| 132 | // mheaderView = LayoutInflater.from(getActivity()).inflate(R.layout.history_detail_header, null); |
| 133 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 134 | llMain = (LinearLayout) inflatedView.findViewById(R.id.llMain); |
| 135 | llMainHolder = (LinearLayout) inflatedView.findViewById(R.id.llMainHolder); |
| 136 | lvMain = (ListView) inflatedView.findViewById(R.id.lvMain); |
| 137 | lvMain.setAdapter(mAdapter); |
| 138 | iv = (RelativeLayout) inflatedView.findViewById(R.id.iv); |
| 139 | |
| 140 | ((TextView) iv.findViewById(R.id.history_call_name)).setText(toDisplay.getContact().getmDisplayName()); |
Alexandre Lision | ab0cc9f | 2013-12-12 11:27:25 -0500 | [diff] [blame] | 141 | |
| 142 | tasker = new ContactPictureTask(getActivity(), (ImageView) iv.findViewById(R.id.contact_photo), toDisplay.getContact()); |
| 143 | tasker.run(); |
| 144 | anotherView = (AnotherView) inflatedView.findViewById(R.id.anotherView); |
| 145 | |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 146 | ((TextView) anotherView.findViewById(R.id.history_entry_number)).setText(getString(R.string.detail_hist_call_number, toDisplay.getNumber())); |
Alexandre Lision | ab0cc9f | 2013-12-12 11:27:25 -0500 | [diff] [blame] | 147 | ((RelativeLayout) anotherView.findViewById(R.id.call_main_action)).setOnClickListener(new OnClickListener() { |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public void onClick(View v) { |
Alexandre Lision | ddbb46f | 2013-12-12 12:18:58 -0500 | [diff] [blame] | 151 | try { |
| 152 | SipCall.SipCallBuilder callBuilder = SipCall.SipCallBuilder.getInstance(); |
| 153 | |
| 154 | HashMap<String, String> details = (HashMap<String, String>) mCallbacks.getService().getAccountDetails(toDisplay.getAccountID()); |
| 155 | ArrayList<HashMap<String, String>> creds; |
| 156 | |
| 157 | creds = (ArrayList<HashMap<String, String>>) mCallbacks.getService().getCredentials(toDisplay.getAccountID()); |
| 158 | |
| 159 | callBuilder.startCallCreation().setAccount(new Account(toDisplay.getAccountID(), details, creds)) |
| 160 | .setCallType(SipCall.state.CALL_TYPE_OUTGOING); |
| 161 | callBuilder.setContact(toDisplay.getContact()); |
| 162 | |
| 163 | mCallbacks.onCall(callBuilder.build()); |
| 164 | |
| 165 | } catch (RemoteException e) { |
| 166 | // TODO Bloc catch généré automatiquement |
| 167 | e.printStackTrace(); |
| 168 | } catch (InvalidObjectException e) { |
| 169 | // TODO Bloc catch généré automatiquement |
| 170 | e.printStackTrace(); |
| 171 | } |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 172 | } |
| 173 | }); |
| 174 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 175 | lvMain.post(new Runnable() { |
| 176 | |
| 177 | @Override |
| 178 | public void run() { |
| 179 | |
| 180 | // Adjusts llMain's height to match ListView's height |
| 181 | setListViewHeight(lvMain, llMain); |
| 182 | |
| 183 | // LayoutParams to set the top margin of LinearLayout holding |
| 184 | // the content. |
| 185 | // topMargin = iv.getHeight() - tvTitle.getHeight() |
| 186 | LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) llMainHolder.getLayoutParams(); |
| 187 | // p.topMargin = iv.getHeight() - tvTitle.getHeight(); |
| 188 | llMainHolder.setLayoutParams(p); |
| 189 | } |
| 190 | }); |
| 191 | return inflatedView; |
| 192 | } |
| 193 | |
| 194 | public void onActivityCreated(Bundle savedInstanceState) { |
| 195 | super.onActivityCreated(savedInstanceState); |
| 196 | |
| 197 | } |
| 198 | |
| 199 | // Sets the ListView holder's height |
| 200 | public void setListViewHeight(ListView listView, LinearLayout llMain) { |
| 201 | ListAdapter listAdapter = listView.getAdapter(); |
| 202 | if (listAdapter == null) { |
| 203 | |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | int totalHeight = 0; |
| 208 | int firstHeight = 0; |
| 209 | int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); |
| 210 | |
| 211 | for (int i = 0; i < listAdapter.getCount(); i++) { |
| 212 | |
| 213 | if (i == 0) { |
| 214 | View listItem = listAdapter.getView(i, null, listView); |
| 215 | listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); |
| 216 | firstHeight = listItem.getMeasuredHeight(); |
| 217 | } |
| 218 | totalHeight += firstHeight; |
| 219 | } |
| 220 | |
Alexandre Lision | 4e458dc | 2013-11-12 10:07:40 -0500 | [diff] [blame] | 221 | // totalHeight -= iv.getMeasuredHeight(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 222 | |
| 223 | LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams(); |
| 224 | |
| 225 | params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); |
| 226 | llMain.setLayoutParams(params); |
| 227 | anotherView.requestLayout(); |
| 228 | } |
| 229 | |
| 230 | private class DetailHistoryAdapter extends BaseAdapter implements ListAdapter { |
| 231 | |
| 232 | ArrayList<HistoryCall> dataset; |
| 233 | Context mContext; |
| 234 | |
| 235 | public DetailHistoryAdapter(NavigableMap<Long, HistoryCall> calls, Context c) { |
| 236 | dataset = new ArrayList<HistoryCall>(calls.descendingMap().values()); |
| 237 | mContext = c; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public int getCount() { |
| 242 | return dataset.size(); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Object getItem(int position) { |
| 247 | return dataset.get(position); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public long getItemId(int position) { |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public View getView(int position, View convertView, ViewGroup parent) { |
| 257 | HistoryCallView entryView = null; |
| 258 | |
| 259 | if (convertView == null) { |
| 260 | // Get a new instance of the row layout view |
| 261 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 262 | convertView = inflater.inflate(R.layout.item_history_call, null); |
| 263 | |
| 264 | // Hold the view objects in an object |
| 265 | // so they don't need to be re-fetched |
| 266 | entryView = new HistoryCallView(); |
| 267 | entryView.historyCallState = (TextView) convertView.findViewById(R.id.history_call_state); |
| 268 | entryView.formatted_date = (TextView) convertView.findViewById(R.id.history_call_date_formatted); |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 269 | entryView.formatted_hour = (TextView) convertView.findViewById(R.id.history_call_hour); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 270 | entryView.record = (Button) convertView.findViewById(R.id.history_call_record); |
| 271 | entryView.duration = (TextView) convertView.findViewById(R.id.history_call_duration); |
| 272 | |
| 273 | convertView.setTag(entryView); |
| 274 | } else { |
| 275 | entryView = (HistoryCallView) convertView.getTag(); |
| 276 | } |
| 277 | |
| 278 | final HistoryCall item = dataset.get(position); |
| 279 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 280 | entryView.historyCallState.setText(item.getDirection()); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 281 | entryView.formatted_date.setText(item.getDate()); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 282 | entryView.duration.setText(item.getDurationString()); |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 283 | entryView.formatted_hour.setText(item.getStartString("h:mm a")); |
| 284 | if (item.isIncoming() && item.isMissed()) |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 285 | convertView.setBackgroundColor(getResources().getColor(R.color.holo_red_light)); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 286 | |
| 287 | if (item.hasRecord()) { |
| 288 | entryView.record.setVisibility(View.VISIBLE); |
| 289 | entryView.record.setTag(R.id.history_call_record, true); |
| 290 | entryView.record.setOnClickListener(new OnClickListener() { |
| 291 | |
| 292 | @Override |
| 293 | public void onClick(View v) { |
| 294 | try { |
| 295 | if ((Boolean) v.getTag(R.id.history_call_record)) { |
| 296 | mCallbacks.getService().startRecordedFilePlayback(item.getRecordPath()); |
| 297 | v.setTag(R.id.replay, false); |
| 298 | ((Button) v).setText(getString(R.string.hist_replay_button_stop)); |
| 299 | } else { |
| 300 | mCallbacks.getService().stopRecordedFilePlayback(item.getRecordPath()); |
| 301 | v.setTag(R.id.history_call_record, true); |
| 302 | ((Button) v).setText(getString(R.string.hist_replay_button)); |
| 303 | } |
| 304 | } catch (RemoteException e) { |
| 305 | // TODO Auto-generated catch block |
| 306 | e.printStackTrace(); |
| 307 | } |
| 308 | } |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | return convertView; |
| 313 | } |
| 314 | |
| 315 | /********************* |
| 316 | * ViewHolder Pattern |
| 317 | *********************/ |
| 318 | public class HistoryCallView { |
| 319 | protected TextView historyCallState; |
| 320 | protected TextView formatted_date; |
Alexandre Lision | 6da3bb9 | 2013-12-10 17:32:46 -0500 | [diff] [blame] | 321 | protected TextView formatted_hour; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 322 | protected Button record; |
| 323 | protected TextView duration; |
| 324 | } |
| 325 | |
| 326 | } |
| 327 | |
| 328 | } |