blob: e1234f52e043d831d01eb3dc836832a761ac07b9 [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;
46import 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;
51import android.app.Fragment;
alision907bde72013-06-20 14:40:37 -040052import android.app.LoaderManager.LoaderCallbacks;
alision2cb99562013-05-30 17:02:20 -040053import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040054import android.content.IntentFilter;
55import android.content.Loader;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040056import android.database.Cursor;
57import android.net.Uri;
alision9f7a6ec2013-05-24 16:26:26 -040058import android.os.Bundle;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040059import android.os.RemoteException;
alisionbd45a822013-06-06 17:35:35 -040060import android.provider.ContactsContract.Profile;
alision9f7a6ec2013-05-24 16:26:26 -040061import android.view.LayoutInflater;
62import android.view.View;
63import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040064import android.widget.AdapterView;
65import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040066import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040067import android.widget.ArrayAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040068import android.widget.ImageView;
alision9f7a6ec2013-05-24 16:26:26 -040069import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040070import android.widget.RadioButton;
71import android.widget.Spinner;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040072import android.widget.TextView;
alision9f7a6ec2013-05-24 16:26:26 -040073
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040074public class MenuFragment extends Fragment implements LoaderCallbacks<Bundle>, AccountsInterface {
alision84813a12013-05-27 17:40:39 -040075
alision9f7a6ec2013-05-24 16:26:26 -040076 private static final String TAG = MenuFragment.class.getSimpleName();
77 public static final String ARG_SECTION_NUMBER = "section_number";
78
79 MenuAdapter mAdapter;
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -040080 String[] mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_URI };
alision907bde72013-06-20 14:40:37 -040081 AccountSelectionAdapter mAccountAdapter;
82 private Spinner spinnerAccounts;
83 AccountsReceiver accountReceiver;
alision907bde72013-06-20 14:40:37 -040084 private Callbacks mCallbacks = sDummyCallbacks;
alision907bde72013-06-20 14:40:37 -040085
alision907bde72013-06-20 14:40:37 -040086 private static Callbacks sDummyCallbacks = new Callbacks() {
87
88 @Override
89 public ISipService getService() {
alision907bde72013-06-20 14:40:37 -040090 return null;
91 }
92 };
93
alision907bde72013-06-20 14:40:37 -040094 public interface Callbacks {
95
96 public ISipService getService();
97
98 }
alision9f7a6ec2013-05-24 16:26:26 -040099
alision9f7a6ec2013-05-24 16:26:26 -0400100 @Override
101 public void onAttach(Activity activity) {
102 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -0400103 if (!(activity instanceof Callbacks)) {
104 throw new IllegalStateException("Activity must implement fragment's callbacks.");
105 }
106
107 mCallbacks = (Callbacks) activity;
108 getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision84813a12013-05-27 17:40:39 -0400109
alision9f7a6ec2013-05-24 16:26:26 -0400110 }
111
112 @Override
113 public void onDetach() {
114 super.onDetach();
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400115 mCallbacks = sDummyCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -0400116 }
117
118 @Override
119 public void onCreate(Bundle savedInstanceState) {
120 super.onCreate(savedInstanceState);
alision84813a12013-05-27 17:40:39 -0400121
122 mAdapter = new MenuAdapter(getActivity());
alision907bde72013-06-20 14:40:37 -0400123 accountReceiver = new AccountsReceiver(this);
alision84813a12013-05-27 17:40:39 -0400124
125 String[] categories = getResources().getStringArray(R.array.menu_categories);
Alexandre Lision4ab53972013-11-04 16:59:18 -0500126 // ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
127 // R.array.menu_items_param));
128 // ArrayAdapter<String> helpAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
129 // R.array.menu_items_help));
alision84813a12013-05-27 17:40:39 -0400130
131 // Add Sections
Alexandre Lision4ab53972013-11-04 16:59:18 -0500132 // mAdapter.addSection(categories[0], paramAdapter);
133 // mAdapter.addSection(categories[1], helpAdapter);
alision84813a12013-05-27 17:40:39 -0400134
alision9f7a6ec2013-05-24 16:26:26 -0400135 }
136
alision907bde72013-06-20 14:40:37 -0400137 public void onResume() {
138 super.onResume();
139
140 IntentFilter intentFilter2 = new IntentFilter();
141 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
142 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
143 getActivity().registerReceiver(accountReceiver, intentFilter2);
144
145 }
146
147 @Override
148 public void onPause() {
149 super.onPause();
150 getActivity().unregisterReceiver(accountReceiver);
151 }
152
alision9f7a6ec2013-05-24 16:26:26 -0400153 @Override
154 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
155 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
156
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400157 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
158 R.array.menu_items_param));
159 ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(paramAdapter);
alision2cb99562013-05-30 17:02:20 -0400160 ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() {
161
162 @Override
163 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400164
alision96ff9b72013-07-05 10:59:16 -0400165 Intent in = new Intent();
Alexandre Lision6d75d062013-09-13 14:18:34 -0400166 switch (pos) {
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400167 case 0:
Alexandre Lision4ab53972013-11-04 16:59:18 -0500168 in.setClass(getActivity(), SettingsActivity.class);
169 getActivity().startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES);
alision2424b512013-07-05 10:05:38 -0400170 break;
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400171 case 1:
alision5de91782013-07-10 10:47:30 -0400172 in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_ABOUT);
alision96ff9b72013-07-05 10:59:16 -0400173 in.setClass(getActivity(), ActivityHolder.class);
174 getActivity().startActivity(in);
alision2424b512013-07-05 10:05:38 -0400175 break;
alisionbd45a822013-06-06 17:35:35 -0400176 }
alision2cb99562013-05-30 17:02:20 -0400177 }
178 });
alisionbd45a822013-06-06 17:35:35 -0400179
alision907bde72013-06-20 14:40:37 -0400180 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400181 mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>());
alision907bde72013-06-20 14:40:37 -0400182 spinnerAccounts.setAdapter(mAccountAdapter);
183 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400184
alision907bde72013-06-20 14:40:37 -0400185 @Override
186 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
alision907bde72013-06-20 14:40:37 -0400187 if (null != view) {
188 ((RadioButton) view.findViewById(R.id.account_checked)).toggle();
189 }
190 mAccountAdapter.setSelectedAccount(pos);
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400191 try {
192 mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder());
193 } catch (RemoteException e) {
194 e.printStackTrace();
195 }
alision55c36cb2013-06-14 14:57:38 -0400196 }
alision907bde72013-06-20 14:40:37 -0400197
198 @Override
199 public void onNothingSelected(AdapterView<?> arg0) {
Alexandre Lision80a99162013-09-13 14:19:23 -0400200 mAccountAdapter.setSelectedAccount(-1);
alision907bde72013-06-20 14:40:37 -0400201 }
202 });
203
Alexandre Lisionfd3fcd92013-10-25 15:23:52 -0400204 Cursor mProfileCursor = getActivity().getContentResolver().query(Profile.CONTENT_URI, mProjection, null, null, null);
205
206 if (mProfileCursor.getCount() > 0) {
207 mProfileCursor.moveToFirst();
208 long contact_id = mProfileCursor.getLong(mProfileCursor.getColumnIndex(Profile._ID));
209
210 new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), contact_id).run();
211
212
213 ((TextView) inflatedView.findViewById(R.id.user_name)).setText(mProfileCursor.getString(mProfileCursor
214 .getColumnIndex(Profile.DISPLAY_NAME_PRIMARY)));
215 mProfileCursor.close();
216 }
alision9f7a6ec2013-05-24 16:26:26 -0400217 return inflatedView;
218 }
219
220 @Override
221 public void onStart() {
222 super.onStart();
223
224 }
225
alision907bde72013-06-20 14:40:37 -0400226 public Account getSelectedAccount() {
227 return mAccountAdapter.getSelectedAccount();
228 }
229
230 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400231 public Loader<Bundle> onCreateLoader(int id, Bundle args) {
alision907bde72013-06-20 14:40:37 -0400232 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
233 l.forceLoad();
234 return l;
235 }
236
237 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400238 public void onLoadFinished(Loader<Bundle> loader, Bundle bun) {
alision907bde72013-06-20 14:40:37 -0400239 mAccountAdapter.removeAll();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400240 ArrayList<Account> accounts = bun.getParcelableArrayList(AccountsLoader.ACCOUNTS);
241 mAccountAdapter.addAll(accounts);
alision907bde72013-06-20 14:40:37 -0400242
243 }
244
245 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400246 public void onLoaderReset(Loader<Bundle> arg0) {
alision907bde72013-06-20 14:40:37 -0400247 // TODO Auto-generated method stub
248
249 }
250
251 /**
252 * Called by activity to pass a reference to sipservice to Fragment.
253 *
254 * @param isip
255 */
256 public void onServiceSipBinded(ISipService isip) {
257
258 }
259
260 public void updateAllAccounts() {
261 if (getActivity() != null)
262 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400263 }
264
265 public void updateAccount(Intent accountState) {
266 if (mAccountAdapter != null)
267 mAccountAdapter.updateAccount(accountState);
268 }
alision907bde72013-06-20 14:40:37 -0400269
Alexandre Lision80a99162013-09-13 14:19:23 -0400270 @Override
271 public void accountsChanged() {
272 updateAllAccounts();
273
274 }
275
276 @Override
277 public void accountStateChanged(Intent accountState) {
278 updateAccount(accountState);
279
280 }
alision907bde72013-06-20 14:40:37 -0400281
alision9f7a6ec2013-05-24 16:26:26 -0400282}