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 xmppConnectionService.getNotificationService().setIsInForeground(true);
102 Intent intent = pendingViewIntent.pop();
103 if (intent != null) {
104 if (processViewIntent(intent)) {
105 return;
106 }
107 }
108 if (binding.secondaryFragment != null && ConversationFragment.getConversation(this) == null) {
109 Conversation conversation = ConversationsOverviewFragment.getSuggestion(this);
110 if (conversation != null) {
111 openConversation(conversation, null);
112 }
113 }
114 }
115
116 private void notifyFragmentOfBackendConnected(@IdRes int id) {
117 final Fragment fragment = getFragmentManager().findFragmentById(id);
118 if (fragment != null && fragment instanceof XmppFragment) {
119 ((XmppFragment) fragment).onBackendConnected();
120 }
121 }
122
123 private void refreshFragment(@IdRes int id) {
124 final Fragment fragment = getFragmentManager().findFragmentById(id);
125 if (fragment != null && fragment instanceof XmppFragment) {
126 ((XmppFragment) fragment).refresh();
127 }
128 }
129
130 private boolean processViewIntent(Intent intent) {
131 String uuid = intent.getStringExtra(EXTRA_CONVERSATION);
132 Conversation conversation = uuid != null ? xmppConnectionService.findConversationByUuid(uuid) : null;
133 if (conversation == null) {
134 Log.d(Config.LOGTAG, "unable to view conversation with uuid:" + uuid);
135 return false;
136 }
137 openConversation(conversation, intent.getExtras());
138 return true;
139 }
140
141 @Override
142 protected void onCreate(final Bundle savedInstanceState) {
143 super.onCreate(savedInstanceState);
144 new EmojiService(this).init();
145 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations);
146 this.getFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle);
147 this.initializeFragments();
148 this.invalidateActionBarTitle();
149 final Intent intent = getIntent();
150 if (isViewIntent(intent)) {
151 pendingViewIntent.push(intent);
152 setIntent(createLauncherIntent(this));
153 }
154 }
155
156 @Override
157 public boolean onCreateOptionsMenu(Menu menu) {
158 getMenuInflater().inflate(R.menu.activity_conversations, menu);
159 return super.onCreateOptionsMenu(menu);
160 }
161
162 @Override
163 public void onConversationSelected(Conversation conversation) {
164 Log.d(Config.LOGTAG, "selected " + conversation.getName());
165 openConversation(conversation, null);
166 }
167
168 private void openConversation(Conversation conversation, Bundle extras) {
169 ConversationFragment conversationFragment = (ConversationFragment) getFragmentManager().findFragmentById(R.id.secondary_fragment);
170 final boolean mainNeedsRefresh;
171 if (conversationFragment == null) {
172 mainNeedsRefresh = false;
173 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
174 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
175 conversationFragment = (ConversationFragment) mainFragment;
176 } else {
177 conversationFragment = new ConversationFragment();
178 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
179 fragmentTransaction.replace(R.id.main_fragment, conversationFragment);
180 fragmentTransaction.addToBackStack(null);
181 fragmentTransaction.commit();
182 }
183 } else {
184 mainNeedsRefresh = true;
185 }
186 conversationFragment.reInit(conversation, extras);
187 if (mainNeedsRefresh) {
188 refreshFragment(R.id.main_fragment);
189 } else {
190 invalidateActionBarTitle();
191 }
192 }
193
194 @Override
195 public boolean onOptionsItemSelected(MenuItem item) {
196 switch (item.getItemId()) {
197 case android.R.id.home:
198 FragmentManager fm = getFragmentManager();
199 if (fm.getBackStackEntryCount() > 0) {
200 fm.popBackStack();
201 return true;
202 }
203 break;
204 }
205 return super.onOptionsItemSelected(item);
206 }
207
208 @Override
209 protected void onStart() {
210 final int theme = findTheme();
211 if (this.mTheme != theme) {
212 recreate();
213 }
214 super.onStart();
215 }
216
217 @Override
218 protected void onNewIntent(final Intent intent) {
219 if (isViewIntent(intent)) {
220 if (xmppConnectionService != null) {
221 processViewIntent(intent);
222 } else {
223 pendingViewIntent.push(intent);
224 }
225 }
226 }
227
228 @Override
229 public void onPause() {
230 this.mActivityPaused = true;
231 super.onPause();
232 }
233
234 @Override
235 public void onResume() {
236 super.onResume();
237 this.mActivityPaused = false;
238 }
239
240 private void initializeFragments() {
241 FragmentTransaction transaction = getFragmentManager().beginTransaction();
242 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
243 Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
244 if (mainFragment != null) {
245 Log.d(Config.LOGTAG, "initializeFragment(). main fragment exists");
246 if (binding.secondaryFragment != null) {
247 if (mainFragment instanceof ConversationFragment) {
248 Log.d(Config.LOGTAG, "gained secondary fragment. moving...");
249 getFragmentManager().popBackStack();
250 transaction.remove(mainFragment);
251 transaction.commit();
252 getFragmentManager().executePendingTransactions();
253 transaction = getFragmentManager().beginTransaction();
254 transaction.replace(R.id.secondary_fragment, mainFragment);
255 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
256 transaction.commit();
257 return;
258 }
259 } else {
260 if (secondaryFragment != null && secondaryFragment instanceof ConversationFragment) {
261 Log.d(Config.LOGTAG, "lost secondary fragment. moving...");
262 transaction.remove(secondaryFragment);
263 transaction.commit();
264 getFragmentManager().executePendingTransactions();
265 transaction = getFragmentManager().beginTransaction();
266 transaction.replace(R.id.main_fragment, secondaryFragment);
267 transaction.addToBackStack(null);
268 transaction.commit();
269 return;
270 }
271 }
272 } else {
273 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
274 }
275 if (binding.secondaryFragment != null && secondaryFragment == null) {
276 transaction.replace(R.id.secondary_fragment, new ConversationFragment());
277 }
278 transaction.commit();
279 }
280
281 private void invalidateActionBarTitle() {
282 final ActionBar actionBar = getSupportActionBar();
283 if (actionBar != null) {
284 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
285 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
286 final Conversation conversation = ((ConversationFragment) mainFragment).getConversation();
287 if (conversation != null) {
288 actionBar.setTitle(conversation.getName());
289 actionBar.setDisplayHomeAsUpEnabled(true);
290 return;
291 }
292 }
293 actionBar.setTitle(R.string.app_name);
294 actionBar.setDisplayHomeAsUpEnabled(false);
295 }
296 }
297
298 @Override
299 public void onConversationArchived(Conversation conversation) {
300 //TODO; check if nothing more left;
301 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
302 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
303 getFragmentManager().popBackStack();
304 return;
305 }
306 Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
307 if (secondaryFragment != null && secondaryFragment instanceof ConversationFragment) {
308 if (((ConversationFragment) secondaryFragment).getConversation() == conversation) {
309 Conversation suggestion = ConversationsOverviewFragment.getSuggestion(this, conversation);
310 if (suggestion != null) {
311 openConversation(suggestion, null);
312 return;
313 }
314 }
315 }
316 }
317
318 @Override
319 public void onConversationsListItemUpdated() {
320 Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
321 if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
322 ((ConversationsOverviewFragment) fragment).refresh();
323 }
324 }
325
326 @Override
327 public void onConversationRead(Conversation conversation) {
328 if (!mActivityPaused && pendingViewIntent.peek() == null) {
329 xmppConnectionService.sendReadMarker(conversation);
330 }
331 }
332
333 @Override
334 public void onAccountUpdate() {
335 this.refreshUi();
336 }
337
338 @Override
339 public void onConversationUpdate() {
340 this.refreshUi();
341 }
342
343 @Override
344 public void onRosterUpdate() {
345 this.refreshUi();
346 }
347
348 @Override
349 public void OnUpdateBlocklist(OnUpdateBlocklist.Status status) {
350 this.refreshUi();
351 }
352
353 @Override
354 public void onShowErrorToast(int resId) {
355 runOnUiThread(() -> Toast.makeText(this, resId, Toast.LENGTH_SHORT).show());
356 }
357}