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