Loïc Siret | 0202fbf | 2017-09-28 16:34:28 -0400 | [diff] [blame] | 1 | /* |
Pierre Duchemin | 2802bfd | 2018-02-21 16:41:32 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2018 Savoir-faire Linux Inc. |
Loïc Siret | 0202fbf | 2017-09-28 16:34:28 -0400 | [diff] [blame] | 3 | * |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 4 | * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> |
| 5 | * |
Loïc Siret | 0202fbf | 2017-09-28 16:34:28 -0400 | [diff] [blame] | 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 | */ |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 20 | package cx.ring.fragments; |
| 21 | |
Pierre Duchemin | 4af6b39 | 2018-02-02 11:23:56 -0500 | [diff] [blame] | 22 | import android.Manifest; |
Adrien Béraud | 1b8ac9f | 2018-05-31 14:34:23 -0400 | [diff] [blame] | 23 | import android.app.DownloadManager; |
Adrien Béraud | ae45ffa | 2018-07-06 16:21:29 -0400 | [diff] [blame] | 24 | import android.content.ClipData; |
Adrien Béraud | 1b8ac9f | 2018-05-31 14:34:23 -0400 | [diff] [blame] | 25 | import android.content.Context; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 26 | import android.content.Intent; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 27 | import android.media.MediaScannerConnection; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 28 | import android.os.Bundle; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 29 | import android.provider.MediaStore; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 30 | import android.support.design.widget.Snackbar; |
Pierre Duchemin | 4af6b39 | 2018-02-02 11:23:56 -0500 | [diff] [blame] | 31 | import android.support.v4.app.ActivityCompat; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 32 | import android.support.v4.content.FileProvider; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 33 | import android.support.v7.app.ActionBar; |
| 34 | import android.support.v7.app.AlertDialog; |
Aline Bonnet | e8e87f6 | 2016-12-26 10:54:37 -0500 | [diff] [blame] | 35 | import android.support.v7.app.AppCompatActivity; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 36 | import android.support.v7.widget.DefaultItemAnimator; |
| 37 | import android.support.v7.widget.LinearLayoutManager; |
| 38 | import android.support.v7.widget.RecyclerView; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 39 | import android.util.Log; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 40 | import android.view.Menu; |
| 41 | import android.view.MenuInflater; |
| 42 | import android.view.MenuItem; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewGroup; |
| 45 | import android.view.inputmethod.EditorInfo; |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 46 | import android.widget.ImageButton; |
| 47 | import android.widget.ProgressBar; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 48 | import android.widget.Spinner; |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 49 | import android.widget.TextView; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 50 | import android.widget.Toast; |
| 51 | |
| 52 | import java.io.File; |
| 53 | import java.io.IOException; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 54 | import java.text.SimpleDateFormat; |
| 55 | import java.util.Date; |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 56 | import java.util.List; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 57 | |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 58 | import butterknife.BindView; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 59 | import butterknife.OnClick; |
| 60 | import butterknife.OnEditorAction; |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 61 | import cx.ring.BuildConfig; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 62 | import cx.ring.R; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 63 | import cx.ring.adapters.ConversationAdapter; |
| 64 | import cx.ring.adapters.NumberAdapter; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 65 | import cx.ring.client.CallActivity; |
Aline Bonnet | 830c4c6 | 2016-12-27 14:02:02 -0500 | [diff] [blame] | 66 | import cx.ring.client.ConversationActivity; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 67 | import cx.ring.client.HomeActivity; |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 68 | import cx.ring.conversation.ConversationPresenter; |
| 69 | import cx.ring.conversation.ConversationView; |
Hadrien De Sousa | 73d6dd7 | 2017-09-07 10:54:51 -0400 | [diff] [blame] | 70 | import cx.ring.dependencyinjection.RingInjectionComponent; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 71 | import cx.ring.model.CallContact; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 72 | import cx.ring.model.Conversation; |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 73 | import cx.ring.model.ConversationElement; |
Adrien Béraud | 2153329 | 2018-03-22 14:11:35 -0400 | [diff] [blame] | 74 | import cx.ring.model.DataTransfer; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 75 | import cx.ring.model.Phone; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 76 | import cx.ring.model.RingError; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 77 | import cx.ring.model.Uri; |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 78 | import cx.ring.mvp.BaseFragment; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 79 | import cx.ring.services.NotificationService; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 80 | import cx.ring.utils.ActionHelper; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 81 | import cx.ring.utils.AndroidFileUtils; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 82 | import cx.ring.utils.ClipboardHelper; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 83 | import cx.ring.utils.ContentUriHandler; |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 84 | import cx.ring.utils.MediaButtonsHelper; |
Adrien Béraud | 9d4a81f | 2018-07-09 01:19:52 -0400 | [diff] [blame^] | 85 | import cx.ring.views.MessageEditText; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 86 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 87 | import static android.app.Activity.RESULT_OK; |
| 88 | |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 89 | public class ConversationFragment extends BaseFragment<ConversationPresenter> implements |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 90 | Conversation.ConversationActionCallback, |
| 91 | ClipboardHelper.ClipboardHelperCallback, |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 92 | MediaButtonsHelper.MediaButtonsHelperCallback, |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 93 | ConversationView { |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 94 | |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 95 | protected static final String TAG = ConversationFragment.class.getSimpleName(); |
| 96 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 97 | public static final int REQ_ADD_CONTACT = 42; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 98 | |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 99 | public static final String KEY_CONTACT_RING_ID = BuildConfig.APPLICATION_ID + "CONTACT_RING_ID"; |
| 100 | public static final String KEY_ACCOUNT_ID = BuildConfig.APPLICATION_ID + "ACCOUNT_ID"; |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 101 | |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 102 | private static final String CONVERSATION_DELETE = "CONVERSATION_DELETE"; |
| 103 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 104 | private static final int REQUEST_CODE_FILE_PICKER = 1000; |
Pierre Duchemin | 4af6b39 | 2018-02-02 11:23:56 -0500 | [diff] [blame] | 105 | private static final int REQUEST_PERMISSION_CAMERA = 1001; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 106 | private static final int REQUEST_CODE_TAKE_PICTURE = 1002; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 107 | |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 108 | @BindView(R.id.msg_input_txt) |
Adrien Béraud | 9d4a81f | 2018-07-09 01:19:52 -0400 | [diff] [blame^] | 109 | protected MessageEditText mMsgEditTxt; |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 110 | |
| 111 | @BindView(R.id.ongoingcall_pane) |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 112 | protected ViewGroup mTopPane; |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 113 | |
| 114 | @BindView(R.id.hist_list) |
| 115 | protected RecyclerView mHistList; |
| 116 | |
| 117 | @BindView(R.id.number_selector) |
| 118 | protected Spinner mNumberSpinner; |
| 119 | |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 120 | @BindView(R.id.pb_data_transfer) |
| 121 | protected ProgressBar pbDataTransfer; |
| 122 | |
| 123 | @BindView(R.id.send_data) |
| 124 | protected ImageButton sendData; |
| 125 | |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 126 | @BindView(R.id.btn_take_picture) |
| 127 | protected ImageButton takePicture; |
| 128 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 129 | @BindView(R.id.cvMessageInput) |
| 130 | protected View mMessageInput; |
| 131 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 132 | @BindView(R.id.unknownContactPrompt) |
| 133 | protected View mUnknownPrompt; |
| 134 | |
| 135 | @BindView(R.id.trustRequestPrompt) |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 136 | protected View mTrustRequestPrompt; |
| 137 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 138 | @BindView(R.id.trustRequestMessageLayout) |
| 139 | protected View mTrustRequestMessageLayout; |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 140 | |
| 141 | @BindView(R.id.tvTrustRequestMessage) |
| 142 | protected TextView mTvTrustRequestMessage; |
| 143 | |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 144 | private AlertDialog mDeleteDialog; |
| 145 | private boolean mDeleteConversation = false; |
| 146 | |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 147 | private MenuItem mAudioCallBtn = null; |
| 148 | private MenuItem mVideoCallBtn = null; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 149 | |
| 150 | private ConversationAdapter mAdapter = null; |
| 151 | private NumberAdapter mNumberAdapter = null; |
| 152 | |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 153 | private File mCurrentPhoto = null; |
| 154 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 155 | private static int getIndex(Spinner spinner, Uri myString) { |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 156 | for (int i = 0, n = spinner.getCount(); i < n; i++) |
| 157 | if (((Phone) spinner.getItemAtPosition(i)).getNumber().equals(myString)) { |
| 158 | return i; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 163 | @Override |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 164 | public void refreshView(final List<ConversationElement> conversation) { |
Adrien Béraud | 20d4221 | 2018-03-02 14:40:38 -0500 | [diff] [blame] | 165 | if (conversation == null) { |
| 166 | return; |
| 167 | } |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 168 | if (mAdapter != null) { |
| 169 | mAdapter.updateDataset(conversation); |
| 170 | } |
| 171 | getActivity().invalidateOptionsMenu(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Aline Bonnet | e8e87f6 | 2016-12-26 10:54:37 -0500 | [diff] [blame] | 174 | @Override |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 175 | public void scrollToEnd() { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 176 | if (mAdapter.getItemCount() > 0) { |
| 177 | mHistList.smoothScrollToPosition(mAdapter.getItemCount() - 1); |
| 178 | } |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Hadrien De Sousa | 73d6dd7 | 2017-09-07 10:54:51 -0400 | [diff] [blame] | 182 | public int getLayout() { |
| 183 | return R.layout.frag_conversation; |
| 184 | } |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 185 | |
Hadrien De Sousa | 73d6dd7 | 2017-09-07 10:54:51 -0400 | [diff] [blame] | 186 | @Override |
| 187 | public void injectFragment(RingInjectionComponent component) { |
| 188 | component.inject(this); |
| 189 | } |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 190 | |
Hadrien De Sousa | 73d6dd7 | 2017-09-07 10:54:51 -0400 | [diff] [blame] | 191 | @Override |
| 192 | public void onViewCreated(View view, Bundle savedInstanceState) { |
| 193 | super.onViewCreated(view, savedInstanceState); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 194 | |
Adrien Béraud | 9d4a81f | 2018-07-09 01:19:52 -0400 | [diff] [blame^] | 195 | mMsgEditTxt.setMediaListener(contentInfo -> { |
| 196 | try { |
| 197 | presenter.sendFile(AndroidFileUtils.getCacheFile(getActivity(), contentInfo.getContentUri())); |
| 198 | contentInfo.releasePermission(); |
| 199 | } catch (IOException e) { |
| 200 | e.printStackTrace(); |
| 201 | } |
| 202 | }); |
| 203 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 204 | if (mTopPane != null) { |
| 205 | mTopPane.setVisibility(View.GONE); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); |
| 209 | mLayoutManager.setStackFromEnd(true); |
| 210 | |
| 211 | if (mHistList != null) { |
| 212 | mHistList.setLayoutManager(mLayoutManager); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 213 | mHistList.setAdapter(null); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 214 | mHistList.setItemAnimator(new DefaultItemAnimator()); |
| 215 | } |
| 216 | |
| 217 | // reload delete conversation state (before rotation) |
| 218 | mDeleteConversation = savedInstanceState != null && savedInstanceState.getBoolean(CONVERSATION_DELETE); |
| 219 | |
| 220 | setHasOptionsMenu(true); |
Alexandre Lision | 62c6978 | 2017-04-04 17:26:44 -0400 | [diff] [blame] | 221 | if (mHistList != null) { |
| 222 | mHistList.setAdapter(mAdapter); |
| 223 | } |
| 224 | |
| 225 | if (mDeleteConversation) { |
| 226 | presenter.deleteAction(); |
Aline Bonnet | e8e87f6 | 2016-12-26 10:54:37 -0500 | [diff] [blame] | 227 | } |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Adrien Béraud | 7c50511 | 2018-03-20 16:03:28 -0400 | [diff] [blame] | 230 | @Override |
| 231 | public boolean onContextItemSelected(MenuItem item) { |
| 232 | if (mAdapter.onContextItemSelected(item)) |
| 233 | return true; |
| 234 | return super.onContextItemSelected(item); |
| 235 | } |
| 236 | |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 237 | @OnClick(R.id.msg_send) |
Alexandre Lision | 66a9f9a | 2017-02-26 20:28:27 -0500 | [diff] [blame] | 238 | public void sendMessageText() { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 239 | String message = mMsgEditTxt.getText().toString(); |
| 240 | clearMsgEdit(); |
| 241 | presenter.sendTextMessage(message); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 242 | } |
| 243 | |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 244 | @OnClick(R.id.send_data) |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 245 | public void selectFile() { |
| 246 | presenter.selectFile(); |
| 247 | } |
| 248 | |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 249 | private File createImageFile() throws IOException { |
| 250 | // Create an image file name |
| 251 | String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); |
| 252 | String imageFileName = "img_" + timeStamp + "_"; |
| 253 | |
| 254 | // Save a file: path for use with ACTION_VIEW intents |
| 255 | return File.createTempFile(imageFileName, ".jpg", getActivity().getExternalCacheDir()); |
| 256 | } |
| 257 | |
| 258 | @OnClick(R.id.btn_take_picture) |
| 259 | public void takePicture() { |
| 260 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 261 | if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { |
| 262 | |
| 263 | // Create the File where the photo should go |
| 264 | File photoFile = null; |
| 265 | try { |
| 266 | photoFile = createImageFile(); |
| 267 | } catch (IOException ex) { |
| 268 | Log.e(TAG, "takePicture: error creating temporary file", ex); |
| 269 | return; |
| 270 | } |
| 271 | Log.i(TAG, "takePicture: trying to save to " + photoFile); |
| 272 | mCurrentPhoto = photoFile; |
| 273 | android.net.Uri photoURI = FileProvider.getUriForFile(getActivity(), |
| 274 | ContentUriHandler.AUTHORITY_FILES, |
| 275 | photoFile); |
| 276 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); |
| 277 | startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PICTURE); |
| 278 | } |
| 279 | } |
| 280 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 281 | @Override |
Pierre Duchemin | 4af6b39 | 2018-02-02 11:23:56 -0500 | [diff] [blame] | 282 | public void askWriteExternalStoragePermission() { |
| 283 | ActivityCompat.requestPermissions(getActivity(), |
| 284 | new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, |
| 285 | REQUEST_PERMISSION_CAMERA); |
| 286 | } |
| 287 | |
| 288 | @Override |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 289 | public void openFilePicker() { |
| 290 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 291 | intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 292 | intent.setType("*/*"); |
| 293 | |
| 294 | startActivityForResult(intent, REQUEST_CODE_FILE_PICKER); |
| 295 | } |
| 296 | |
| 297 | @Override |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 298 | public void onActivityResult(int requestCode, int resultCode, Intent resultData) { |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 299 | Log.w(TAG, "onActivityResult: " + requestCode + " " + resultCode + " " + resultData); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 300 | super.onActivityResult(requestCode, resultCode, resultData); |
| 301 | |
| 302 | if (requestCode == REQUEST_CODE_FILE_PICKER && resultCode == RESULT_OK) { |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 303 | if (resultData == null) { |
| 304 | return; |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 305 | } |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 306 | android.net.Uri uri = resultData.getData(); |
| 307 | if (uri == null) { |
| 308 | return; |
| 309 | } |
| 310 | setLoading(true); |
| 311 | new Thread(() -> { |
| 312 | try { |
| 313 | File cacheFile = AndroidFileUtils.getCacheFile(getActivity(), uri); |
| 314 | presenter.sendFile(cacheFile); |
| 315 | } catch (IOException e) { |
| 316 | Log.e(TAG, "onActivityResult: not able to create cache file"); |
| 317 | getActivity().runOnUiThread(() -> displayErrorToast(RingError.INVALID_FILE)); |
| 318 | } |
| 319 | getActivity().runOnUiThread(() -> setLoading(false)); |
| 320 | }).start(); |
| 321 | } else if (requestCode == REQUEST_CODE_TAKE_PICTURE) { |
| 322 | if (resultCode != RESULT_OK) { |
| 323 | mCurrentPhoto = null; |
| 324 | return; |
| 325 | } |
| 326 | Log.w(TAG, "onActivityResult: mCurrentPhoto "+mCurrentPhoto.getAbsolutePath() + " " + mCurrentPhoto.exists() + " " + mCurrentPhoto.length()); |
| 327 | if (mCurrentPhoto == null || !mCurrentPhoto.exists() || mCurrentPhoto.length() == 0) { |
| 328 | Toast.makeText(getActivity(), "Can't find picture", Toast.LENGTH_SHORT).show(); |
| 329 | } |
| 330 | setLoading(true); |
| 331 | new Thread(() -> { |
| 332 | File file = mCurrentPhoto; |
| 333 | mCurrentPhoto = null; |
| 334 | presenter.sendFile(file); |
| 335 | getActivity().runOnUiThread(() -> setLoading(false)); |
| 336 | }).start(); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | @Override |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 341 | public void writeCacheFile(String cacheFilename) { |
Adrien Béraud | 20d4221 | 2018-03-02 14:40:38 -0500 | [diff] [blame] | 342 | // todo use rx + move to presenter |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 343 | File cacheFile = new File(getActivity().getCacheDir(), cacheFilename); |
| 344 | try { |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 345 | String finalFilePath = AndroidFileUtils.writeCacheFileToExtStorage(getActivity(), android.net.Uri.fromFile(cacheFile), cacheFile.getName()); |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 346 | |
| 347 | // Tell the media scanner about the new file so that it is immediately available to the user |
| 348 | MediaScannerConnection.scanFile(getActivity(), new String[]{finalFilePath}, null, null); |
| 349 | |
| 350 | getActivity().runOnUiThread(() -> Toast.makeText(getActivity(), getActivity().getString(R.string.file_saved_in, finalFilePath), Toast.LENGTH_LONG).show()); |
| 351 | } catch (IOException e) { |
| 352 | Log.e(TAG, "writeCacheFile: ", e); |
| 353 | getActivity().runOnUiThread(() -> displayErrorToast(RingError.NOT_ABLE_TO_WRITE_FILE)); |
| 354 | } |
| 355 | } |
| 356 | |
Adrien Béraud | 2153329 | 2018-03-22 14:11:35 -0400 | [diff] [blame] | 357 | @Override |
| 358 | public void updateTransfer(DataTransfer transfer) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 359 | mAdapter.updateTransfer(transfer); |
| 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public void addElement(ConversationElement element) { |
| 364 | mAdapter.add(element); |
| 365 | scrollToEnd(); |
| 366 | } |
| 367 | @Override |
| 368 | public void updateElement(ConversationElement element) { |
| 369 | mAdapter.update(element); |
| 370 | } |
| 371 | @Override |
| 372 | public void removeElement(ConversationElement element) { |
| 373 | mAdapter.remove(element); |
Adrien Béraud | 2153329 | 2018-03-22 14:11:35 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Adrien Béraud | fcf457f | 2018-04-03 17:46:34 -0400 | [diff] [blame] | 376 | @Override |
| 377 | public void shareFile(File path) { |
| 378 | android.net.Uri fileUri = null; |
| 379 | try { |
| 380 | fileUri = FileProvider.getUriForFile( |
| 381 | getActivity(), |
| 382 | ContentUriHandler.AUTHORITY_FILES, |
| 383 | path); |
| 384 | } catch (IllegalArgumentException e) { |
| 385 | Log.e("File Selector", |
| 386 | "The selected file can't be shared: " + |
| 387 | path.getName()); |
| 388 | } |
| 389 | if (fileUri != null) { |
| 390 | Intent sendIntent = new Intent(); |
| 391 | sendIntent.setAction(Intent.ACTION_SEND); |
| 392 | sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 393 | String type = getActivity().getContentResolver().getType(fileUri); |
| 394 | sendIntent.setDataAndType(fileUri, type); |
| 395 | sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri); |
| 396 | startActivity(Intent.createChooser(sendIntent, null)); |
| 397 | } |
| 398 | } |
| 399 | |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 400 | @OnEditorAction(R.id.msg_input_txt) |
Alexandre Lision | 66a9f9a | 2017-02-26 20:28:27 -0500 | [diff] [blame] | 401 | public boolean actionSendMsgText(int actionId) { |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 402 | switch (actionId) { |
| 403 | case EditorInfo.IME_ACTION_SEND: |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 404 | sendMessageText(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 405 | return true; |
| 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | @OnClick(R.id.ongoingcall_pane) |
Alexandre Lision | 66a9f9a | 2017-02-26 20:28:27 -0500 | [diff] [blame] | 411 | public void onClick() { |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 412 | presenter.clickOnGoingPane(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public void onPause() { |
| 417 | super.onPause(); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 418 | presenter.pause(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void onResume() { |
| 423 | super.onResume(); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 424 | presenter.resume(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public void onDestroy() { |
| 429 | if (mDeleteConversation) { |
| 430 | mDeleteDialog.dismiss(); |
| 431 | } |
| 432 | |
| 433 | super.onDestroy(); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public void onSaveInstanceState(Bundle outState) { |
| 438 | super.onSaveInstanceState(outState); |
| 439 | |
| 440 | // persist the delete popup state in case of Activity rotation |
| 441 | mDeleteConversation = mDeleteDialog != null && mDeleteDialog.isShowing(); |
| 442 | outState.putBoolean(CONVERSATION_DELETE, mDeleteConversation); |
| 443 | } |
| 444 | |
| 445 | @Override |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 446 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 447 | inflater.inflate(R.menu.conversation_actions, menu); |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 448 | mAudioCallBtn = menu.findItem(R.id.conv_action_audiocall); |
| 449 | mVideoCallBtn = menu.findItem(R.id.conv_action_videocall); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public boolean onOptionsItemSelected(MenuItem item) { |
| 454 | switch (item.getItemId()) { |
| 455 | case android.R.id.home: |
| 456 | startActivity(new Intent(getActivity(), HomeActivity.class)); |
| 457 | return true; |
| 458 | case R.id.conv_action_audiocall: |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 459 | presenter.callWithAudioOnly(true); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 460 | return true; |
| 461 | case R.id.conv_action_videocall: |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 462 | presenter.callWithAudioOnly(false); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 463 | return true; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 464 | case R.id.menuitem_delete: |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 465 | presenter.deleteAction(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 466 | return true; |
| 467 | case R.id.menuitem_copy_content: |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 468 | presenter.copyToClipboard(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 469 | return true; |
Aline Bonnet | 54cf69b | 2017-03-28 12:03:45 -0400 | [diff] [blame] | 470 | case R.id.menuitem_block: |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 471 | presenter.blockContact(); |
Aline Bonnet | 54cf69b | 2017-03-28 12:03:45 -0400 | [diff] [blame] | 472 | return true; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 473 | default: |
| 474 | return super.onOptionsItemSelected(item); |
| 475 | } |
| 476 | } |
| 477 | |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 478 | @Override |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 479 | public void deleteConversation(CallContact callContact) { |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 480 | presenter.deleteConversation(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public void copyContactNumberToClipboard(String contactNumber) { |
| 485 | ClipboardHelper.copyNumberToClipboard(getActivity(), contactNumber, this); |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void clipBoardDidCopyNumber(String copiedNumber) { |
| 490 | View view = getActivity().findViewById(android.R.id.content); |
| 491 | if (view != null) { |
| 492 | String snackbarText = getString(R.string.conversation_action_copied_peer_number_clipboard, |
Alexandre Lision | 6fd56c8 | 2017-01-30 09:05:54 -0500 | [diff] [blame] | 493 | ActionHelper.getShortenedNumber(copiedNumber)); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 494 | Snackbar.make(view, snackbarText, Snackbar.LENGTH_LONG).show(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | @Override |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 499 | protected void initPresenter(ConversationPresenter presenter) { |
| 500 | super.initPresenter(presenter); |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 501 | String contactRingID = getArguments().getString(KEY_CONTACT_RING_ID); |
| 502 | String accountId = getArguments().getString(KEY_ACCOUNT_ID); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 503 | mAdapter = new ConversationAdapter(this, presenter); |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 504 | presenter.init(contactRingID, accountId); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | @Override |
Cyrille Béraud | 35b5a89 | 2017-07-22 17:20:13 -0400 | [diff] [blame] | 508 | public void displayContactName(final CallContact contact) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 509 | ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); |
| 510 | if (actionBar == null) { |
| 511 | return; |
| 512 | } |
| 513 | String displayName = contact.getDisplayName(); |
| 514 | actionBar.setTitle(displayName); |
| 515 | String identity = contact.getRingUsername(); |
| 516 | if (identity != null && !identity.equals(displayName)) { |
| 517 | actionBar.setSubtitle(identity); |
| 518 | } |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void displayOnGoingCallPane(final boolean display) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 523 | mTopPane.setVisibility(display ? View.VISIBLE : View.GONE); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @Override |
Alexandre Lision | 62c6978 | 2017-04-04 17:26:44 -0400 | [diff] [blame] | 527 | public void displayContactPhoto(final byte[] photo) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 528 | mAdapter.setPhoto(photo); |
Alexandre Lision | 62c6978 | 2017-04-04 17:26:44 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | @Override |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 532 | public void displayNumberSpinner(final Conversation conversation, final Uri number) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 533 | mNumberSpinner.setVisibility(View.VISIBLE); |
| 534 | mNumberAdapter = new NumberAdapter(getActivity(), |
| 535 | conversation.getContact(), |
| 536 | false); |
| 537 | mNumberSpinner.setAdapter(mNumberAdapter); |
| 538 | mNumberSpinner.setSelection(getIndex(mNumberSpinner, number)); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | @Override |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 542 | public void displayDeleteDialog(final Conversation conversation) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 543 | mDeleteDialog = ActionHelper.launchDeleteAction(getActivity(), |
Pierre Duchemin | 45707ca | 2018-01-15 15:05:23 -0500 | [diff] [blame] | 544 | conversation.getContact(), |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 545 | ConversationFragment.this); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public void displayCopyToClipboard(CallContact callContact) { |
| 550 | ActionHelper.launchCopyNumberToClipboardFromContact(getActivity(), |
| 551 | callContact, |
| 552 | this); |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public void hideNumberSpinner() { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 557 | mNumberSpinner.setVisibility(View.GONE); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | @Override |
| 561 | public void clearMsgEdit() { |
| 562 | mMsgEditTxt.setText(""); |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public void goToHome() { |
| 567 | if (getActivity() instanceof ConversationActivity) { |
| 568 | getActivity().finish(); |
Aline Bonnet | eb5f7ff | 2017-01-17 11:09:17 -0500 | [diff] [blame] | 569 | } |
| 570 | } |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 571 | |
| 572 | @Override |
| 573 | public void goToAddContact(CallContact callContact) { |
| 574 | startActivityForResult(ActionHelper.getAddNumberIntentForContact(callContact), REQ_ADD_CONTACT); |
| 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public void goToCallActivity(String conferenceId) { |
| 579 | startActivity(new Intent(Intent.ACTION_VIEW) |
| 580 | .setClass(getActivity().getApplicationContext(), CallActivity.class) |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 581 | .putExtra(NotificationService.KEY_CALL_ID, conferenceId)); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | @Override |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 585 | public void goToCallActivityWithResult(String accountId, String contactRingId, boolean audioOnly) { |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 586 | Intent intent = new Intent(CallActivity.ACTION_CALL) |
| 587 | .setClass(getActivity().getApplicationContext(), CallActivity.class) |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 588 | .putExtra(KEY_ACCOUNT_ID, accountId) |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 589 | .putExtra(CallFragment.KEY_AUDIO_ONLY, audioOnly) |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 590 | .putExtra(KEY_CONTACT_RING_ID, contactRingId); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 591 | startActivityForResult(intent, HomeActivity.REQUEST_CODE_CALL); |
| 592 | } |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 593 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 594 | @OnClick(R.id.btnBlock) |
| 595 | public void blockContactRequest() { |
| 596 | presenter.onBlockIncomingContactRequest(); |
| 597 | } |
| 598 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 599 | @OnClick(R.id.btnRefuse) |
| 600 | public void refuseContactRequest() { |
| 601 | presenter.onRefuseIncomingContactRequest(); |
| 602 | } |
| 603 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 604 | @OnClick(R.id.btnAccept) |
| 605 | public void acceptContactRequest() { |
| 606 | presenter.onAcceptIncomingContactRequest(); |
| 607 | } |
| 608 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 609 | @OnClick(R.id.btnAddContact) |
| 610 | public void addContact() { |
| 611 | presenter.onAddContact(); |
| 612 | } |
| 613 | |
| 614 | @Override |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 615 | public void onPrepareOptionsMenu(Menu menu) { |
| 616 | super.onPrepareOptionsMenu(menu); |
| 617 | boolean visible = mMessageInput.getVisibility() == View.VISIBLE; |
| 618 | if (mAudioCallBtn != null) |
| 619 | mAudioCallBtn.setVisible(visible); |
| 620 | if (mVideoCallBtn != null) |
| 621 | mVideoCallBtn.setVisible(visible); |
| 622 | } |
| 623 | |
| 624 | @Override |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 625 | public void switchToUnknownView(String contactDisplayName) { |
| 626 | mMessageInput.setVisibility(View.GONE); |
| 627 | mUnknownPrompt.setVisibility(View.VISIBLE); |
| 628 | mTrustRequestPrompt.setVisibility(View.GONE); |
| 629 | mTvTrustRequestMessage.setText(String.format(getString(R.string.message_contact_not_trusted), contactDisplayName)); |
| 630 | mTrustRequestMessageLayout.setVisibility(View.VISIBLE); |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 631 | getActivity().invalidateOptionsMenu(); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 632 | } |
| 633 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 634 | @Override |
| 635 | public void switchToIncomingTrustRequestView(String contactDisplayName) { |
| 636 | mMessageInput.setVisibility(View.GONE); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 637 | mUnknownPrompt.setVisibility(View.GONE); |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 638 | mTrustRequestPrompt.setVisibility(View.VISIBLE); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 639 | mTvTrustRequestMessage.setText(String.format(getString(R.string.message_contact_not_trusted_yet), contactDisplayName)); |
| 640 | mTrustRequestMessageLayout.setVisibility(View.VISIBLE); |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 641 | getActivity().invalidateOptionsMenu(); |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public void switchToConversationView() { |
| 646 | mMessageInput.setVisibility(View.VISIBLE); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 647 | mUnknownPrompt.setVisibility(View.GONE); |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 648 | mTrustRequestPrompt.setVisibility(View.GONE); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 649 | mTrustRequestMessageLayout.setVisibility(View.GONE); |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 650 | getActivity().invalidateOptionsMenu(); |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public void positiveMediaButtonClicked() { |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 655 | presenter.clickOnGoingPane(); |
| 656 | } |
| 657 | |
| 658 | @Override |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 659 | public void negativeMediaButtonClicked() { |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 660 | presenter.clickOnGoingPane(); |
| 661 | } |
| 662 | |
| 663 | @Override |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 664 | public void toggleMediaButtonClicked() { |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 665 | presenter.clickOnGoingPane(); |
| 666 | } |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 667 | |
| 668 | private void setLoading(boolean isLoading) { |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 669 | if (takePicture == null || sendData == null || pbDataTransfer == null) |
| 670 | return; |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 671 | if (isLoading) { |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 672 | takePicture.setVisibility(View.GONE); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 673 | sendData.setVisibility(View.GONE); |
| 674 | pbDataTransfer.setVisibility(View.VISIBLE); |
| 675 | } else { |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 676 | takePicture.setVisibility(View.VISIBLE); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 677 | sendData.setVisibility(View.VISIBLE); |
| 678 | pbDataTransfer.setVisibility(View.GONE); |
| 679 | } |
| 680 | } |
Adrien Béraud | 1b8ac9f | 2018-05-31 14:34:23 -0400 | [diff] [blame] | 681 | |
| 682 | @Override |
| 683 | public void displayCompletedDownload(DataTransfer transfer, File destination) { |
| 684 | DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE); |
| 685 | if (downloadManager != null) { |
| 686 | downloadManager.addCompletedDownload(transfer.getDisplayName(), |
| 687 | transfer.getDisplayName(), |
| 688 | true, |
| 689 | transfer.isPicture() ? "image/jpeg" : "text/plain", |
| 690 | destination.getAbsolutePath(), |
| 691 | destination.length(), |
| 692 | true); |
| 693 | } |
| 694 | } |
Adrien Béraud | ae45ffa | 2018-07-06 16:21:29 -0400 | [diff] [blame] | 695 | |
| 696 | public void handleShareIntent(Intent intent) { |
| 697 | String type = intent.getType(); |
| 698 | if (type == null) { |
| 699 | Log.w(TAG, "Can't share with no type"); |
| 700 | return; |
| 701 | } |
| 702 | if (type.startsWith("text/")) { |
| 703 | mMsgEditTxt.setText(intent.getStringExtra(Intent.EXTRA_TEXT)); |
| 704 | } else if (type.startsWith("image/") || type.startsWith("video/") || type.startsWith("application/")) { |
| 705 | android.net.Uri uri = intent.getData(); |
| 706 | ClipData clip = intent.getClipData(); |
| 707 | if (uri == null && clip != null && clip.getItemCount() > 0) |
| 708 | uri = clip.getItemAt(0).getUri(); |
| 709 | if (uri == null) |
| 710 | return; |
| 711 | final android.net.Uri shareUri = uri; |
| 712 | setLoading(true); |
| 713 | new Thread(() -> { |
| 714 | try { |
| 715 | File cacheFile = AndroidFileUtils.getCacheFile(getActivity(), shareUri); |
| 716 | presenter.sendFile(cacheFile); |
| 717 | } catch (IOException e) { |
| 718 | Log.e(TAG, "onActivityResult: not able to create cache file"); |
| 719 | getActivity().runOnUiThread(() -> displayErrorToast(RingError.INVALID_FILE)); |
| 720 | } |
| 721 | getActivity().runOnUiThread(() -> setLoading(false)); |
| 722 | }).start(); |
| 723 | } |
| 724 | } |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 725 | } |