blob: 38f80e840e95b362efe78f5d1ba91d33cc3f2176 [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.adapters.MenuAdapter;
39import org.sflphone.client.ActivityHolder;
Alexandre Lision4ab53972013-11-04 16:59:18 -050040import org.sflphone.client.HomeActivity;
41import org.sflphone.client.SettingsActivity;
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;
52import android.app.Fragment;
alision907bde72013-06-20 14:40:37 -040053import android.app.LoaderManager.LoaderCallbacks;
alision2cb99562013-05-30 17:02:20 -040054import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040055import android.content.IntentFilter;
56import android.content.Loader;
alision9f7a6ec2013-05-24 16:26:26 -040057import android.os.Bundle;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040058import android.os.RemoteException;
alisionbd45a822013-06-06 17:35:35 -040059import android.provider.ContactsContract.Profile;
alision9f7a6ec2013-05-24 16:26:26 -040060import android.view.LayoutInflater;
61import android.view.View;
62import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040063import android.widget.AdapterView;
64import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040065import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040066import android.widget.ArrayAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040067import android.widget.ImageView;
alision9f7a6ec2013-05-24 16:26:26 -040068import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040069import android.widget.Spinner;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040070import android.widget.TextView;
alision9f7a6ec2013-05-24 16:26:26 -040071
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040072public class MenuFragment extends Fragment implements LoaderCallbacks<Bundle>, AccountsInterface {
alision84813a12013-05-27 17:40:39 -040073
Alexandre Lisionf30ff852013-12-09 17:08:40 -050074 @SuppressWarnings("unused")
alision9f7a6ec2013-05-24 16:26:26 -040075 private static final String TAG = MenuFragment.class.getSimpleName();
alision9f7a6ec2013-05-24 16:26:26 -040076
77 MenuAdapter mAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040078 String[] mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_URI };
alision907bde72013-06-20 14:40:37 -040079 AccountSelectionAdapter mAccountAdapter;
80 private Spinner spinnerAccounts;
81 AccountsReceiver accountReceiver;
alision907bde72013-06-20 14:40:37 -040082 private Callbacks mCallbacks = sDummyCallbacks;
alision907bde72013-06-20 14:40:37 -040083
alision907bde72013-06-20 14:40:37 -040084 private static Callbacks sDummyCallbacks = new Callbacks() {
85
86 @Override
87 public ISipService getService() {
alision907bde72013-06-20 14:40:37 -040088 return null;
89 }
90 };
91
alision907bde72013-06-20 14:40:37 -040092 public interface Callbacks {
93
94 public ISipService getService();
95
96 }
alision9f7a6ec2013-05-24 16:26:26 -040097
alision9f7a6ec2013-05-24 16:26:26 -040098 @Override
99 public void onAttach(Activity activity) {
100 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -0400101 if (!(activity instanceof Callbacks)) {
102 throw new IllegalStateException("Activity must implement fragment's callbacks.");
103 }
104
105 mCallbacks = (Callbacks) activity;
Alexandre Lision3e2a1d02013-11-19 17:23:00 -0500106 try {
107 mCallbacks.getService().registerAllAccounts();
108 } catch (RemoteException e) {
109 e.printStackTrace();
Alexandre Lisiondd6623b2013-12-02 16:23:40 -0500110 } catch (Exception e1) {
111
Alexandre Lision3e2a1d02013-11-19 17:23:00 -0500112 }
alision907bde72013-06-20 14:40:37 -0400113 getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
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);
alision84813a12013-05-27 17:40:39 -0400126
127 mAdapter = new MenuAdapter(getActivity());
alision907bde72013-06-20 14:40:37 -0400128 accountReceiver = new AccountsReceiver(this);
alision9f7a6ec2013-05-24 16:26:26 -0400129 }
130
alision907bde72013-06-20 14:40:37 -0400131 public void onResume() {
132 super.onResume();
133
134 IntentFilter intentFilter2 = new IntentFilter();
135 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
136 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
137 getActivity().registerReceiver(accountReceiver, intentFilter2);
138
139 }
140
141 @Override
142 public void onPause() {
143 super.onPause();
144 getActivity().unregisterReceiver(accountReceiver);
145 }
146
alision9f7a6ec2013-05-24 16:26:26 -0400147 @Override
148 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
149 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
150
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400151 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
152 R.array.menu_items_param));
153 ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(paramAdapter);
alision2cb99562013-05-30 17:02:20 -0400154 ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() {
155
156 @Override
157 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400158
alision96ff9b72013-07-05 10:59:16 -0400159 Intent in = new Intent();
Alexandre Lision6d75d062013-09-13 14:18:34 -0400160 switch (pos) {
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400161 case 0:
Alexandre Lision4ab53972013-11-04 16:59:18 -0500162 in.setClass(getActivity(), SettingsActivity.class);
163 getActivity().startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
alision2424b512013-07-05 10:05:38 -0400164 break;
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400165 case 1:
alision5de91782013-07-10 10:47:30 -0400166 in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_ABOUT);
alision96ff9b72013-07-05 10:59:16 -0400167 in.setClass(getActivity(), ActivityHolder.class);
168 getActivity().startActivity(in);
alision2424b512013-07-05 10:05:38 -0400169 break;
alisionbd45a822013-06-06 17:35:35 -0400170 }
alision2cb99562013-05-30 17:02:20 -0400171 }
172 });
alisionbd45a822013-06-06 17:35:35 -0400173
alision907bde72013-06-20 14:40:37 -0400174 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400175 mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>());
alision907bde72013-06-20 14:40:37 -0400176 spinnerAccounts.setAdapter(mAccountAdapter);
177 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400178
alision907bde72013-06-20 14:40:37 -0400179 @Override
180 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
alision907bde72013-06-20 14:40:37 -0400181 mAccountAdapter.setSelectedAccount(pos);
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400182 try {
183 mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder());
184 } catch (RemoteException e) {
185 e.printStackTrace();
186 }
alision55c36cb2013-06-14 14:57:38 -0400187 }
alision907bde72013-06-20 14:40:37 -0400188
189 @Override
190 public void onNothingSelected(AdapterView<?> arg0) {
Alexandre Lision80a99162013-09-13 14:19:23 -0400191 mAccountAdapter.setSelectedAccount(-1);
alision907bde72013-06-20 14:40:37 -0400192 }
193 });
194
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500195 CallContact user = CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver());
196 new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), user).run();
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400197
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500198 ((TextView) inflatedView.findViewById(R.id.user_name)).setText(user.getmDisplayName());
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400199
alision9f7a6ec2013-05-24 16:26:26 -0400200 return inflatedView;
201 }
202
203 @Override
204 public void onStart() {
205 super.onStart();
206
207 }
208
alision907bde72013-06-20 14:40:37 -0400209 public Account getSelectedAccount() {
210 return mAccountAdapter.getSelectedAccount();
211 }
212
213 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400214 public Loader<Bundle> onCreateLoader(int id, Bundle args) {
alision907bde72013-06-20 14:40:37 -0400215 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
216 l.forceLoad();
217 return l;
218 }
219
220 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400221 public void onLoadFinished(Loader<Bundle> loader, Bundle bun) {
alision907bde72013-06-20 14:40:37 -0400222 mAccountAdapter.removeAll();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400223 ArrayList<Account> accounts = bun.getParcelableArrayList(AccountsLoader.ACCOUNTS);
224 mAccountAdapter.addAll(accounts);
alision907bde72013-06-20 14:40:37 -0400225 }
226
227 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400228 public void onLoaderReset(Loader<Bundle> arg0) {
alision907bde72013-06-20 14:40:37 -0400229 // TODO Auto-generated method stub
230
231 }
232
233 /**
234 * Called by activity to pass a reference to sipservice to Fragment.
235 *
236 * @param isip
237 */
238 public void onServiceSipBinded(ISipService isip) {
239
240 }
241
242 public void updateAllAccounts() {
243 if (getActivity() != null)
244 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400245 }
246
247 public void updateAccount(Intent accountState) {
248 if (mAccountAdapter != null)
249 mAccountAdapter.updateAccount(accountState);
250 }
alision907bde72013-06-20 14:40:37 -0400251
Alexandre Lision80a99162013-09-13 14:19:23 -0400252 @Override
253 public void accountsChanged() {
254 updateAllAccounts();
255
256 }
257
258 @Override
259 public void accountStateChanged(Intent accountState) {
260 updateAccount(accountState);
261
262 }
alision907bde72013-06-20 14:40:37 -0400263
alision9f7a6ec2013-05-24 16:26:26 -0400264}