Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 1 | /* |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 2 | * Copyright (C) 2004-2013 Savoir-Faire Linux Inc. |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 5 | * Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * Additional permission under GNU GPL version 3 section 7: |
| 22 | * |
| 23 | * If you modify this program, or any covered work, by linking or |
| 24 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 25 | * modified version of that library), containing parts covered by the |
| 26 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 27 | * grants you additional permission to convey the resulting work. |
| 28 | * Corresponding Source for a non-source form of such a combination |
| 29 | * shall include the source code for the parts of OpenSSL used as well |
| 30 | * as that of the covered work. |
| 31 | */ |
| 32 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 33 | package org.sflphone.fragments; |
| 34 | |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 37 | import org.sflphone.R; |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 38 | import org.sflphone.model.Codec; |
| 39 | import org.sflphone.service.ISipService; |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 40 | import org.sflphone.views.dragsortlv.DragSortListView; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 41 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 42 | import android.app.Activity; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 43 | import android.content.Context; |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 44 | import android.os.Bundle; |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 45 | import android.os.RemoteException; |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 46 | import android.preference.Preference; |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 47 | import android.preference.PreferenceFragment; |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 49 | import android.view.View; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 50 | import android.view.ViewGroup; |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 51 | import android.view.View.MeasureSpec; |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 52 | import android.widget.AdapterView; |
| 53 | import android.widget.AdapterView.OnItemClickListener; |
| 54 | import android.widget.BaseAdapter; |
| 55 | import android.widget.CheckBox; |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 56 | import android.widget.LinearLayout; |
| 57 | import android.widget.ListAdapter; |
| 58 | import android.widget.ListView; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 59 | import android.widget.TextView; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 60 | |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 61 | public class AudioManagementFragment extends PreferenceFragment { |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 62 | static final String TAG = AudioManagementFragment.class.getSimpleName(); |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 63 | |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 64 | protected Callbacks mCallbacks = sDummyCallbacks; |
| 65 | ArrayList<Codec> codecs; |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame^] | 66 | private DragSortListView mCodecList; |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 67 | CodecAdapter listAdapter; |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 68 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 69 | |
| 70 | @Override |
| 71 | public ISipService getService() { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String getAccountID() { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | public interface Callbacks { |
| 83 | |
| 84 | public ISipService getService(); |
| 85 | |
| 86 | public String getAccountID(); |
| 87 | |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 88 | } |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 89 | |
| 90 | @Override |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 91 | public void onAttach(Activity activity) { |
| 92 | super.onAttach(activity); |
| 93 | if (!(activity instanceof Callbacks)) { |
| 94 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 95 | } |
| 96 | |
| 97 | mCallbacks = (Callbacks) activity; |
| 98 | try { |
| 99 | codecs = (ArrayList<Codec>) mCallbacks.getService().getAudioCodecList(mCallbacks.getAccountID()); |
Alexandre Lision | 4b4233a | 2013-10-16 17:24:17 -0400 | [diff] [blame] | 100 | mCallbacks.getService().getRingtoneList(); |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 101 | } catch (RemoteException e) { |
| 102 | e.printStackTrace(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onDetach() { |
| 108 | super.onDetach(); |
| 109 | mCallbacks = sDummyCallbacks; |
| 110 | } |
| 111 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 112 | private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() { |
| 113 | @Override |
| 114 | public void drop(int from, int to) { |
| 115 | if (from != to) { |
| 116 | Codec item = listAdapter.getItem(from); |
| 117 | listAdapter.remove(item); |
| 118 | listAdapter.insert(item, to); |
Alexandre Lision | 4df961d | 2013-10-16 13:44:49 -0400 | [diff] [blame] | 119 | try { |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 120 | mCallbacks.getService().setActiveCodecList(getActiveCodecList(), mCallbacks.getAccountID()); |
Alexandre Lision | 4df961d | 2013-10-16 13:44:49 -0400 | [diff] [blame] | 121 | } catch (RemoteException e) { |
| 122 | e.printStackTrace(); |
| 123 | } |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | }; |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 127 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 128 | public ArrayList<String> getActiveCodecList() { |
| 129 | ArrayList<String> results = new ArrayList<String>(); |
| 130 | for (int i = 0; i < listAdapter.getCount(); ++i) { |
| 131 | if (listAdapter.getItem(i).isEnabled()) { |
| 132 | results.add(listAdapter.getItem(i).getPayload().toString()); |
| 133 | } |
| 134 | } |
| 135 | return results; |
| 136 | } |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 137 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 138 | @Override |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 139 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 140 | super.onCreateView(inflater, container, savedInstanceState); |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 141 | View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.frag_audio_mgmt, null); |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame^] | 142 | mCodecList = (DragSortListView) rootView.findViewById(R.id.dndlistview); |
| 143 | mCodecList.setAdapter(listAdapter); |
| 144 | mCodecList.setDropListener(onDrop); |
| 145 | mCodecList.setOnItemClickListener(new OnItemClickListener() { |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 146 | |
| 147 | @Override |
| 148 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
| 149 | listAdapter.getItem(pos).toggleState(); |
| 150 | listAdapter.notifyDataSetChanged(); |
| 151 | try { |
| 152 | mCallbacks.getService().setActiveCodecList(getActiveCodecList(), mCallbacks.getAccountID()); |
| 153 | } catch (RemoteException e) { |
| 154 | e.printStackTrace(); |
| 155 | } |
| 156 | |
Alexandre Lision | 4df961d | 2013-10-16 13:44:49 -0400 | [diff] [blame] | 157 | } |
| 158 | }); |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 159 | return rootView; |
| 160 | } |
| 161 | |
| 162 | @Override |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 163 | public void onViewCreated(View view, Bundle savedInstanceState) { |
| 164 | super.onViewCreated(view, savedInstanceState); |
| 165 | final LinearLayout holder = (LinearLayout) getView().findViewById(R.id.lv_holder); |
| 166 | holder.post(new Runnable() { |
| 167 | |
| 168 | @Override |
| 169 | public void run() { |
Alexandre Lision | c9c30b7 | 2013-11-18 16:27:26 -0500 | [diff] [blame^] | 170 | setListViewHeight(mCodecList, holder); |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 171 | } |
| 172 | }); |
| 173 | |
| 174 | } |
| 175 | |
| 176 | // Sets the ListView holder's height |
| 177 | public void setListViewHeight(ListView listView, LinearLayout llMain) { |
| 178 | ListAdapter listAdapter = listView.getAdapter(); |
| 179 | if (listAdapter == null) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | int totalHeight = 0; |
| 184 | int firstHeight = 0; |
| 185 | int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); |
| 186 | |
| 187 | for (int i = 0; i < listAdapter.getCount(); i++) { |
| 188 | |
| 189 | if (i == 0) { |
| 190 | View listItem = listAdapter.getView(i, null, listView); |
| 191 | listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); |
| 192 | firstHeight = listItem.getMeasuredHeight(); |
| 193 | } |
| 194 | totalHeight += firstHeight; |
| 195 | } |
| 196 | |
| 197 | // totalHeight -= iv.getMeasuredHeight(); |
| 198 | |
| 199 | LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llMain.getLayoutParams(); |
| 200 | |
| 201 | params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); |
| 202 | llMain.setLayoutParams(params); |
| 203 | getView().requestLayout(); |
| 204 | } |
| 205 | |
| 206 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 207 | public void onCreate(Bundle savedInstanceState) { |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 208 | super.onCreate(savedInstanceState); |
| 209 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 210 | addPreferencesFromResource(R.xml.account_audio_prefs); |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 211 | listAdapter = new CodecAdapter(getActivity()); |
| 212 | listAdapter.setDataset(codecs); |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 215 | Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 216 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 217 | preference.setSummary((CharSequence) newValue); |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | }; |
| 221 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 222 | public static class CodecAdapter extends BaseAdapter { |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 223 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 224 | ArrayList<Codec> items; |
| 225 | private Context mContext; |
| 226 | |
| 227 | public CodecAdapter(Context context) { |
| 228 | items = new ArrayList<Codec>(); |
| 229 | mContext = context; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 232 | public void insert(Codec item, int to) { |
| 233 | items.add(to, item); |
| 234 | notifyDataSetChanged(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 237 | public void remove(Codec item) { |
| 238 | items.remove(item); |
| 239 | notifyDataSetChanged(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 242 | public ArrayList<Codec> getDataset() { |
| 243 | return items; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 247 | public int getCount() { |
| 248 | return items.size(); |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 251 | @Override |
| 252 | public Codec getItem(int position) { |
| 253 | return items.get(position); |
| 254 | } |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 255 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 256 | @Override |
| 257 | public long getItemId(int position) { |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public int getItemViewType(int position) { |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public View getView(int pos, View convertView, ViewGroup parent) { |
| 268 | View rowView = convertView; |
| 269 | CodecView entryView = null; |
| 270 | |
| 271 | if (rowView == null) { |
| 272 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 273 | rowView = inflater.inflate(R.layout.item_codec, null); |
| 274 | |
| 275 | entryView = new CodecView(); |
| 276 | entryView.name = (TextView) rowView.findViewById(R.id.codec_name); |
| 277 | entryView.bitrate = (TextView) rowView.findViewById(R.id.codec_bitrate); |
| 278 | entryView.samplerate = (TextView) rowView.findViewById(R.id.codec_samplerate); |
| 279 | entryView.channels = (TextView) rowView.findViewById(R.id.codec_channels); |
| 280 | entryView.enabled = (CheckBox) rowView.findViewById(R.id.codec_checked); |
| 281 | rowView.setTag(entryView); |
| 282 | } else { |
| 283 | entryView = (CodecView) rowView.getTag(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 286 | entryView.name.setText(items.get(pos).getName()); |
| 287 | entryView.samplerate.setText(items.get(pos).getSampleRate()); |
| 288 | entryView.bitrate.setText(items.get(pos).getBitRate()); |
| 289 | entryView.channels.setText(items.get(pos).getChannels()); |
| 290 | entryView.enabled.setChecked(items.get(pos).isEnabled()); |
| 291 | ; |
| 292 | return rowView; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 293 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 296 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 297 | public int getViewTypeCount() { |
| 298 | return 1; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 302 | public boolean hasStableIds() { |
| 303 | return true; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 306 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 307 | public boolean isEmpty() { |
| 308 | return getCount() == 0; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 311 | @Override |
| 312 | public boolean areAllItemsEnabled() { |
| 313 | return true; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 316 | @Override |
| 317 | public boolean isEnabled(int position) { |
| 318 | return true; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 319 | } |
| 320 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 321 | public void setDataset(ArrayList<Codec> codecs) { |
| 322 | items = new ArrayList<Codec>(codecs); |
| 323 | } |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 324 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 325 | /********************* |
| 326 | * ViewHolder Pattern |
| 327 | *********************/ |
| 328 | public class CodecView { |
| 329 | public TextView name; |
| 330 | public TextView samplerate; |
| 331 | public TextView bitrate; |
| 332 | public TextView channels; |
| 333 | public CheckBox enabled; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 334 | } |
| 335 | } |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 336 | } |