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
76 private static boolean isViewIntent(Intent i) {
77 return i != null && ACTION_VIEW_CONVERSATION.equals(i.getAction()) && i.hasExtra(EXTRA_CONVERSATION);
78 }
79
80 private static Intent createLauncherIntent(Context context) {
81 final Intent intent = new Intent(context, ConversationActivity.class);
82 intent.setAction(Intent.ACTION_MAIN);
83 intent.addCategory(Intent.CATEGORY_LAUNCHER);
84 return intent;
85 }
86
87 @Override
88 protected void refreshUiReal() {
89 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
90 refreshFragment(id);
91 }
92 }
93
94 @Override
95 void onBackendConnected() {
96 for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
97 notifyFragmentOfBackendConnected(id);
98 }
99 invalidateActionBarTitle();
100 Intent intent = pendingViewIntent.pop();
101 if (intent != null) {
102 if (processViewIntent(intent)) {
103 return;
104 }
105 }
106 if (binding.secondaryFragment != null && ConversationFragment.getConversation(this) == null) {
107 Conversation conversation = ConversationsOverviewFragment.getSuggestion(this);
108 if (conversation != null) {
109 openConversation(conversation, null);
110 }
111 }
112 }
113
114 private void notifyFragmentOfBackendConnected(@IdRes int id) {
115 final Fragment fragment = getFragmentManager().findFragmentById(id);
116 if (fragment != null && fragment instanceof XmppFragment) {
117 ((XmppFragment) fragment).onBackendConnected();
118 }
119 }
120
121 private void refreshFragment(@IdRes int id) {
122 final Fragment fragment = getFragmentManager().findFragmentById(id);
123 if (fragment != null && fragment instanceof XmppFragment) {
124 ((XmppFragment) fragment).refresh();
125 }
126 }
127
128 private boolean processViewIntent(Intent intent) {
129 String uuid = intent.getStringExtra(EXTRA_CONVERSATION);
130 Conversation conversation = uuid != null ? xmppConnectionService.findConversationByUuid(uuid) : null;
131 if (conversation == null) {
132 Log.d(Config.LOGTAG, "unable to view conversation with uuid:" + uuid);
133 return false;
134 }
135 openConversation(conversation, intent.getExtras());
136 return true;
137 }
138
139 @Override
140 protected void onCreate(final Bundle savedInstanceState) {
141 super.onCreate(savedInstanceState);
142 new EmojiService(this).init();
143 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_conversations);
144 this.getFragmentManager().addOnBackStackChangedListener(this::invalidateActionBarTitle);
145 this.initializeFragments();
146 this.invalidateActionBarTitle();
147 final Intent intent = getIntent();
148 if (isViewIntent(intent)) {
149 pendingViewIntent.push(intent);
150 setIntent(createLauncherIntent(this));
151 }
152 }
153
154 @Override
155 public boolean onCreateOptionsMenu(Menu menu) {
156 getMenuInflater().inflate(R.menu.activity_conversations, menu);
157 return super.onCreateOptionsMenu(menu);
158 }
159
160 @Override
161 public void onConversationSelected(Conversation conversation) {
162 Log.d(Config.LOGTAG, "selected " + conversation.getName());
163 openConversation(conversation, null);
164 }
165
166 private void openConversation(Conversation conversation, Bundle extras) {
167 ConversationFragment conversationFragment = (ConversationFragment) getFragmentManager().findFragmentById(R.id.secondary_fragment);
168 final boolean mainNeedsRefresh;
169 if (conversationFragment == null) {
170 mainNeedsRefresh = false;
171 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
172 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
173 conversationFragment = (ConversationFragment) mainFragment;
174 } else {
175 conversationFragment = new ConversationFragment();
176 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
177 fragmentTransaction.replace(R.id.main_fragment, conversationFragment);
178 fragmentTransaction.addToBackStack(null);
179 fragmentTransaction.commit();
180 }
181 } else {
182 mainNeedsRefresh = true;
183 }
184 conversationFragment.reInit(conversation);
185 if (mainNeedsRefresh) {
186 refreshFragment(R.id.main_fragment);
187 } else {
188 invalidateActionBarTitle();
189 }
190 }
191
192 @Override
193 public boolean onOptionsItemSelected(MenuItem item) {
194 switch (item.getItemId()) {
195 case android.R.id.home:
196 FragmentManager fm = getFragmentManager();
197 if (fm.getBackStackEntryCount() > 0) {
198 fm.popBackStack();
199 return true;
200 }
201 break;
202 }
203 return super.onOptionsItemSelected(item);
204 }
205
206 @Override
207 protected void onNewIntent(final Intent intent) {
208 if (isViewIntent(intent)) {
209 if (xmppConnectionService != null) {
210 processViewIntent(intent);
211 } else {
212 pendingViewIntent.push(intent);
213 }
214 }
215 }
216
217 private void initializeFragments() {
218 FragmentTransaction transaction = getFragmentManager().beginTransaction();
219 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
220 Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
221 if (mainFragment != null) {
222 Log.d(Config.LOGTAG, "initializeFragment(). main fragment exists");
223 if (binding.secondaryFragment != null) {
224 if (mainFragment instanceof ConversationFragment) {
225 Log.d(Config.LOGTAG, "gained secondary fragment. moving...");
226 getFragmentManager().popBackStack();
227 transaction.remove(mainFragment);
228 transaction.commit();
229 getFragmentManager().executePendingTransactions();
230 transaction = getFragmentManager().beginTransaction();
231 transaction.replace(R.id.secondary_fragment, mainFragment);
232 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
233 transaction.commit();
234 return;
235 }
236 } else {
237 if (secondaryFragment != null && secondaryFragment instanceof ConversationFragment) {
238 Log.d(Config.LOGTAG, "lost secondary fragment. moving...");
239 transaction.remove(secondaryFragment);
240 transaction.commit();
241 getFragmentManager().executePendingTransactions();
242 transaction = getFragmentManager().beginTransaction();
243 transaction.replace(R.id.main_fragment, secondaryFragment);
244 transaction.addToBackStack(null);
245 transaction.commit();
246 return;
247 }
248 }
249 } else {
250 transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
251 }
252 if (binding.secondaryFragment != null && secondaryFragment == null) {
253 transaction.replace(R.id.secondary_fragment, new ConversationFragment());
254 }
255 transaction.commit();
256 }
257
258 private void invalidateActionBarTitle() {
259 final ActionBar actionBar = getSupportActionBar();
260 if (actionBar != null) {
261 Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
262 if (mainFragment != null && mainFragment instanceof ConversationFragment) {
263 final Conversation conversation = ((ConversationFragment) mainFragment).getConversation();
264 if (conversation != null) {
265 actionBar.setTitle(conversation.getName());
266 actionBar.setDisplayHomeAsUpEnabled(true);
267 return;
268 }
269 }
270 actionBar.setTitle(R.string.app_name);
271 actionBar.setDisplayHomeAsUpEnabled(false);
272 }
273 }
274
275 @Override
276 public void onConversationArchived(Conversation conversation) {
277
278 }
279
280 @Override
281 public void onConversationsListItemUpdated() {
282 Fragment fragment = getFragmentManager().findFragmentById(R.id.main_fragment);
283 if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
284 ((ConversationsOverviewFragment) fragment).refresh();
285 }
286 }
287
288 @Override
289 public void onConversationRead(Conversation conversation) {
290 Log.d(Config.LOGTAG, "read event for " + conversation.getName() + " received");
291 }
292
293 @Override
294 public void onAccountUpdate() {
295 this.refreshUi();
296 }
297
298 @Override
299 public void onConversationUpdate() {
300 this.refreshUi();
301 }
302
303 @Override
304 public void onRosterUpdate() {
305 this.refreshUi();
306 }
307
308 @Override
309 public void OnUpdateBlocklist(OnUpdateBlocklist.Status status) {
310 this.refreshUi();
311 }
312
313 @Override
314 public void onShowErrorToast(int resId) {
315 runOnUiThread(() -> Toast.makeText(this, resId, Toast.LENGTH_SHORT).show());
316 }
317}