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