Loïc Siret | 0202fbf | 2017-09-28 16:34:28 -0400 | [diff] [blame] | 1 | /* |
Adrien Béraud | e552f69 | 2020-11-03 14:30:52 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2020 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 | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 23 | import android.animation.LayoutTransition; |
Adrien Béraud | 772e216 | 2019-02-12 16:00:21 -0500 | [diff] [blame] | 24 | import android.animation.ValueAnimator; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 25 | import android.annotation.SuppressLint; |
Adrien Béraud | 21a3ea2 | 2020-04-01 16:06:09 -0400 | [diff] [blame] | 26 | import android.app.Activity; |
Rayan Osseiran | e445027 | 2019-06-03 14:17:19 -0400 | [diff] [blame] | 27 | import android.content.ActivityNotFoundException; |
Adrien Béraud | ae45ffa | 2018-07-06 16:21:29 -0400 | [diff] [blame] | 28 | import android.content.ClipData; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 29 | import android.content.ComponentName; |
Adrien Béraud | 1b8ac9f | 2018-05-31 14:34:23 -0400 | [diff] [blame] | 30 | import android.content.Context; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 31 | import android.content.Intent; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 32 | import android.content.ServiceConnection; |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 33 | import android.content.SharedPreferences; |
Adrien Béraud | 4f542f9 | 2019-01-03 12:17:53 -0500 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 35 | import android.graphics.Typeface; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 36 | import android.content.res.Resources; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 37 | import android.os.Bundle; |
Adrien Béraud | b9a1cfe | 2020-08-02 09:06:26 -0400 | [diff] [blame] | 38 | import android.os.Environment; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 39 | import android.os.IBinder; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 40 | import android.provider.MediaStore; |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 41 | import android.text.Editable; |
Adrien Béraud | 58bd92b | 2018-08-23 10:54:25 -0400 | [diff] [blame] | 42 | import android.text.TextUtils; |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 43 | import android.text.TextWatcher; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 44 | import android.util.Log; |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 46 | import android.view.Menu; |
| 47 | import android.view.MenuInflater; |
| 48 | import android.view.MenuItem; |
| 49 | import android.view.View; |
| 50 | import android.view.ViewGroup; |
| 51 | import android.view.inputmethod.EditorInfo; |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 52 | import android.widget.ImageView; |
| 53 | import android.widget.RelativeLayout; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 54 | import android.widget.Spinner; |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 55 | import android.widget.TextView; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 56 | import android.widget.Toast; |
| 57 | |
Rayan Osseiran | e445027 | 2019-06-03 14:17:19 -0400 | [diff] [blame] | 58 | import com.google.android.material.snackbar.Snackbar; |
| 59 | |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 60 | import androidx.annotation.NonNull; |
| 61 | import androidx.annotation.Nullable; |
| 62 | import androidx.appcompat.app.ActionBar; |
| 63 | import androidx.appcompat.app.AppCompatActivity; |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 64 | import androidx.appcompat.view.menu.MenuBuilder; |
| 65 | import androidx.appcompat.view.menu.MenuPopupHelper; |
| 66 | import androidx.appcompat.widget.PopupMenu; |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 67 | import androidx.appcompat.widget.Toolbar; |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 68 | import androidx.core.view.ViewCompat; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 69 | import androidx.fragment.app.Fragment; |
| 70 | import androidx.fragment.app.FragmentManager; |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 71 | import androidx.recyclerview.widget.DefaultItemAnimator; |
| 72 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 73 | import java.io.File; |
| 74 | import java.io.IOException; |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 75 | import java.util.HashMap; |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 76 | import java.util.List; |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 77 | import java.util.Map; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 78 | |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 79 | import cx.ring.BuildConfig; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 80 | import cx.ring.R; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 81 | import cx.ring.adapters.ConversationAdapter; |
| 82 | import cx.ring.adapters.NumberAdapter; |
Adrien Béraud | e4f901a | 2019-10-25 17:26:30 -0400 | [diff] [blame] | 83 | import cx.ring.application.JamiApplication; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 84 | import cx.ring.client.CallActivity; |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 85 | import cx.ring.client.ContactDetailsActivity; |
Aline Bonnet | 830c4c6 | 2016-12-27 14:02:02 -0500 | [diff] [blame] | 86 | import cx.ring.client.ConversationActivity; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 87 | import cx.ring.client.HomeActivity; |
Adrien Béraud | 78ae7b9 | 2018-08-25 19:04:17 -0400 | [diff] [blame] | 88 | import cx.ring.contacts.AvatarFactory; |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 89 | import cx.ring.conversation.ConversationPresenter; |
| 90 | import cx.ring.conversation.ConversationView; |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 91 | import cx.ring.databinding.FragConversationBinding; |
Adrien Béraud | 171ba36 | 2018-12-14 21:35:40 -0500 | [diff] [blame] | 92 | import cx.ring.interfaces.Colorable; |
Adrien Béraud | fd458ea | 2020-02-28 13:40:18 -0500 | [diff] [blame] | 93 | import cx.ring.model.Account; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 94 | import cx.ring.model.CallContact; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 95 | import cx.ring.model.Conversation; |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 96 | import cx.ring.model.Interaction; |
Adrien Béraud | 2153329 | 2018-03-22 14:11:35 -0400 | [diff] [blame] | 97 | import cx.ring.model.DataTransfer; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 98 | import cx.ring.model.Phone; |
Adrien Béraud | a0226ff | 2019-11-21 15:24:42 -0500 | [diff] [blame] | 99 | import cx.ring.model.Error; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 100 | import cx.ring.model.Uri; |
Adrien Béraud | a373e3b | 2018-12-12 11:14:50 -0500 | [diff] [blame] | 101 | import cx.ring.mvp.BaseSupportFragment; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 102 | import cx.ring.services.LocationSharingService; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 103 | import cx.ring.services.NotificationService; |
Amirhossein | 6891638 | 2020-07-19 11:23:59 -0400 | [diff] [blame] | 104 | import cx.ring.services.NotificationServiceImpl; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 105 | import cx.ring.utils.ActionHelper; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 106 | import cx.ring.utils.AndroidFileUtils; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 107 | import cx.ring.utils.ContentUriHandler; |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 108 | import cx.ring.utils.DeviceUtils; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 109 | import cx.ring.utils.ConversationPath; |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 110 | import cx.ring.utils.MediaButtonsHelper; |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 111 | import cx.ring.views.AvatarDrawable; |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 112 | import io.reactivex.Completable; |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 113 | import io.reactivex.Single; |
Adrien Béraud | 78ae7b9 | 2018-08-25 19:04:17 -0400 | [diff] [blame] | 114 | import io.reactivex.android.schedulers.AndroidSchedulers; |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 115 | import io.reactivex.disposables.CompositeDisposable; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 116 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 117 | import static android.app.Activity.RESULT_OK; |
| 118 | |
Adrien Béraud | a373e3b | 2018-12-12 11:14:50 -0500 | [diff] [blame] | 119 | public class ConversationFragment extends BaseSupportFragment<ConversationPresenter> implements |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 120 | MediaButtonsHelper.MediaButtonsHelperCallback, |
Rayan Osseiran | e634a56 | 2019-05-29 13:44:55 -0400 | [diff] [blame] | 121 | ConversationView, SharedPreferences.OnSharedPreferenceChangeListener { |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 122 | private static final String TAG = ConversationFragment.class.getSimpleName(); |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 123 | |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 124 | public static final int REQ_ADD_CONTACT = 42; |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 125 | |
Adrien Béraud | 6ac6363 | 2018-12-09 00:11:21 -0500 | [diff] [blame] | 126 | public static final String KEY_CONTACT_RING_ID = BuildConfig.APPLICATION_ID + ".CONTACT_RING_ID"; |
| 127 | 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] | 128 | public static final String KEY_PREFERENCE_PENDING_MESSAGE = "pendingMessage"; |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 129 | public static final String KEY_PREFERENCE_CONVERSATION_COLOR = "color"; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 130 | public static final String EXTRA_SHOW_MAP = "showMap"; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 131 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 132 | private static final int REQUEST_CODE_FILE_PICKER = 1000; |
Pierre Duchemin | 4af6b39 | 2018-02-02 11:23:56 -0500 | [diff] [blame] | 133 | private static final int REQUEST_PERMISSION_CAMERA = 1001; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 134 | private static final int REQUEST_CODE_TAKE_PICTURE = 1002; |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 135 | private static final int REQUEST_CODE_SAVE_FILE = 1003; |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 136 | private static final int REQUEST_CODE_CAPTURE_AUDIO = 1004; |
| 137 | private static final int REQUEST_CODE_CAPTURE_VIDEO = 1005; |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 138 | |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 139 | private ServiceConnection locationServiceConnection = null; |
| 140 | |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 141 | private FragConversationBinding binding; |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 142 | private MenuItem mAudioCallBtn = null; |
| 143 | private MenuItem mVideoCallBtn = null; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 144 | |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 145 | private View currentBottomView = null; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 146 | private ConversationAdapter mAdapter = null; |
Adrien Béraud | 772e216 | 2019-02-12 16:00:21 -0500 | [diff] [blame] | 147 | private int marginPx; |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 148 | private int marginPxTotal; |
Adrien Béraud | 772e216 | 2019-02-12 16:00:21 -0500 | [diff] [blame] | 149 | private final ValueAnimator animation = new ValueAnimator(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 150 | |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 151 | private SharedPreferences mPreferences; |
| 152 | |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 153 | private File mCurrentPhoto = null; |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 154 | private String mCurrentFileAbsolutePath = null; |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 155 | private final CompositeDisposable mCompositeDisposable = new CompositeDisposable(); |
Adrien Béraud | 742d320 | 2019-11-15 14:28:59 -0500 | [diff] [blame] | 156 | private int mSelectedPosition; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 157 | |
Amirhossein | 6891638 | 2020-07-19 11:23:59 -0400 | [diff] [blame] | 158 | private boolean mIsBubble; |
| 159 | |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 160 | private AvatarDrawable mConversationAvatar; |
Adrien Béraud | 263cd5c | 2020-03-13 13:36:57 -0400 | [diff] [blame] | 161 | private final Map<String, AvatarDrawable> mParticipantAvatars = new HashMap<>(); |
| 162 | private final Map<String, AvatarDrawable> mSmallParticipantAvatars = new HashMap<>(); |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 163 | private int mapWidth, mapHeight; |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 164 | |
| 165 | public AvatarDrawable getConversationAvatar(String uri) { |
| 166 | return mParticipantAvatars.get(uri); |
| 167 | } |
Adrien Béraud | 263cd5c | 2020-03-13 13:36:57 -0400 | [diff] [blame] | 168 | public AvatarDrawable getSmallConversationAvatar(String uri) { |
| 169 | synchronized (mSmallParticipantAvatars) { |
| 170 | return mSmallParticipantAvatars.get(uri); |
| 171 | } |
| 172 | } |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 173 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 174 | private static int getIndex(Spinner spinner, Uri myString) { |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 175 | for (int i = 0, n = spinner.getCount(); i < n; i++) |
| 176 | if (((Phone) spinner.getItemAtPosition(i)).getNumber().equals(myString)) { |
| 177 | return i; |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 182 | @Override |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 183 | public void refreshView(final List<Interaction> conversation) { |
Adrien Béraud | 20d4221 | 2018-03-02 14:40:38 -0500 | [diff] [blame] | 184 | if (conversation == null) { |
| 185 | return; |
| 186 | } |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 187 | if (binding != null) |
| 188 | binding.pbLoading.setVisibility(View.GONE); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 189 | if (mAdapter != null) { |
| 190 | mAdapter.updateDataset(conversation); |
| 191 | } |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 192 | requireActivity().invalidateOptionsMenu(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Aline Bonnet | e8e87f6 | 2016-12-26 10:54:37 -0500 | [diff] [blame] | 195 | @Override |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 196 | public void scrollToEnd() { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 197 | if (mAdapter.getItemCount() > 0) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 198 | binding.histList.scrollToPosition(mAdapter.getItemCount() - 1); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 199 | } |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 202 | private static void setBottomPadding(@NonNull View view, int padding) { |
| 203 | view.setPadding( |
| 204 | view.getPaddingLeft(), |
| 205 | view.getPaddingTop(), |
| 206 | view.getPaddingRight(), |
| 207 | padding); |
| 208 | } |
| 209 | |
| 210 | private void updateListPadding() { |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 211 | if (currentBottomView != null && currentBottomView.getHeight() != 0) { |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 212 | setBottomPadding(binding.histList, currentBottomView.getHeight() + marginPxTotal); |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 213 | RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) binding.mapCard.getLayoutParams(); |
| 214 | params.bottomMargin = currentBottomView.getHeight() + marginPxTotal; |
| 215 | binding.mapCard.setLayoutParams(params); |
| 216 | } |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 219 | @Nullable |
| 220 | @Override |
Adrien Béraud | 3c483ce | 2019-01-02 17:33:49 -0500 | [diff] [blame] | 221 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
Adrien Béraud | 7307cd5 | 2020-04-13 23:29:11 -0400 | [diff] [blame] | 222 | ((JamiApplication) getActivity().getApplication()).getInjectionComponent().inject(this); |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 223 | Resources res = getResources(); |
| 224 | marginPx = res.getDimensionPixelSize(R.dimen.conversation_message_input_margin); |
| 225 | mapWidth = res.getDimensionPixelSize(R.dimen.location_sharing_minmap_width); |
| 226 | mapHeight = res.getDimensionPixelSize(R.dimen.location_sharing_minmap_height); |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 227 | marginPxTotal = marginPx; |
Adrien Béraud | fd08367 | 2019-10-31 13:52:38 -0400 | [diff] [blame] | 228 | |
Adrien Béraud | 3c483ce | 2019-01-02 17:33:49 -0500 | [diff] [blame] | 229 | binding = FragConversationBinding.inflate(inflater, container, false); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 230 | binding.setPresenter(this); |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 231 | |
Adrien Béraud | fd08367 | 2019-10-31 13:52:38 -0400 | [diff] [blame] | 232 | animation.setDuration(150); |
| 233 | animation.addUpdateListener(valueAnimator -> setBottomPadding(binding.histList, (Integer)valueAnimator.getAnimatedValue())); |
| 234 | |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 235 | ViewCompat.setOnApplyWindowInsetsListener(binding.histList, (v, insets) -> { |
| 236 | marginPxTotal = marginPx + insets.getSystemWindowInsetBottom(); |
| 237 | updateListPadding(); |
| 238 | insets.consumeSystemWindowInsets(); |
| 239 | return insets; |
| 240 | }); |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 241 | View layout = binding.conversationLayout; |
| 242 | |
| 243 | // remove action bar height for tablet layout |
| 244 | if (DeviceUtils.isTablet(getContext())) { |
| 245 | layout.setPadding(layout.getPaddingLeft(), 0, layout.getPaddingRight(), layout.getPaddingBottom()); |
| 246 | } |
| 247 | |
| 248 | int paddingTop = layout.getPaddingTop(); |
| 249 | ViewCompat.setOnApplyWindowInsetsListener(layout, (v, insets) -> { |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 250 | v.setPadding( |
| 251 | v.getPaddingLeft(), |
| 252 | paddingTop + insets.getSystemWindowInsetTop(), |
| 253 | v.getPaddingRight(), |
| 254 | v.getPaddingBottom()); |
| 255 | insets.consumeSystemWindowInsets(); |
| 256 | return insets; |
| 257 | }); |
| 258 | |
Adrien Béraud | fd08367 | 2019-10-31 13:52:38 -0400 | [diff] [blame] | 259 | binding.ongoingcallPane.setVisibility(View.GONE); |
Adrien Béraud | 1af6c01 | 2019-05-25 23:20:29 -0400 | [diff] [blame] | 260 | binding.msgInputTxt.setMediaListener(contentInfo -> startFileSend(AndroidFileUtils |
Adrien Béraud | fd08367 | 2019-10-31 13:52:38 -0400 | [diff] [blame] | 261 | .getCacheFile(requireContext(), contentInfo.getContentUri()) |
| 262 | .flatMapCompletable(this::sendFile) |
| 263 | .doFinally(contentInfo::releasePermission))); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 264 | binding.msgInputTxt.setOnEditorActionListener((v, actionId, event) -> actionSendMsgText(actionId)); |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 265 | binding.msgInputTxt.setOnFocusChangeListener((view, hasFocus) -> { |
| 266 | if (hasFocus) { |
| 267 | Fragment fragment = getChildFragmentManager().findFragmentById(R.id.mapLayout); |
| 268 | if (fragment != null) { |
| 269 | ((LocationSharingFragment) fragment).hideControls(); |
| 270 | } |
| 271 | } |
| 272 | }); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 273 | binding.msgInputTxt.addTextChangedListener(new TextWatcher() { |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 274 | @Override |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 275 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 276 | } |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 277 | |
| 278 | @Override |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 279 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 280 | } |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 281 | |
| 282 | @Override |
| 283 | public void afterTextChanged(Editable s) { |
Adrien Béraud | 58bd92b | 2018-08-23 10:54:25 -0400 | [diff] [blame] | 284 | String message = s.toString(); |
Adrien Béraud | e09dd01 | 2019-11-15 14:57:18 -0500 | [diff] [blame] | 285 | boolean hasMessage = !TextUtils.isEmpty(message); |
Adrien Béraud | fd458ea | 2020-02-28 13:40:18 -0500 | [diff] [blame] | 286 | presenter.onComposingChanged(hasMessage); |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 287 | if (hasMessage) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 288 | binding.msgSend.setVisibility(View.VISIBLE); |
| 289 | binding.emojiSend.setVisibility(View.GONE); |
Adrien Béraud | e09dd01 | 2019-11-15 14:57:18 -0500 | [diff] [blame] | 290 | } else { |
| 291 | binding.msgSend.setVisibility(View.GONE); |
| 292 | binding.emojiSend.setVisibility(View.VISIBLE); |
Adrien Béraud | 58bd92b | 2018-08-23 10:54:25 -0400 | [diff] [blame] | 293 | } |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 294 | if (mPreferences != null) { |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 295 | if (hasMessage) |
| 296 | mPreferences.edit().putString(KEY_PREFERENCE_PENDING_MESSAGE, message).apply(); |
| 297 | else |
| 298 | mPreferences.edit().remove(KEY_PREFERENCE_PENDING_MESSAGE).apply(); |
Adrien Béraud | 3a875a0 | 2018-07-18 17:54:17 -0400 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | }); |
Adrien Béraud | 9d4a81f | 2018-07-09 01:19:52 -0400 | [diff] [blame] | 302 | |
Adrien Béraud | fd08367 | 2019-10-31 13:52:38 -0400 | [diff] [blame] | 303 | setHasOptionsMenu(true); |
| 304 | return binding.getRoot(); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { |
| 309 | super.onViewCreated(view, savedInstanceState); |
Adrien Béraud | fd08367 | 2019-10-31 13:52:38 -0400 | [diff] [blame] | 310 | if (mPreferences != null) { |
| 311 | String pendingMessage = mPreferences.getString(KEY_PREFERENCE_PENDING_MESSAGE, null); |
| 312 | if (!TextUtils.isEmpty(pendingMessage)) { |
| 313 | binding.msgInputTxt.setText(pendingMessage); |
| 314 | binding.msgSend.setVisibility(View.VISIBLE); |
| 315 | binding.emojiSend.setVisibility(View.GONE); |
| 316 | } |
| 317 | } |
Adrien Béraud | 772e216 | 2019-02-12 16:00:21 -0500 | [diff] [blame] | 318 | |
| 319 | binding.msgInputTxt.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> { |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 320 | if (oldBottom == 0 && oldTop == 0) { |
| 321 | updateListPadding(); |
| 322 | } else { |
| 323 | if (animation.isStarted()) |
| 324 | animation.cancel(); |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 325 | animation.setIntValues(binding.histList.getPaddingBottom(), (currentBottomView == null ? 0 : currentBottomView.getHeight()) + marginPxTotal); |
Adrien Béraud | 91f8874 | 2019-09-09 00:46:46 -0400 | [diff] [blame] | 326 | animation.start(); |
| 327 | } |
Adrien Béraud | 772e216 | 2019-02-12 16:00:21 -0500 | [diff] [blame] | 328 | }); |
| 329 | |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 330 | DefaultItemAnimator animator = (DefaultItemAnimator) binding.histList.getItemAnimator(); |
Adrien Béraud | 3c483ce | 2019-01-02 17:33:49 -0500 | [diff] [blame] | 331 | if (animator != null) |
| 332 | animator.setSupportsChangeAnimations(false); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 333 | binding.histList.setAdapter(mAdapter); |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 334 | } |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 335 | |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 336 | @Override |
| 337 | public void setConversationColor(int color) { |
Adrien Béraud | 171ba36 | 2018-12-14 21:35:40 -0500 | [diff] [blame] | 338 | Colorable activity = (Colorable) getActivity(); |
| 339 | if (activity != null) |
| 340 | activity.setColor(color); |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 341 | mAdapter.setPrimaryColor(color); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Adrien Béraud | 7c50511 | 2018-03-20 16:03:28 -0400 | [diff] [blame] | 344 | @Override |
Adrien Béraud | 4030d2e | 2018-12-02 23:08:03 -0500 | [diff] [blame] | 345 | public void onDestroyView() { |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 346 | if (mPreferences != null) |
| 347 | mPreferences.unregisterOnSharedPreferenceChangeListener(this); |
Adrien Béraud | 772e216 | 2019-02-12 16:00:21 -0500 | [diff] [blame] | 348 | animation.removeAllUpdateListeners(); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 349 | binding.histList.setAdapter(null); |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 350 | mCompositeDisposable.clear(); |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 351 | if (locationServiceConnection != null) { |
| 352 | try { |
| 353 | requireContext().unbindService(locationServiceConnection); |
| 354 | } catch (Exception e) { |
| 355 | Log.w(TAG, "Error unbinding service: " + e.getMessage()); |
| 356 | } |
| 357 | } |
Adrien Béraud | fd458ea | 2020-02-28 13:40:18 -0500 | [diff] [blame] | 358 | mAdapter = null; |
Adrien Béraud | 4030d2e | 2018-12-02 23:08:03 -0500 | [diff] [blame] | 359 | super.onDestroyView(); |
Adrien Béraud | c35f7f4 | 2020-04-15 15:29:29 -0400 | [diff] [blame] | 360 | binding = null; |
Adrien Béraud | 4030d2e | 2018-12-02 23:08:03 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | @Override |
Adrien Béraud | fd08367 | 2019-10-31 13:52:38 -0400 | [diff] [blame] | 364 | public boolean onContextItemSelected(@NonNull MenuItem item) { |
Adrien Béraud | 7c50511 | 2018-03-20 16:03:28 -0400 | [diff] [blame] | 365 | if (mAdapter.onContextItemSelected(item)) |
| 366 | return true; |
| 367 | return super.onContextItemSelected(item); |
| 368 | } |
| 369 | |
Rayan Osseiran | e634a56 | 2019-05-29 13:44:55 -0400 | [diff] [blame] | 370 | public void updateAdapterItem() { |
Adrien Béraud | 742d320 | 2019-11-15 14:28:59 -0500 | [diff] [blame] | 371 | if (mSelectedPosition != -1) { |
| 372 | mAdapter.notifyItemChanged(mSelectedPosition); |
| 373 | mSelectedPosition = -1; |
Rayan Osseiran | e634a56 | 2019-05-29 13:44:55 -0400 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Alexandre Lision | 66a9f9a | 2017-02-26 20:28:27 -0500 | [diff] [blame] | 377 | public void sendMessageText() { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 378 | String message = binding.msgInputTxt.getText().toString(); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 379 | clearMsgEdit(); |
| 380 | presenter.sendTextMessage(message); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Adrien Béraud | 58bd92b | 2018-08-23 10:54:25 -0400 | [diff] [blame] | 383 | public void sendEmoji() { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 384 | presenter.sendTextMessage(binding.emojiSend.getText().toString()); |
Adrien Béraud | 58bd92b | 2018-08-23 10:54:25 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 387 | @SuppressLint("RestrictedApi") |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 388 | public void expandMenu(View v) { |
| 389 | Context context = requireContext(); |
| 390 | PopupMenu popup = new PopupMenu(context, v); |
| 391 | popup.inflate(R.menu.conversation_share_actions); |
| 392 | popup.setOnMenuItemClickListener(item -> { |
| 393 | switch(item.getItemId()) { |
Adrien Béraud | d5637f2 | 2020-05-14 12:41:50 -0400 | [diff] [blame] | 394 | case R.id.conv_send_audio: |
| 395 | sendAudioMessage(); |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 396 | break; |
Adrien Béraud | d5637f2 | 2020-05-14 12:41:50 -0400 | [diff] [blame] | 397 | case R.id.conv_send_video: |
| 398 | sendVideoMessage(); |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 399 | break; |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 400 | case R.id.conv_send_file: |
Adrien Béraud | c35f7f4 | 2020-04-15 15:29:29 -0400 | [diff] [blame] | 401 | presenter.selectFile(); |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 402 | break; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 403 | case R.id.conv_share_location: |
| 404 | shareLocation(); |
| 405 | break; |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 406 | } |
| 407 | return false; |
| 408 | }); |
| 409 | MenuPopupHelper menuHelper = new MenuPopupHelper(context, (MenuBuilder) popup.getMenu(), v); |
| 410 | menuHelper.setForceShowIcon(true); |
| 411 | menuHelper.show(); |
| 412 | } |
| 413 | |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 414 | public void shareLocation() { |
| 415 | presenter.shareLocation(); |
| 416 | } |
| 417 | |
| 418 | public void closeLocationSharing(boolean isSharing) { |
| 419 | RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) binding.mapCard.getLayoutParams(); |
| 420 | if (params.width != mapWidth) { |
| 421 | params.width = mapWidth; |
| 422 | params.height = mapHeight; |
| 423 | binding.mapCard.setLayoutParams(params); |
| 424 | } |
| 425 | if (!isSharing) |
| 426 | hideMap(); |
| 427 | } |
| 428 | |
| 429 | public void openLocationSharing() { |
| 430 | binding.conversationLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING); |
| 431 | RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) binding.mapCard.getLayoutParams(); |
| 432 | if (params.width != ViewGroup.LayoutParams.MATCH_PARENT) { |
| 433 | params.width = ViewGroup.LayoutParams.MATCH_PARENT; |
| 434 | params.height = ViewGroup.LayoutParams.MATCH_PARENT; |
| 435 | binding.mapCard.setLayoutParams(params); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public void startShareLocation(String accountId, String conversationId) { |
| 441 | showMap(accountId, conversationId, true); |
| 442 | } |
| 443 | |
Rayan Osseiran | e634a56 | 2019-05-29 13:44:55 -0400 | [diff] [blame] | 444 | /** |
| 445 | * Used to update with the past adapter position when a long click was registered |
Rayan Osseiran | e634a56 | 2019-05-29 13:44:55 -0400 | [diff] [blame] | 446 | */ |
| 447 | public void updatePosition(int position) { |
Adrien Béraud | 742d320 | 2019-11-15 14:28:59 -0500 | [diff] [blame] | 448 | mSelectedPosition = position; |
Rayan Osseiran | e634a56 | 2019-05-29 13:44:55 -0400 | [diff] [blame] | 449 | } |
| 450 | |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 451 | @Override |
| 452 | public void showMap(String accountId, String contactId, boolean open) { |
| 453 | if (binding.mapCard.getVisibility() == View.GONE) { |
| 454 | Log.w(TAG, "showMap " + accountId + " " + contactId); |
| 455 | |
| 456 | FragmentManager fragmentManager = getChildFragmentManager(); |
| 457 | LocationSharingFragment fragment = LocationSharingFragment.newInstance(accountId, contactId, open); |
| 458 | fragmentManager.beginTransaction() |
| 459 | .add(R.id.mapLayout, fragment, "map") |
| 460 | .commit(); |
| 461 | binding.mapCard.setVisibility(View.VISIBLE); |
| 462 | } |
| 463 | if (open) { |
| 464 | Fragment fragment = getChildFragmentManager().findFragmentById(R.id.mapLayout); |
| 465 | if (fragment != null) { |
| 466 | ((LocationSharingFragment) fragment).showControls(); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void hideMap() { |
| 473 | if (binding.mapCard.getVisibility() != View.GONE) { |
| 474 | binding.mapCard.setVisibility(View.GONE); |
| 475 | |
| 476 | FragmentManager fragmentManager = getChildFragmentManager(); |
| 477 | Fragment fragment = fragmentManager.findFragmentById(R.id.mapLayout); |
| 478 | |
| 479 | if (fragment != null) { |
| 480 | fragmentManager.beginTransaction() |
| 481 | .remove(fragment) |
| 482 | .commit(); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Adrien Béraud | d5637f2 | 2020-05-14 12:41:50 -0400 | [diff] [blame] | 487 | public void sendAudioMessage() { |
| 488 | if (!presenter.getDeviceRuntimeService().hasAudioPermission()) { |
| 489 | requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_CODE_CAPTURE_AUDIO); |
| 490 | } else { |
| 491 | Context ctx = requireContext(); |
| 492 | Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); |
| 493 | |
| 494 | if (intent.resolveActivity(ctx.getPackageManager()) != null) { |
| 495 | try { |
| 496 | mCurrentPhoto = AndroidFileUtils.createAudioFile(ctx); |
| 497 | } catch (IOException ex) { |
| 498 | Log.e(TAG, "takePicture: error creating temporary file", ex); |
| 499 | return; |
| 500 | } |
| 501 | startActivityForResult(intent, REQUEST_CODE_CAPTURE_AUDIO); |
| 502 | } else { |
| 503 | Toast.makeText(getActivity(), "Can't find audio recorder app", Toast.LENGTH_SHORT).show(); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | public void sendVideoMessage() { |
| 509 | if (!presenter.getDeviceRuntimeService().hasVideoPermission()) { |
| 510 | requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_CAPTURE_VIDEO); |
| 511 | } else { |
| 512 | Context context = requireContext(); |
| 513 | Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
| 514 | intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT); |
| 515 | intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1); |
| 516 | intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true); |
| 517 | if (intent.resolveActivity(context.getPackageManager()) != null) { |
| 518 | try { |
| 519 | mCurrentPhoto = AndroidFileUtils.createVideoFile(context); |
| 520 | } catch (IOException ex) { |
| 521 | Log.e(TAG, "takePicture: error creating temporary file", ex); |
| 522 | return; |
| 523 | } |
| 524 | intent.putExtra(MediaStore.EXTRA_OUTPUT, ContentUriHandler.getUriForFile(context, ContentUriHandler.AUTHORITY_FILES, mCurrentPhoto)); |
| 525 | startActivityForResult(intent, REQUEST_CODE_CAPTURE_VIDEO); |
| 526 | } else { |
| 527 | Toast.makeText(getActivity(), "Can't find video recorder app", Toast.LENGTH_SHORT).show(); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 532 | public void takePicture() { |
Adrien Béraud | 4f542f9 | 2019-01-03 12:17:53 -0500 | [diff] [blame] | 533 | if (!presenter.getDeviceRuntimeService().hasVideoPermission()) { |
Adrien Béraud | d5637f2 | 2020-05-14 12:41:50 -0400 | [diff] [blame] | 534 | requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_TAKE_PICTURE); |
Adrien Béraud | 24027dd | 2020-10-19 18:21:31 -0400 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | Context c = getContext(); |
| 538 | if (c == null) |
| 539 | return; |
| 540 | try { |
| 541 | File photoFile = AndroidFileUtils.createImageFile(c); |
| 542 | Log.i(TAG, "takePicture: trying to save to " + photoFile); |
| 543 | android.net.Uri photoURI = ContentUriHandler.getUriForFile(c, ContentUriHandler.AUTHORITY_FILES, photoFile); |
| 544 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, photoURI) |
| 545 | .putExtra("android.intent.extras.CAMERA_FACING", 1) |
| 546 | .putExtra("android.intent.extras.LENS_FACING_FRONT", 1) |
| 547 | .putExtra("android.intent.extra.USE_FRONT_CAMERA", true); |
| 548 | mCurrentPhoto = photoFile; |
| 549 | startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PICTURE); |
| 550 | } catch (Exception e) { |
| 551 | Toast.makeText(c, "Error taking picture: " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 555 | @Override |
Pierre Duchemin | 4af6b39 | 2018-02-02 11:23:56 -0500 | [diff] [blame] | 556 | public void askWriteExternalStoragePermission() { |
Adrien Béraud | e4f901a | 2019-10-25 17:26:30 -0400 | [diff] [blame] | 557 | requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, JamiApplication.PERMISSIONS_REQUEST); |
Pierre Duchemin | 4af6b39 | 2018-02-02 11:23:56 -0500 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | @Override |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 561 | public void openFilePicker() { |
| 562 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 563 | intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 564 | intent.setType("*/*"); |
| 565 | |
| 566 | startActivityForResult(intent, REQUEST_CODE_FILE_PICKER); |
| 567 | } |
| 568 | |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 569 | private Completable sendFile(File file) { |
| 570 | return Completable.fromAction(() -> presenter.sendFile(file)); |
| 571 | } |
| 572 | |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 573 | private void startFileSend(Completable op) { |
| 574 | setLoading(true); |
Adrien Béraud | 1af6c01 | 2019-05-25 23:20:29 -0400 | [diff] [blame] | 575 | op.observeOn(AndroidSchedulers.mainThread()) |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 576 | .doFinally(() -> setLoading(false)) |
| 577 | .subscribe(() -> {}, e -> { |
| 578 | Log.e(TAG, "startFileSend: not able to create cache file", e); |
Adrien Béraud | a0226ff | 2019-11-21 15:24:42 -0500 | [diff] [blame] | 579 | displayErrorToast(Error.INVALID_FILE); |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 580 | }); |
| 581 | } |
| 582 | |
Pierre Duchemin | e0c3a49 | 2018-02-01 16:22:45 -0500 | [diff] [blame] | 583 | @Override |
Adrien Béraud | 39ce17a | 2020-10-25 02:47:23 -0400 | [diff] [blame] | 584 | public void onActivityResult(int requestCode, int resultCode, @Nullable Intent resultData) { |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 585 | Log.w(TAG, "onActivityResult: " + requestCode + " " + resultCode + " " + resultData); |
Adrien Béraud | 70dd5aa | 2020-12-04 15:18:47 -0500 | [diff] [blame^] | 586 | android.net.Uri uri = resultData == null ? null : resultData.getData(); |
Adrien Béraud | 39ce17a | 2020-10-25 02:47:23 -0400 | [diff] [blame] | 587 | if (requestCode == REQUEST_CODE_FILE_PICKER) { |
| 588 | if (resultCode == RESULT_OK && uri != null) { |
| 589 | startFileSend(AndroidFileUtils.getCacheFile(requireContext(), uri) |
| 590 | .observeOn(AndroidSchedulers.mainThread()) |
| 591 | .flatMapCompletable(this::sendFile)); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 592 | } |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 593 | } else if (requestCode == REQUEST_CODE_TAKE_PICTURE |
| 594 | || requestCode == REQUEST_CODE_CAPTURE_AUDIO |
Adrien Béraud | 39ce17a | 2020-10-25 02:47:23 -0400 | [diff] [blame] | 595 | || requestCode == REQUEST_CODE_CAPTURE_VIDEO) |
| 596 | { |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 597 | if (resultCode != RESULT_OK) { |
| 598 | mCurrentPhoto = null; |
| 599 | return; |
| 600 | } |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 601 | Log.w(TAG, "onActivityResult: mCurrentPhoto " + mCurrentPhoto.getAbsolutePath() + " " + mCurrentPhoto.exists() + " " + mCurrentPhoto.length()); |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 602 | Single<File> file = null; |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 603 | if (mCurrentPhoto == null || !mCurrentPhoto.exists() || mCurrentPhoto.length() == 0) { |
Adrien Béraud | 39ce17a | 2020-10-25 02:47:23 -0400 | [diff] [blame] | 604 | if (uri != null) { |
| 605 | file = AndroidFileUtils.getCacheFile(requireContext(), uri); |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 606 | } |
| 607 | } else { |
| 608 | file = Single.just(mCurrentPhoto); |
Adrien Béraud | 6453872 | 2018-03-15 17:34:00 -0400 | [diff] [blame] | 609 | } |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 610 | mCurrentPhoto = null; |
Adrien Béraud | 898c2bf | 2019-09-26 14:49:33 -0400 | [diff] [blame] | 611 | if (file == null) { |
| 612 | Toast.makeText(getActivity(), "Can't find picture", Toast.LENGTH_SHORT).show(); |
| 613 | return; |
| 614 | } |
| 615 | startFileSend(file.flatMapCompletable(this::sendFile)); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 616 | } |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 617 | // File download trough SAF |
Adrien Béraud | 39ce17a | 2020-10-25 02:47:23 -0400 | [diff] [blame] | 618 | else if (requestCode == ConversationFragment.REQUEST_CODE_SAVE_FILE) { |
| 619 | if (resultCode == RESULT_OK && uri != null) { |
| 620 | writeToFile(uri); |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 621 | } |
| 622 | } |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 623 | } |
| 624 | |
Adrien Béraud | b9a1cfe | 2020-08-02 09:06:26 -0400 | [diff] [blame] | 625 | private void writeToFile(android.net.Uri data) { |
| 626 | File input = new File(mCurrentFileAbsolutePath); |
| 627 | if (requireContext().getContentResolver() != null) |
| 628 | mCompositeDisposable.add(AndroidFileUtils.copyFileToUri(requireContext().getContentResolver(), input, data). |
| 629 | observeOn(AndroidSchedulers.mainThread()). |
| 630 | subscribe(() -> Toast.makeText(getContext(), R.string.file_saved_successfully, Toast.LENGTH_SHORT).show(), |
| 631 | error -> Toast.makeText(getContext(), R.string.generic_error, Toast.LENGTH_SHORT).show())); |
| 632 | } |
| 633 | |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 634 | @Override |
Adrien Béraud | 4f542f9 | 2019-01-03 12:17:53 -0500 | [diff] [blame] | 635 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
Adrien Béraud | 4f542f9 | 2019-01-03 12:17:53 -0500 | [diff] [blame] | 636 | for (int i = 0, n = permissions.length; i < n; i++) { |
Adrien Béraud | d5637f2 | 2020-05-14 12:41:50 -0400 | [diff] [blame] | 637 | boolean granted = grantResults[i] == PackageManager.PERMISSION_GRANTED; |
Adrien Béraud | 4f542f9 | 2019-01-03 12:17:53 -0500 | [diff] [blame] | 638 | switch (permissions[i]) { |
| 639 | case Manifest.permission.CAMERA: |
Rayan Osseiran | 38206c6 | 2019-05-15 14:49:03 -0400 | [diff] [blame] | 640 | presenter.cameraPermissionChanged(granted); |
Adrien Béraud | 4f542f9 | 2019-01-03 12:17:53 -0500 | [diff] [blame] | 641 | if (granted) { |
Adrien Béraud | d5637f2 | 2020-05-14 12:41:50 -0400 | [diff] [blame] | 642 | if (requestCode == REQUEST_CODE_CAPTURE_VIDEO) { |
| 643 | sendVideoMessage(); |
| 644 | } else if (requestCode == REQUEST_CODE_TAKE_PICTURE) { |
| 645 | takePicture(); |
| 646 | } |
| 647 | } |
| 648 | return; |
| 649 | case Manifest.permission.RECORD_AUDIO: |
| 650 | if (granted) { |
| 651 | if (requestCode == REQUEST_CODE_CAPTURE_AUDIO) { |
| 652 | sendAudioMessage(); |
| 653 | } |
Adrien Béraud | 4f542f9 | 2019-01-03 12:17:53 -0500 | [diff] [blame] | 654 | } |
| 655 | return; |
| 656 | default: |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | @Override |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 663 | public void addElement(Interaction element) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 664 | mAdapter.add(element); |
| 665 | scrollToEnd(); |
| 666 | } |
Adrien Béraud | 3cbc7fa | 2018-07-18 18:36:21 -0400 | [diff] [blame] | 667 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 668 | @Override |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 669 | public void updateElement(Interaction element) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 670 | mAdapter.update(element); |
| 671 | } |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 672 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 673 | @Override |
Rayan Osseiran | 6f41566 | 2019-10-25 14:23:25 -0400 | [diff] [blame] | 674 | public void removeElement(Interaction element) { |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 675 | mAdapter.remove(element); |
Adrien Béraud | 2153329 | 2018-03-22 14:11:35 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Adrien Béraud | fcf457f | 2018-04-03 17:46:34 -0400 | [diff] [blame] | 678 | @Override |
Adrien Béraud | fd458ea | 2020-02-28 13:40:18 -0500 | [diff] [blame] | 679 | public void setComposingStatus(Account.ComposingStatus composingStatus) { |
| 680 | mAdapter.setComposingStatus(composingStatus); |
| 681 | if (composingStatus == Account.ComposingStatus.Active) |
| 682 | scrollToEnd(); |
| 683 | } |
| 684 | |
| 685 | @Override |
Adrien Béraud | 263cd5c | 2020-03-13 13:36:57 -0400 | [diff] [blame] | 686 | public void setLastDisplayed(Interaction interaction) { |
| 687 | mAdapter.setLastDisplayed(interaction); |
| 688 | } |
| 689 | |
| 690 | @Override |
Adrien Béraud | fcf457f | 2018-04-03 17:46:34 -0400 | [diff] [blame] | 691 | public void shareFile(File path) { |
Adrien Béraud | 5054953 | 2019-01-03 12:22:16 -0500 | [diff] [blame] | 692 | Context c = getContext(); |
| 693 | if (c == null) |
| 694 | return; |
Adrien Béraud | fcf457f | 2018-04-03 17:46:34 -0400 | [diff] [blame] | 695 | android.net.Uri fileUri = null; |
| 696 | try { |
Andreas Traczyk | bc5d235 | 2020-01-23 14:27:59 -0500 | [diff] [blame] | 697 | fileUri = ContentUriHandler.getUriForFile(c, ContentUriHandler.AUTHORITY_FILES, path); |
Adrien Béraud | fcf457f | 2018-04-03 17:46:34 -0400 | [diff] [blame] | 698 | } catch (IllegalArgumentException e) { |
Adrien Béraud | b2b3f44 | 2018-12-01 17:42:38 -0500 | [diff] [blame] | 699 | 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] | 700 | } |
| 701 | if (fileUri != null) { |
| 702 | Intent sendIntent = new Intent(); |
| 703 | sendIntent.setAction(Intent.ACTION_SEND); |
| 704 | sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Adrien Béraud | 5054953 | 2019-01-03 12:22:16 -0500 | [diff] [blame] | 705 | String type = c.getContentResolver().getType(fileUri); |
Adrien Béraud | fcf457f | 2018-04-03 17:46:34 -0400 | [diff] [blame] | 706 | sendIntent.setDataAndType(fileUri, type); |
| 707 | sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri); |
| 708 | startActivity(Intent.createChooser(sendIntent, null)); |
| 709 | } |
| 710 | } |
| 711 | |
Adrien Béraud | b2b3f44 | 2018-12-01 17:42:38 -0500 | [diff] [blame] | 712 | @Override |
| 713 | public void openFile(File path) { |
Adrien Béraud | 5054953 | 2019-01-03 12:22:16 -0500 | [diff] [blame] | 714 | Context c = getContext(); |
| 715 | if (c == null) |
| 716 | return; |
Adrien Béraud | b2b3f44 | 2018-12-01 17:42:38 -0500 | [diff] [blame] | 717 | android.net.Uri fileUri = null; |
| 718 | try { |
Andreas Traczyk | bc5d235 | 2020-01-23 14:27:59 -0500 | [diff] [blame] | 719 | fileUri = ContentUriHandler.getUriForFile(c, ContentUriHandler.AUTHORITY_FILES, path); |
Adrien Béraud | b2b3f44 | 2018-12-01 17:42:38 -0500 | [diff] [blame] | 720 | } catch (IllegalArgumentException e) { |
| 721 | Log.e("File Selector", "The selected file can't be shared: " + path.getName()); |
| 722 | } |
| 723 | if (fileUri != null) { |
| 724 | Intent sendIntent = new Intent(); |
| 725 | sendIntent.setAction(Intent.ACTION_VIEW); |
| 726 | sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Adrien Béraud | 5054953 | 2019-01-03 12:22:16 -0500 | [diff] [blame] | 727 | String type = c.getContentResolver().getType(fileUri); |
Adrien Béraud | b2b3f44 | 2018-12-01 17:42:38 -0500 | [diff] [blame] | 728 | sendIntent.setDataAndType(fileUri, type); |
| 729 | sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri); |
| 730 | //startActivity(Intent.createChooser(sendIntent, null)); |
Rayan Osseiran | e445027 | 2019-06-03 14:17:19 -0400 | [diff] [blame] | 731 | try { |
| 732 | startActivity(sendIntent); |
| 733 | } catch (ActivityNotFoundException e) { |
| 734 | Snackbar.make(getView(), R.string.conversation_open_file_error, Snackbar.LENGTH_LONG).show(); |
| 735 | Log.e("File Loader", "File of unknown type, could not open: " + path.getName()); |
| 736 | } |
Adrien Béraud | b2b3f44 | 2018-12-01 17:42:38 -0500 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 740 | boolean actionSendMsgText(int actionId) { |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 741 | switch (actionId) { |
| 742 | case EditorInfo.IME_ACTION_SEND: |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 743 | sendMessageText(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 744 | return true; |
| 745 | } |
| 746 | return false; |
| 747 | } |
| 748 | |
Alexandre Lision | 66a9f9a | 2017-02-26 20:28:27 -0500 | [diff] [blame] | 749 | public void onClick() { |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 750 | presenter.clickOnGoingPane(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | @Override |
| 754 | public void onPause() { |
| 755 | super.onPause(); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 756 | presenter.pause(); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | @Override |
| 760 | public void onResume() { |
| 761 | super.onResume(); |
Amirhossein | 6891638 | 2020-07-19 11:23:59 -0400 | [diff] [blame] | 762 | presenter.resume(mIsBubble); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | @Override |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 766 | public void onDestroy() { |
| 767 | mCompositeDisposable.dispose(); |
| 768 | super.onDestroy(); |
Adrien Béraud | 78ae7b9 | 2018-08-25 19:04:17 -0400 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | @Override |
Adrien Béraud | 263cd5c | 2020-03-13 13:36:57 -0400 | [diff] [blame] | 772 | public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { |
| 773 | if (!isVisible()) { |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 774 | return; |
| 775 | } |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 776 | inflater.inflate(R.menu.conversation_actions, menu); |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 777 | mAudioCallBtn = menu.findItem(R.id.conv_action_audiocall); |
| 778 | mVideoCallBtn = menu.findItem(R.id.conv_action_videocall); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | @Override |
| 782 | public boolean onOptionsItemSelected(MenuItem item) { |
| 783 | switch (item.getItemId()) { |
| 784 | case android.R.id.home: |
| 785 | startActivity(new Intent(getActivity(), HomeActivity.class)); |
| 786 | return true; |
| 787 | case R.id.conv_action_audiocall: |
Adrien Béraud | 4a64305 | 2019-11-18 12:41:23 -0500 | [diff] [blame] | 788 | presenter.goToCall(true); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 789 | return true; |
| 790 | case R.id.conv_action_videocall: |
Adrien Béraud | 4a64305 | 2019-11-18 12:41:23 -0500 | [diff] [blame] | 791 | presenter.goToCall(false); |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 792 | return true; |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 793 | case R.id.conv_contact_details: |
| 794 | presenter.openContact(); |
Aline Bonnet | 54cf69b | 2017-03-28 12:03:45 -0400 | [diff] [blame] | 795 | return true; |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 796 | default: |
| 797 | return super.onOptionsItemSelected(item); |
| 798 | } |
| 799 | } |
| 800 | |
Aline Bonnet | 9c6defe | 2016-12-21 13:21:02 -0500 | [diff] [blame] | 801 | @Override |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 802 | protected void initPresenter(ConversationPresenter presenter) { |
Adrien Béraud | bba7658 | 2020-05-15 19:18:23 -0400 | [diff] [blame] | 803 | ConversationPath path = ConversationPath.fromBundle(getArguments()); |
Amirhossein | 6891638 | 2020-07-19 11:23:59 -0400 | [diff] [blame] | 804 | mIsBubble = getArguments().getBoolean(NotificationServiceImpl.EXTRA_BUBBLE); |
Adrien Béraud | bba7658 | 2020-05-15 19:18:23 -0400 | [diff] [blame] | 805 | if (path == null) |
Adrien Béraud | 3c483ce | 2019-01-02 17:33:49 -0500 | [diff] [blame] | 806 | return; |
Adrien Béraud | 2d8a8a8 | 2020-10-15 11:05:38 -0400 | [diff] [blame] | 807 | |
| 808 | Uri contactUri = path.getConversationUri(); |
Adrien Béraud | 3c483ce | 2019-01-02 17:33:49 -0500 | [diff] [blame] | 809 | mAdapter = new ConversationAdapter(this, presenter); |
Adrien Béraud | bba7658 | 2020-05-15 19:18:23 -0400 | [diff] [blame] | 810 | presenter.init(contactUri, path.getAccountId()); |
Adrien Béraud | 56c3413 | 2018-11-29 13:32:00 -0500 | [diff] [blame] | 811 | try { |
Adrien Béraud | bba7658 | 2020-05-15 19:18:23 -0400 | [diff] [blame] | 812 | mPreferences = requireActivity().getSharedPreferences(path.getAccountId() + "_" + contactUri.getRawRingId(), Context.MODE_PRIVATE); |
Adrien Béraud | 3c483ce | 2019-01-02 17:33:49 -0500 | [diff] [blame] | 813 | mPreferences.registerOnSharedPreferenceChangeListener(this); |
| 814 | 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] | 815 | } catch (Exception e) { |
| 816 | Log.e(TAG, "Can't load conversation preferences"); |
| 817 | } |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 818 | |
| 819 | if (locationServiceConnection == null) { |
| 820 | locationServiceConnection = new ServiceConnection() { |
| 821 | @Override |
| 822 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 823 | Log.w(TAG, "onServiceConnected"); |
| 824 | LocationSharingService.LocalBinder binder = (LocationSharingService.LocalBinder) service; |
| 825 | LocationSharingService locationService = binder.getService(); |
| 826 | ConversationPath path = new ConversationPath(presenter.getPath()); |
| 827 | if (locationService.isSharing(path)) { |
Adrien Béraud | bba7658 | 2020-05-15 19:18:23 -0400 | [diff] [blame] | 828 | showMap(path.getAccountId(), contactUri.getUri(), false); |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 829 | } |
| 830 | try { |
| 831 | requireContext().unbindService(locationServiceConnection); |
| 832 | } catch (Exception e) { |
| 833 | Log.w(TAG, "Error unbinding service", e); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | @Override |
| 838 | public void onServiceDisconnected(ComponentName name) { |
| 839 | Log.w(TAG, "onServiceDisconnected"); |
| 840 | locationServiceConnection = null; |
| 841 | } |
| 842 | }; |
| 843 | |
| 844 | Log.w(TAG, "bindService"); |
| 845 | requireContext().bindService(new Intent(requireContext(), LocationSharingService.class), locationServiceConnection, 0); |
| 846 | } |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | @Override |
| 850 | public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
| 851 | switch (key) { |
| 852 | case KEY_PREFERENCE_CONVERSATION_COLOR: |
| 853 | presenter.setConversationColor(prefs.getInt(KEY_PREFERENCE_CONVERSATION_COLOR, getResources().getColor(R.color.color_primary_light))); |
| 854 | break; |
| 855 | } |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | @Override |
Adrien Béraud | 78ae7b9 | 2018-08-25 19:04:17 -0400 | [diff] [blame] | 859 | public void displayContact(final CallContact contact) { |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 860 | mCompositeDisposable.clear(); |
| 861 | mCompositeDisposable.add(AvatarFactory.getAvatar(requireContext(), contact) |
| 862 | .doOnSuccess(d -> { |
| 863 | mConversationAvatar = (AvatarDrawable) d; |
| 864 | mParticipantAvatars.put(contact.getPrimaryNumber(), |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 865 | new AvatarDrawable((AvatarDrawable) d)); |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 866 | }) |
| 867 | .flatMapObservable(d -> contact.getUpdatesSubject()) |
Adrien Béraud | 78ae7b9 | 2018-08-25 19:04:17 -0400 | [diff] [blame] | 868 | .observeOn(AndroidSchedulers.mainThread()) |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 869 | .subscribe(c -> { |
| 870 | mConversationAvatar.update(c); |
| 871 | String uri = contact.getPrimaryNumber(); |
Adrien Béraud | 263cd5c | 2020-03-13 13:36:57 -0400 | [diff] [blame] | 872 | AvatarDrawable ad = mParticipantAvatars.get(uri); |
| 873 | if (ad != null) |
| 874 | ad.update(c); |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 875 | setupActionbar(contact); |
| 876 | mAdapter.setPhoto(); |
| 877 | })); |
Adrien Béraud | 263cd5c | 2020-03-13 13:36:57 -0400 | [diff] [blame] | 878 | mCompositeDisposable.add(AvatarFactory.getAvatar(requireContext(), contact, false) |
| 879 | .doOnSuccess(d -> mSmallParticipantAvatars.put(contact.getPrimaryNumber(), new AvatarDrawable((AvatarDrawable) d))) |
| 880 | .flatMapObservable(d -> contact.getUpdatesSubject()) |
| 881 | .subscribe(c -> { |
| 882 | synchronized (mSmallParticipantAvatars) { |
| 883 | String uri = contact.getPrimaryNumber(); |
| 884 | AvatarDrawable ad = mSmallParticipantAvatars.get(uri); |
| 885 | if (ad != null) |
| 886 | ad.update(c); |
| 887 | } |
| 888 | })); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | @Override |
| 892 | public void displayOnGoingCallPane(final boolean display) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 893 | binding.ongoingcallPane.setVisibility(display ? View.VISIBLE : View.GONE); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | @Override |
| 897 | public void displayNumberSpinner(final Conversation conversation, final Uri number) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 898 | binding.numberSelector.setVisibility(View.VISIBLE); |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 899 | binding.numberSelector.setAdapter(new NumberAdapter(getActivity(), |
| 900 | conversation.getContact(), false)); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 901 | binding.numberSelector.setSelection(getIndex(binding.numberSelector, number)); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | @Override |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 905 | public void hideNumberSpinner() { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 906 | binding.numberSelector.setVisibility(View.GONE); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | @Override |
| 910 | public void clearMsgEdit() { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 911 | binding.msgInputTxt.setText(""); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public void goToHome() { |
| 916 | if (getActivity() instanceof ConversationActivity) { |
| 917 | getActivity().finish(); |
Aline Bonnet | eb5f7ff | 2017-01-17 11:09:17 -0500 | [diff] [blame] | 918 | } |
| 919 | } |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 920 | |
| 921 | @Override |
| 922 | public void goToAddContact(CallContact callContact) { |
| 923 | startActivityForResult(ActionHelper.getAddNumberIntentForContact(callContact), REQ_ADD_CONTACT); |
| 924 | } |
| 925 | |
| 926 | @Override |
| 927 | public void goToCallActivity(String conferenceId) { |
| 928 | startActivity(new Intent(Intent.ACTION_VIEW) |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 929 | .setClass(requireActivity().getApplicationContext(), CallActivity.class) |
Hadrien De Sousa | ccc947d | 2017-04-12 14:26:52 -0400 | [diff] [blame] | 930 | .putExtra(NotificationService.KEY_CALL_ID, conferenceId)); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | @Override |
Adrien Béraud | 770ef47 | 2020-10-19 16:47:14 -0400 | [diff] [blame] | 934 | public void goToContactActivity(String accountId, String contactId) { |
| 935 | startActivity(new Intent(Intent.ACTION_VIEW, ConversationPath.toUri(accountId, contactId), |
| 936 | requireActivity().getApplicationContext(), ContactDetailsActivity.class)); |
Adrien Béraud | 0f3ee3d | 2018-12-08 22:03:54 -0500 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | @Override |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 940 | public void goToCallActivityWithResult(String accountId, String contactRingId, boolean audioOnly) { |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 941 | Intent intent = new Intent(CallActivity.ACTION_CALL) |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 942 | .setClass(requireActivity().getApplicationContext(), CallActivity.class) |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 943 | .putExtra(KEY_ACCOUNT_ID, accountId) |
Hadrien De Sousa | 203164c | 2017-12-05 17:41:42 -0500 | [diff] [blame] | 944 | .putExtra(CallFragment.KEY_AUDIO_ONLY, audioOnly) |
Hadrien De Sousa | 0dc8316 | 2017-11-10 16:15:18 -0500 | [diff] [blame] | 945 | .putExtra(KEY_CONTACT_RING_ID, contactRingId); |
Hadrien De Sousa | ec95e5e | 2017-03-21 15:28:23 -0400 | [diff] [blame] | 946 | startActivityForResult(intent, HomeActivity.REQUEST_CODE_CALL); |
| 947 | } |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 948 | |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 949 | private void setupActionbar(CallContact contact) { |
Adrien Béraud | 21a3ea2 | 2020-04-01 16:06:09 -0400 | [diff] [blame] | 950 | if (!isVisible()) { |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 951 | return; |
| 952 | } |
| 953 | |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 954 | ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar(); |
| 955 | if (actionBar == null) { |
| 956 | return; |
| 957 | } |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 958 | |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 959 | Context context = actionBar.getThemedContext(); |
| 960 | String displayName = contact.getDisplayName(); |
| 961 | String identity = contact.getRingUsername(); |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 962 | |
Adrien Béraud | 21a3ea2 | 2020-04-01 16:06:09 -0400 | [diff] [blame] | 963 | Activity activity = getActivity(); |
| 964 | if (activity instanceof HomeActivity) { |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 965 | Toolbar toolbar = getActivity().findViewById(R.id.main_toolbar); |
| 966 | TextView title = toolbar.findViewById(R.id.contact_title); |
| 967 | TextView subtitle = toolbar.findViewById(R.id.contact_subtitle); |
| 968 | ImageView logo = toolbar.findViewById(R.id.contact_image); |
| 969 | |
Adrien Béraud | 21a3ea2 | 2020-04-01 16:06:09 -0400 | [diff] [blame] | 970 | if (!((HomeActivity) activity).isConversationSelected()) { |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 971 | title.setText(""); |
| 972 | subtitle.setText(""); |
| 973 | logo.setImageDrawable(null); |
Adrien Béraud | b96a7d9 | 2020-02-24 11:55:05 -0500 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | |
| 977 | logo.setVisibility(View.VISIBLE); |
| 978 | title.setText(displayName); |
| 979 | title.setTextSize(15); |
| 980 | title.setTypeface(null, Typeface.NORMAL); |
| 981 | |
| 982 | if (identity != null && !identity.equals(displayName)) { |
| 983 | subtitle.setText(identity); |
| 984 | |
| 985 | RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title.getLayoutParams(); |
| 986 | params.addRule(RelativeLayout.ALIGN_TOP, R.id.contact_image); |
| 987 | title.setLayoutParams(params); |
| 988 | } else { |
| 989 | subtitle.setText(""); |
| 990 | |
| 991 | RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title.getLayoutParams(); |
| 992 | params.removeRule(RelativeLayout.ALIGN_TOP); |
| 993 | params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); |
| 994 | title.setLayoutParams(params); |
| 995 | } |
| 996 | |
| 997 | logo.setImageDrawable(mConversationAvatar); |
| 998 | } else { |
| 999 | if (identity != null && !identity.equals(displayName)) { |
| 1000 | actionBar.setSubtitle(identity); |
| 1001 | } |
| 1002 | actionBar.setTitle(displayName); |
| 1003 | int targetSize = (int) (AvatarFactory.SIZE_AB * context.getResources().getDisplayMetrics().density); |
| 1004 | mConversationAvatar.setInSize(targetSize); |
| 1005 | actionBar.setLogo(null); |
| 1006 | actionBar.setLogo(mConversationAvatar); |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 1007 | } |
Andreas Traczyk | cee415a | 2019-12-03 15:45:04 -0500 | [diff] [blame] | 1008 | } |
| 1009 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1010 | public void blockContactRequest() { |
| 1011 | presenter.onBlockIncomingContactRequest(); |
| 1012 | } |
| 1013 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1014 | public void refuseContactRequest() { |
| 1015 | presenter.onRefuseIncomingContactRequest(); |
| 1016 | } |
| 1017 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1018 | public void acceptContactRequest() { |
| 1019 | presenter.onAcceptIncomingContactRequest(); |
| 1020 | } |
| 1021 | |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 1022 | public void addContact() { |
| 1023 | presenter.onAddContact(); |
| 1024 | } |
| 1025 | |
| 1026 | @Override |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 1027 | public void onPrepareOptionsMenu(@NonNull Menu menu) { |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 1028 | super.onPrepareOptionsMenu(menu); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1029 | boolean visible = binding.cvMessageInput.getVisibility() == View.VISIBLE; |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 1030 | if (mAudioCallBtn != null) |
| 1031 | mAudioCallBtn.setVisible(visible); |
| 1032 | if (mVideoCallBtn != null) |
| 1033 | mVideoCallBtn.setVisible(visible); |
| 1034 | } |
| 1035 | |
| 1036 | @Override |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 1037 | public void switchToUnknownView(String contactDisplayName) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1038 | binding.cvMessageInput.setVisibility(View.GONE); |
| 1039 | binding.unknownContactPrompt.setVisibility(View.VISIBLE); |
| 1040 | binding.trustRequestPrompt.setVisibility(View.GONE); |
| 1041 | binding.tvTrustRequestMessage.setText(String.format(getString(R.string.message_contact_not_trusted), contactDisplayName)); |
| 1042 | binding.trustRequestMessageLayout.setVisibility(View.VISIBLE); |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 1043 | currentBottomView = binding.unknownContactPrompt; |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 1044 | requireActivity().invalidateOptionsMenu(); |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 1045 | updateListPadding(); |
Adrien Béraud | ace37a2 | 2018-06-22 14:58:36 -0400 | [diff] [blame] | 1046 | } |
| 1047 | |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1048 | @Override |
| 1049 | public void switchToIncomingTrustRequestView(String contactDisplayName) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1050 | binding.cvMessageInput.setVisibility(View.GONE); |
| 1051 | binding.unknownContactPrompt.setVisibility(View.GONE); |
| 1052 | binding.trustRequestPrompt.setVisibility(View.VISIBLE); |
| 1053 | binding.tvTrustRequestMessage.setText(String.format(getString(R.string.message_contact_not_trusted_yet), contactDisplayName)); |
| 1054 | binding.trustRequestMessageLayout.setVisibility(View.VISIBLE); |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 1055 | currentBottomView = binding.trustRequestPrompt; |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 1056 | requireActivity().invalidateOptionsMenu(); |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 1057 | updateListPadding(); |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | @Override |
| 1061 | public void switchToConversationView() { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1062 | binding.cvMessageInput.setVisibility(View.VISIBLE); |
| 1063 | binding.unknownContactPrompt.setVisibility(View.GONE); |
| 1064 | binding.trustRequestPrompt.setVisibility(View.GONE); |
| 1065 | binding.trustRequestMessageLayout.setVisibility(View.GONE); |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 1066 | currentBottomView = binding.cvMessageInput; |
Adrien Béraud | 4493948 | 2019-02-13 21:42:08 -0500 | [diff] [blame] | 1067 | requireActivity().invalidateOptionsMenu(); |
Adrien Béraud | 0dcc71b | 2019-09-25 13:23:44 -0400 | [diff] [blame] | 1068 | updateListPadding(); |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | @Override |
| 1072 | public void positiveMediaButtonClicked() { |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 1073 | presenter.clickOnGoingPane(); |
| 1074 | } |
| 1075 | |
| 1076 | @Override |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1077 | public void negativeMediaButtonClicked() { |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 1078 | presenter.clickOnGoingPane(); |
| 1079 | } |
| 1080 | |
| 1081 | @Override |
Pierre Duchemin | 1fa6192 | 2018-01-18 10:09:02 -0500 | [diff] [blame] | 1082 | public void toggleMediaButtonClicked() { |
Pierre Duchemin | 8b2c1b5 | 2017-12-29 17:17:13 -0500 | [diff] [blame] | 1083 | presenter.clickOnGoingPane(); |
| 1084 | } |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 1085 | |
| 1086 | private void setLoading(boolean isLoading) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1087 | if (binding == null) |
Adrien Béraud | 5b47cb5 | 2018-07-06 16:24:14 -0400 | [diff] [blame] | 1088 | return; |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 1089 | if (isLoading) { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1090 | binding.btnTakePicture.setVisibility(View.GONE); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1091 | binding.pbDataTransfer.setVisibility(View.VISIBLE); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 1092 | } else { |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1093 | binding.btnTakePicture.setVisibility(View.VISIBLE); |
Adrien Béraud | b28c491 | 2018-12-02 22:58:52 -0500 | [diff] [blame] | 1094 | binding.pbDataTransfer.setVisibility(View.GONE); |
Pierre Duchemin | 17542a7 | 2018-02-14 16:38:36 -0500 | [diff] [blame] | 1095 | } |
| 1096 | } |
Adrien Béraud | 1b8ac9f | 2018-05-31 14:34:23 -0400 | [diff] [blame] | 1097 | |
Adrien Béraud | ae45ffa | 2018-07-06 16:21:29 -0400 | [diff] [blame] | 1098 | public void handleShareIntent(Intent intent) { |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 1099 | Log.w(TAG, "handleShareIntent " + intent); |
| 1100 | |
| 1101 | String action = intent.getAction(); |
| 1102 | if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 1103 | String type = intent.getType(); |
| 1104 | if (type == null) { |
| 1105 | Log.w(TAG, "Can't share with no type"); |
Adrien Béraud | ae45ffa | 2018-07-06 16:21:29 -0400 | [diff] [blame] | 1106 | return; |
Adrien Béraud | 5477ce8 | 2020-02-24 11:13:54 -0500 | [diff] [blame] | 1107 | } |
| 1108 | if (type.startsWith("text/plain")) { |
| 1109 | binding.msgInputTxt.setText(intent.getStringExtra(Intent.EXTRA_TEXT)); |
| 1110 | } else { |
| 1111 | android.net.Uri uri = intent.getData(); |
| 1112 | ClipData clip = intent.getClipData(); |
| 1113 | if (uri == null && clip != null && clip.getItemCount() > 0) |
| 1114 | uri = clip.getItemAt(0).getUri(); |
| 1115 | if (uri == null) |
| 1116 | return; |
| 1117 | startFileSend(AndroidFileUtils.getCacheFile(requireContext(), uri).flatMapCompletable(this::sendFile)); |
| 1118 | } |
| 1119 | } else if (Intent.ACTION_VIEW.equals(action)) { |
| 1120 | ConversationPath path = ConversationPath.fromIntent(intent); |
| 1121 | if (path != null && intent.getBooleanExtra(EXTRA_SHOW_MAP, false)) { |
| 1122 | shareLocation(); |
| 1123 | } |
Adrien Béraud | ae45ffa | 2018-07-06 16:21:29 -0400 | [diff] [blame] | 1124 | } |
| 1125 | } |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 1126 | |
| 1127 | /** |
| 1128 | * Creates an intent using Android Storage Access Framework |
| 1129 | * This intent is then received by applications that can handle it like |
| 1130 | * Downloads or Google drive |
| 1131 | * @param file DataTransfer of the file that is going to be stored |
| 1132 | * @param currentFileAbsolutePath absolute path of the file we want to save |
| 1133 | */ |
| 1134 | public void startSaveFile(DataTransfer file, String currentFileAbsolutePath){ |
| 1135 | //Get the current file absolute path and store it |
| 1136 | mCurrentFileAbsolutePath = currentFileAbsolutePath; |
| 1137 | |
Adrien Béraud | b9a1cfe | 2020-08-02 09:06:26 -0400 | [diff] [blame] | 1138 | try { |
| 1139 | //Use Android Storage File Access to download the file |
| 1140 | Intent downloadFileIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT); |
| 1141 | downloadFileIntent.setType(AndroidFileUtils.getMimeTypeFromExtension(file.getExtension())); |
| 1142 | downloadFileIntent.addCategory(Intent.CATEGORY_OPENABLE); |
| 1143 | downloadFileIntent.putExtra(Intent.EXTRA_TITLE,file.getDisplayName()); |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 1144 | |
Adrien Béraud | b9a1cfe | 2020-08-02 09:06:26 -0400 | [diff] [blame] | 1145 | startActivityForResult(downloadFileIntent, ConversationFragment.REQUEST_CODE_SAVE_FILE); |
| 1146 | } catch (Exception e) { |
| 1147 | Log.i(TAG, "No app detected for saving files."); |
| 1148 | File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); |
| 1149 | if (!directory.exists()) { |
| 1150 | directory.mkdirs(); |
| 1151 | } |
| 1152 | writeToFile(android.net.Uri.fromFile(new File(directory, file.getDisplayName()))); |
| 1153 | } |
amine younes | 7c56f31 | 2019-06-10 11:34:25 -0400 | [diff] [blame] | 1154 | } |
| 1155 | |
Amirhossein | e0846c5 | 2020-03-12 15:34:35 -0400 | [diff] [blame] | 1156 | @Override |
| 1157 | public void displayNetworkErrorPanel() { |
Adrien Béraud | c35f7f4 | 2020-04-15 15:29:29 -0400 | [diff] [blame] | 1158 | if (binding != null) { |
| 1159 | binding.errorMsgPane.setVisibility(View.VISIBLE); |
Adrien Béraud | c279092 | 2020-04-28 17:34:03 -0400 | [diff] [blame] | 1160 | binding.errorMsgPane.setOnClickListener(null); |
| 1161 | binding.errorMsgPane.setText(R.string.error_no_network); |
Amirhossein | e0846c5 | 2020-03-12 15:34:35 -0400 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | @Override |
| 1166 | public void hideErrorPanel() { |
Adrien Béraud | c35f7f4 | 2020-04-15 15:29:29 -0400 | [diff] [blame] | 1167 | if (binding != null) { |
| 1168 | binding.errorMsgPane.setVisibility(View.GONE); |
Amirhossein | e0846c5 | 2020-03-12 15:34:35 -0400 | [diff] [blame] | 1169 | } |
Amirhossein | e0846c5 | 2020-03-12 15:34:35 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
Adrien Béraud | e32ab74 | 2018-08-25 21:15:08 -0400 | [diff] [blame] | 1172 | } |