alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 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 Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 31 | package org.sflphone.fragments; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 32 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 33 | import java.util.ArrayList; |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 34 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 35 | import org.sflphone.R; |
| 36 | import org.sflphone.adapters.AccountSelectionAdapter; |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 37 | import org.sflphone.adapters.ContactPictureTask; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 38 | import org.sflphone.interfaces.AccountsInterface; |
| 39 | import org.sflphone.loaders.AccountsLoader; |
| 40 | import org.sflphone.loaders.LoaderConstants; |
| 41 | import org.sflphone.model.Account; |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 42 | import org.sflphone.model.CallContact; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 43 | import org.sflphone.receivers.AccountsReceiver; |
| 44 | import org.sflphone.service.ConfigurationManagerCallback; |
| 45 | import org.sflphone.service.ISipService; |
| 46 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 47 | import android.app.Activity; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 48 | import android.content.Intent; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 49 | import android.content.IntentFilter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 50 | import android.os.Bundle; |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 51 | import android.os.RemoteException; |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 52 | import android.provider.ContactsContract.Profile; |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 53 | import android.support.v4.app.Fragment; |
| 54 | import android.support.v4.app.LoaderManager.LoaderCallbacks; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 55 | import android.view.LayoutInflater; |
| 56 | import android.view.View; |
| 57 | import android.view.ViewGroup; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 58 | import android.widget.AdapterView; |
| 59 | import android.widget.AdapterView.OnItemClickListener; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 60 | import android.widget.AdapterView.OnItemSelectedListener; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 61 | import android.widget.ArrayAdapter; |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 62 | import android.widget.ImageView; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 63 | import android.widget.ListView; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 64 | import android.widget.Spinner; |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 65 | import android.widget.TextView; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 66 | |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 67 | public class MenuFragment extends Fragment implements LoaderCallbacks<Bundle>, AccountsInterface { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 68 | |
Alexandre Lision | f30ff85 | 2013-12-09 17:08:40 -0500 | [diff] [blame] | 69 | @SuppressWarnings("unused") |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 70 | private static final String TAG = MenuFragment.class.getSimpleName(); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 71 | |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 72 | String[] mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_URI }; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 73 | AccountSelectionAdapter mAccountAdapter; |
| 74 | private Spinner spinnerAccounts; |
| 75 | AccountsReceiver accountReceiver; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 76 | private Callbacks mCallbacks = sDummyCallbacks; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 77 | |
Alexandre Lision | fc9b4e7 | 2013-12-13 14:55:25 -0500 | [diff] [blame] | 78 | private ListView sections; |
| 79 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 80 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 81 | |
| 82 | @Override |
| 83 | public ISipService getService() { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 84 | return null; |
| 85 | } |
Alexandre Lision | 269e604 | 2013-12-11 16:15:13 -0500 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | public void onSectionSelected(int pos) { |
Alexandre Lision | 658999c | 2013-12-13 14:42:45 -0500 | [diff] [blame] | 89 | |
Alexandre Lision | 269e604 | 2013-12-11 16:15:13 -0500 | [diff] [blame] | 90 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 91 | }; |
| 92 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 93 | public interface Callbacks { |
| 94 | |
| 95 | public ISipService getService(); |
Alexandre Lision | 658999c | 2013-12-13 14:42:45 -0500 | [diff] [blame] | 96 | |
Alexandre Lision | 269e604 | 2013-12-11 16:15:13 -0500 | [diff] [blame] | 97 | public void onSectionSelected(int pos); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 98 | |
| 99 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 100 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 101 | @Override |
| 102 | public void onAttach(Activity activity) { |
| 103 | super.onAttach(activity); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 104 | if (!(activity instanceof Callbacks)) { |
| 105 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 106 | } |
| 107 | |
| 108 | mCallbacks = (Callbacks) activity; |
Alexandre Lision | 3e2a1d0 | 2013-11-19 17:23:00 -0500 | [diff] [blame] | 109 | try { |
| 110 | mCallbacks.getService().registerAllAccounts(); |
| 111 | } catch (RemoteException e) { |
| 112 | e.printStackTrace(); |
Alexandre Lision | dd6623b | 2013-12-02 16:23:40 -0500 | [diff] [blame] | 113 | } catch (Exception e1) { |
| 114 | |
Alexandre Lision | 3e2a1d0 | 2013-11-19 17:23:00 -0500 | [diff] [blame] | 115 | } |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 116 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onDetach() { |
| 121 | super.onDetach(); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 122 | mCallbacks = sDummyCallbacks; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onCreate(Bundle savedInstanceState) { |
| 127 | super.onCreate(savedInstanceState); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 128 | accountReceiver = new AccountsReceiver(this); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 129 | } |
| 130 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 131 | 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); |
Alexandre Lision | ab0cc9f | 2013-12-12 11:27:25 -0500 | [diff] [blame] | 138 | getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 139 | |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void onPause() { |
| 144 | super.onPause(); |
| 145 | getActivity().unregisterReceiver(accountReceiver); |
| 146 | } |
| 147 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 148 | @Override |
| 149 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 150 | View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false); |
| 151 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 152 | ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 153 | R.array.menu_items_param)); |
Alexandre Lision | fc9b4e7 | 2013-12-13 14:55:25 -0500 | [diff] [blame] | 154 | sections = (ListView) inflatedView.findViewById(R.id.listView); |
| 155 | sections.setAdapter(paramAdapter); |
| 156 | backToHome(); |
| 157 | sections.setOnItemClickListener(new OnItemClickListener() { |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 158 | |
| 159 | @Override |
Alexandre Lision | 658999c | 2013-12-13 14:42:45 -0500 | [diff] [blame] | 160 | public void onItemClick(AdapterView<?> arg0, View selected, int pos, long arg3) { |
Alexandre Lision | 269e604 | 2013-12-11 16:15:13 -0500 | [diff] [blame] | 161 | mCallbacks.onSectionSelected(pos); |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 162 | } |
| 163 | }); |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 164 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 165 | spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection); |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 166 | mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>()); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 167 | spinnerAccounts.setAdapter(mAccountAdapter); |
| 168 | spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() { |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 169 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 170 | @Override |
| 171 | public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 172 | mAccountAdapter.setSelectedAccount(pos); |
Alexandre Lision | 7d05833 | 2013-12-10 17:43:52 -0500 | [diff] [blame] | 173 | view.findViewById(R.id.account_selected).setVisibility(View.GONE); |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 174 | try { |
| 175 | mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder()); |
| 176 | } catch (RemoteException e) { |
| 177 | e.printStackTrace(); |
| 178 | } |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 179 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 180 | |
| 181 | @Override |
| 182 | public void onNothingSelected(AdapterView<?> arg0) { |
Alexandre Lision | 80a9916 | 2013-09-13 14:19:23 -0400 | [diff] [blame] | 183 | mAccountAdapter.setSelectedAccount(-1); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 184 | } |
| 185 | }); |
| 186 | |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 187 | CallContact user = CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()); |
| 188 | new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), user).run(); |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 189 | |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 190 | ((TextView) inflatedView.findViewById(R.id.user_name)).setText(user.getmDisplayName()); |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 191 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 192 | return inflatedView; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onStart() { |
| 197 | super.onStart(); |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 198 | } |
| 199 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 200 | public Account getSelectedAccount() { |
| 201 | return mAccountAdapter.getSelectedAccount(); |
| 202 | } |
| 203 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 204 | public void updateAllAccounts() { |
| 205 | if (getActivity() != null) |
Alexandre Lision | d5934b5 | 2014-01-07 17:17:37 -0500 | [diff] [blame] | 206 | getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | public void updateAccount(Intent accountState) { |
| 210 | if (mAccountAdapter != null) |
| 211 | mAccountAdapter.updateAccount(accountState); |
| 212 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 213 | |
Alexandre Lision | 80a9916 | 2013-09-13 14:19:23 -0400 | [diff] [blame] | 214 | @Override |
| 215 | public void accountsChanged() { |
| 216 | updateAllAccounts(); |
| 217 | |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void accountStateChanged(Intent accountState) { |
| 222 | updateAccount(accountState); |
| 223 | |
| 224 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 225 | |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 226 | @Override |
| 227 | public android.support.v4.content.Loader<Bundle> onCreateLoader(int arg0, Bundle arg1) { |
Alexandre Lision | 658999c | 2013-12-13 14:42:45 -0500 | [diff] [blame] | 228 | AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService()); |
| 229 | l.forceLoad(); |
| 230 | return l; |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onLoadFinished(android.support.v4.content.Loader<Bundle> arg0, Bundle bun) { |
Alexandre Lision | 658999c | 2013-12-13 14:42:45 -0500 | [diff] [blame] | 235 | mAccountAdapter.removeAll(); |
| 236 | ArrayList<Account> accounts = bun.getParcelableArrayList(AccountsLoader.ACCOUNTS); |
| 237 | mAccountAdapter.addAll(accounts); |
| 238 | |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void onLoaderReset(android.support.v4.content.Loader<Bundle> arg0) { |
| 243 | // TODO Stub de la méthode généré automatiquement |
Alexandre Lision | 658999c | 2013-12-13 14:42:45 -0500 | [diff] [blame] | 244 | |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Alexandre Lision | fc9b4e7 | 2013-12-13 14:55:25 -0500 | [diff] [blame] | 247 | public void backToHome() { |
| 248 | sections.setItemChecked(0, true); |
| 249 | } |
| 250 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 251 | } |