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 | */ |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 31 | package com.savoirfairelinux.sflphone.fragments; |
| 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 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 35 | import android.app.Activity; |
| 36 | import android.app.Fragment; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 37 | import android.app.LoaderManager.LoaderCallbacks; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 38 | import android.content.Intent; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 39 | import android.content.IntentFilter; |
| 40 | import android.content.Loader; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 41 | import android.os.Bundle; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 42 | import android.os.RemoteException; |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 43 | import android.provider.ContactsContract.Profile; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 44 | import android.util.Log; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
| 46 | import android.view.View; |
| 47 | import android.view.ViewGroup; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 48 | import android.widget.AdapterView; |
| 49 | import android.widget.AdapterView.OnItemClickListener; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 50 | import android.widget.AdapterView.OnItemSelectedListener; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 51 | import android.widget.ArrayAdapter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 52 | import android.widget.ListView; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 53 | import android.widget.RadioButton; |
| 54 | import android.widget.Spinner; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 55 | |
| 56 | import com.savoirfairelinux.sflphone.R; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 57 | import com.savoirfairelinux.sflphone.adapters.AccountSelectionAdapter; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 58 | import com.savoirfairelinux.sflphone.adapters.MenuAdapter; |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 59 | import com.savoirfairelinux.sflphone.client.SFLPhoneHomeActivity; |
| 60 | import com.savoirfairelinux.sflphone.client.SFLPhonePreferenceActivity; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 61 | import com.savoirfairelinux.sflphone.interfaces.AccountsInterface; |
| 62 | import com.savoirfairelinux.sflphone.loaders.AccountsLoader; |
| 63 | import com.savoirfairelinux.sflphone.loaders.LoaderConstants; |
| 64 | import com.savoirfairelinux.sflphone.model.Account; |
| 65 | import com.savoirfairelinux.sflphone.receivers.AccountsReceiver; |
| 66 | import com.savoirfairelinux.sflphone.service.CallManagerCallBack; |
| 67 | import com.savoirfairelinux.sflphone.service.ConfigurationManagerCallback; |
| 68 | import com.savoirfairelinux.sflphone.service.ISipService; |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 69 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 70 | public class MenuFragment extends Fragment implements LoaderCallbacks<ArrayList<Account>>, 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; |
| 80 | |
| 81 | private Callbacks mCallbacks = sDummyCallbacks; |
| 82 | // private Spinner spinnerAccounts; |
| 83 | |
| 84 | /** |
| 85 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 86 | */ |
| 87 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 88 | |
| 89 | @Override |
| 90 | public ISipService getService() { |
| 91 | // TODO Auto-generated method stub |
| 92 | return null; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * The Activity calling this fragment has to implement this interface |
| 98 | * |
| 99 | */ |
| 100 | public interface Callbacks { |
| 101 | |
| 102 | public ISipService getService(); |
| 103 | |
| 104 | } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 105 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 106 | @Override |
| 107 | public void onAttach(Activity activity) { |
| 108 | super.onAttach(activity); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 109 | if (!(activity instanceof Callbacks)) { |
| 110 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 111 | } |
| 112 | |
| 113 | mCallbacks = (Callbacks) activity; |
| 114 | getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
| 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(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onCreate(Bundle savedInstanceState) { |
| 126 | super.onCreate(savedInstanceState); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 127 | |
| 128 | mAdapter = new MenuAdapter(getActivity()); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 129 | accountReceiver = new AccountsReceiver(this); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 130 | |
| 131 | String[] categories = getResources().getStringArray(R.array.menu_categories); |
| 132 | ArrayAdapter<String> paramAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 133 | R.array.menu_items_param)); |
| 134 | ArrayAdapter<String> helpAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_menu, getResources().getStringArray( |
| 135 | R.array.menu_items_help)); |
| 136 | |
| 137 | // Add Sections |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 138 | mAdapter.addSection(categories[0], paramAdapter); |
| 139 | mAdapter.addSection(categories[1], helpAdapter); |
| 140 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 141 | } |
| 142 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 143 | public void onResume() { |
| 144 | super.onResume(); |
| 145 | |
| 146 | IntentFilter intentFilter2 = new IntentFilter(); |
| 147 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED); |
| 148 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED); |
| 149 | getActivity().registerReceiver(accountReceiver, intentFilter2); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onPause() { |
| 155 | super.onPause(); |
| 156 | getActivity().unregisterReceiver(accountReceiver); |
| 157 | } |
| 158 | |
| 159 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 160 | @Override |
| 161 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 162 | View inflatedView = inflater.inflate(R.layout.frag_menu, parent, false); |
| 163 | |
| 164 | ((ListView) inflatedView.findViewById(R.id.listView)).setAdapter(mAdapter); |
alision | 2cb9956 | 2013-05-30 17:02:20 -0400 | [diff] [blame] | 165 | ((ListView) inflatedView.findViewById(R.id.listView)).setOnItemClickListener(new OnItemClickListener() { |
| 166 | |
| 167 | @Override |
| 168 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
alision | bd45a82 | 2013-06-06 17:35:35 -0400 | [diff] [blame] | 169 | if (pos == 1 || pos == 2 || pos == 3) { |
| 170 | Intent launchPreferencesIntent = new Intent().setClass(getActivity(), SFLPhonePreferenceActivity.class); |
| 171 | getActivity().startActivityForResult(launchPreferencesIntent, SFLPhoneHomeActivity.REQUEST_CODE_PREFERENCES); |
| 172 | } |
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); |
| 177 | mAccountAdapter = new AccountSelectionAdapter(getActivity(), mCallbacks.getService(), new ArrayList<Account>()); |
| 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) { |
| 183 | // public void onClick(DialogInterface dialog, int which) { |
| 184 | |
| 185 | Log.i(TAG, "Selected Account: " + mAdapter.getItem(pos)); |
| 186 | if (null != view) { |
| 187 | ((RadioButton) view.findViewById(R.id.account_checked)).toggle(); |
| 188 | } |
| 189 | mAccountAdapter.setSelectedAccount(pos); |
| 190 | // accountSelectedNotifyAccountList(mAdapter.getItem(pos)); |
| 191 | // setSelection(cursor.getPosition(),true); |
| 192 | |
alision | 55c36cb | 2013-06-14 14:57:38 -0400 | [diff] [blame] | 193 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 194 | |
| 195 | @Override |
| 196 | public void onNothingSelected(AdapterView<?> arg0) { |
| 197 | // TODO Auto-generated method stub |
| 198 | |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | // Cursor mProfileCursor = getActivity().getContentResolver().query(Profile.CONTENT_URI, mProjection, null, null, null); |
| 203 | // |
| 204 | // if (mProfileCursor.getCount() > 0) { |
| 205 | // mProfileCursor.moveToFirst(); |
| 206 | // String photo_uri = mProfileCursor.getString(mProfileCursor.getColumnIndex(Profile.PHOTO_THUMBNAIL_URI)); |
| 207 | // if (photo_uri != null) { |
| 208 | // ((ImageView) inflatedView.findViewById(R.id.user_photo)).setImageURI(Uri.parse(photo_uri)); |
| 209 | // } |
| 210 | // ((TextView) inflatedView.findViewById(R.id.user_name)).setText(mProfileCursor.getString(mProfileCursor |
| 211 | // .getColumnIndex(Profile.DISPLAY_NAME_PRIMARY))); |
| 212 | // mProfileCursor.close(); |
| 213 | // } |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 214 | return inflatedView; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void onStart() { |
| 219 | super.onStart(); |
| 220 | |
| 221 | } |
| 222 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 223 | public Account getSelectedAccount() { |
| 224 | return mAccountAdapter.getSelectedAccount(); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) { |
| 229 | AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService()); |
| 230 | l.forceLoad(); |
| 231 | return l; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) { |
| 236 | mAccountAdapter.removeAll(); |
| 237 | mAccountAdapter.addAll(results); |
| 238 | |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void onLoaderReset(Loader<ArrayList<Account>> arg0) { |
| 243 | // TODO Auto-generated method stub |
| 244 | |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Called by activity to pass a reference to sipservice to Fragment. |
| 249 | * |
| 250 | * @param isip |
| 251 | */ |
| 252 | public void onServiceSipBinded(ISipService isip) { |
| 253 | |
| 254 | } |
| 255 | |
| 256 | public void updateAllAccounts() { |
| 257 | if (getActivity() != null) |
| 258 | getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
| 259 | |
| 260 | } |
| 261 | |
| 262 | public void updateAccount(Intent accountState) { |
| 263 | if (mAccountAdapter != null) |
| 264 | mAccountAdapter.updateAccount(accountState); |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void accountsChanged() { |
| 269 | updateAllAccounts(); |
| 270 | |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void accountStateChanged(Intent accountState) { |
| 275 | updateAccount(accountState); |
| 276 | |
| 277 | } |
| 278 | |
alision | 9f7a6ec | 2013-05-24 16:26:26 -0400 | [diff] [blame] | 279 | } |