NotificationsActivity.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Mario Danic
  6. * @author Chris Narkiewicz
  7. * Copyright (C) 2017 Andy Scherzinger
  8. * Copyright (C) 2017 Mario Danic
  9. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.owncloud.android.ui.activity;
  25. import android.content.Intent;
  26. import android.graphics.PorterDuff;
  27. import android.os.Bundle;
  28. import android.view.Menu;
  29. import android.view.MenuItem;
  30. import android.view.View;
  31. import android.widget.ImageView;
  32. import android.widget.LinearLayout;
  33. import android.widget.ProgressBar;
  34. import android.widget.TextView;
  35. import com.google.android.material.snackbar.Snackbar;
  36. import com.nextcloud.client.account.User;
  37. import com.nextcloud.client.account.UserAccountManager;
  38. import com.nextcloud.client.network.ClientFactory;
  39. import com.nextcloud.java.util.Optional;
  40. import com.owncloud.android.R;
  41. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  42. import com.owncloud.android.jobs.NotificationJob;
  43. import com.owncloud.android.lib.common.OwnCloudClient;
  44. import com.owncloud.android.lib.common.operations.RemoteOperation;
  45. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  46. import com.owncloud.android.lib.common.utils.Log_OC;
  47. import com.owncloud.android.lib.resources.notifications.GetNotificationsRemoteOperation;
  48. import com.owncloud.android.lib.resources.notifications.models.Notification;
  49. import com.owncloud.android.ui.adapter.NotificationListAdapter;
  50. import com.owncloud.android.ui.asynctasks.DeleteAllNotificationsTask;
  51. import com.owncloud.android.ui.notifications.NotificationsContract;
  52. import com.owncloud.android.utils.DisplayUtils;
  53. import com.owncloud.android.utils.PushUtils;
  54. import com.owncloud.android.utils.ThemeUtils;
  55. import java.util.List;
  56. import javax.inject.Inject;
  57. import androidx.recyclerview.widget.LinearLayoutManager;
  58. import androidx.recyclerview.widget.RecyclerView;
  59. import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  60. import butterknife.BindString;
  61. import butterknife.BindView;
  62. import butterknife.ButterKnife;
  63. import butterknife.Unbinder;
  64. /**
  65. * Activity displaying all server side stored notification items.
  66. */
  67. public class NotificationsActivity extends FileActivity implements NotificationsContract.View {
  68. private static final String TAG = NotificationsActivity.class.getSimpleName();
  69. @BindView(R.id.empty_list_view)
  70. public LinearLayout emptyContentContainer;
  71. public SwipeRefreshLayout swipeListRefreshLayout;
  72. public SwipeRefreshLayout swipeEmptyListRefreshLayout;
  73. @BindView(R.id.empty_list_view_text)
  74. public TextView emptyContentMessage;
  75. @BindView(R.id.empty_list_view_headline)
  76. public TextView emptyContentHeadline;
  77. @BindView(R.id.empty_list_icon)
  78. public ImageView emptyContentIcon;
  79. @BindView(R.id.empty_list_progress)
  80. public ProgressBar emptyContentProgressBar;
  81. @BindView(android.R.id.list)
  82. public RecyclerView recyclerView;
  83. @BindString(R.string.notifications_no_results_headline)
  84. public String noResultsHeadline;
  85. @BindString(R.string.notifications_no_results_message)
  86. public String noResultsMessage;
  87. private Unbinder unbinder;
  88. private NotificationListAdapter adapter;
  89. private Snackbar snackbar;
  90. private OwnCloudClient client;
  91. private Optional<User> optionalUser;
  92. @Inject ClientFactory clientFactory;
  93. @Override
  94. protected void onCreate(Bundle savedInstanceState) {
  95. Log_OC.v(TAG, "onCreate() start");
  96. super.onCreate(savedInstanceState);
  97. setContentView(R.layout.notifications_layout);
  98. unbinder = ButterKnife.bind(this);
  99. optionalUser = getUser();
  100. // use account from intent (opened via android notification can have a different account than current one)
  101. if (getIntent() != null && getIntent().getExtras() != null) {
  102. String accountName = getIntent().getExtras().getString(NotificationJob.KEY_NOTIFICATION_ACCOUNT);
  103. if(accountName != null && optionalUser.isPresent()) {
  104. User user = optionalUser.get();
  105. if (user.getAccountName().equalsIgnoreCase(accountName)) {
  106. accountManager.setCurrentOwnCloudAccount(accountName);
  107. setUser(getUserAccountManager().getUser());
  108. optionalUser = getUser();
  109. }
  110. }
  111. }
  112. // setup toolbar
  113. setupToolbar();
  114. swipeEmptyListRefreshLayout = findViewById(R.id.swipe_containing_empty);
  115. swipeListRefreshLayout = findViewById(R.id.swipe_containing_list);
  116. // setup drawer
  117. setupDrawer(R.id.nav_notifications);
  118. ThemeUtils.setColoredTitle(getSupportActionBar(), getString(R.string.drawer_item_notifications), this);
  119. if (!optionalUser.isPresent()) {
  120. // show error
  121. runOnUiThread(() -> setEmptyContent(noResultsHeadline, getString(R.string.account_not_found)));
  122. return;
  123. }
  124. swipeListRefreshLayout.setOnRefreshListener(() -> {
  125. setLoadingMessage();
  126. fetchAndSetData();
  127. });
  128. swipeEmptyListRefreshLayout.setOnRefreshListener(() -> {
  129. setLoadingMessage();
  130. fetchAndSetData();
  131. });
  132. setupPushWarning();
  133. setupContent();
  134. }
  135. private void setupPushWarning() {
  136. if (snackbar != null) {
  137. if (!snackbar.isShown()) {
  138. snackbar.show();
  139. }
  140. } else {
  141. String pushUrl = getResources().getString(R.string.push_server_url);
  142. if (pushUrl.isEmpty()) {
  143. snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_not_implemented,
  144. Snackbar.LENGTH_INDEFINITE);
  145. } else {
  146. final ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  147. final String accountName = optionalUser.isPresent() ? optionalUser.get().getAccountName() : "";
  148. final boolean usesOldLogin = arbitraryDataProvider.getBooleanValue(accountName,
  149. UserAccountManager.ACCOUNT_USES_STANDARD_PASSWORD);
  150. if (usesOldLogin) {
  151. snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_old_login,
  152. Snackbar.LENGTH_INDEFINITE);
  153. } else {
  154. String pushValue = arbitraryDataProvider.getValue(accountName, PushUtils.KEY_PUSH);
  155. if (pushValue == null || pushValue.isEmpty()) {
  156. snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_temp_error,
  157. Snackbar.LENGTH_INDEFINITE);
  158. }
  159. }
  160. }
  161. if (snackbar != null && !snackbar.isShown()) {
  162. snackbar.show();
  163. }
  164. }
  165. }
  166. @Override
  167. public void openDrawer() {
  168. super.openDrawer();
  169. if (snackbar != null && snackbar.isShown()) {
  170. snackbar.dismiss();
  171. }
  172. }
  173. @Override
  174. public void closeDrawer() {
  175. super.closeDrawer();
  176. setupPushWarning();
  177. }
  178. public void onDestroy() {
  179. super.onDestroy();
  180. unbinder.unbind();
  181. }
  182. @Override
  183. public void showFiles(boolean onDeviceOnly) {
  184. super.showFiles(onDeviceOnly);
  185. Intent i = new Intent(getApplicationContext(), FileDisplayActivity.class);
  186. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  187. startActivity(i);
  188. }
  189. /**
  190. * sets up the UI elements and loads all notification items.
  191. */
  192. private void setupContent() {
  193. emptyContentIcon.setImageResource(R.drawable.ic_notification_light_grey);
  194. emptyContentProgressBar.getIndeterminateDrawable().setColorFilter(ThemeUtils.primaryAccentColor(this),
  195. PorterDuff.Mode.SRC_IN);
  196. setLoadingMessage();
  197. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  198. recyclerView.setLayoutManager(layoutManager);
  199. fetchAndSetData();
  200. }
  201. private void populateList(List<Notification> notifications) {
  202. adapter.setNotificationItems(notifications);
  203. }
  204. private void fetchAndSetData() {
  205. Thread t = new Thread(() -> {
  206. if (client == null && optionalUser.isPresent()) {
  207. try {
  208. User user = optionalUser.get();
  209. client = clientFactory.create(user);
  210. client.setOwnCloudVersion(user.getServer().getVersion());
  211. } catch (ClientFactory.CreationException e) {
  212. Log_OC.e(TAG, "Error initializing client", e);
  213. }
  214. }
  215. if (adapter == null) {
  216. adapter = new NotificationListAdapter(client, this);
  217. recyclerView.setAdapter(adapter);
  218. }
  219. RemoteOperation getRemoteNotificationOperation = new GetNotificationsRemoteOperation();
  220. final RemoteOperationResult result = getRemoteNotificationOperation.execute(client);
  221. if (result.isSuccess() && result.getNotificationData() != null) {
  222. final List<Notification> notifications = result.getNotificationData();
  223. runOnUiThread(() -> {
  224. populateList(notifications);
  225. if (notifications.size() > 0) {
  226. swipeEmptyListRefreshLayout.setVisibility(View.GONE);
  227. swipeListRefreshLayout.setVisibility(View.VISIBLE);
  228. } else {
  229. setEmptyContent(noResultsHeadline, noResultsMessage);
  230. swipeListRefreshLayout.setVisibility(View.GONE);
  231. swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
  232. }
  233. });
  234. } else {
  235. Log_OC.d(TAG, result.getLogMessage());
  236. // show error
  237. runOnUiThread(() -> setEmptyContent(noResultsHeadline, result.getLogMessage()));
  238. }
  239. hideRefreshLayoutLoader();
  240. });
  241. t.start();
  242. }
  243. private void hideRefreshLayoutLoader() {
  244. runOnUiThread(() -> {
  245. swipeListRefreshLayout.setRefreshing(false);
  246. swipeEmptyListRefreshLayout.setRefreshing(false);
  247. });
  248. }
  249. public boolean onCreateOptionsMenu(Menu menu) {
  250. getMenuInflater().inflate(R.menu.activity_notifications, menu);
  251. return true;
  252. }
  253. @Override
  254. public boolean onOptionsItemSelected(MenuItem item) {
  255. boolean retval = true;
  256. switch (item.getItemId()) {
  257. case android.R.id.home:
  258. if (isDrawerOpen()) {
  259. closeDrawer();
  260. } else {
  261. openDrawer();
  262. }
  263. break;
  264. case R.id.action_empty_notifications:
  265. new DeleteAllNotificationsTask(client, this).execute();
  266. break;
  267. default:
  268. retval = super.onOptionsItemSelected(item);
  269. break;
  270. }
  271. return retval;
  272. }
  273. private void setLoadingMessage() {
  274. emptyContentHeadline.setText(R.string.notifications_loading_activity);
  275. emptyContentMessage.setText("");
  276. emptyContentIcon.setVisibility(View.GONE);
  277. emptyContentProgressBar.setVisibility(View.VISIBLE);
  278. }
  279. private void setEmptyContent(String headline, String message) {
  280. if (emptyContentContainer != null && emptyContentMessage != null) {
  281. emptyContentHeadline.setText(headline);
  282. emptyContentMessage.setText(message);
  283. emptyContentMessage.setVisibility(View.VISIBLE);
  284. emptyContentProgressBar.setVisibility(View.GONE);
  285. emptyContentIcon.setImageResource(R.drawable.ic_notification_light_grey);
  286. emptyContentIcon.setVisibility(View.VISIBLE);
  287. }
  288. }
  289. @Override
  290. protected void onResume() {
  291. super.onResume();
  292. setDrawerMenuItemChecked(R.id.nav_notifications);
  293. }
  294. @Override
  295. public void onRemovedNotification(boolean isSuccess) {
  296. if (!isSuccess) {
  297. DisplayUtils.showSnackMessage(this, getString(R.string.remove_notification_failed));
  298. fetchAndSetData();
  299. }
  300. }
  301. @Override
  302. public void removeNotification(NotificationListAdapter.NotificationViewHolder holder) {
  303. adapter.removeNotification(holder);
  304. if (adapter.getItemCount() == 0) {
  305. setEmptyContent(noResultsHeadline, noResultsMessage);
  306. swipeListRefreshLayout.setVisibility(View.GONE);
  307. swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
  308. }
  309. }
  310. @Override
  311. public void onRemovedAllNotifications(boolean isSuccess) {
  312. if (isSuccess) {
  313. adapter.removeAllNotifications();
  314. setEmptyContent(noResultsHeadline, noResultsMessage);
  315. swipeListRefreshLayout.setVisibility(View.GONE);
  316. swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
  317. } else {
  318. DisplayUtils.showSnackMessage(this, getString(R.string.clear_notifications_failed));
  319. }
  320. }
  321. @Override
  322. public void onActionCallback(boolean isSuccess,
  323. Notification notification,
  324. NotificationListAdapter.NotificationViewHolder holder) {
  325. if (isSuccess) {
  326. adapter.removeNotification(holder);
  327. } else {
  328. adapter.setButtons(holder, notification);
  329. DisplayUtils.showSnackMessage(this, getString(R.string.notification_action_failed));
  330. }
  331. }
  332. }