blob: c85acf21287033d7d31d6ae56a565a4162aab80d [file] [log] [blame]
alision2ec64f92013-06-17 17:28:58 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
alision2ec64f92013-06-17 17:28:58 -04003 *
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 */
Alexandre Lision064e1e02013-10-01 16:18:42 -040031package org.sflphone.fragments;
alision9f7a6ec2013-05-24 16:26:26 -040032
alision907bde72013-06-20 14:40:37 -040033import java.util.ArrayList;
alisionbd45a822013-06-06 17:35:35 -040034
Alexandre Lisionbe54c342014-01-21 17:10:33 -050035import android.support.v4.app.Fragment;
36import android.support.v4.app.LoaderManager;
37import android.support.v4.content.AsyncTaskLoader;
38import android.support.v4.content.Loader;
Alexandre Lision064e1e02013-10-01 16:18:42 -040039import org.sflphone.R;
40import org.sflphone.adapters.AccountSelectionAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040041import org.sflphone.adapters.ContactPictureTask;
Alexandre Lision064e1e02013-10-01 16:18:42 -040042import org.sflphone.interfaces.AccountsInterface;
43import org.sflphone.loaders.AccountsLoader;
44import org.sflphone.loaders.LoaderConstants;
45import org.sflphone.model.Account;
Alexandre Lision35c3cbb2013-11-04 17:11:54 -050046import org.sflphone.model.CallContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.receivers.AccountsReceiver;
48import org.sflphone.service.ConfigurationManagerCallback;
49import org.sflphone.service.ISipService;
50
alision9f7a6ec2013-05-24 16:26:26 -040051import android.app.Activity;
alision2cb99562013-05-30 17:02:20 -040052import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040053import android.content.IntentFilter;
alision9f7a6ec2013-05-24 16:26:26 -040054import android.os.Bundle;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040055import android.os.RemoteException;
alision9f7a6ec2013-05-24 16:26:26 -040056import android.view.LayoutInflater;
57import android.view.View;
58import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040059import android.widget.AdapterView;
60import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040061import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040062import android.widget.ArrayAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040063import android.widget.ImageView;
alision9f7a6ec2013-05-24 16:26:26 -040064import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040065import android.widget.Spinner;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040066import android.widget.TextView;
alision9f7a6ec2013-05-24 16:26:26 -040067
Alexandre Lisionb4e60612014-01-14 17:47:23 -050068public class MenuFragment extends Fragment implements LoaderManager.LoaderCallbacks<Bundle>, AccountsInterface {
alision84813a12013-05-27 17:40:39 -040069
Alexandre Lisionf30ff852013-12-09 17:08:40 -050070 @SuppressWarnings("unused")
alision9f7a6ec2013-05-24 16:26:26 -040071 private static final String TAG = MenuFragment.class.getSimpleName();
alision9f7a6ec2013-05-24 16:26:26 -040072
alision907bde72013-06-20 14:40:37 -040073 AccountSelectionAdapter mAccountAdapter;
74 private Spinner spinnerAccounts;
75 AccountsReceiver accountReceiver;
alision907bde72013-06-20 14:40:37 -040076 private Callbacks mCallbacks = sDummyCallbacks;
alision907bde72013-06-20 14:40:37 -040077
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -050078 private ListView sections;
79
alision907bde72013-06-20 14:40:37 -040080 private static Callbacks sDummyCallbacks = new Callbacks() {
81
82 @Override
83 public ISipService getService() {
alision907bde72013-06-20 14:40:37 -040084 return null;
85 }
Alexandre Lision269e6042013-12-11 16:15:13 -050086
87 @Override
88 public void onSectionSelected(int pos) {
Alexandre Lision658999c2013-12-13 14:42:45 -050089
Alexandre Lision269e6042013-12-11 16:15:13 -050090 }
alision907bde72013-06-20 14:40:37 -040091 };
92
Alexandre Lisione796a872014-01-17 17:08:13 -050093 public Account retrieveAccountById(String accountID) {
94 Account toReturn;
95 toReturn = mAccountAdapter.getAccount(accountID);
96
97 if(toReturn == null || !toReturn.isRegistered())
98 return getSelectedAccount();
99
100 return toReturn;
101 }
102
alision907bde72013-06-20 14:40:37 -0400103 public interface Callbacks {
104
105 public ISipService getService();
Alexandre Lision658999c2013-12-13 14:42:45 -0500106
Alexandre Lision269e6042013-12-11 16:15:13 -0500107 public void onSectionSelected(int pos);
alision907bde72013-06-20 14:40:37 -0400108
109 }
alision9f7a6ec2013-05-24 16:26:26 -0400110
alision9f7a6ec2013-05-24 16:26:26 -0400111 @Override
112 public void onAttach(Activity activity) {
113 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -0400114 if (!(activity instanceof Callbacks)) {
115 throw new IllegalStateException("Activity must implement fragment's callbacks.");
116 }
alision907bde72013-06-20 14:40:37 -0400117 mCallbacks = (Callbacks) activity;
alision9f7a6ec2013-05-24 16:26:26 -0400118 }
119
120 @Override
121 public void onDetach() {
122 super.onDetach();
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400123 mCallbacks = sDummyCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -0400124 }
125
126 @Override
127 public void onCreate(Bundle savedInstanceState) {
128 super.onCreate(savedInstanceState);
alision907bde72013-06-20 14:40:37 -0400129 accountReceiver = new AccountsReceiver(this);
alision9f7a6ec2013-05-24 16:26:26 -0400130 }
131
alision907bde72013-06-20 14:40:37 -0400132 public void onResume() {
133 super.onResume();
134
135 IntentFilter intentFilter2 = new IntentFilter();
136 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
137 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
138 getActivity().registerReceiver(accountReceiver, intentFilter2);
Alexandre Lisionb2669692014-01-28 14:06:08 -0500139 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400140
141 }
142
143 @Override
144 public void onPause() {
145 super.onPause();
146 getActivity().unregisterReceiver(accountReceiver);
147 }
148
alision9f7a6ec2013-05-24 16:26:26 -0400149 @Override
150 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
151 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
152
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400153 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
154 R.array.menu_items_param));
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500155 sections = (ListView) inflatedView.findViewById(R.id.listView);
156 sections.setAdapter(paramAdapter);
157 backToHome();
158 sections.setOnItemClickListener(new OnItemClickListener() {
alision2cb99562013-05-30 17:02:20 -0400159
160 @Override
Alexandre Lision658999c2013-12-13 14:42:45 -0500161 public void onItemClick(AdapterView<?> arg0, View selected, int pos, long arg3) {
Alexandre Lision269e6042013-12-11 16:15:13 -0500162 mCallbacks.onSectionSelected(pos);
alision2cb99562013-05-30 17:02:20 -0400163 }
164 });
alisionbd45a822013-06-06 17:35:35 -0400165
alision907bde72013-06-20 14:40:37 -0400166 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400167 mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>());
alision907bde72013-06-20 14:40:37 -0400168 spinnerAccounts.setAdapter(mAccountAdapter);
169 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400170
alision907bde72013-06-20 14:40:37 -0400171 @Override
172 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
alision907bde72013-06-20 14:40:37 -0400173 mAccountAdapter.setSelectedAccount(pos);
Alexandre Lision7d058332013-12-10 17:43:52 -0500174 view.findViewById(R.id.account_selected).setVisibility(View.GONE);
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400175 try {
176 mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder());
177 } catch (RemoteException e) {
178 e.printStackTrace();
179 }
alision55c36cb2013-06-14 14:57:38 -0400180 }
alision907bde72013-06-20 14:40:37 -0400181
182 @Override
183 public void onNothingSelected(AdapterView<?> arg0) {
Alexandre Lision80a99162013-09-13 14:19:23 -0400184 mAccountAdapter.setSelectedAccount(-1);
alision907bde72013-06-20 14:40:37 -0400185 }
186 });
187
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500188 CallContact user = CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver());
189 new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), user).run();
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400190
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500191 ((TextView) inflatedView.findViewById(R.id.user_name)).setText(user.getmDisplayName());
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400192
alision9f7a6ec2013-05-24 16:26:26 -0400193 return inflatedView;
194 }
195
196 @Override
197 public void onStart() {
198 super.onStart();
alision9f7a6ec2013-05-24 16:26:26 -0400199 }
200
alision907bde72013-06-20 14:40:37 -0400201 public Account getSelectedAccount() {
202 return mAccountAdapter.getSelectedAccount();
203 }
204
alision907bde72013-06-20 14:40:37 -0400205 public void updateAllAccounts() {
206 if (getActivity() != null)
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500207 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400208 }
209
210 public void updateAccount(Intent accountState) {
211 if (mAccountAdapter != null)
212 mAccountAdapter.updateAccount(accountState);
213 }
alision907bde72013-06-20 14:40:37 -0400214
Alexandre Lision80a99162013-09-13 14:19:23 -0400215 @Override
216 public void accountsChanged() {
217 updateAllAccounts();
218
219 }
220
221 @Override
222 public void accountStateChanged(Intent accountState) {
223 updateAccount(accountState);
224
225 }
alision907bde72013-06-20 14:40:37 -0400226
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500227 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500228 public AsyncTaskLoader<Bundle> onCreateLoader(int arg0, Bundle arg1) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500229 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
230 l.forceLoad();
231 return l;
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500232 }
233
234 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500235 public void onLoadFinished(Loader<Bundle> loader, Bundle data) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500236 mAccountAdapter.removeAll();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500237 ArrayList<Account> accounts = data.getParcelableArrayList(AccountsLoader.ACCOUNTS);
Alexandre Lision658999c2013-12-13 14:42:45 -0500238 mAccountAdapter.addAll(accounts);
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500239 }
240
241 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500242 public void onLoaderReset(Loader<Bundle> loader) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500243
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500244 }
245
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500246 public void backToHome() {
247 sections.setItemChecked(0, true);
248 }
249
alision9f7a6ec2013-05-24 16:26:26 -0400250}