blob: b0fa2e11a70a26c904ae2b342747c5188dc714cf [file] [log] [blame]
alision2ec64f92013-06-17 17:28:58 -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 */
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 Lision064e1e02013-10-01 16:18:42 -040035import org.sflphone.R;
36import org.sflphone.adapters.AccountSelectionAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040037import org.sflphone.adapters.ContactPictureTask;
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.interfaces.AccountsInterface;
39import org.sflphone.loaders.AccountsLoader;
40import org.sflphone.loaders.LoaderConstants;
41import org.sflphone.model.Account;
Alexandre Lision35c3cbb2013-11-04 17:11:54 -050042import org.sflphone.model.CallContact;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.receivers.AccountsReceiver;
44import org.sflphone.service.ConfigurationManagerCallback;
45import org.sflphone.service.ISipService;
46
alision9f7a6ec2013-05-24 16:26:26 -040047import android.app.Activity;
alision2cb99562013-05-30 17:02:20 -040048import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040049import android.content.IntentFilter;
alision9f7a6ec2013-05-24 16:26:26 -040050import android.os.Bundle;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040051import android.os.RemoteException;
alisionbd45a822013-06-06 17:35:35 -040052import android.provider.ContactsContract.Profile;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050053import android.support.v4.app.Fragment;
54import android.support.v4.app.LoaderManager.LoaderCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -040055import android.view.LayoutInflater;
56import android.view.View;
57import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040058import android.widget.AdapterView;
59import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040060import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040061import android.widget.ArrayAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040062import android.widget.ImageView;
alision9f7a6ec2013-05-24 16:26:26 -040063import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040064import android.widget.Spinner;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040065import android.widget.TextView;
alision9f7a6ec2013-05-24 16:26:26 -040066
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040067public class MenuFragment extends Fragment implements LoaderCallbacks<Bundle>, AccountsInterface {
alision84813a12013-05-27 17:40:39 -040068
Alexandre Lisionf30ff852013-12-09 17:08:40 -050069 @SuppressWarnings("unused")
alision9f7a6ec2013-05-24 16:26:26 -040070 private static final String TAG = MenuFragment.class.getSimpleName();
alision9f7a6ec2013-05-24 16:26:26 -040071
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040072 String[] mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_URI };
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
alision907bde72013-06-20 14:40:37 -040078 private static Callbacks sDummyCallbacks = new Callbacks() {
79
80 @Override
81 public ISipService getService() {
alision907bde72013-06-20 14:40:37 -040082 return null;
83 }
Alexandre Lision269e6042013-12-11 16:15:13 -050084
85 @Override
86 public void onSectionSelected(int pos) {
Alexandre Lision658999c2013-12-13 14:42:45 -050087
Alexandre Lision269e6042013-12-11 16:15:13 -050088 }
alision907bde72013-06-20 14:40:37 -040089 };
90
alision907bde72013-06-20 14:40:37 -040091 public interface Callbacks {
92
93 public ISipService getService();
Alexandre Lision658999c2013-12-13 14:42:45 -050094
Alexandre Lision269e6042013-12-11 16:15:13 -050095 public void onSectionSelected(int pos);
alision907bde72013-06-20 14:40:37 -040096
97 }
alision9f7a6ec2013-05-24 16:26:26 -040098
alision9f7a6ec2013-05-24 16:26:26 -040099 @Override
100 public void onAttach(Activity activity) {
101 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -0400102 if (!(activity instanceof Callbacks)) {
103 throw new IllegalStateException("Activity must implement fragment's callbacks.");
104 }
105
106 mCallbacks = (Callbacks) activity;
Alexandre Lision3e2a1d02013-11-19 17:23:00 -0500107 try {
108 mCallbacks.getService().registerAllAccounts();
109 } catch (RemoteException e) {
110 e.printStackTrace();
Alexandre Lisiondd6623b2013-12-02 16:23:40 -0500111 } catch (Exception e1) {
112
Alexandre Lision3e2a1d02013-11-19 17:23:00 -0500113 }
alision84813a12013-05-27 17:40:39 -0400114
alision9f7a6ec2013-05-24 16:26:26 -0400115 }
116
117 @Override
118 public void onDetach() {
119 super.onDetach();
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400120 mCallbacks = sDummyCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -0400121 }
122
123 @Override
124 public void onCreate(Bundle savedInstanceState) {
125 super.onCreate(savedInstanceState);
alision907bde72013-06-20 14:40:37 -0400126 accountReceiver = new AccountsReceiver(this);
alision9f7a6ec2013-05-24 16:26:26 -0400127 }
128
alision907bde72013-06-20 14:40:37 -0400129 public void onResume() {
130 super.onResume();
131
132 IntentFilter intentFilter2 = new IntentFilter();
133 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
134 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
135 getActivity().registerReceiver(accountReceiver, intentFilter2);
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500136 getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400137
138 }
139
140 @Override
141 public void onPause() {
142 super.onPause();
143 getActivity().unregisterReceiver(accountReceiver);
144 }
145
alision9f7a6ec2013-05-24 16:26:26 -0400146 @Override
147 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
148 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
149
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400150 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
151 R.array.menu_items_param));
152 ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(paramAdapter);
alision2cb99562013-05-30 17:02:20 -0400153 ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() {
154
155 @Override
Alexandre Lision658999c2013-12-13 14:42:45 -0500156 public void onItemClick(AdapterView<?> arg0, View selected, int pos, long arg3) {
Alexandre Lision269e6042013-12-11 16:15:13 -0500157 mCallbacks.onSectionSelected(pos);
alision2cb99562013-05-30 17:02:20 -0400158 }
159 });
alisionbd45a822013-06-06 17:35:35 -0400160
alision907bde72013-06-20 14:40:37 -0400161 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400162 mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>());
alision907bde72013-06-20 14:40:37 -0400163 spinnerAccounts.setAdapter(mAccountAdapter);
164 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400165
alision907bde72013-06-20 14:40:37 -0400166 @Override
167 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
alision907bde72013-06-20 14:40:37 -0400168 mAccountAdapter.setSelectedAccount(pos);
Alexandre Lision7d058332013-12-10 17:43:52 -0500169 view.findViewById(R.id.account_selected).setVisibility(View.GONE);
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400170 try {
171 mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder());
172 } catch (RemoteException e) {
173 e.printStackTrace();
174 }
alision55c36cb2013-06-14 14:57:38 -0400175 }
alision907bde72013-06-20 14:40:37 -0400176
177 @Override
178 public void onNothingSelected(AdapterView<?> arg0) {
Alexandre Lision80a99162013-09-13 14:19:23 -0400179 mAccountAdapter.setSelectedAccount(-1);
alision907bde72013-06-20 14:40:37 -0400180 }
181 });
182
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500183 CallContact user = CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver());
184 new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), user).run();
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400185
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500186 ((TextView) inflatedView.findViewById(R.id.user_name)).setText(user.getmDisplayName());
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400187
alision9f7a6ec2013-05-24 16:26:26 -0400188 return inflatedView;
189 }
190
191 @Override
192 public void onStart() {
193 super.onStart();
alision9f7a6ec2013-05-24 16:26:26 -0400194 }
195
alision907bde72013-06-20 14:40:37 -0400196 public Account getSelectedAccount() {
197 return mAccountAdapter.getSelectedAccount();
198 }
199
alision907bde72013-06-20 14:40:37 -0400200 public void updateAllAccounts() {
201 if (getActivity() != null)
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500202 getActivity().getSupportLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400203 }
204
205 public void updateAccount(Intent accountState) {
206 if (mAccountAdapter != null)
207 mAccountAdapter.updateAccount(accountState);
208 }
alision907bde72013-06-20 14:40:37 -0400209
Alexandre Lision80a99162013-09-13 14:19:23 -0400210 @Override
211 public void accountsChanged() {
212 updateAllAccounts();
213
214 }
215
216 @Override
217 public void accountStateChanged(Intent accountState) {
218 updateAccount(accountState);
219
220 }
alision907bde72013-06-20 14:40:37 -0400221
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500222 @Override
223 public android.support.v4.content.Loader<Bundle> onCreateLoader(int arg0, Bundle arg1) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500224 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
225 l.forceLoad();
226 return l;
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500227 }
228
229 @Override
230 public void onLoadFinished(android.support.v4.content.Loader<Bundle> arg0, Bundle bun) {
Alexandre Lision658999c2013-12-13 14:42:45 -0500231 mAccountAdapter.removeAll();
232 ArrayList<Account> accounts = bun.getParcelableArrayList(AccountsLoader.ACCOUNTS);
233 mAccountAdapter.addAll(accounts);
234
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500235 }
236
237 @Override
238 public void onLoaderReset(android.support.v4.content.Loader<Bundle> arg0) {
239 // TODO Stub de la méthode généré automatiquement
Alexandre Lision658999c2013-12-13 14:42:45 -0500240
Alexandre Lisiona8b78722013-12-13 10:18:33 -0500241 }
242
alision9f7a6ec2013-05-24 16:26:26 -0400243}