blob: cabfc66cddbb7f4f8485cb554813c29d16325286 [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;
37import org.sflphone.adapters.MenuAdapter;
38import org.sflphone.client.ActivityHolder;
39import org.sflphone.client.SFLPhoneHomeActivity;
40import org.sflphone.client.SFLPhonePreferenceActivity;
41import org.sflphone.interfaces.AccountsInterface;
42import org.sflphone.loaders.AccountsLoader;
43import org.sflphone.loaders.LoaderConstants;
44import org.sflphone.model.Account;
45import org.sflphone.receivers.AccountsReceiver;
46import org.sflphone.service.ConfigurationManagerCallback;
47import org.sflphone.service.ISipService;
48
alision9f7a6ec2013-05-24 16:26:26 -040049import android.app.Activity;
50import android.app.Fragment;
alision907bde72013-06-20 14:40:37 -040051import android.app.LoaderManager.LoaderCallbacks;
alision2cb99562013-05-30 17:02:20 -040052import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040053import android.content.IntentFilter;
54import android.content.Loader;
alision9f7a6ec2013-05-24 16:26:26 -040055import android.os.Bundle;
alisionbd45a822013-06-06 17:35:35 -040056import android.provider.ContactsContract.Profile;
alision907bde72013-06-20 14:40:37 -040057import android.util.Log;
alision9f7a6ec2013-05-24 16:26:26 -040058import android.view.LayoutInflater;
59import android.view.View;
60import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040061import android.widget.AdapterView;
62import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040063import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040064import android.widget.ArrayAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040065import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040066import android.widget.RadioButton;
67import android.widget.Spinner;
alision9f7a6ec2013-05-24 16:26:26 -040068
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040069public class MenuFragment extends Fragment implements LoaderCallbacks<Bundle>, AccountsInterface {
alision84813a12013-05-27 17:40:39 -040070
alision9f7a6ec2013-05-24 16:26:26 -040071 private static final String TAG = MenuFragment.class.getSimpleName();
72 public static final String ARG_SECTION_NUMBER = "section_number";
73
74 MenuAdapter mAdapter;
alisionbd45a822013-06-06 17:35:35 -040075 String[] mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_THUMBNAIL_URI };
alision907bde72013-06-20 14:40:37 -040076 AccountSelectionAdapter mAccountAdapter;
77 private Spinner spinnerAccounts;
78 AccountsReceiver accountReceiver;
alision907bde72013-06-20 14:40:37 -040079 private Callbacks mCallbacks = sDummyCallbacks;
alision907bde72013-06-20 14:40:37 -040080
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 }
87 };
88
alision907bde72013-06-20 14:40:37 -040089 public interface Callbacks {
90
91 public ISipService getService();
92
93 }
alision9f7a6ec2013-05-24 16:26:26 -040094
alision9f7a6ec2013-05-24 16:26:26 -040095 @Override
96 public void onAttach(Activity activity) {
97 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -040098 if (!(activity instanceof Callbacks)) {
99 throw new IllegalStateException("Activity must implement fragment's callbacks.");
100 }
101
102 mCallbacks = (Callbacks) activity;
103 getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision84813a12013-05-27 17:40:39 -0400104
alision9f7a6ec2013-05-24 16:26:26 -0400105 }
106
107 @Override
108 public void onDetach() {
109 super.onDetach();
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400110 mCallbacks = sDummyCallbacks;
alision9f7a6ec2013-05-24 16:26:26 -0400111 }
112
113 @Override
114 public void onCreate(Bundle savedInstanceState) {
115 super.onCreate(savedInstanceState);
alision84813a12013-05-27 17:40:39 -0400116
117 mAdapter = new MenuAdapter(getActivity());
alision907bde72013-06-20 14:40:37 -0400118 accountReceiver = new AccountsReceiver(this);
alision84813a12013-05-27 17:40:39 -0400119
120 String[] categories = getResources().getStringArray(R.array.menu_categories);
121 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
122 R.array.menu_items_param));
123 ArrayAdapter<String> helpAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
124 R.array.menu_items_help));
125
126 // Add Sections
alision84813a12013-05-27 17:40:39 -0400127 mAdapter.addSection(categories[0], paramAdapter);
128 mAdapter.addSection(categories[1], helpAdapter);
129
alision9f7a6ec2013-05-24 16:26:26 -0400130 }
131
alision907bde72013-06-20 14:40:37 -0400132 public void onResume() {
133 super.onResume();
134
135 IntentFilter intentFilter2 = new IntentFilter();
136 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
137 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
138 getActivity().registerReceiver(accountReceiver, intentFilter2);
139
140 }
141
142 @Override
143 public void onPause() {
144 super.onPause();
145 getActivity().unregisterReceiver(accountReceiver);
146 }
147
alision9f7a6ec2013-05-24 16:26:26 -0400148 @Override
149 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
150 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
151
152 ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(mAdapter);
alision2cb99562013-05-30 17:02:20 -0400153 ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() {
154
155 @Override
156 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision6d75d062013-09-13 14:18:34 -0400157
alision96ff9b72013-07-05 10:59:16 -0400158 Intent in = new Intent();
Alexandre Lision6d75d062013-09-13 14:18:34 -0400159 switch (pos) {
alision2424b512013-07-05 10:05:38 -0400160 case 1:
alision96ff9b72013-07-05 10:59:16 -0400161 in.setClass(getActivity(), SFLPhonePreferenceActivity.class);
162 getActivity().startActivityForResult(in, SFLPhoneHomeActivity.REQUEST_CODE_PREFERENCES);
alision2424b512013-07-05 10:05:38 -0400163 break;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400164 // case 3:
165 // in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_GESTURES);
166 // in.setClass(getActivity(), ActivityHolder.class);
167 // getActivity().startActivity(in);
168 // break;
alision2424b512013-07-05 10:05:38 -0400169 case 3:
alision5de91782013-07-10 10:47:30 -0400170 in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_ABOUT);
alision96ff9b72013-07-05 10:59:16 -0400171 in.setClass(getActivity(), ActivityHolder.class);
172 getActivity().startActivity(in);
alision2424b512013-07-05 10:05:38 -0400173 break;
alisionbd45a822013-06-06 17:35:35 -0400174 }
alision2cb99562013-05-30 17:02:20 -0400175 }
176 });
alisionbd45a822013-06-06 17:35:35 -0400177
alision907bde72013-06-20 14:40:37 -0400178 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -0400179 mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>());
alision907bde72013-06-20 14:40:37 -0400180 spinnerAccounts.setAdapter(mAccountAdapter);
181 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400182
alision907bde72013-06-20 14:40:37 -0400183 @Override
184 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
alision907bde72013-06-20 14:40:37 -0400185 if (null != view) {
186 ((RadioButton) view.findViewById(R.id.account_checked)).toggle();
187 }
188 mAccountAdapter.setSelectedAccount(pos);
alision55c36cb2013-06-14 14:57:38 -0400189 }
alision907bde72013-06-20 14:40:37 -0400190
191 @Override
192 public void onNothingSelected(AdapterView<?> arg0) {
Alexandre Lision80a99162013-09-13 14:19:23 -0400193 mAccountAdapter.setSelectedAccount(-1);
alision907bde72013-06-20 14:40:37 -0400194 }
195 });
196
197 // Cursor mProfileCursor = getActivity().getContentResolver().query(Profile.CONTENT_URI, mProjection, null, null, null);
198 //
199 // if (mProfileCursor.getCount() > 0) {
200 // mProfileCursor.moveToFirst();
201 // String photo_uri = mProfileCursor.getString(mProfileCursor.getColumnIndex(Profile.PHOTO_THUMBNAIL_URI));
202 // if (photo_uri != null) {
203 // ((ImageView) inflatedView.findViewById(R.id.user_photo)).setImageURI(Uri.parse(photo_uri));
204 // }
205 // ((TextView) inflatedView.findViewById(R.id.user_name)).setText(mProfileCursor.getString(mProfileCursor
206 // .getColumnIndex(Profile.DISPLAY_NAME_PRIMARY)));
207 // mProfileCursor.close();
208 // }
alision9f7a6ec2013-05-24 16:26:26 -0400209 return inflatedView;
210 }
211
212 @Override
213 public void onStart() {
214 super.onStart();
215
216 }
217
alision907bde72013-06-20 14:40:37 -0400218 public Account getSelectedAccount() {
219 return mAccountAdapter.getSelectedAccount();
220 }
221
222 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400223 public Loader<Bundle> onCreateLoader(int id, Bundle args) {
alision907bde72013-06-20 14:40:37 -0400224 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
225 l.forceLoad();
226 return l;
227 }
228
229 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400230 public void onLoadFinished(Loader<Bundle> loader, Bundle bun) {
alision907bde72013-06-20 14:40:37 -0400231 mAccountAdapter.removeAll();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400232 ArrayList<Account> accounts = bun.getParcelableArrayList(AccountsLoader.ACCOUNTS);
233 mAccountAdapter.addAll(accounts);
alision907bde72013-06-20 14:40:37 -0400234
235 }
236
237 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400238 public void onLoaderReset(Loader<Bundle> arg0) {
alision907bde72013-06-20 14:40:37 -0400239 // TODO Auto-generated method stub
240
241 }
242
243 /**
244 * Called by activity to pass a reference to sipservice to Fragment.
245 *
246 * @param isip
247 */
248 public void onServiceSipBinded(ISipService isip) {
249
250 }
251
252 public void updateAllAccounts() {
253 if (getActivity() != null)
254 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
alision907bde72013-06-20 14:40:37 -0400255 }
256
257 public void updateAccount(Intent accountState) {
258 if (mAccountAdapter != null)
259 mAccountAdapter.updateAccount(accountState);
260 }
alision907bde72013-06-20 14:40:37 -0400261
Alexandre Lision80a99162013-09-13 14:19:23 -0400262 @Override
263 public void accountsChanged() {
264 updateAllAccounts();
265
266 }
267
268 @Override
269 public void accountStateChanged(Intent accountState) {
270 updateAccount(accountState);
271
272 }
alision907bde72013-06-20 14:40:37 -0400273
alision9f7a6ec2013-05-24 16:26:26 -0400274}