ConversationActivity.java

  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 onNewIntent(final Intent intent) {
209		if (isViewIntent(intent)) {
210			if (xmppConnectionService != null) {
211				processViewIntent(intent);
212			} else {
213				pendingViewIntent.push(intent);
214			}
215		}
216	}
217
218	@Override
219	public void onPause() {
220		this.mActivityPaused = true;
221		super.onPause();
222	}
223
224	@Override
225	public void onResume() {
226		super.onResume();
227		final int theme = findTheme();
228		if (this.mTheme != theme) {
229			recreate();
230		}
231		this.mActivityPaused = false;
232	}
233
234	private void initializeFragments() {
235		FragmentTransaction transaction = getFragmentManager().beginTransaction();
236		Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
237		Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
238		if (mainFragment != null) {
239			Log.d(Config.LOGTAG, "initializeFragment(). main fragment exists");
240			if (binding.secondaryFragment != null) {
241				if (mainFragment instanceof ConversationFragment) {
242					Log.d(Config.LOGTAG, "gained secondary fragment. moving...");
243					getFragmentManager().popBackStack();
244					transaction.remove(mainFragment);
245					transaction.commit();
246					getFragmentManager().executePendingTransactions();
247					transaction = getFragmentManager().beginTransaction();
248					transaction.replace(R.id.secondary_fragment, mainFragment);
249					transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
250					transaction.commit();
251					return;
252				}
253			} else {
254				if (secondaryFragment != null && secondaryFragment instanceof ConversationFragment) {
255					Log.d(Config.LOGTAG, "lost secondary fragment. moving...");
256					transaction.remove(secondaryFragment);
257					transaction.commit();
258					getFragmentManager().executePendingTransactions();
259					transaction = getFragmentManager().beginTransaction();
260					transaction.replace(R.id.main_fragment, secondaryFragment);
261					transaction.addToBackStack(null);
262					transaction.commit();
263					return;
264				}
265			}
266		} else {
267			transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
268		}
269		if (binding.secondaryFragment != null && secondaryFragment == null) {
270			transaction.replace(R.id.secondary_fragment, new ConversationFragment());
271		}
272		transaction.commit();
273	}
274
275	private void invalidateActionBarTitle() {
276		final ActionBar actionBar = getSupportActionBar();
277		if (actionBar != null) {
278			Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
279			if (mainFragment != null && mainFragment instanceof ConversationFragment) {
280				final Conversation conversation = ((ConversationFragment) mainFragment).getConversation();
281				if (conversation != null) {
282					actionBar.setTitle(conversation.getName());
283					actionBar.setDisplayHomeAsUpEnabled(true);
284					return;
285				}
286			}
287			actionBar.setTitle(R.string.app_name);
288			actionBar.setDisplayHomeAsUpEnabled(false);
289		}
290	}
291
292	@Override
293	public void onConversationArchived(Conversation conversation) {
294
295	}
296
297	@Override
298	public void onConversationsListItemUpdated() {
299		Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
300		if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
301			((ConversationsOverviewFragment) fragment).refresh();
302		}
303	}
304
305	@Override
306	public void onConversationRead(Conversation conversation) {
307		if (!mActivityPaused && pendingViewIntent.peek() == null) {
308			xmppConnectionService.sendReadMarker(conversation);
309		}
310	}
311
312	@Override
313	public void onAccountUpdate() {
314		this.refreshUi();
315	}
316
317	@Override
318	public void onConversationUpdate() {
319		this.refreshUi();
320	}
321
322	@Override
323	public void onRosterUpdate() {
324		this.refreshUi();
325	}
326
327	@Override
328	public void OnUpdateBlocklist(OnUpdateBlocklist.Status status) {
329		this.refreshUi();
330	}
331
332	@Override
333	public void onShowErrorToast(int resId) {
334		runOnUiThread(() -> Toast.makeText(this, resId, Toast.LENGTH_SHORT).show());
335	}
336}