blob: 68998ff7927fb2e0c4589b33619b3da238e4c75b [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 */
alision9f7a6ec2013-05-24 16:26:26 -040031package com.savoirfairelinux.sflphone.fragments;
32
alision907bde72013-06-20 14:40:37 -040033import java.util.ArrayList;
alisionbd45a822013-06-06 17:35:35 -040034
alision9f7a6ec2013-05-24 16:26:26 -040035import android.app.Activity;
36import android.app.Fragment;
alision907bde72013-06-20 14:40:37 -040037import android.app.LoaderManager.LoaderCallbacks;
alision2cb99562013-05-30 17:02:20 -040038import android.content.Intent;
alision907bde72013-06-20 14:40:37 -040039import android.content.IntentFilter;
40import android.content.Loader;
alision9f7a6ec2013-05-24 16:26:26 -040041import android.os.Bundle;
alisionbd45a822013-06-06 17:35:35 -040042import android.provider.ContactsContract.Profile;
alision907bde72013-06-20 14:40:37 -040043import android.util.Log;
alision9f7a6ec2013-05-24 16:26:26 -040044import android.view.LayoutInflater;
45import android.view.View;
46import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040047import android.widget.AdapterView;
48import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040049import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040050import android.widget.ArrayAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040051import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040052import android.widget.RadioButton;
53import android.widget.Spinner;
alision2424b512013-07-05 10:05:38 -040054import android.widget.Toast;
alision9f7a6ec2013-05-24 16:26:26 -040055
56import com.savoirfairelinux.sflphone.R;
alision907bde72013-06-20 14:40:37 -040057import com.savoirfairelinux.sflphone.adapters.AccountSelectionAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040058import com.savoirfairelinux.sflphone.adapters.MenuAdapter;
alision96ff9b72013-07-05 10:59:16 -040059import com.savoirfairelinux.sflphone.client.ActivityHolder;
alision2cb99562013-05-30 17:02:20 -040060import com.savoirfairelinux.sflphone.client.SFLPhoneHomeActivity;
61import com.savoirfairelinux.sflphone.client.SFLPhonePreferenceActivity;
alision907bde72013-06-20 14:40:37 -040062import com.savoirfairelinux.sflphone.interfaces.AccountsInterface;
63import com.savoirfairelinux.sflphone.loaders.AccountsLoader;
64import com.savoirfairelinux.sflphone.loaders.LoaderConstants;
65import com.savoirfairelinux.sflphone.model.Account;
66import com.savoirfairelinux.sflphone.receivers.AccountsReceiver;
alision907bde72013-06-20 14:40:37 -040067import com.savoirfairelinux.sflphone.service.ConfigurationManagerCallback;
68import com.savoirfairelinux.sflphone.service.ISipService;
alision9f7a6ec2013-05-24 16:26:26 -040069
alision907bde72013-06-20 14:40:37 -040070public class MenuFragment extends Fragment implements LoaderCallbacks<ArrayList<Account>>, AccountsInterface {
alision84813a12013-05-27 17:40:39 -040071
alision9f7a6ec2013-05-24 16:26:26 -040072 private static final String TAG = MenuFragment.class.getSimpleName();
73 public static final String ARG_SECTION_NUMBER = "section_number";
74
75 MenuAdapter mAdapter;
alisionbd45a822013-06-06 17:35:35 -040076 String[] mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_THUMBNAIL_URI };
alision907bde72013-06-20 14:40:37 -040077 AccountSelectionAdapter mAccountAdapter;
78 private Spinner spinnerAccounts;
79 AccountsReceiver accountReceiver;
80
81 private Callbacks mCallbacks = sDummyCallbacks;
82 // private Spinner spinnerAccounts;
83
84 /**
85 * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity.
86 */
87 private static Callbacks sDummyCallbacks = new Callbacks() {
88
89 @Override
90 public ISipService getService() {
91 // TODO Auto-generated method stub
92 return null;
93 }
94 };
95
96 /**
97 * The Activity calling this fragment has to implement this interface
98 *
99 */
100 public interface Callbacks {
101
102 public ISipService getService();
103
104 }
alision9f7a6ec2013-05-24 16:26:26 -0400105
alision9f7a6ec2013-05-24 16:26:26 -0400106 @Override
107 public void onAttach(Activity activity) {
108 super.onAttach(activity);
alision907bde72013-06-20 14:40:37 -0400109 if (!(activity instanceof Callbacks)) {
110 throw new IllegalStateException("Activity must implement fragment's callbacks.");
111 }
112
113 mCallbacks = (Callbacks) activity;
114 getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
115
alision84813a12013-05-27 17:40:39 -0400116
alision9f7a6ec2013-05-24 16:26:26 -0400117 }
118
119 @Override
120 public void onDetach() {
121 super.onDetach();
122 }
123
124 @Override
125 public void onCreate(Bundle savedInstanceState) {
126 super.onCreate(savedInstanceState);
alision84813a12013-05-27 17:40:39 -0400127
128 mAdapter = new MenuAdapter(getActivity());
alision907bde72013-06-20 14:40:37 -0400129 accountReceiver = new AccountsReceiver(this);
alision84813a12013-05-27 17:40:39 -0400130
131 String[] categories = getResources().getStringArray(R.array.menu_categories);
132 ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
133 R.array.menu_items_param));
134 ArrayAdapter<String> helpAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray(
135 R.array.menu_items_help));
136
137 // Add Sections
alision84813a12013-05-27 17:40:39 -0400138 mAdapter.addSection(categories[0], paramAdapter);
139 mAdapter.addSection(categories[1], helpAdapter);
140
alision9f7a6ec2013-05-24 16:26:26 -0400141 }
142
alision907bde72013-06-20 14:40:37 -0400143 public void onResume() {
144 super.onResume();
145
146 IntentFilter intentFilter2 = new IntentFilter();
147 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
148 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
149 getActivity().registerReceiver(accountReceiver, intentFilter2);
150
151 }
152
153 @Override
154 public void onPause() {
155 super.onPause();
156 getActivity().unregisterReceiver(accountReceiver);
157 }
158
159
alision9f7a6ec2013-05-24 16:26:26 -0400160 @Override
161 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
162 View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false);
163
164 ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(mAdapter);
alision2cb99562013-05-30 17:02:20 -0400165 ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() {
166
167 @Override
168 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
alision96ff9b72013-07-05 10:59:16 -0400169
170 Intent in = new Intent();
alision2424b512013-07-05 10:05:38 -0400171 switch(pos){
172 case 1:
alision96ff9b72013-07-05 10:59:16 -0400173 in.setClass(getActivity(), SFLPhonePreferenceActivity.class);
174 getActivity().startActivityForResult(in, SFLPhoneHomeActivity.REQUEST_CODE_PREFERENCES);
alision2424b512013-07-05 10:05:38 -0400175 break;
alision2424b512013-07-05 10:05:38 -0400176 case 3:
alision96ff9b72013-07-05 10:59:16 -0400177 in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_GESTURES);
178 in.setClass(getActivity(), ActivityHolder.class);
179 getActivity().startActivity(in);
180 break;
Alexandre Lisioncdec5952013-07-17 14:18:22 -0400181 case 4:
alision5de91782013-07-10 10:47:30 -0400182 in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_ABOUT);
alision96ff9b72013-07-05 10:59:16 -0400183 in.setClass(getActivity(), ActivityHolder.class);
184 getActivity().startActivity(in);
alision2424b512013-07-05 10:05:38 -0400185 break;
alisionbd45a822013-06-06 17:35:35 -0400186 }
alision2cb99562013-05-30 17:02:20 -0400187 }
188 });
alisionbd45a822013-06-06 17:35:35 -0400189
alision907bde72013-06-20 14:40:37 -0400190 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
191 mAccountAdapter = new AccountSelectionAdapter(getActivity(), mCallbacks.getService(), new ArrayList<Account>());
192 spinnerAccounts.setAdapter(mAccountAdapter);
193 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400194
alision907bde72013-06-20 14:40:37 -0400195 @Override
196 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
197 // public void onClick(DialogInterface dialog, int which) {
198
199 Log.i(TAG, "Selected Account: " + mAdapter.getItem(pos));
200 if (null != view) {
201 ((RadioButton) view.findViewById(R.id.account_checked)).toggle();
202 }
203 mAccountAdapter.setSelectedAccount(pos);
204 // accountSelectedNotifyAccountList(mAdapter.getItem(pos));
205 // setSelection(cursor.getPosition(),true);
206
alision55c36cb2013-06-14 14:57:38 -0400207 }
alision907bde72013-06-20 14:40:37 -0400208
209 @Override
210 public void onNothingSelected(AdapterView<?> arg0) {
211 // TODO Auto-generated method stub
212
213 }
214 });
215
216 // Cursor mProfileCursor = getActivity().getContentResolver().query(Profile.CONTENT_URI, mProjection, null, null, null);
217 //
218 // if (mProfileCursor.getCount() > 0) {
219 // mProfileCursor.moveToFirst();
220 // String photo_uri = mProfileCursor.getString(mProfileCursor.getColumnIndex(Profile.PHOTO_THUMBNAIL_URI));
221 // if (photo_uri != null) {
222 // ((ImageView) inflatedView.findViewById(R.id.user_photo)).setImageURI(Uri.parse(photo_uri));
223 // }
224 // ((TextView) inflatedView.findViewById(R.id.user_name)).setText(mProfileCursor.getString(mProfileCursor
225 // .getColumnIndex(Profile.DISPLAY_NAME_PRIMARY)));
226 // mProfileCursor.close();
227 // }
alision9f7a6ec2013-05-24 16:26:26 -0400228 return inflatedView;
229 }
230
231 @Override
232 public void onStart() {
233 super.onStart();
234
235 }
236
alision907bde72013-06-20 14:40:37 -0400237 public Account getSelectedAccount() {
238 return mAccountAdapter.getSelectedAccount();
239 }
240
241 @Override
242 public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) {
243 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
244 l.forceLoad();
245 return l;
246 }
247
248 @Override
249 public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) {
250 mAccountAdapter.removeAll();
251 mAccountAdapter.addAll(results);
252
253 }
254
255 @Override
256 public void onLoaderReset(Loader<ArrayList<Account>> arg0) {
257 // TODO Auto-generated method stub
258
259 }
260
261 /**
262 * Called by activity to pass a reference to sipservice to Fragment.
263 *
264 * @param isip
265 */
266 public void onServiceSipBinded(ISipService isip) {
267
268 }
269
270 public void updateAllAccounts() {
271 if (getActivity() != null)
272 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
273
274 }
275
276 public void updateAccount(Intent accountState) {
277 if (mAccountAdapter != null)
278 mAccountAdapter.updateAccount(accountState);
279 }
280
281 @Override
282 public void accountsChanged() {
283 updateAllAccounts();
284
285 }
286
287 @Override
288 public void accountStateChanged(Intent accountState) {
289 updateAccount(accountState);
290
291 }
292
alision9f7a6ec2013-05-24 16:26:26 -0400293}