NotificationsActivity.java 12 KB

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