blob: 6a83aa9981a037ae33608562a08b016af0b79136 [file] [log] [blame]
Alexandre Lision2affbaa2013-09-27 16:30:35 -04001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
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
alisionb1763882013-06-18 17:30:51 -040032package com.savoirfairelinux.sflphone.fragments;
33
34import java.io.IOException;
35import java.util.ArrayList;
36import java.util.List;
37
Adrien Béraud97512842013-09-19 13:37:19 +100038import android.app.Activity;
alisionb1763882013-06-18 17:30:51 -040039import android.app.AlertDialog;
40import android.app.Dialog;
41import android.app.DialogFragment;
42import android.app.LoaderManager;
43import android.content.Context;
44import android.content.DialogInterface;
alision1005ba12013-06-19 13:52:44 -040045import android.content.DialogInterface.OnShowListener;
alisionb1763882013-06-18 17:30:51 -040046import android.content.Intent;
47import android.content.Loader;
48import android.location.Address;
49import android.location.Geocoder;
50import android.net.Uri;
51import android.os.Bundle;
52import android.provider.ContactsContract.Contacts;
53import android.view.LayoutInflater;
54import android.view.View;
55import android.view.ViewGroup;
56import android.widget.AdapterView;
57import android.widget.AdapterView.OnItemClickListener;
58import android.widget.ArrayAdapter;
59import android.widget.AutoCompleteTextView;
60import android.widget.BaseAdapter;
alision1005ba12013-06-19 13:52:44 -040061import android.widget.Button;
alisionb1763882013-06-18 17:30:51 -040062import android.widget.Filter;
63import android.widget.Filterable;
64import android.widget.ListView;
65import android.widget.TextView;
alision1005ba12013-06-19 13:52:44 -040066import android.widget.Toast;
alisionb1763882013-06-18 17:30:51 -040067
68import com.savoirfairelinux.sflphone.R;
69import com.savoirfairelinux.sflphone.loaders.ContactsLoader;
alisiondf1dac92013-06-27 17:35:53 -040070import com.savoirfairelinux.sflphone.model.Conference;
alisionb1763882013-06-18 17:30:51 -040071import com.savoirfairelinux.sflphone.model.SipCall;
72
alision1005ba12013-06-19 13:52:44 -040073public class TransferDFragment extends DialogFragment implements LoaderManager.LoaderCallbacks<Bundle> {
Adrien Béraud97512842013-09-19 13:37:19 +100074 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;
alisionb1763882013-06-18 17:30:51 -040076
77 private AutoCompleteTextView mEditText;
78 private AutoCompleteAdapter autoCompleteAdapter;
79 SimpleCallListAdapter mAdapter;
alision1005ba12013-06-19 13:52:44 -040080
alisionb1763882013-06-18 17:30:51 -040081 /**
82 * Create a new instance of CallActionsDFragment
83 */
Alexandre Lision2affbaa2013-09-27 16:30:35 -040084 static TransferDFragment newInstance() {
alision1005ba12013-06-19 13:52:44 -040085 TransferDFragment f = new TransferDFragment();
alisionb1763882013-06-18 17:30:51 -040086 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 }
alisionb1763882013-06-18 17:30:51 -040097
98 @Override
alision1005ba12013-06-19 13:52:44 -040099 public Dialog onCreateDialog(Bundle savedInstanceState) {
100 View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_transfer, null);
101
alisiondf1dac92013-06-27 17:35:53 -0400102 ArrayList<Conference> calls = getArguments().getParcelableArrayList("calls");
Alexandre Lision2b237922013-09-09 16:23:02 -0400103 final SipCall call_selected = getArguments().getParcelable("call_selected");
alision1005ba12013-06-19 13:52:44 -0400104
alisionb1763882013-06-18 17:30:51 -0400105 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) {
alision1005ba12013-06-19 13:52:44 -0400112
alisionb1763882013-06-18 17:30:51 -0400113 Intent in = new Intent();
alision1005ba12013-06-19 13:52:44 -0400114 in.putExtra("target", mAdapter.getItem(pos));
115 in.putExtra("transfer", call_selected);
Adrien Béraud97512842013-09-19 13:37:19 +1000116
Adrien Béraude78d06f2013-09-19 13:33:44 +1000117 getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_TRANSFER_CONF, in);
alisionb1763882013-06-18 17:30:51 -0400118 dismiss();
119 }
120 });
121 list.setEmptyView(rootView.findViewById(R.id.empty_view));
alision1005ba12013-06-19 13:52:44 -0400122
alisionb1763882013-06-18 17:30:51 -0400123 mEditText = (AutoCompleteTextView) rootView.findViewById(R.id.external_number);
124 mEditText.setAdapter(autoCompleteAdapter);
alisionb1763882013-06-18 17:30:51 -0400125
alision1005ba12013-06-19 13:52:44 -0400126 final AlertDialog a = new AlertDialog.Builder(getActivity()).setView(rootView)
Alexandre Lision2b237922013-09-09 16:23:02 -0400127 .setTitle("Transfer " + call_selected.getContact().getmDisplayName())
alision1005ba12013-06-19 13:52:44 -0400128 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Adrien Béraude78d06f2013-09-19 13:33:44 +1000129 @Override
alision1005ba12013-06-19 13:52:44 -0400130 public void onClick(DialogInterface dialog, int whichButton) {
alisionb1763882013-06-18 17:30:51 -0400131
Adrien Béraude78d06f2013-09-19 13:33:44 +1000132 }
133 }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
134 @Override
135 public void onClick(DialogInterface dialog, int whichButton) {
Adrien Béraud97512842013-09-19 13:37:19 +1000136 getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, new Intent());
alision1005ba12013-06-19 13:52:44 -0400137 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éraude78d06f2013-09-19 13:33:44 +1000156 getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_TRANSFER_NUMBER, in);
alision1005ba12013-06-19 13:52:44 -0400157 dismiss();
158 }
159 }
160 });
161
162 }
163 });
164 return a;
alisionb1763882013-06-18 17:30:51 -0400165 }
alision1005ba12013-06-19 13:52:44 -0400166
alisionb1763882013-06-18 17:30:51 -0400167 @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 Lision6e8931e2013-09-19 16:49:34 -0400184// ArrayList<CallContact> tmp = data.getParcelableArrayList("Contacts");
alisionb1763882013-06-18 17:30:51 -0400185
alisionb1763882013-06-18 17:30:51 -0400186 }
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 }
alisionb1763882013-06-18 17:30:51 -0400193
194 private class AutoCompleteAdapter extends ArrayAdapter<Address> implements Filterable {
alision1005ba12013-06-19 13:52:44 -0400195
alisionb1763882013-06-18 17:30:51 -0400196 private LayoutInflater mInflater;
197 private Geocoder mGeocoder;
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400198// private StringBuilder mSb = new StringBuilder();
alision1005ba12013-06-19 13:52:44 -0400199
alisionb1763882013-06-18 17:30:51 -0400200 public AutoCompleteAdapter(final Context context) {
201 super(context, -1);
202 mInflater = LayoutInflater.from(context);
203 mGeocoder = new Geocoder(context);
204 }
alision1005ba12013-06-19 13:52:44 -0400205
alisionb1763882013-06-18 17:30:51 -0400206 @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 }
alision1005ba12013-06-19 13:52:44 -0400214
alisionb1763882013-06-18 17:30:51 -0400215 return tv;
216 }
alisionb1763882013-06-18 17:30:51 -0400217
alisionb1763882013-06-18 17:30:51 -0400218 @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 }
alision1005ba12013-06-19 13:52:44 -0400233
alisionb1763882013-06-18 17:30:51 -0400234 final FilterResults filterResults = new FilterResults();
235 filterResults.values = addressList;
236 filterResults.count = addressList.size();
alision1005ba12013-06-19 13:52:44 -0400237
alisionb1763882013-06-18 17:30:51 -0400238 return filterResults;
239 }
alision1005ba12013-06-19 13:52:44 -0400240
alisionb1763882013-06-18 17:30:51 -0400241 @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 }
alision1005ba12013-06-19 13:52:44 -0400254
alisionb1763882013-06-18 17:30:51 -0400255 @Override
256 public CharSequence convertResultToString(final Object resultValue) {
257 return resultValue == null ? "" : ((Address) resultValue).getAddressLine(0);
258 }
259 };
260 return myFilter;
261 }
262 }
alision1005ba12013-06-19 13:52:44 -0400263
264 private class SimpleCallListAdapter extends BaseAdapter {
265
alisionb1763882013-06-18 17:30:51 -0400266 private LayoutInflater mInflater;
alisiondf1dac92013-06-27 17:35:53 -0400267 ArrayList<Conference> calls;
alision1005ba12013-06-19 13:52:44 -0400268
alisiondf1dac92013-06-27 17:35:53 -0400269 public SimpleCallListAdapter(final Context context, ArrayList<Conference> calls2) {
alisionb1763882013-06-18 17:30:51 -0400270 super();
271 mInflater = LayoutInflater.from(context);
272 calls = calls2;
273 }
alision1005ba12013-06-19 13:52:44 -0400274
alisionb1763882013-06-18 17:30:51 -0400275 @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 }
alision1005ba12013-06-19 13:52:44 -0400283
alisiondf1dac92013-06-27 17:35:53 -0400284 tv.setText(calls.get(position).getParticipants().get(0).getContact().getmDisplayName());
alisionb1763882013-06-18 17:30:51 -0400285 return tv;
286 }
287
288 @Override
289 public int getCount() {
290 return calls.size();
291 }
292
293 @Override
alisiondf1dac92013-06-27 17:35:53 -0400294 public Conference getItem(int pos) {
alisionb1763882013-06-18 17:30:51 -0400295 return calls.get(pos);
296 }
297
298 @Override
299 public long getItemId(int position) {
300 return 0;
301 }
302 }
303}