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; |
| 37 | import org.sflphone.adapters.MenuAdapter; |
| 38 | import org.sflphone.client.ActivityHolder; |
| 39 | import org.sflphone.client.SFLPhoneHomeActivity; |
| 40 | import org.sflphone.client.SFLPhonePreferenceActivity; |
| 41 | import org.sflphone.interfaces.AccountsInterface; |
| 42 | import org.sflphone.loaders.AccountsLoader; |
| 43 | import org.sflphone.loaders.LoaderConstants; |
| 44 | import org.sflphone.model.Account; |
| 45 | import org.sflphone.receivers.AccountsReceiver; |
| 46 | import org.sflphone.service.ConfigurationManagerCallback; |
| 47 | import org.sflphone.service.ISipService; |
| 48 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 49 | import android.app.Activity; |
| 50 | import android.app.Fragment; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 51 | import android.app.LoaderManager.LoaderCallbacks; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 52 | import android.content.Intent; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 53 | import android.content.IntentFilter; |
| 54 | import android.content.Loader; |
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 | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 57 | import android.provider.ContactsContract.Profile; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 58 | import android.util.Log; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 59 | import android.view.LayoutInflater; |
| 60 | import android.view.View; |
| 61 | import android.view.ViewGroup; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 62 | import android.widget.AdapterView; |
| 63 | import android.widget.AdapterView.OnItemClickListener; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 64 | import android.widget.AdapterView.OnItemSelectedListener; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 65 | import android.widget.ArrayAdapter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 66 | import android.widget.ListView; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 67 | import android.widget.RadioButton; |
| 68 | import android.widget.Spinner; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 69 | |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 70 | public class MenuFragment extends Fragment implements LoaderCallbacks<Bundle>, AccountsInterface { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 71 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 72 | private static final String TAG = MenuFragment.class.getSimpleName(); |
| 73 | public static final String ARG_SECTION_NUMBER = "section_number"; |
| 74 | |
| 75 | MenuAdapter mAdapter; |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 76 | String[] mProjection = new String[] { Profile._ID, Profile.DISPLAY_NAME_PRIMARY, Profile.LOOKUP_KEY, Profile.PHOTO_THUMBNAIL_URI }; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 77 | AccountSelectionAdapter mAccountAdapter; |
| 78 | private Spinner spinnerAccounts; |
| 79 | AccountsReceiver accountReceiver; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 80 | private Callbacks mCallbacks = sDummyCallbacks; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 81 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 82 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 83 | |
| 84 | @Override |
| 85 | public ISipService getService() { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 86 | return null; |
| 87 | } |
| 88 | }; |
| 89 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 90 | public interface Callbacks { |
| 91 | |
| 92 | public ISipService getService(); |
| 93 | |
| 94 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 95 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 96 | @Override |
| 97 | public void onAttach(Activity activity) { |
| 98 | super.onAttach(activity); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 99 | if (!(activity instanceof Callbacks)) { |
| 100 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 101 | } |
| 102 | |
| 103 | mCallbacks = (Callbacks) activity; |
| 104 | getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 105 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onDetach() { |
| 110 | super.onDetach(); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 111 | mCallbacks = sDummyCallbacks; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onCreate(Bundle savedInstanceState) { |
| 116 | super.onCreate(savedInstanceState); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 117 | |
| 118 | mAdapter = new MenuAdapter(getActivity()); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 119 | accountReceiver = new AccountsReceiver(this); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 120 | |
| 121 | String[] categories = getResources().getStringArray(R.array.menu_categories); |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 122 | // ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 123 | // R.array.menu_items_param)); |
| 124 | // ArrayAdapter<String> helpAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 125 | // R.array.menu_items_help)); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 126 | |
| 127 | // Add Sections |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 128 | //mAdapter.addSection(categories[0], paramAdapter); |
| 129 | //mAdapter.addSection(categories[1], helpAdapter); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 130 | |
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 | |
| 136 | IntentFilter intentFilter2 = new IntentFilter(); |
| 137 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED); |
| 138 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED); |
| 139 | getActivity().registerReceiver(accountReceiver, intentFilter2); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onPause() { |
| 145 | super.onPause(); |
| 146 | getActivity().unregisterReceiver(accountReceiver); |
| 147 | } |
| 148 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 149 | @Override |
| 150 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 151 | View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false); |
| 152 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 153 | ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 154 | R.array.menu_items_param)); |
| 155 | ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(paramAdapter); |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 156 | ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() { |
| 157 | |
| 158 | @Override |
| 159 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 160 | |
alision | 96ff9b7 | 2013-07-05 10:59:16 -0400 | [diff] [blame] | 161 | Intent in = new Intent(); |
Alexandre Lision | 6d75d06 | 2013-09-13 14:18:34 -0400 | [diff] [blame] | 162 | switch (pos) { |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 163 | case 0: |
alision | 96ff9b7 | 2013-07-05 10:59:16 -0400 | [diff] [blame] | 164 | in.setClass(getActivity(), SFLPhonePreferenceActivity.class); |
| 165 | getActivity().startActivityForResult(in, SFLPhoneHomeActivity.REQUEST_CODE_PREFERENCES); |
alision | 2424b51 | 2013-07-05 10:05:38 -0400 | [diff] [blame] | 166 | break; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 167 | // case 3: |
| 168 | // in.putExtra("ActivityHolder.args", ActivityHolder.args.FRAG_GESTURES); |
| 169 | // in.setClass(getActivity(), ActivityHolder.class); |
| 170 | // getActivity().startActivity(in); |
| 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 | |
| 205 | // Cursor mProfileCursor = getActivity().getContentResolver().query(Profile.CONTENT_URI, mProjection, null, null, null); |
| 206 | // |
| 207 | // if (mProfileCursor.getCount() > 0) { |
| 208 | // mProfileCursor.moveToFirst(); |
| 209 | // String photo_uri = mProfileCursor.getString(mProfileCursor.getColumnIndex(Profile.PHOTO_THUMBNAIL_URI)); |
| 210 | // if (photo_uri != null) { |
| 211 | // ((ImageView) inflatedView.findViewById(R.id.user_photo)).setImageURI(Uri.parse(photo_uri)); |
| 212 | // } |
| 213 | // ((TextView) inflatedView.findViewById(R.id.user_name)).setText(mProfileCursor.getString(mProfileCursor |
| 214 | // .getColumnIndex(Profile.DISPLAY_NAME_PRIMARY))); |
| 215 | // mProfileCursor.close(); |
| 216 | // } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 217 | return inflatedView; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void onStart() { |
| 222 | super.onStart(); |
| 223 | |
| 224 | } |
| 225 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 226 | public Account getSelectedAccount() { |
| 227 | return mAccountAdapter.getSelectedAccount(); |
| 228 | } |
| 229 | |
| 230 | @Override |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 231 | public Loader<Bundle> onCreateLoader(int id, Bundle args) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 232 | AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService()); |
| 233 | l.forceLoad(); |
| 234 | return l; |
| 235 | } |
| 236 | |
| 237 | @Override |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 238 | public void onLoadFinished(Loader<Bundle> loader, Bundle bun) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 239 | mAccountAdapter.removeAll(); |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 240 | ArrayList<Account> accounts = bun.getParcelableArrayList(AccountsLoader.ACCOUNTS); |
| 241 | mAccountAdapter.addAll(accounts); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 242 | |
| 243 | } |
| 244 | |
| 245 | @Override |
Alexandre Lision | d2e9e06 | 2013-10-21 16:27:33 -0400 | [diff] [blame] | 246 | public void onLoaderReset(Loader<Bundle> arg0) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 247 | // TODO Auto-generated method stub |
| 248 | |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Called by activity to pass a reference to sipservice to Fragment. |
| 253 | * |
| 254 | * @param isip |
| 255 | */ |
| 256 | public void onServiceSipBinded(ISipService isip) { |
| 257 | |
| 258 | } |
| 259 | |
| 260 | public void updateAllAccounts() { |
| 261 | if (getActivity() != null) |
| 262 | getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | public void updateAccount(Intent accountState) { |
| 266 | if (mAccountAdapter != null) |
| 267 | mAccountAdapter.updateAccount(accountState); |
| 268 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 269 | |
Alexandre Lision | 80a9916 | 2013-09-13 14:19:23 -0400 | [diff] [blame] | 270 | @Override |
| 271 | public void accountsChanged() { |
| 272 | updateAllAccounts(); |
| 273 | |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void accountStateChanged(Intent accountState) { |
| 278 | updateAccount(accountState); |
| 279 | |
| 280 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 281 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 282 | } |