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 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.NavigableMap; |
| 36 | |
| 37 | import org.sflphone.R; |
| 38 | import org.sflphone.adapters.ContactPictureTask; |
| 39 | import org.sflphone.model.HistoryEntry; |
| 40 | import org.sflphone.model.HistoryEntry.HistoryCall; |
| 41 | import org.sflphone.service.ISipService; |
| 42 | import org.sflphone.views.parallaxscrollview.AnotherView; |
| 43 | import org.sflphone.views.parallaxscrollview.ParallaxScrollView; |
| 44 | |
| 45 | import android.app.Activity; |
| 46 | import android.app.Fragment; |
| 47 | import android.content.Context; |
| 48 | import android.os.Bundle; |
| 49 | import android.os.RemoteException; |
| 50 | import android.view.LayoutInflater; |
| 51 | import android.view.View; |
| 52 | import android.view.View.MeasureSpec; |
| 53 | import android.view.View.OnClickListener; |
| 54 | import android.view.ViewGroup; |
| 55 | import android.widget.BaseAdapter; |
| 56 | import android.widget.Button; |
| 57 | import android.widget.ImageView; |
| 58 | import android.widget.LinearLayout; |
| 59 | import android.widget.ListAdapter; |
| 60 | import android.widget.ListView; |
| 61 | import android.widget.RelativeLayout; |
| 62 | import android.widget.TextView; |
| 63 | |
| 64 | public class DetailsHistoryEntryFragment extends Fragment { |
| 65 | |
| 66 | View mheaderView; |
| 67 | DetailHistoryAdapter mAdapter; |
| 68 | HistoryEntry toDisplay; |
| 69 | private static final String TAG = DetailsHistoryEntryFragment.class.getSimpleName(); |
| 70 | ContactPictureTask tasker; |
| 71 | |
| 72 | private ParallaxScrollView mScrollView; |
| 73 | private ListView lvMain; |
| 74 | private LinearLayout llMain, llMainHolder; |
| 75 | private AnotherView anotherView; |
| 76 | private RelativeLayout iv; |
| 77 | |
| 78 | private Callbacks mCallbacks = sDummyCallbacks; |
| 79 | |
| 80 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 81 | |
| 82 | @Override |
| 83 | public ISipService getService() { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | }; |
| 88 | |
| 89 | public interface Callbacks { |
| 90 | |
| 91 | public ISipService getService(); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onAttach(Activity activity) { |
| 97 | super.onAttach(activity); |
| 98 | |
| 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); |
| 115 | |
| 116 | toDisplay = (HistoryEntry) getArguments().get("entry"); |
| 117 | mAdapter = new DetailHistoryAdapter(toDisplay.getCalls(), getActivity()); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 122 | View inflatedView = inflater.inflate(R.layout.frag_history_detail, parent, false); |
| 123 | |
| 124 | // mheaderView = LayoutInflater.from(getActivity()).inflate(R.layout.history_detail_header, null); |
| 125 | |
| 126 | mScrollView = (ParallaxScrollView) inflatedView.findViewById(R.id.scroll_view); |
| 127 | llMain = (LinearLayout) inflatedView.findViewById(R.id.llMain); |
| 128 | llMainHolder = (LinearLayout) inflatedView.findViewById(R.id.llMainHolder); |
| 129 | lvMain = (ListView) inflatedView.findViewById(R.id.lvMain); |
| 130 | lvMain.setAdapter(mAdapter); |
| 131 | iv = (RelativeLayout) inflatedView.findViewById(R.id.iv); |
| 132 | |
| 133 | ((TextView) iv.findViewById(R.id.history_call_name)).setText(toDisplay.getContact().getmDisplayName()); |
| 134 | tasker = new ContactPictureTask(getActivity(), (ImageView) iv.findViewById(R.id.contact_photo), toDisplay.getContact()); |
| 135 | tasker.run(); |
| 136 | anotherView = (AnotherView) inflatedView.findViewById(R.id.anotherView); |
| 137 | |
| 138 | lvMain.post(new Runnable() { |
| 139 | |
| 140 | @Override |
| 141 | public void run() { |
| 142 | |
| 143 | // Adjusts llMain's height to match ListView's height |
| 144 | setListViewHeight(lvMain, llMain); |
| 145 | |
| 146 | // LayoutParams to set the top margin of LinearLayout holding |
| 147 | // the content. |
| 148 | // topMargin = iv.getHeight() - tvTitle.getHeight() |
| 149 | LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) llMainHolder.getLayoutParams(); |
| 150 | // p.topMargin = iv.getHeight() - tvTitle.getHeight(); |
| 151 | llMainHolder.setLayoutParams(p); |
| 152 | } |
| 153 | }); |
| 154 | return inflatedView; |
| 155 | } |
| 156 | |
| 157 | public void onActivityCreated(Bundle savedInstanceState) { |
| 158 | super.onActivityCreated(savedInstanceState); |
| 159 | |
| 160 | } |
| 161 | |
| 162 | // Sets the ListView holder's height |
| 163 | public void setListViewHeight(ListView listView, LinearLayout llMain) { |
| 164 | ListAdapter listAdapter = listView.getAdapter(); |
| 165 | if (listAdapter == null) { |
| 166 | |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | int totalHeight = 0; |
| 171 | int firstHeight = 0; |
| 172 | int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); |
| 173 | |
| 174 | for (int i = 0; i < listAdapter.getCount(); i++) { |
| 175 | |
| 176 | if (i == 0) { |
| 177 | View listItem = listAdapter.getView(i, null, listView); |
| 178 | listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); |
| 179 | firstHeight = listItem.getMeasuredHeight(); |
| 180 | } |
| 181 | totalHeight += firstHeight; |
| 182 | } |
| 183 | |
| 184 | // totalHeight -= iv.getMeasuredHeight(); |
| 185 | |
| 186 | LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams(); |
| 187 | |
| 188 | params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); |
| 189 | llMain.setLayoutParams(params); |
| 190 | anotherView.requestLayout(); |
| 191 | } |
| 192 | |
| 193 | private class DetailHistoryAdapter extends BaseAdapter implements ListAdapter { |
| 194 | |
| 195 | ArrayList<HistoryCall> dataset; |
| 196 | Context mContext; |
| 197 | |
| 198 | public DetailHistoryAdapter(NavigableMap<Long, HistoryCall> calls, Context c) { |
| 199 | dataset = new ArrayList<HistoryCall>(calls.descendingMap().values()); |
| 200 | mContext = c; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public int getCount() { |
| 205 | return dataset.size(); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Object getItem(int position) { |
| 210 | return dataset.get(position); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public long getItemId(int position) { |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public View getView(int position, View convertView, ViewGroup parent) { |
| 220 | HistoryCallView entryView = null; |
| 221 | |
| 222 | if (convertView == null) { |
| 223 | // Get a new instance of the row layout view |
| 224 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 225 | convertView = inflater.inflate(R.layout.item_history_call, null); |
| 226 | |
| 227 | // Hold the view objects in an object |
| 228 | // so they don't need to be re-fetched |
| 229 | entryView = new HistoryCallView(); |
| 230 | entryView.historyCallState = (TextView) convertView.findViewById(R.id.history_call_state); |
| 231 | entryView.formatted_date = (TextView) convertView.findViewById(R.id.history_call_date_formatted); |
| 232 | entryView.record = (Button) convertView.findViewById(R.id.history_call_record); |
| 233 | entryView.duration = (TextView) convertView.findViewById(R.id.history_call_duration); |
| 234 | |
| 235 | convertView.setTag(entryView); |
| 236 | } else { |
| 237 | entryView = (HistoryCallView) convertView.getTag(); |
| 238 | } |
| 239 | |
| 240 | final HistoryCall item = dataset.get(position); |
| 241 | |
| 242 | entryView.historyCallState.setText(item.getState()); |
| 243 | entryView.formatted_date.setText(item.getDate()); |
| 244 | // entryView.displayName.setText(item.getDisplayName()); |
| 245 | entryView.duration.setText(item.getDurationString()); |
| 246 | |
| 247 | if (item.hasRecord()) { |
| 248 | entryView.record.setVisibility(View.VISIBLE); |
| 249 | entryView.record.setTag(R.id.history_call_record, true); |
| 250 | entryView.record.setOnClickListener(new OnClickListener() { |
| 251 | |
| 252 | @Override |
| 253 | public void onClick(View v) { |
| 254 | try { |
| 255 | if ((Boolean) v.getTag(R.id.history_call_record)) { |
| 256 | mCallbacks.getService().startRecordedFilePlayback(item.getRecordPath()); |
| 257 | v.setTag(R.id.replay, false); |
| 258 | ((Button) v).setText(getString(R.string.hist_replay_button_stop)); |
| 259 | } else { |
| 260 | mCallbacks.getService().stopRecordedFilePlayback(item.getRecordPath()); |
| 261 | v.setTag(R.id.history_call_record, true); |
| 262 | ((Button) v).setText(getString(R.string.hist_replay_button)); |
| 263 | } |
| 264 | } catch (RemoteException e) { |
| 265 | // TODO Auto-generated catch block |
| 266 | e.printStackTrace(); |
| 267 | } |
| 268 | } |
| 269 | }); |
| 270 | } |
| 271 | |
| 272 | return convertView; |
| 273 | } |
| 274 | |
| 275 | /********************* |
| 276 | * ViewHolder Pattern |
| 277 | *********************/ |
| 278 | public class HistoryCallView { |
| 279 | protected TextView historyCallState; |
| 280 | protected TextView formatted_date; |
| 281 | protected Button record; |
| 282 | protected TextView duration; |
| 283 | } |
| 284 | |
| 285 | } |
| 286 | |
| 287 | } |