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 | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 51 | import android.widget.AdapterView; |
| 52 | import android.widget.AdapterView.OnItemClickListener; |
| 53 | import android.widget.BaseAdapter; |
| 54 | import android.widget.CheckBox; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 55 | import android.widget.TextView; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 56 | |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 57 | public class AudioManagementFragment extends PreferenceFragment { |
| 58 | static final String TAG = "AudioManagementFragment"; |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 59 | |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 60 | 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 Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 82 | } |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 83 | |
| 84 | @Override |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 85 | 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 Lision | 4b4233a | 2013-10-16 17:24:17 -0400 | [diff] [blame] | 94 | mCallbacks.getService().getRingtoneList(); |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 95 | } catch (RemoteException e) { |
| 96 | e.printStackTrace(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onDetach() { |
| 102 | super.onDetach(); |
| 103 | mCallbacks = sDummyCallbacks; |
| 104 | } |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 105 | |
| 106 | CodecAdapter listAdapter; |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 107 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 108 | 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 Lision | 4df961d | 2013-10-16 13:44:49 -0400 | [diff] [blame] | 115 | try { |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 116 | mCallbacks.getService().setActiveCodecList(getActiveCodecList(), mCallbacks.getAccountID()); |
Alexandre Lision | 4df961d | 2013-10-16 13:44:49 -0400 | [diff] [blame] | 117 | } catch (RemoteException e) { |
| 118 | e.printStackTrace(); |
| 119 | } |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 120 | } |
| 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 Lision | 4df961d | 2013-10-16 13:44:49 -0400 | [diff] [blame] | 153 | } |
| 154 | }); |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 155 | return rootView; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void onCreate(Bundle savedInstanceState) { |
| 160 | super.onCreate(savedInstanceState); |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 161 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 162 | addPreferencesFromResource(R.xml.audio_prefs); |
| 163 | listAdapter = new CodecAdapter(getActivity()); |
| 164 | listAdapter.setDataset(codecs); |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 167 | Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 168 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 169 | preference.setSummary((CharSequence) newValue); |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | }; |
| 173 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 174 | |
Alexandre Lision | 933ef0a | 2013-10-15 17:28:40 -0400 | [diff] [blame] | 175 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 176 | public static class CodecAdapter extends BaseAdapter { |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 177 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 178 | ArrayList<Codec> items; |
| 179 | private Context mContext; |
| 180 | |
| 181 | public CodecAdapter(Context context) { |
| 182 | items = new ArrayList<Codec>(); |
| 183 | mContext = context; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 186 | public void insert(Codec item, int to) { |
| 187 | items.add(to, item); |
| 188 | notifyDataSetChanged(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 191 | public void remove(Codec item) { |
| 192 | items.remove(item); |
| 193 | notifyDataSetChanged(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 196 | public ArrayList<Codec> getDataset() { |
| 197 | return items; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 201 | public int getCount() { |
| 202 | return items.size(); |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 205 | @Override |
| 206 | public Codec getItem(int position) { |
| 207 | return items.get(position); |
| 208 | } |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 209 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 210 | @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 Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 240 | 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 Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 247 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 250 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 251 | public int getViewTypeCount() { |
| 252 | return 1; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 256 | public boolean hasStableIds() { |
| 257 | return true; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 260 | @Override |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 261 | public boolean isEmpty() { |
| 262 | return getCount() == 0; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 265 | @Override |
| 266 | public boolean areAllItemsEnabled() { |
| 267 | return true; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 270 | @Override |
| 271 | public boolean isEnabled(int position) { |
| 272 | return true; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 275 | public void setDataset(ArrayList<Codec> codecs) { |
| 276 | items = new ArrayList<Codec>(codecs); |
| 277 | } |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 278 | |
Alexandre Lision | c184993 | 2013-10-24 10:09:32 -0400 | [diff] [blame] | 279 | /********************* |
| 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 Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 288 | } |
| 289 | } |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 290 | } |