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