blob: 10a8bce561518aeaa47c3f8dd5fa4f97c6af31e8 [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;
alision907bde72013-06-20 14:40:37 -040042import android.os.RemoteException;
alisionbd45a822013-06-06 17:35:35 -040043import android.provider.ContactsContract.Profile;
alision907bde72013-06-20 14:40:37 -040044import android.util.Log;
alision9f7a6ec2013-05-24 16:26:26 -040045import android.view.LayoutInflater;
46import android.view.View;
47import android.view.ViewGroup;
alision2cb99562013-05-30 17:02:20 -040048import android.widget.AdapterView;
49import android.widget.AdapterView.OnItemClickListener;
alision907bde72013-06-20 14:40:37 -040050import android.widget.AdapterView.OnItemSelectedListener;
alision84813a12013-05-27 17:40:39 -040051import android.widget.ArrayAdapter;
alision9f7a6ec2013-05-24 16:26:26 -040052import android.widget.ListView;
alision907bde72013-06-20 14:40:37 -040053import android.widget.RadioButton;
54import android.widget.Spinner;
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;
alision2cb99562013-05-30 17:02:20 -040059import com.savoirfairelinux.sflphone.client.SFLPhoneHomeActivity;
60import com.savoirfairelinux.sflphone.client.SFLPhonePreferenceActivity;
alision907bde72013-06-20 14:40:37 -040061import com.savoirfairelinux.sflphone.interfaces.AccountsInterface;
62import com.savoirfairelinux.sflphone.loaders.AccountsLoader;
63import com.savoirfairelinux.sflphone.loaders.LoaderConstants;
64import com.savoirfairelinux.sflphone.model.Account;
65import com.savoirfairelinux.sflphone.receivers.AccountsReceiver;
66import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
67import 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) {
alisionbd45a822013-06-06 17:35:35 -0400169 if (pos == 1 || pos == 2 || pos == 3) {
170 Intent launchPreferencesIntent = new Intent().setClass(getActivity(), SFLPhonePreferenceActivity.class);
171 getActivity().startActivityForResult(launchPreferencesIntent, SFLPhoneHomeActivity.REQUEST_CODE_PREFERENCES);
172 }
alision2cb99562013-05-30 17:02:20 -0400173 }
174 });
alisionbd45a822013-06-06 17:35:35 -0400175
alision907bde72013-06-20 14:40:37 -0400176 spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection);
177 mAccountAdapter = new AccountSelectionAdapter(getActivity(), mCallbacks.getService(), new ArrayList<Account>());
178 spinnerAccounts.setAdapter(mAccountAdapter);
179 spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() {
alisionbd45a822013-06-06 17:35:35 -0400180
alision907bde72013-06-20 14:40:37 -0400181 @Override
182 public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) {
183 // public void onClick(DialogInterface dialog, int which) {
184
185 Log.i(TAG, "Selected Account: " + mAdapter.getItem(pos));
186 if (null != view) {
187 ((RadioButton) view.findViewById(R.id.account_checked)).toggle();
188 }
189 mAccountAdapter.setSelectedAccount(pos);
190 // accountSelectedNotifyAccountList(mAdapter.getItem(pos));
191 // setSelection(cursor.getPosition(),true);
192
alision55c36cb2013-06-14 14:57:38 -0400193 }
alision907bde72013-06-20 14:40:37 -0400194
195 @Override
196 public void onNothingSelected(AdapterView<?> arg0) {
197 // TODO Auto-generated method stub
198
199 }
200 });
201
202 // Cursor mProfileCursor = getActivity().getContentResolver().query(Profile.CONTENT_URI, mProjection, null, null, null);
203 //
204 // if (mProfileCursor.getCount() > 0) {
205 // mProfileCursor.moveToFirst();
206 // String photo_uri = mProfileCursor.getString(mProfileCursor.getColumnIndex(Profile.PHOTO_THUMBNAIL_URI));
207 // if (photo_uri != null) {
208 // ((ImageView) inflatedView.findViewById(R.id.user_photo)).setImageURI(Uri.parse(photo_uri));
209 // }
210 // ((TextView) inflatedView.findViewById(R.id.user_name)).setText(mProfileCursor.getString(mProfileCursor
211 // .getColumnIndex(Profile.DISPLAY_NAME_PRIMARY)));
212 // mProfileCursor.close();
213 // }
alision9f7a6ec2013-05-24 16:26:26 -0400214 return inflatedView;
215 }
216
217 @Override
218 public void onStart() {
219 super.onStart();
220
221 }
222
alision907bde72013-06-20 14:40:37 -0400223 public Account getSelectedAccount() {
224 return mAccountAdapter.getSelectedAccount();
225 }
226
227 @Override
228 public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) {
229 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
230 l.forceLoad();
231 return l;
232 }
233
234 @Override
235 public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) {
236 mAccountAdapter.removeAll();
237 mAccountAdapter.addAll(results);
238
239 }
240
241 @Override
242 public void onLoaderReset(Loader<ArrayList<Account>> arg0) {
243 // TODO Auto-generated method stub
244
245 }
246
247 /**
248 * Called by activity to pass a reference to sipservice to Fragment.
249 *
250 * @param isip
251 */
252 public void onServiceSipBinded(ISipService isip) {
253
254 }
255
256 public void updateAllAccounts() {
257 if (getActivity() != null)
258 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
259
260 }
261
262 public void updateAccount(Intent accountState) {
263 if (mAccountAdapter != null)
264 mAccountAdapter.updateAccount(accountState);
265 }
266
267 @Override
268 public void accountsChanged() {
269 updateAllAccounts();
270
271 }
272
273 @Override
274 public void accountStateChanged(Intent accountState) {
275 updateAccount(accountState);
276
277 }
278
alision9f7a6ec2013-05-24 16:26:26 -0400279}