alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
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.adapters.MenuAdapter; |
| 39 | import org.sflphone.client.ActivityHolder; |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 40 | import org.sflphone.client.HomeActivity; |
| 41 | import org.sflphone.client.SettingsActivity; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 42 | import org.sflphone.interfaces.AccountsInterface; |
| 43 | import org.sflphone.loaders.AccountsLoader; |
| 44 | import org.sflphone.loaders.LoaderConstants; |
| 45 | import org.sflphone.model.Account; |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 46 | import org.sflphone.model.CallContact; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 47 | import org.sflphone.receivers.AccountsReceiver; |
| 48 | import org.sflphone.service.ConfigurationManagerCallback; |
| 49 | import org.sflphone.service.ISipService; |
| 50 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 51 | import android.app.Activity; |
| 52 | import android.app.Fragment; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 53 | import android.app.LoaderManager.LoaderCallbacks; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 54 | import android.content.Intent; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 55 | import android.content.IntentFilter; |
| 56 | import android.content.Loader; |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 57 | import android.database.Cursor; |
| 58 | import android.net.Uri; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 59 | import android.os.Bundle; |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 60 | import android.os.RemoteException; |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 61 | import android.provider.ContactsContract.Profile; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 62 | import android.view.LayoutInflater; |
| 63 | import android.view.View; |
| 64 | import android.view.ViewGroup; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 65 | import android.widget.AdapterView; |
| 66 | import android.widget.AdapterView.OnItemClickListener; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 67 | import android.widget.AdapterView.OnItemSelectedListener; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 68 | import android.widget.ArrayAdapter; |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 69 | import android.widget.ImageView; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 70 | import android.widget.ListView; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 71 | import android.widget.RadioButton; |
| 72 | import android.widget.Spinner; |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 73 | import android.widget.TextView; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 74 | |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 75 | public class MenuFragment extends Fragment implements LoaderCallbacks<Bundle>, AccountsInterface { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 76 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 77 | private static final String TAG = MenuFragment.class.getSimpleName(); |
| 78 | public static final String ARG_SECTION_NUMBER = "section_number"; |
| 79 | |
| 80 | MenuAdapter mAdapter; |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 81 | 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] | 82 | AccountSelectionAdapter mAccountAdapter; |
| 83 | private Spinner spinnerAccounts; |
| 84 | AccountsReceiver accountReceiver; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 85 | private Callbacks mCallbacks = sDummyCallbacks; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 86 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 87 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 88 | |
| 89 | @Override |
| 90 | public ISipService getService() { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 91 | return null; |
| 92 | } |
| 93 | }; |
| 94 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 95 | public interface Callbacks { |
| 96 | |
| 97 | public ISipService getService(); |
| 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; |
| 109 | getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 110 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onDetach() { |
| 115 | super.onDetach(); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 116 | mCallbacks = sDummyCallbacks; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onCreate(Bundle savedInstanceState) { |
| 121 | super.onCreate(savedInstanceState); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 122 | |
| 123 | mAdapter = new MenuAdapter(getActivity()); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 124 | accountReceiver = new AccountsReceiver(this); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 125 | |
| 126 | String[] categories = getResources().getStringArray(R.array.menu_categories); |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 127 | // ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 128 | // R.array.menu_items_param)); |
| 129 | // ArrayAdapter<String> helpAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 130 | // R.array.menu_items_help)); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 131 | |
| 132 | // Add Sections |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 133 | // mAdapter.addSection(categories[0], paramAdapter); |
| 134 | // mAdapter.addSection(categories[1], helpAdapter); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 135 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 136 | } |
| 137 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 138 | public void onResume() { |
| 139 | super.onResume(); |
| 140 | |
| 141 | IntentFilter intentFilter2 = new IntentFilter(); |
| 142 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED); |
| 143 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED); |
| 144 | getActivity().registerReceiver(accountReceiver, intentFilter2); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onPause() { |
| 150 | super.onPause(); |
| 151 | getActivity().unregisterReceiver(accountReceiver); |
| 152 | } |
| 153 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 154 | @Override |
| 155 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 156 | View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false); |
| 157 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 158 | ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 159 | R.array.menu_items_param)); |
| 160 | ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(paramAdapter); |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 161 | ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() { |
| 162 | |
| 163 | @Override |
| 164 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 165 | |
alision | 96ff9b7 | 2013-07-05 10:59:16 -0400 | [diff] [blame] | 166 | Intent in = new Intent(); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 167 | switch (pos) { |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 168 | case 0: |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 169 | in.setClass(getActivity(), SettingsActivity.class); |
| 170 | getActivity().startActivityForResult(in, HomeActivity.REQUEST_CODE_PREFERENCES); |
alision | 2424b51 | 2013-07-05 10:05:38 -0400 | [diff] [blame] | 171 | break; |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 172 | case 1: |
alision | 5de9178 | 2013-07-10 10:47:30 -0400 | [diff] [blame] | 173 | in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_ABOUT); |
alision | 96ff9b7 | 2013-07-05 10:59:16 -0400 | [diff] [blame] | 174 | in.setClass(getActivity(), ActivityHolder.class); |
| 175 | getActivity().startActivity(in); |
alision | 2424b51 | 2013-07-05 10:05:38 -0400 | [diff] [blame] | 176 | break; |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 177 | } |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 178 | } |
| 179 | }); |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 180 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 181 | spinnerAccounts = (Spinner) inflatedView.findViewById(R.id.account_selection); |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 182 | mAccountAdapter = new AccountSelectionAdapter(getActivity(), new ArrayList<Account>()); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 183 | spinnerAccounts.setAdapter(mAccountAdapter); |
| 184 | spinnerAccounts.setOnItemSelectedListener(new OnItemSelectedListener() { |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 185 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 186 | @Override |
| 187 | public void onItemSelected(AdapterView<?> arg0, View view, int pos, long arg3) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 188 | if (null != view) { |
| 189 | ((RadioButton) view.findViewById(R.id.account_checked)).toggle(); |
| 190 | } |
| 191 | mAccountAdapter.setSelectedAccount(pos); |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 192 | try { |
| 193 | mCallbacks.getService().setAccountOrder(mAccountAdapter.getAccountOrder()); |
| 194 | } catch (RemoteException e) { |
| 195 | e.printStackTrace(); |
| 196 | } |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 197 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 198 | |
| 199 | @Override |
| 200 | public void onNothingSelected(AdapterView<?> arg0) { |
Alexandre Lision | 80a9916 | 2013-09-13 14:19:23 -0400 | [diff] [blame] | 201 | mAccountAdapter.setSelectedAccount(-1); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 202 | } |
| 203 | }); |
| 204 | |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 205 | CallContact user = CallContact.ContactBuilder.buildUserContact(getActivity().getContentResolver()); |
| 206 | new ContactPictureTask(getActivity(), (ImageView) inflatedView.findViewById(R.id.user_photo), user).run(); |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 207 | |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 208 | ((TextView) inflatedView.findViewById(R.id.user_name)).setText(user.getmDisplayName()); |
Alexandre Lision | fd3fcd9 | 2013-10-25 15:23:52 -0400 | [diff] [blame] | 209 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 210 | return inflatedView; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onStart() { |
| 215 | super.onStart(); |
| 216 | |
| 217 | } |
| 218 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 219 | public Account getSelectedAccount() { |
| 220 | return mAccountAdapter.getSelectedAccount(); |
| 221 | } |
| 222 | |
| 223 | @Override |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 224 | public Loader<Bundle> onCreateLoader(int id, Bundle args) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 225 | AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService()); |
| 226 | l.forceLoad(); |
| 227 | return l; |
| 228 | } |
| 229 | |
| 230 | @Override |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 231 | public void onLoadFinished(Loader<Bundle> loader, Bundle bun) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 232 | mAccountAdapter.removeAll(); |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 233 | ArrayList<Account> accounts = bun.getParcelableArrayList(AccountsLoader.ACCOUNTS); |
| 234 | mAccountAdapter.addAll(accounts); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 235 | |
| 236 | } |
| 237 | |
| 238 | @Override |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 239 | public void onLoaderReset(Loader<Bundle> arg0) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 240 | // TODO Auto-generated method stub |
| 241 | |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Called by activity to pass a reference to sipservice to Fragment. |
| 246 | * |
| 247 | * @param isip |
| 248 | */ |
| 249 | public void onServiceSipBinded(ISipService isip) { |
| 250 | |
| 251 | } |
| 252 | |
| 253 | public void updateAllAccounts() { |
| 254 | if (getActivity() != null) |
| 255 | getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | public void updateAccount(Intent accountState) { |
| 259 | if (mAccountAdapter != null) |
| 260 | mAccountAdapter.updateAccount(accountState); |
| 261 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 262 | |
Alexandre Lision | 80a9916 | 2013-09-13 14:19:23 -0400 | [diff] [blame] | 263 | @Override |
| 264 | public void accountsChanged() { |
| 265 | updateAllAccounts(); |
| 266 | |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void accountStateChanged(Intent accountState) { |
| 271 | updateAccount(accountState); |
| 272 | |
| 273 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 274 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 275 | } |