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 | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 31 | package com.savoirfairelinux.sflphone.fragments; |
| 32 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | import java.util.HashMap; |
| 35 | |
| 36 | import android.app.Activity; |
| 37 | import android.app.ListFragment; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 38 | import android.app.LoaderManager.LoaderCallbacks; |
| 39 | import android.content.Loader; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 40 | import android.os.Bundle; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 41 | import android.os.RemoteException; |
| 42 | import android.util.Log; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 43 | import android.view.LayoutInflater; |
| 44 | import android.view.View; |
| 45 | import android.view.ViewGroup; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 46 | import android.widget.AdapterView; |
| 47 | import android.widget.ListView; |
| 48 | import android.widget.AdapterView.OnItemClickListener; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 49 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 50 | import com.savoirfairelinux.sflphone.R; |
| 51 | import com.savoirfairelinux.sflphone.adapters.HistoryAdapter; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 52 | import com.savoirfairelinux.sflphone.loaders.HistoryLoader; |
| 53 | import com.savoirfairelinux.sflphone.loaders.LoaderConstants; |
| 54 | import com.savoirfairelinux.sflphone.model.CallContact; |
| 55 | import com.savoirfairelinux.sflphone.model.HistoryEntry; |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 56 | import com.savoirfairelinux.sflphone.model.SipCall; |
| 57 | import com.savoirfairelinux.sflphone.service.ISipService; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 58 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 59 | public class HistoryFragment extends ListFragment implements LoaderCallbacks<ArrayList<HistoryEntry>> { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 60 | |
| 61 | private static final String TAG = HistoryFragment.class.getSimpleName(); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 62 | public static final String ARG_SECTION_NUMBER = "section_number"; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 63 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 64 | HistoryAdapter mAdapter; |
| 65 | private Callbacks mCallbacks = sDummyCallbacks; |
| 66 | /** |
| 67 | * A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. |
| 68 | */ |
| 69 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 70 | @Override |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame^] | 71 | public void onCallDialed(String to) { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public ISipService getService() { |
| 76 | Log.i(TAG, "Dummy"); |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | public interface Callbacks { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame^] | 83 | public void onCallDialed(String to); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 84 | |
| 85 | public ISipService getService(); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onAttach(Activity activity) { |
| 91 | super.onAttach(activity); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 92 | |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 93 | if (!(activity instanceof Callbacks)) { |
| 94 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 95 | } |
| 96 | |
| 97 | mCallbacks = (Callbacks) activity; |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 98 | getLoaderManager().initLoader(LoaderConstants.HISTORY_LOADER, null, this); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void onDetach() { |
| 103 | super.onDetach(); |
| 104 | mCallbacks = sDummyCallbacks; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onCreate(Bundle savedInstanceState) { |
| 109 | super.onCreate(savedInstanceState); |
alision | b176388 | 2013-06-18 17:30:51 -0400 | [diff] [blame] | 110 | getLoaderManager().restartLoader(LoaderConstants.HISTORY_LOADER, null, this); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 111 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 112 | |
| 113 | @Override |
| 114 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 115 | View inflatedView = inflater.inflate(R.layout.frag_history, parent, false); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 116 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 117 | ((ListView) inflatedView.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener() { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
| 121 | mAdapter.getItem(pos); |
| 122 | } |
| 123 | }); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 124 | return inflatedView; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onStart() { |
| 129 | super.onStart(); |
| 130 | Log.w(TAG, "onStart"); |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 131 | } |
| 132 | |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 133 | public void makeNewCall(int position) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame^] | 134 | mCallbacks.onCallDialed(mAdapter.getItem(position).getNumber()); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 135 | } |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 136 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 137 | @Override |
| 138 | public Loader<ArrayList<HistoryEntry>> onCreateLoader(int id, Bundle args) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 139 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 140 | HistoryLoader loader = new HistoryLoader(getActivity(), mCallbacks.getService()); |
| 141 | loader.forceLoad(); |
| 142 | return loader; |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 143 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onLoadFinished(Loader<ArrayList<HistoryEntry>> arg0, ArrayList<HistoryEntry> history) { |
| 148 | mAdapter = new HistoryAdapter(this, history); |
| 149 | getListView().setAdapter(mAdapter); |
| 150 | mAdapter.notifyDataSetChanged(); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void onLoaderReset(Loader<ArrayList<HistoryEntry>> arg0) { |
| 156 | // TODO Auto-generated method stub |
alision | e2a38e1 | 2013-04-25 14:20:20 -0400 | [diff] [blame] | 157 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 158 | } |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 159 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 160 | } |