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