blob: 9ffde1c1ebf68d3cb5dda746664c136e33c6a665 [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
33import android.app.Activity;
alision2cb99562013-05-30 17:02:20 -040034import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040035import android.content.IntentFilter;
alision9f7a6ec2013-05-24 16:26:26 -040036import android.os.Bundle;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040037import android.os.RemoteException;
Alexandre Lision5f144b82014-02-11 09:59:36 -050038import android.support.v4.app.LoaderManager;
39import android.support.v4.content.AsyncTaskLoader;
40import android.support.v4.content.Loader;
41import android.util.Log;
alision9f7a6ec2013-05-24 16:26:26 -040042import android.view.LayoutInflater;
43import android.view.View;
44import android.view.ViewGroup;
Alexandre Lision5f144b82014-02-11 09:59:36 -050045import android.widget.*;
alision2cb99562013-05-30 17:02:20 -040046import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040047import android.widget.AdapterView.OnItemSelectedListener;
Alexandre Lision5f144b82014-02-11 09:59:36 -050048import org.sflphone.R;
49import org.sflphone.adapters.AccountSelectionAdapter;
50import org.sflphone.adapters.ContactPictureTask;
51import org.sflphone.loaders.AccountsLoader;
52import org.sflphone.loaders.LoaderConstants;
53import org.sflphone.model.Account;
54import org.sflphone.model.CallContact;
55import org.sflphone.service.ConfigurationManagerCallback;
56import org.sflphone.service.ISipService;
alision9f7a6ec2013-05-24 16:26:26 -040057
Alexandre Lision5f144b82014-02-11 09:59:36 -050058import java.util.ArrayList;
59
60public class MenuFragment extends AccountWrapperFragment implements LoaderManager.LoaderCallbacks<Bundle> {
alision84813a12013-05-27 17:40:39 -040061
Alexandre Lisionf30ff852013-12-09 17:08:40 -050062 @SuppressWarnings("unused")
alision9f7a6ec2013-05-24 16:26:26 -040063 private static final String TAG = MenuFragment.class.getSimpleName();
alision9f7a6ec2013-05-24 16:26:26 -040064
alision907bde72013-06-20 14:40:37 -040065 AccountSelectionAdapter mAccountAdapter;
66 private Spinner spinnerAccounts;
alision907bde72013-06-20 14:40:37 -040067 private Callbacks mCallbacks = sDummyCallbacks;
alision907bde72013-06-20 14:40:37 -040068
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -050069 private ListView sections;
70
alision907bde72013-06-20 14:40:37 -040071 private static Callbacks sDummyCallbacks = new Callbacks() {
72
73 @Override
74 public ISipService getService() {
alision907bde72013-06-20 14:40:37 -040075 return null;
76 }
Alexandre Lision269e6042013-12-11 16:15:13 -050077
78 @Override
79 public void onSectionSelected(int pos) {
Alexandre Lision658999c2013-12-13 14:42:45 -050080
Alexandre Lision269e6042013-12-11 16:15:13 -050081 }
alision907bde72013-06-20 14:40:37 -040082 };
83
Alexandre Lisione796a872014-01-17 17:08:13 -050084 public Account retrieveAccountById(String accountID) {
85 Account toReturn;
86 toReturn = mAccountAdapter.getAccount(accountID);
87
88 if(toReturn == null || !toReturn.isRegistered())
89 return getSelectedAccount();
90
91 return toReturn;
92 }
93
alision907bde72013-06-20 14:40:37 -040094 public interface Callbacks {
95
96 public ISipService getService();
Alexandre Lision658999c2013-12-13 14:42:45 -050097
Alexandre Lision269e6042013-12-11 16:15:13 -050098 public void onSectionSelected(int pos);
alision907bde72013-06-20 14:40:37 -040099
100 }
alision9f7a6ec2013-05-24 16:26:26 -0400101
alision9f7a6ec2013-05-24 16:26:26 -0400102 @Override
103 public void onAttach(Activity activity) {
104 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -0400105 if (!(activity instanceof Callbacks)) {
106 throw new IllegalStateException("Activity must implement fragment's callbacks.");
107 }
alision907bde72013-06-20 14:40:37 -0400108 mCallbacks = (Callbacks) activity;
alision9f7a6ec2013-05-24 16:26:26 -0400109 }
110
111 @Override
112 public void onDetach() {
113 super.onDetach();
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400114 mCallbacks = sDummyCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -0400115 }
116
117 @Override
118 public void onCreate(Bundle savedInstanceState) {
119 super.onCreate(savedInstanceState);
alision9f7a6ec2013-05-24 16:26:26 -0400120 }
121
alision907bde72013-06-20 14:40:37 -0400122 public void onResume() {
123 super.onResume();
124
Alexandre Lisiona815eab2014-01-31 13:01:31 -0500125 Log.i(TAG, "Resuming");
Alexandre Lisionb2669692014-01-28 14:06:08 -0500126 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400127
128 }
129
130 @Override
Alexandre Lisiona815eab2014-01-31 13:01:31 -0500131 public void onActivityResult(int requestCode, int resultCode, Intent data) {
132 super.onActivityResult(requestCode, resultCode, data);
133 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
134 }
135
136
137
138 @Override
alision907bde72013-06-20 14:40:37 -0400139 public void onPause() {
140 super.onPause();
alision907bde72013-06-20 14:40:37 -0400141 }
142
alision9f7a6ec2013-05-24 16:26:26 -0400143 @Override
144 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
145 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
146
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400147 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
148 R.array.menu_items_param));
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500149 sections = (ListView) inflatedView.findViewById(R.id.listView);
150 sections.setAdapter(paramAdapter);
151 backToHome();
152 sections.setOnItemClickListener(new OnItemClickListener() {
alision2cb99562013-05-30 17:02:20 -0400153
154 @Override
Alexandre Lision658999c2013-12-13 14:42:45 -0500155 public void onItemClick(AdapterView<?> arg0, View selected, int pos, long arg3) {
Alexandre Lision269e6042013-12-11 16:15:13 -0500156 mCallbacks.onSectionSelected(pos);
alision2cb99562013-05-30 17:02:20 -0400157 }
158 });
alisionbd45a822013-06-06 17:35:35 -0400159
alision907bde72013-06-20 14:40:37 -0400160 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400161 mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>());
alision907bde72013-06-20 14:40:37 -0400162 spinnerAccounts.setAdapter(mAccountAdapter);
163 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400164
alision907bde72013-06-20 14:40:37 -0400165 @Override
166 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
alision907bde72013-06-20 14:40:37 -0400167 mAccountAdapter.setSelectedAccount(pos);
Alexandre Lision7d058332013-12-10 17:43:52 -0500168 view.findViewById(R.id.account_selected).setVisibility(View.GONE);
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400169 try {
170 mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder());
171 } catch (RemoteException e) {
172 e.printStackTrace();
173 }
alision55c36cb2013-06-14 14:57:38 -0400174 }
alision907bde72013-06-20 14:40:37 -0400175
176 @Override
177 public void onNothingSelected(AdapterView<?> arg0) {
Alexandre Lision80a99162013-09-13 14:19:23 -0400178 mAccountAdapter.setSelectedAccount(-1);
alision907bde72013-06-20 14:40:37 -0400179 }
180 });
181
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500182 CallContact user = CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver());
183 new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), user).run();
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400184
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500185 ((TextView) inflatedView.findViewById(R.id.user_name)).setText(user.getmDisplayName());
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400186
alision9f7a6ec2013-05-24 16:26:26 -0400187 return inflatedView;
188 }
189
190 @Override
191 public void onStart() {
192 super.onStart();
alision9f7a6ec2013-05-24 16:26:26 -0400193 }
194
alision907bde72013-06-20 14:40:37 -0400195 public Account getSelectedAccount() {
196 return mAccountAdapter.getSelectedAccount();
197 }
198
alision907bde72013-06-20 14:40:37 -0400199 public void updateAllAccounts() {
200 if (getActivity() != null)
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500201 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400202 }
203
Alexandre Lision80a99162013-09-13 14:19:23 -0400204 @Override
205 public void accountsChanged() {
206 updateAllAccounts();
207
208 }
209
210 @Override
Alexandre Lision48b49eb2014-02-11 13:37:33 -0500211 public void accountStateChanged(String accoundID, String state, int code) {
212 if (mAccountAdapter != null)
213 mAccountAdapter.updateAccount(accoundID, state, code);
Alexandre Lision80a99162013-09-13 14:19:23 -0400214 }
alision907bde72013-06-20 14:40:37 -0400215
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500216 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500217 public AsyncTaskLoader<Bundle> onCreateLoader(int arg0, Bundle arg1) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500218 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
219 l.forceLoad();
220 return l;
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500221 }
222
223 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500224 public void onLoadFinished(Loader<Bundle> loader, Bundle data) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500225 mAccountAdapter.removeAll();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500226 ArrayList<Account> accounts = data.getParcelableArrayList(AccountsLoader.ACCOUNTS);
Alexandre Lision658999c2013-12-13 14:42:45 -0500227 mAccountAdapter.addAll(accounts);
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500228 }
229
230 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500231 public void onLoaderReset(Loader<Bundle> loader) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500232
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500233 }
234
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500235 public void backToHome() {
236 sections.setItemChecked(0, true);
237 }
238
alision9f7a6ec2013-05-24 16:26:26 -0400239}