Alexandre Lision | 2affbaa | 2013-09-27 16:30:35 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | 2affbaa | 2013-09-27 16:30:35 -0400 | [diff] [blame] | 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 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.fragments; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | |
Alexandre Lision | 5f144b8 | 2014-02-11 09:59:36 -0500 | [diff] [blame] | 38 | import android.app.Dialog; |
| 39 | import android.support.v4.app.DialogFragment; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 40 | import org.sflphone.R; |
| 41 | import org.sflphone.loaders.ContactsLoader; |
| 42 | import org.sflphone.model.Conference; |
| 43 | import org.sflphone.model.SipCall; |
| 44 | |
Adrien Béraud | 9751284 | 2013-09-19 13:37:19 +1000 | [diff] [blame] | 45 | import android.app.Activity; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 46 | import android.app.AlertDialog; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 47 | import android.app.LoaderManager; |
| 48 | import android.content.Context; |
| 49 | import android.content.DialogInterface; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 50 | import android.content.DialogInterface.OnShowListener; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 51 | import android.content.Intent; |
| 52 | import android.content.Loader; |
| 53 | import android.location.Address; |
| 54 | import android.location.Geocoder; |
| 55 | import android.net.Uri; |
| 56 | import android.os.Bundle; |
| 57 | import android.provider.ContactsContract.Contacts; |
| 58 | import android.view.LayoutInflater; |
| 59 | import android.view.View; |
| 60 | import android.view.ViewGroup; |
| 61 | import android.widget.AdapterView; |
| 62 | import android.widget.AdapterView.OnItemClickListener; |
| 63 | import android.widget.ArrayAdapter; |
| 64 | import android.widget.AutoCompleteTextView; |
| 65 | import android.widget.BaseAdapter; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 66 | import android.widget.Button; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 67 | import android.widget.Filter; |
| 68 | import android.widget.Filterable; |
| 69 | import android.widget.ListView; |
| 70 | import android.widget.TextView; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 71 | import android.widget.Toast; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 72 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 73 | public class TransferDFragment extends DialogFragment implements LoaderManager.LoaderCallbacks<Bundle> { |
Adrien Béraud | 9751284 | 2013-09-19 13:37:19 +1000 | [diff] [blame] | 74 | public static final int RESULT_TRANSFER_CONF = Activity.RESULT_FIRST_USER + 1; |
| 75 | public static final int RESULT_TRANSFER_NUMBER = Activity.RESULT_FIRST_USER + 2; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 76 | |
| 77 | private AutoCompleteTextView mEditText; |
| 78 | private AutoCompleteAdapter autoCompleteAdapter; |
| 79 | SimpleCallListAdapter mAdapter; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 80 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 81 | /** |
| 82 | * Create a new instance of CallActionsDFragment |
| 83 | */ |
Alexandre Lision | 2affbaa | 2013-09-27 16:30:35 -0400 | [diff] [blame] | 84 | static TransferDFragment newInstance() { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 85 | TransferDFragment f = new TransferDFragment(); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 86 | return f; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onCreate(Bundle savedInstanceState) { |
| 91 | super.onCreate(savedInstanceState); |
| 92 | |
| 93 | // Pick a style based on the num. |
| 94 | int style = DialogFragment.STYLE_NORMAL, theme = 0; |
| 95 | setStyle(style, theme); |
| 96 | } |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 97 | |
| 98 | @Override |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 99 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 100 | View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_transfer, null); |
| 101 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 102 | ArrayList<Conference> calls = getArguments().getParcelableArrayList("calls"); |
Alexandre Lision | 2b23792 | 2013-09-09 16:23:02 -0400 | [diff] [blame] | 103 | final SipCall call_selected = getArguments().getParcelable("call_selected"); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 104 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 105 | mAdapter = new SimpleCallListAdapter(getActivity(), calls); |
| 106 | ListView list = (ListView) rootView.findViewById(R.id.concurrent_calls); |
| 107 | list.setAdapter(mAdapter); |
| 108 | list.setOnItemClickListener(new OnItemClickListener() { |
| 109 | |
| 110 | @Override |
| 111 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 112 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 113 | Intent in = new Intent(); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 114 | in.putExtra("target", mAdapter.getItem(pos)); |
| 115 | in.putExtra("transfer", call_selected); |
Adrien Béraud | 9751284 | 2013-09-19 13:37:19 +1000 | [diff] [blame] | 116 | |
Adrien Béraud | e78d06f | 2013-09-19 13:33:44 +1000 | [diff] [blame] | 117 | getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_TRANSFER_CONF, in); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 118 | dismiss(); |
| 119 | } |
| 120 | }); |
| 121 | list.setEmptyView(rootView.findViewById(R.id.empty_view)); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 122 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 123 | mEditText = (AutoCompleteTextView) rootView.findViewById(R.id.external_number); |
| 124 | mEditText.setAdapter(autoCompleteAdapter); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 125 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 126 | final AlertDialog a = new AlertDialog.Builder(getActivity()).setView(rootView) |
Alexandre Lision | 2b23792 | 2013-09-09 16:23:02 -0400 | [diff] [blame] | 127 | .setTitle("Transfer " + call_selected.getContact().getmDisplayName()) |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 128 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Adrien Béraud | e78d06f | 2013-09-19 13:33:44 +1000 | [diff] [blame] | 129 | @Override |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 130 | public void onClick(DialogInterface dialog, int whichButton) { |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 131 | |
Adrien Béraud | e78d06f | 2013-09-19 13:33:44 +1000 | [diff] [blame] | 132 | } |
| 133 | }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { |
| 134 | @Override |
| 135 | public void onClick(DialogInterface dialog, int whichButton) { |
Adrien Béraud | 9751284 | 2013-09-19 13:37:19 +1000 | [diff] [blame] | 136 | getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, new Intent()); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 137 | dismiss(); |
| 138 | } |
| 139 | }).create(); |
| 140 | |
| 141 | a.setOnShowListener(new OnShowListener() { |
| 142 | |
| 143 | @Override |
| 144 | public void onShow(DialogInterface dialog) { |
| 145 | Button b = a.getButton(AlertDialog.BUTTON_POSITIVE); |
| 146 | b.setOnClickListener(new View.OnClickListener() { |
| 147 | |
| 148 | @Override |
| 149 | public void onClick(View view) { |
| 150 | if(mEditText.getText().length() == 0){ |
| 151 | Toast.makeText(getActivity(), "Enter a number to transfer this call", Toast.LENGTH_SHORT).show(); |
| 152 | } else { |
| 153 | Intent in = new Intent(); |
| 154 | in.putExtra("to_number", mEditText.getText().toString()); |
| 155 | in.putExtra("transfer", call_selected); |
Adrien Béraud | e78d06f | 2013-09-19 13:33:44 +1000 | [diff] [blame] | 156 | getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_TRANSFER_NUMBER, in); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 157 | dismiss(); |
| 158 | } |
| 159 | } |
| 160 | }); |
| 161 | |
| 162 | } |
| 163 | }); |
| 164 | return a; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 165 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 166 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 167 | @Override |
| 168 | public Loader<Bundle> onCreateLoader(int id, Bundle args) { |
| 169 | Uri baseUri; |
| 170 | |
| 171 | if (args != null) { |
| 172 | baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(args.getString("filter"))); |
| 173 | } else { |
| 174 | baseUri = Contacts.CONTENT_URI; |
| 175 | } |
| 176 | ContactsLoader l = new ContactsLoader(getActivity(), baseUri); |
| 177 | l.forceLoad(); |
| 178 | return l; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void onLoadFinished(Loader<Bundle> loader, Bundle data) { |
| 183 | |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 184 | // ArrayList<CallContact> tmp = data.getParcelableArrayList("Contacts"); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 185 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void onLoaderReset(Loader<Bundle> loader) { |
| 190 | // Thi is called when the last Cursor provided to onLoadFinished |
| 191 | // mListAdapter.swapCursor(null); |
| 192 | } |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 193 | |
| 194 | private class AutoCompleteAdapter extends ArrayAdapter<Address> implements Filterable { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 195 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 196 | private LayoutInflater mInflater; |
| 197 | private Geocoder mGeocoder; |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 198 | // private StringBuilder mSb = new StringBuilder(); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 199 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 200 | public AutoCompleteAdapter(final Context context) { |
| 201 | super(context, -1); |
| 202 | mInflater = LayoutInflater.from(context); |
| 203 | mGeocoder = new Geocoder(context); |
| 204 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 205 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 206 | @Override |
| 207 | public View getView(final int position, final View convertView, final ViewGroup parent) { |
| 208 | final TextView tv; |
| 209 | if (convertView != null) { |
| 210 | tv = (TextView) convertView; |
| 211 | } else { |
| 212 | tv = (TextView) mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false); |
| 213 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 214 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 215 | return tv; |
| 216 | } |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 217 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 218 | @Override |
| 219 | public Filter getFilter() { |
| 220 | Filter myFilter = new Filter() { |
| 221 | @Override |
| 222 | protected FilterResults performFiltering(final CharSequence constraint) { |
| 223 | List<Address> addressList = null; |
| 224 | if (constraint != null) { |
| 225 | try { |
| 226 | addressList = mGeocoder.getFromLocationName((String) constraint, 5); |
| 227 | } catch (IOException e) { |
| 228 | } |
| 229 | } |
| 230 | if (addressList == null) { |
| 231 | addressList = new ArrayList<Address>(); |
| 232 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 233 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 234 | final FilterResults filterResults = new FilterResults(); |
| 235 | filterResults.values = addressList; |
| 236 | filterResults.count = addressList.size(); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 237 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 238 | return filterResults; |
| 239 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 240 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 241 | @SuppressWarnings("unchecked") |
| 242 | @Override |
| 243 | protected void publishResults(final CharSequence contraint, final FilterResults results) { |
| 244 | clear(); |
| 245 | for (Address address : (List<Address>) results.values) { |
| 246 | add(address); |
| 247 | } |
| 248 | if (results.count > 0) { |
| 249 | notifyDataSetChanged(); |
| 250 | } else { |
| 251 | notifyDataSetInvalidated(); |
| 252 | } |
| 253 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 254 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 255 | @Override |
| 256 | public CharSequence convertResultToString(final Object resultValue) { |
| 257 | return resultValue == null ? "" : ((Address) resultValue).getAddressLine(0); |
| 258 | } |
| 259 | }; |
| 260 | return myFilter; |
| 261 | } |
| 262 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 263 | |
| 264 | private class SimpleCallListAdapter extends BaseAdapter { |
| 265 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 266 | private LayoutInflater mInflater; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 267 | ArrayList<Conference> calls; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 268 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 269 | public SimpleCallListAdapter(final Context context, ArrayList<Conference> calls2) { |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 270 | super(); |
| 271 | mInflater = LayoutInflater.from(context); |
| 272 | calls = calls2; |
| 273 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 274 | |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 275 | @Override |
| 276 | public View getView(final int position, final View convertView, final ViewGroup parent) { |
| 277 | final TextView tv; |
| 278 | if (convertView != null) { |
| 279 | tv = (TextView) convertView; |
| 280 | } else { |
| 281 | tv = (TextView) mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false); |
| 282 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 283 | |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 284 | tv.setText(calls.get(position).getParticipants().get(0).getContact().getmDisplayName()); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 285 | return tv; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public int getCount() { |
| 290 | return calls.size(); |
| 291 | } |
| 292 | |
| 293 | @Override |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 294 | public Conference getItem(int pos) { |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 295 | return calls.get(pos); |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public long getItemId(int position) { |
| 300 | return 0; |
| 301 | } |
| 302 | } |
| 303 | } |