blob: fb440ef69fff0ca8da37b66fbb974f4ee6dd34d5 [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 Lisionc1849932013-10-24 10:09:32 -040051import android.widget.AdapterView;
52import android.widget.AdapterView.OnItemClickListener;
53import android.widget.BaseAdapter;
54import android.widget.CheckBox;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040055import android.widget.TextView;
Alexandre Savarddcd37402012-09-06 18:36:48 -040056
Alexandre Lision933ef0a2013-10-15 17:28:40 -040057public class AudioManagementFragment extends PreferenceFragment {
58 static final String TAG = "AudioManagementFragment";
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040059
Alexandre Lision933ef0a2013-10-15 17:28:40 -040060 protected Callbacks mCallbacks = sDummyCallbacks;
61 ArrayList<Codec> codecs;
62 private static Callbacks sDummyCallbacks = new Callbacks() {
63
64 @Override
65 public ISipService getService() {
66 return null;
67 }
68
69 @Override
70 public String getAccountID() {
71 return null;
72 }
73
74 };
75
76 public interface Callbacks {
77
78 public ISipService getService();
79
80 public String getAccountID();
81
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040082 }
Alexandre Savarddcd37402012-09-06 18:36:48 -040083
84 @Override
Alexandre Lision933ef0a2013-10-15 17:28:40 -040085 public void onAttach(Activity activity) {
86 super.onAttach(activity);
87 if (!(activity instanceof Callbacks)) {
88 throw new IllegalStateException("Activity must implement fragment's callbacks.");
89 }
90
91 mCallbacks = (Callbacks) activity;
92 try {
93 codecs = (ArrayList<Codec>) mCallbacks.getService().getAudioCodecList(mCallbacks.getAccountID());
Alexandre Lision4b4233a2013-10-16 17:24:17 -040094 mCallbacks.getService().getRingtoneList();
Alexandre Lision933ef0a2013-10-15 17:28:40 -040095 } catch (RemoteException e) {
96 e.printStackTrace();
97 }
98 }
99
100 @Override
101 public void onDetach() {
102 super.onDetach();
103 mCallbacks = sDummyCallbacks;
104 }
Alexandre Lisionc1849932013-10-24 10:09:32 -0400105
106 CodecAdapter listAdapter;
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400107
Alexandre Lisionc1849932013-10-24 10:09:32 -0400108 private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
109 @Override
110 public void drop(int from, int to) {
111 if (from != to) {
112 Codec item = listAdapter.getItem(from);
113 listAdapter.remove(item);
114 listAdapter.insert(item, to);
Alexandre Lision4df961d2013-10-16 13:44:49 -0400115 try {
Alexandre Lisionc1849932013-10-24 10:09:32 -0400116 mCallbacks.getService().setActiveCodecList(getActiveCodecList(), mCallbacks.getAccountID());
Alexandre Lision4df961d2013-10-16 13:44:49 -0400117 } catch (RemoteException e) {
118 e.printStackTrace();
119 }
Alexandre Lisionc1849932013-10-24 10:09:32 -0400120 }
121 }
122 };
123
124 public ArrayList<String> getActiveCodecList() {
125 ArrayList<String> results = new ArrayList<String>();
126 for (int i = 0; i < listAdapter.getCount(); ++i) {
127 if (listAdapter.getItem(i).isEnabled()) {
128 results.add(listAdapter.getItem(i).getPayload().toString());
129 }
130 }
131 return results;
132 }
133
134 @Override
135 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
136 super.onCreateView(inflater, container, savedInstanceState);
137 View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_codecs_list, null);
138 DragSortListView v = (DragSortListView) rootView.findViewById(R.id.dndlistview);
139 v.setAdapter(listAdapter);
140 v.setDropListener(onDrop);
141 v.setOnItemClickListener(new OnItemClickListener() {
142
143 @Override
144 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
145 listAdapter.getItem(pos).toggleState();
146 listAdapter.notifyDataSetChanged();
147 try {
148 mCallbacks.getService().setActiveCodecList(getActiveCodecList(), mCallbacks.getAccountID());
149 } catch (RemoteException e) {
150 e.printStackTrace();
151 }
152
Alexandre Lision4df961d2013-10-16 13:44:49 -0400153 }
154 });
Alexandre Lisionc1849932013-10-24 10:09:32 -0400155 return rootView;
156 }
157
158 @Override
159 public void onCreate(Bundle savedInstanceState) {
160 super.onCreate(savedInstanceState);
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400161
Alexandre Lisionc1849932013-10-24 10:09:32 -0400162 addPreferencesFromResource(R.xml.audio_prefs);
163 listAdapter = new CodecAdapter(getActivity());
164 listAdapter.setDataset(codecs);
Alexandre Savarddcd37402012-09-06 18:36:48 -0400165 }
166
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400167 Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() {
Alexandre Savard393ccab2012-09-11 15:01:20 -0400168 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400169 preference.setSummary((CharSequence) newValue);
Alexandre Savard393ccab2012-09-11 15:01:20 -0400170 return true;
171 }
172 };
173
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400174
Alexandre Lision933ef0a2013-10-15 17:28:40 -0400175
Alexandre Lisionc1849932013-10-24 10:09:32 -0400176 public static class CodecAdapter extends BaseAdapter {
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400177
Alexandre Lisionc1849932013-10-24 10:09:32 -0400178 ArrayList<Codec> items;
179 private Context mContext;
180
181 public CodecAdapter(Context context) {
182 items = new ArrayList<Codec>();
183 mContext = context;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400184 }
185
Alexandre Lisionc1849932013-10-24 10:09:32 -0400186 public void insert(Codec item, int to) {
187 items.add(to, item);
188 notifyDataSetChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400189 }
190
Alexandre Lisionc1849932013-10-24 10:09:32 -0400191 public void remove(Codec item) {
192 items.remove(item);
193 notifyDataSetChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400194 }
195
Alexandre Lisionc1849932013-10-24 10:09:32 -0400196 public ArrayList<Codec> getDataset() {
197 return items;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400198 }
199
200 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400201 public int getCount() {
202 return items.size();
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400203 }
204
Alexandre Lisionc1849932013-10-24 10:09:32 -0400205 @Override
206 public Codec getItem(int position) {
207 return items.get(position);
208 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400209
Alexandre Lisionc1849932013-10-24 10:09:32 -0400210 @Override
211 public long getItemId(int position) {
212 return 0;
213 }
214
215 @Override
216 public int getItemViewType(int position) {
217 return 0;
218 }
219
220 @Override
221 public View getView(int pos, View convertView, ViewGroup parent) {
222 View rowView = convertView;
223 CodecView entryView = null;
224
225 if (rowView == null) {
226 LayoutInflater inflater = LayoutInflater.from(mContext);
227 rowView = inflater.inflate(R.layout.item_codec, null);
228
229 entryView = new CodecView();
230 entryView.name = (TextView) rowView.findViewById(R.id.codec_name);
231 entryView.bitrate = (TextView) rowView.findViewById(R.id.codec_bitrate);
232 entryView.samplerate = (TextView) rowView.findViewById(R.id.codec_samplerate);
233 entryView.channels = (TextView) rowView.findViewById(R.id.codec_channels);
234 entryView.enabled = (CheckBox) rowView.findViewById(R.id.codec_checked);
235 rowView.setTag(entryView);
236 } else {
237 entryView = (CodecView) rowView.getTag();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400238 }
239
Alexandre Lisionc1849932013-10-24 10:09:32 -0400240 entryView.name.setText(items.get(pos).getName());
241 entryView.samplerate.setText(items.get(pos).getSampleRate());
242 entryView.bitrate.setText(items.get(pos).getBitRate());
243 entryView.channels.setText(items.get(pos).getChannels());
244 entryView.enabled.setChecked(items.get(pos).isEnabled());
245 ;
246 return rowView;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400247
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400248 }
249
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400250 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400251 public int getViewTypeCount() {
252 return 1;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400253 }
254
255 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400256 public boolean hasStableIds() {
257 return true;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400258 }
259
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400260 @Override
Alexandre Lisionc1849932013-10-24 10:09:32 -0400261 public boolean isEmpty() {
262 return getCount() == 0;
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400263 }
264
Alexandre Lisionc1849932013-10-24 10:09:32 -0400265 @Override
266 public boolean areAllItemsEnabled() {
267 return true;
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400268 }
269
Alexandre Lisionc1849932013-10-24 10:09:32 -0400270 @Override
271 public boolean isEnabled(int position) {
272 return true;
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400273 }
274
Alexandre Lisionc1849932013-10-24 10:09:32 -0400275 public void setDataset(ArrayList<Codec> codecs) {
276 items = new ArrayList<Codec>(codecs);
277 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400278
Alexandre Lisionc1849932013-10-24 10:09:32 -0400279 /*********************
280 * ViewHolder Pattern
281 *********************/
282 public class CodecView {
283 public TextView name;
284 public TextView samplerate;
285 public TextView bitrate;
286 public TextView channels;
287 public CheckBox enabled;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400288 }
289 }
Alexandre Savarddcd37402012-09-06 18:36:48 -0400290}