blob: 6afdbdbd71bc581c1a4806af4846f8a9ef5eaab5 [file] [log] [blame]
Alexandre Savarddcd37402012-09-06 18:36:48 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
Alexandre Savarddcd37402012-09-06 18:36:48 -04003 *
4 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
alision2ec64f92013-06-17 17:28:58 -04005 * Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Alexandre Savarddcd37402012-09-06 18:36:48 -04006 *
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 Lision064e1e02013-10-01 16:18:42 -040033package org.sflphone.fragments;
34
Alexandre Lision933ef0a2013-10-15 17:28:40 -040035import java.util.ArrayList;
36
Alexandre Lision064e1e02013-10-01 16:18:42 -040037import org.sflphone.R;
Alexandre Lision933ef0a2013-10-15 17:28:40 -040038import org.sflphone.model.Codec;
39import org.sflphone.service.ISipService;
Alexandre Lisionc1849932013-10-24 10:09:32 -040040import org.sflphone.views.dragsortlv.DragSortListView;
Alexandre Savarddcd37402012-09-06 18:36:48 -040041
Alexandre Savard393ccab2012-09-11 15:01:20 -040042import android.app.Activity;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040043import android.content.Context;
Alexandre Lision6e8931e2013-09-19 16:49:34 -040044import android.os.Bundle;
Alexandre Lision933ef0a2013-10-15 17:28:40 -040045import android.os.RemoteException;
Alexandre Savard393ccab2012-09-11 15:01:20 -040046import android.preference.Preference;
Alexandre Savard393ccab2012-09-11 15:01:20 -040047import android.preference.PreferenceFragment;
Alexandre Lisionc1849932013-10-24 10:09:32 -040048import android.view.LayoutInflater;
Alexandre Savarddcd37402012-09-06 18:36:48 -040049import android.view.View;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040050import android.view.ViewGroup;
Alexandre Lisiona3650992013-11-13 14:19:35 -050051import android.view.View.MeasureSpec;
Alexandre Lisionc1849932013-10-24 10:09:32 -040052import android.widget.AdapterView;
53import android.widget.AdapterView.OnItemClickListener;
54import android.widget.BaseAdapter;
55import android.widget.CheckBox;
Alexandre Lisiona3650992013-11-13 14:19:35 -050056import android.widget.LinearLayout;
57import android.widget.ListAdapter;
58import android.widget.ListView;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040059import android.widget.TextView;
Alexandre Savarddcd37402012-09-06 18:36:48 -040060
Alexandre Lision933ef0a2013-10-15 17:28:40 -040061public class AudioManagementFragment extends PreferenceFragment {
Alexandre Lisiona3650992013-11-13 14:19:35 -050062 static final String TAG = AudioManagementFragment.class.getSimpleName();
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040063
Alexandre Lision933ef0a2013-10-15 17:28:40 -040064 protected Callbacks mCallbacks = sDummyCallbacks;
65 ArrayList<Codec> codecs;
Alexandre Lisiona3650992013-11-13 14:19:35 -050066 private DragSortListView v;
67 CodecAdapter listAdapter;
Alexandre Lision933ef0a2013-10-15 17:28:40 -040068 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 Savard12dc3ac2012-09-27 11:17:39 -040088 }
Alexandre Savarddcd37402012-09-06 18:36:48 -040089
90 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -040091 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 Lision4b4233a2013-10-16 17:24:17 -0400100 mCallbacks.getService().getRingtoneList();
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400101 } catch (RemoteException e) {
102 e.printStackTrace();
103 }
104 }
105
106 @Override
107 public void onDetach() {
108 super.onDetach();
109 mCallbacks = sDummyCallbacks;
110 }
111
Alexandre Lisionc1849932013-10-24 10:09:32 -0400112 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 Lision4df961d2013-10-16 13:44:49 -0400119 try {
Alexandre Lisionc1849932013-10-24 10:09:32 -0400120 mCallbacks.getService().setActiveCodecList(getActiveCodecList(), mCallbacks.getAccountID());
Alexandre Lision4df961d2013-10-16 13:44:49 -0400121 } catch (RemoteException e) {
122 e.printStackTrace();
123 }
Alexandre Lisionc1849932013-10-24 10:09:32 -0400124 }
125 }
126 };
Alexandre Lisiona3650992013-11-13 14:19:35 -0500127
Alexandre Lisionc1849932013-10-24 10:09:32 -0400128 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 Lisiona3650992013-11-13 14:19:35 -0500137
Alexandre Lisionc1849932013-10-24 10:09:32 -0400138 @Override
Alexandre Lisiona3650992013-11-13 14:19:35 -0500139 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Alexandre Lisionc1849932013-10-24 10:09:32 -0400140 super.onCreateView(inflater, container, savedInstanceState);
Alexandre Lisiona3650992013-11-13 14:19:35 -0500141 View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.frag_audio_mgmt, null);
142 v = (DragSortListView) rootView.findViewById(R.id.dndlistview);
Alexandre Lisionc1849932013-10-24 10:09:32 -0400143 v.setAdapter(listAdapter);
144 v.setDropListener(onDrop);
145 v.setOnItemClickListener(new OnItemClickListener() {
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 Lision4df961d2013-10-16 13:44:49 -0400157 }
158 });
Alexandre Lisionc1849932013-10-24 10:09:32 -0400159 return rootView;
160 }
161
162 @Override
Alexandre Lisiona3650992013-11-13 14:19:35 -0500163 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() {
170 setListViewHeight(v, holder);
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 Lisionc1849932013-10-24 10:09:32 -0400207 public void onCreate(Bundle savedInstanceState) {
Alexandre Lisiona3650992013-11-13 14:19:35 -0500208 super.onCreate(savedInstanceState);
209
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500210 addPreferencesFromResource(R.xml.account_audio_prefs);
Alexandre Lisionc1849932013-10-24 10:09:32 -0400211 listAdapter = new CodecAdapter(getActivity());
212 listAdapter.setDataset(codecs);
Alexandre Savarddcd37402012-09-06 18:36:48 -0400213 }
214
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400215 Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() {
Alexandre Savard393ccab2012-09-11 15:01:20 -0400216 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400217 preference.setSummary((CharSequence) newValue);
Alexandre Savard393ccab2012-09-11 15:01:20 -0400218 return true;
219 }
220 };
221
Alexandre Lisionc1849932013-10-24 10:09:32 -0400222 public static class CodecAdapter extends BaseAdapter {
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400223
Alexandre Lisionc1849932013-10-24 10:09:32 -0400224 ArrayList<Codec> items;
225 private Context mContext;
226
227 public CodecAdapter(Context context) {
228 items = new ArrayList<Codec>();
229 mContext = context;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400230 }
231
Alexandre Lisionc1849932013-10-24 10:09:32 -0400232 public void insert(Codec item, int to) {
233 items.add(to, item);
234 notifyDataSetChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400235 }
236
Alexandre Lisionc1849932013-10-24 10:09:32 -0400237 public void remove(Codec item) {
238 items.remove(item);
239 notifyDataSetChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400240 }
241
Alexandre Lisionc1849932013-10-24 10:09:32 -0400242 public ArrayList<Codec> getDataset() {
243 return items;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400244 }
245
246 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400247 public int getCount() {
248 return items.size();
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400249 }
250
Alexandre Lisionc1849932013-10-24 10:09:32 -0400251 @Override
252 public Codec getItem(int position) {
253 return items.get(position);
254 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400255
Alexandre Lisionc1849932013-10-24 10:09:32 -0400256 @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 Savard5e0b9472012-09-11 17:56:30 -0400284 }
285
Alexandre Lisionc1849932013-10-24 10:09:32 -0400286 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 Savard5e0b9472012-09-11 17:56:30 -0400293
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400294 }
295
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400296 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400297 public int getViewTypeCount() {
298 return 1;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400299 }
300
301 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400302 public boolean hasStableIds() {
303 return true;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400304 }
305
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400306 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400307 public boolean isEmpty() {
308 return getCount() == 0;
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400309 }
310
Alexandre Lisionc1849932013-10-24 10:09:32 -0400311 @Override
312 public boolean areAllItemsEnabled() {
313 return true;
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400314 }
315
Alexandre Lisionc1849932013-10-24 10:09:32 -0400316 @Override
317 public boolean isEnabled(int position) {
318 return true;
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400319 }
320
Alexandre Lisionc1849932013-10-24 10:09:32 -0400321 public void setDataset(ArrayList<Codec> codecs) {
322 items = new ArrayList<Codec>(codecs);
323 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400324
Alexandre Lisionc1849932013-10-24 10:09:32 -0400325 /*********************
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 Savard5e0b9472012-09-11 17:56:30 -0400334 }
335 }
Alexandre Savarddcd37402012-09-06 18:36:48 -0400336}