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