blob: daa70b054297b6c011c119ebe58ae8d80837b2b0 [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 Lisionb4e60612014-01-14 17:47:23 -050035import android.app.LoaderManager;
36import android.content.AsyncTaskLoader;
37import android.content.Loader;
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.R;
39import org.sflphone.adapters.AccountSelectionAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040040import org.sflphone.adapters.ContactPictureTask;
Alexandre Lision064e1e02013-10-01 16:18:42 -040041import org.sflphone.interfaces.AccountsInterface;
42import org.sflphone.loaders.AccountsLoader;
43import org.sflphone.loaders.LoaderConstants;
44import org.sflphone.model.Account;
Alexandre Lision35c3cbb2013-11-04 17:11:54 -050045import org.sflphone.model.CallContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040046import org.sflphone.receivers.AccountsReceiver;
47import org.sflphone.service.ConfigurationManagerCallback;
48import org.sflphone.service.ISipService;
49
alision9f7a6ec2013-05-24 16:26:26 -040050import android.app.Activity;
alision2cb99562013-05-30 17:02:20 -040051import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040052import android.content.IntentFilter;
alision9f7a6ec2013-05-24 16:26:26 -040053import android.os.Bundle;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040054import android.os.RemoteException;
alisionbd45a822013-06-06 17:35:35 -040055import android.provider.ContactsContract.Profile;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050056import android.app.Fragment;
alision9f7a6ec2013-05-24 16:26:26 -040057import android.view.LayoutInflater;
58import android.view.View;
59import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040060import android.widget.AdapterView;
61import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040062import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040063import android.widget.ArrayAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040064import android.widget.ImageView;
alision9f7a6ec2013-05-24 16:26:26 -040065import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040066import android.widget.Spinner;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040067import android.widget.TextView;
alision9f7a6ec2013-05-24 16:26:26 -040068
Alexandre Lisionb4e60612014-01-14 17:47:23 -050069public class MenuFragment extends Fragment implements LoaderManager.LoaderCallbacks<Bundle>, AccountsInterface {
alision84813a12013-05-27 17:40:39 -040070
Alexandre Lisionf30ff852013-12-09 17:08:40 -050071 @SuppressWarnings("unused")
alision9f7a6ec2013-05-24 16:26:26 -040072 private static final String TAG = MenuFragment.class.getSimpleName();
alision9f7a6ec2013-05-24 16:26:26 -040073
alision907bde72013-06-20 14:40:37 -040074 AccountSelectionAdapter mAccountAdapter;
75 private Spinner spinnerAccounts;
76 AccountsReceiver accountReceiver;
alision907bde72013-06-20 14:40:37 -040077 private Callbacks mCallbacks = sDummyCallbacks;
alision907bde72013-06-20 14:40:37 -040078
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -050079 private ListView sections;
80
alision907bde72013-06-20 14:40:37 -040081 private static Callbacks sDummyCallbacks = new Callbacks() {
82
83 @Override
84 public ISipService getService() {
alision907bde72013-06-20 14:40:37 -040085 return null;
86 }
Alexandre Lision269e6042013-12-11 16:15:13 -050087
88 @Override
89 public void onSectionSelected(int pos) {
Alexandre Lision658999c2013-12-13 14:42:45 -050090
Alexandre Lision269e6042013-12-11 16:15:13 -050091 }
alision907bde72013-06-20 14:40:37 -040092 };
93
Alexandre Lisione796a872014-01-17 17:08:13 -050094 public Account retrieveAccountById(String accountID) {
95 Account toReturn;
96 toReturn = mAccountAdapter.getAccount(accountID);
97
98 if(toReturn == null || !toReturn.isRegistered())
99 return getSelectedAccount();
100
101 return toReturn;
102 }
103
alision907bde72013-06-20 14:40:37 -0400104 public interface Callbacks {
105
106 public ISipService getService();
Alexandre Lision658999c2013-12-13 14:42:45 -0500107
Alexandre Lision269e6042013-12-11 16:15:13 -0500108 public void onSectionSelected(int pos);
alision907bde72013-06-20 14:40:37 -0400109
110 }
alision9f7a6ec2013-05-24 16:26:26 -0400111
alision9f7a6ec2013-05-24 16:26:26 -0400112 @Override
113 public void onAttach(Activity activity) {
114 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -0400115 if (!(activity instanceof Callbacks)) {
116 throw new IllegalStateException("Activity must implement fragment's callbacks.");
117 }
118
119 mCallbacks = (Callbacks) activity;
Alexandre Lision3e2a1d02013-11-19 17:23:00 -0500120 try {
121 mCallbacks.getService().registerAllAccounts();
122 } catch (RemoteException e) {
123 e.printStackTrace();
Alexandre Lisiondd6623b2013-12-02 16:23:40 -0500124 } catch (Exception e1) {
125
Alexandre Lision3e2a1d02013-11-19 17:23:00 -0500126 }
alision84813a12013-05-27 17:40:39 -0400127
alision9f7a6ec2013-05-24 16:26:26 -0400128 }
129
130 @Override
131 public void onDetach() {
132 super.onDetach();
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400133 mCallbacks = sDummyCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -0400134 }
135
136 @Override
137 public void onCreate(Bundle savedInstanceState) {
138 super.onCreate(savedInstanceState);
alision907bde72013-06-20 14:40:37 -0400139 accountReceiver = new AccountsReceiver(this);
alision9f7a6ec2013-05-24 16:26:26 -0400140 }
141
alision907bde72013-06-20 14:40:37 -0400142 public void onResume() {
143 super.onResume();
144
145 IntentFilter intentFilter2 = new IntentFilter();
146 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
147 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
148 getActivity().registerReceiver(accountReceiver, intentFilter2);
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500149 getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400150
151 }
152
153 @Override
154 public void onPause() {
155 super.onPause();
156 getActivity().unregisterReceiver(accountReceiver);
157 }
158
alision9f7a6ec2013-05-24 16:26:26 -0400159 @Override
160 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
161 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
162
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400163 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
164 R.array.menu_items_param));
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500165 sections = (ListView) inflatedView.findViewById(R.id.listView);
166 sections.setAdapter(paramAdapter);
167 backToHome();
168 sections.setOnItemClickListener(new OnItemClickListener() {
alision2cb99562013-05-30 17:02:20 -0400169
170 @Override
Alexandre Lision658999c2013-12-13 14:42:45 -0500171 public void onItemClick(AdapterView<?> arg0, View selected, int pos, long arg3) {
Alexandre Lision269e6042013-12-11 16:15:13 -0500172 mCallbacks.onSectionSelected(pos);
alision2cb99562013-05-30 17:02:20 -0400173 }
174 });
alisionbd45a822013-06-06 17:35:35 -0400175
alision907bde72013-06-20 14:40:37 -0400176 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400177 mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>());
alision907bde72013-06-20 14:40:37 -0400178 spinnerAccounts.setAdapter(mAccountAdapter);
179 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400180
alision907bde72013-06-20 14:40:37 -0400181 @Override
182 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
alision907bde72013-06-20 14:40:37 -0400183 mAccountAdapter.setSelectedAccount(pos);
Alexandre Lision7d058332013-12-10 17:43:52 -0500184 view.findViewById(R.id.account_selected).setVisibility(View.GONE);
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400185 try {
186 mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder());
187 } catch (RemoteException e) {
188 e.printStackTrace();
189 }
alision55c36cb2013-06-14 14:57:38 -0400190 }
alision907bde72013-06-20 14:40:37 -0400191
192 @Override
193 public void onNothingSelected(AdapterView<?> arg0) {
Alexandre Lision80a99162013-09-13 14:19:23 -0400194 mAccountAdapter.setSelectedAccount(-1);
alision907bde72013-06-20 14:40:37 -0400195 }
196 });
197
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500198 CallContact user = CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver());
199 new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), user).run();
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400200
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500201 ((TextView) inflatedView.findViewById(R.id.user_name)).setText(user.getmDisplayName());
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400202
alision9f7a6ec2013-05-24 16:26:26 -0400203 return inflatedView;
204 }
205
206 @Override
207 public void onStart() {
208 super.onStart();
alision9f7a6ec2013-05-24 16:26:26 -0400209 }
210
alision907bde72013-06-20 14:40:37 -0400211 public Account getSelectedAccount() {
212 return mAccountAdapter.getSelectedAccount();
213 }
214
alision907bde72013-06-20 14:40:37 -0400215 public void updateAllAccounts() {
216 if (getActivity() != null)
Alexandre Lisiond5934b52014-01-07 17:17:37 -0500217 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400218 }
219
220 public void updateAccount(Intent accountState) {
221 if (mAccountAdapter != null)
222 mAccountAdapter.updateAccount(accountState);
223 }
alision907bde72013-06-20 14:40:37 -0400224
Alexandre Lision80a99162013-09-13 14:19:23 -0400225 @Override
226 public void accountsChanged() {
227 updateAllAccounts();
228
229 }
230
231 @Override
232 public void accountStateChanged(Intent accountState) {
233 updateAccount(accountState);
234
235 }
alision907bde72013-06-20 14:40:37 -0400236
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500237 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500238 public AsyncTaskLoader<Bundle> onCreateLoader(int arg0, Bundle arg1) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500239 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
240 l.forceLoad();
241 return l;
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500242 }
243
244 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500245 public void onLoadFinished(Loader<Bundle> loader, Bundle data) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500246 mAccountAdapter.removeAll();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500247 ArrayList<Account> accounts = data.getParcelableArrayList(AccountsLoader.ACCOUNTS);
Alexandre Lision658999c2013-12-13 14:42:45 -0500248 mAccountAdapter.addAll(accounts);
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500249 }
250
251 @Override
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500252 public void onLoaderReset(Loader<Bundle> loader) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500253
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500254 }
255
Alexandre Lisionfc9b4e72013-12-13 14:55:25 -0500256 public void backToHome() {
257 sections.setItemChecked(0, true);
258 }
259
alision9f7a6ec2013-05-24 16:26:26 -0400260}