1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.ui;
31
32
33import android.app.Fragment;
34import android.app.FragmentManager;
35import android.app.FragmentTransaction;
36import android.content.Context;
37import android.content.Intent;
38import android.databinding.DataBindingUtil;
39import android.os.Bundle;
40import android.support.annotation.IdRes;
41import android.support.v7.app.ActionBar;
42import android.util.Log;
43import android.view.Menu;
44import android.view.MenuItem;
45import android.widget.Toast;
46
47import eu.siacs.conversations.Config;
48import eu.siacs.conversations.R;
49import eu.siacs.conversations.databinding.ActivityConversationsBinding;
50import eu.siacs.conversations.entities.Conversation;
51import eu.siacs.conversations.services.XmppConnectionService;
52import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
53import eu.siacs.conversations.ui.interfaces.OnConversationRead;
54import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
55import eu.siacs.conversations.ui.interfaces.OnConversationsListItemUpdated;
56import eu.siacs.conversations.ui.service.EmojiService;
57import eu.siacs.conversations.ui.util.PendingItem;
58import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
59
60public class ConversationActivity extends XmppActivity implements OnConversationSelected, OnConversationArchived, OnConversationsListItemUpdated, OnConversationRead, XmppConnectionService.OnAccountUpdate, XmppConnectionService.OnConversationUpdate, XmppConnectionService.OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
61
62 public static final String ACTION_VIEW_CONVERSATION = "eu.siacs.conversations.action.VIEW";
63 public static final String EXTRA_CONVERSATION = "conversationUuid";
64 public static final String EXTRA_DOWNLOAD_UUID = "eu.siacs.conversations.download_uuid";
65 public static final String EXTRA_TEXT = "text";
66 public static final String EXTRA_NICK = "nick";
67 public static final String EXTRA_IS_PRIVATE_MESSAGE = "pm";
68
69
70 //secondary fragment (when holding the conversation, must be initialized before refreshing the overview fragment
71 private static final @IdRes
72 int[] FRAGMENT_ID_NOTIFICATION_ORDER = {R.id.secondary_fragment, R.id.main_fragment};
73 private final PendingItem<Intent> pendingViewIntent = new PendingItem<>();
74 private ActivityConversationsBinding binding;
75 private boolean mActivityPaused = true;
76
77 private static boolean isViewIntent(Intent i) {
78 return i != null && ACTION_VIEW_CONVERSATION.equals(i.getAction()) && i.hasExtra(EXTRA_CONVERSATION);
79 }
80
81 private static Intent createLauncherIntent(Context context) {
82 final Intent intent = new Intent(context, ConversationActivity.class);
83 intent.setAction(Intent.ACTION_MAIN);
84 intent.addCategory(Intent.CATEGORY_LAUNCHER);
85 return intent;
86 }
87
88 @Override
89 protected void refreshUiReal() {
90 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
91 refreshFragment(id);
92 }
93 }
94
95 @Override
96 void onBackendConnected() {
97 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
98 notifyFragmentOfBackendConnected(id);
99 }
100 invalidateActionBarTitle();
101 Intent intent = pendingViewIntent.pop();
102 if (intent != null) {
103 if (processViewIntent(intent)) {
104 return;
105 }
106 }
107 if (binding.secondaryFragment != null && ConversationFragment.getConversation(this) == null) {
108 Conversation conversation = ConversationsOverviewFragment.getSuggestion(this);
109 if (conversation != null) {
110 openConversation(conversation, null);
111 }
112 }
113 }
114
115 private void notifyFragmentOfBackendConnected(@IdRes int id) {
116 final Fragment fragment = getFragmentManager().findFragmentById(id);
117 if (fragment != null && fragment instanceof XmppFragment) {
118 ((XmppFragment) fragment).onBackendConnected();
119 }
120 }
121
122 private void refreshFragment(@IdRes int id) {
123 final Fragment fragment = getFragmentManager().findFragmentById(id);
124 if (fragment != null && fragment instanceof XmppFragment) {
125 ((XmppFragment) fragment).refresh();
126 }
127 }
128
129 private boolean processViewIntent(Intent intent) {
130 String uuid = intent.getStringExtra(EXTRA_CONVERSATION);
131 Conversation conversation = uuid != null ? xmppConnectionService.findConversationByUuid(uuid) : null;
132 if (conversation == null) {
133 Log.d(Config.LOGTAG, "unable to view conversation with uuid:" + uuid);
134 return false;
135 }
136 openConversation(conversation, intent.getExtras());
137 return true;
138 }
139
140 @Override
141 protected void onCreate(final Bundle savedInstanceState) {
142 super.onCreate(savedInstanceState);
143 new EmojiService(this).init();
144 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations);
145 this.getFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle);
146 this.initializeFragments();
147 this.invalidateActionBarTitle();
148 final Intent intent = getIntent();
149 if (isViewIntent(intent)) {
150 pendingViewIntent.push(intent);
151 setIntent(createLauncherIntent(this));
152 }
153 }
154
155 @Override
156 public boolean onCreateOptionsMenu(Menu menu) {
157 getMenuInflater().inflate(R.menu.activity_conversations, menu);
158 return super.onCreateOptionsMenu(menu);
159 }
160
161 @Override
162 public void onConversationSelected(Conversation conversation) {
163 Log.d(Config.LOGTAG, "selected " + conversation.getName());
164 openConversation(conversation, null);
165 }
166
167 private void openConversation(Conversation conversation, Bundle extras) {
168 ConversationFragment conversationFragment = (ConversationFragment) getFragmentManager().findFragmentById(R.id.secondary_fragment);
169 final boolean mainNeedsRefresh;
170 if (conversationFragment == null) {
171 mainNeedsRefresh = false;
172 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
173 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
174 conversationFragment = (ConversationFragment) mainFragment;
175 } else {
176 conversationFragment = new ConversationFragment();
177 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
178 fragmentTransaction.replace(R.id.main_fragment, conversationFragment);
179 fragmentTransaction.addToBackStack(null);
180 fragmentTransaction.commit();
181 }
182 } else {
183 mainNeedsRefresh = true;
184 }
185 conversationFragment.reInit(conversation);
186 if (mainNeedsRefresh) {
187 refreshFragment(R.id.main_fragment);
188 } else {
189 invalidateActionBarTitle();
190 }
191 }
192
193 @Override
194 public boolean onOptionsItemSelected(MenuItem item) {
195 switch (item.getItemId()) {
196 case android.R.id.home:
197 FragmentManager fm = getFragmentManager();
198 if (fm.getBackStackEntryCount() > 0) {
199 fm.popBackStack();
200 return true;
201 }
202 break;
203 }
204 return super.onOptionsItemSelected(item);
205 }
206
207 @Override
208 protected void onStart() {
209 final int theme = findTheme();
210 if (this.mTheme != theme) {
211 recreate();
212 }
213 super.onStart();
214 }
215
216 @Override
217 protected void onNewIntent(final Intent intent) {
218 if (isViewIntent(intent)) {
219 if (xmppConnectionService != null) {
220 processViewIntent(intent);
221 } else {
222 pendingViewIntent.push(intent);
223 }
224 }
225 }
226
227 @Override
228 public void onPause() {
229 this.mActivityPaused = true;
230 super.onPause();
231 }
232
233 @Override
234 public void onResume() {
235 super.onResume();
236 this.mActivityPaused = false;
237 }
238
239 private void initializeFragments() {
240 FragmentTransaction transaction = getFragmentManager().beginTransaction();
241 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
242 Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
243 if (mainFragment != null) {
244 Log.d(Config.LOGTAG, "initializeFragment(). main fragment exists");
245 if (binding.secondaryFragment != null) {
246 if (mainFragment instanceof ConversationFragment) {
247 Log.d(Config.LOGTAG, "gained secondary fragment. moving...");
248 getFragmentManager().popBackStack();
249 transaction.remove(mainFragment);
250 transaction.commit();
251 getFragmentManager().executePendingTransactions();
252 transaction = getFragmentManager().beginTransaction();
253 transaction.replace(R.id.secondary_fragment, mainFragment);
254 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
255 transaction.commit();
256 return;
257 }
258 } else {
259 if (secondaryFragment != null && secondaryFragment instanceof ConversationFragment) {
260 Log.d(Config.LOGTAG, "lost secondary fragment. moving...");
261 transaction.remove(secondaryFragment);
262 transaction.commit();
263 getFragmentManager().executePendingTransactions();
264 transaction = getFragmentManager().beginTransaction();
265 transaction.replace(R.id.main_fragment, secondaryFragment);
266 transaction.addToBackStack(null);
267 transaction.commit();
268 return;
269 }
270 }
271 } else {
272 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
273 }
274 if (binding.secondaryFragment != null && secondaryFragment == null) {
275 transaction.replace(R.id.secondary_fragment, new ConversationFragment());
276 }
277 transaction.commit();
278 }
279
280 private void invalidateActionBarTitle() {
281 final ActionBar actionBar = getSupportActionBar();
282 if (actionBar != null) {
283 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
284 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
285 final Conversation conversation = ((ConversationFragment) mainFragment).getConversation();
286 if (conversation != null) {
287 actionBar.setTitle(conversation.getName());
288 actionBar.setDisplayHomeAsUpEnabled(true);
289 return;
290 }
291 }
292 actionBar.setTitle(R.string.app_name);
293 actionBar.setDisplayHomeAsUpEnabled(false);
294 }
295 }
296
297 @Override
298 public void onConversationArchived(Conversation conversation) {
299 //TODO; check if nothing more left;
300 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
301 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
302 getFragmentManager().popBackStack();
303 return;
304 }
305 Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
306 if (secondaryFragment != null && secondaryFragment instanceof ConversationFragment) {
307 if (((ConversationFragment) secondaryFragment).getConversation() == conversation) {
308 Conversation suggestion = ConversationsOverviewFragment.getSuggestion(this, conversation);
309 if (suggestion != null) {
310 openConversation(suggestion, null);
311 return;
312 }
313 }
314 }
315 }
316
317 @Override
318 public void onConversationsListItemUpdated() {
319 Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
320 if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
321 ((ConversationsOverviewFragment) fragment).refresh();
322 }
323 }
324
325 @Override
326 public void onConversationRead(Conversation conversation) {
327 if (!mActivityPaused && pendingViewIntent.peek() == null) {
328 xmppConnectionService.sendReadMarker(conversation);
329 }
330 }
331
332 @Override
333 public void onAccountUpdate() {
334 this.refreshUi();
335 }
336
337 @Override
338 public void onConversationUpdate() {
339 this.refreshUi();
340 }
341
342 @Override
343 public void onRosterUpdate() {
344 this.refreshUi();
345 }
346
347 @Override
348 public void OnUpdateBlocklist(OnUpdateBlocklist.Status status) {
349 this.refreshUi();
350 }
351
352 @Override
353 public void onShowErrorToast(int resId) {
354 runOnUiThread(() -> Toast.makeText(this, resId, Toast.LENGTH_SHORT).show());
355 }
356}