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