NotificationsActivity.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.Context;
  27. import android.content.Intent;
  28. import android.graphics.PorterDuff;
  29. import android.os.Bundle;
  30. import android.support.design.widget.BottomNavigationView;
  31. import android.support.design.widget.Snackbar;
  32. import android.support.v4.widget.SwipeRefreshLayout;
  33. import android.support.v7.widget.DividerItemDecoration;
  34. import android.support.v7.widget.LinearLayoutManager;
  35. import android.support.v7.widget.RecyclerView;
  36. import android.view.MenuItem;
  37. import android.view.View;
  38. import android.widget.ImageView;
  39. import android.widget.LinearLayout;
  40. import android.widget.ProgressBar;
  41. import android.widget.TextView;
  42. import com.owncloud.android.MainApp;
  43. import com.owncloud.android.R;
  44. import com.owncloud.android.authentication.AccountUtils;
  45. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  46. import com.owncloud.android.jobs.NotificationJob;
  47. import com.owncloud.android.lib.common.OwnCloudAccount;
  48. import com.owncloud.android.lib.common.OwnCloudClient;
  49. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  50. import com.owncloud.android.lib.common.operations.RemoteOperation;
  51. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  52. import com.owncloud.android.lib.common.utils.Log_OC;
  53. import com.owncloud.android.lib.resources.notifications.GetRemoteNotificationsOperation;
  54. import com.owncloud.android.lib.resources.notifications.models.Notification;
  55. import com.owncloud.android.ui.adapter.NotificationListAdapter;
  56. import com.owncloud.android.utils.DisplayUtils;
  57. import com.owncloud.android.utils.PushUtils;
  58. import com.owncloud.android.utils.ThemeUtils;
  59. import java.io.IOException;
  60. import java.util.List;
  61. import butterknife.BindString;
  62. import butterknife.BindView;
  63. import butterknife.ButterKnife;
  64. import butterknife.Unbinder;
  65. /**
  66. * Activity displaying all server side stored activity items.
  67. */
  68. public class NotificationsActivity extends FileActivity {
  69. private static final String TAG = NotificationsActivity.class.getSimpleName();
  70. @BindView(R.id.empty_list_view)
  71. public LinearLayout emptyContentContainer;
  72. public SwipeRefreshLayout swipeListRefreshLayout;
  73. public SwipeRefreshLayout swipeEmptyListRefreshLayout;
  74. @BindView(R.id.empty_list_view_text)
  75. public TextView emptyContentMessage;
  76. @BindView(R.id.empty_list_view_headline)
  77. public TextView emptyContentHeadline;
  78. @BindView(R.id.empty_list_icon)
  79. public ImageView emptyContentIcon;
  80. @BindView(R.id.empty_list_progress)
  81. public ProgressBar emptyContentProgressBar;
  82. @BindView(android.R.id.list)
  83. public RecyclerView recyclerView;
  84. @BindString(R.string.notifications_no_results_headline)
  85. public String noResultsHeadline;
  86. @BindString(R.string.notifications_no_results_message)
  87. public String noResultsMessage;
  88. private Unbinder unbinder;
  89. private NotificationListAdapter adapter;
  90. private Snackbar snackbar = null;
  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. String account;
  98. Account currentAccount;
  99. if (getIntent() != null && getIntent().getExtras() != null &&
  100. (account = getIntent().getExtras().getString(NotificationJob.KEY_NOTIFICATION_ACCOUNT)) != null &&
  101. (currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext())) != null &&
  102. !account.equalsIgnoreCase(currentAccount.name)) {
  103. AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account);
  104. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  105. }
  106. // setup toolbar
  107. setupToolbar();
  108. swipeEmptyListRefreshLayout = findViewById(R.id.swipe_containing_empty);
  109. swipeListRefreshLayout = findViewById(R.id.swipe_containing_list);
  110. // setup drawer
  111. setupDrawer(R.id.nav_notifications);
  112. ThemeUtils.setColoredTitle(getSupportActionBar(), getString(R.string.drawer_item_notifications), this);
  113. swipeListRefreshLayout.setOnRefreshListener(() -> {
  114. setLoadingMessage();
  115. fetchAndSetData();
  116. });
  117. swipeEmptyListRefreshLayout.setOnRefreshListener(() -> {
  118. setLoadingMessage();
  119. fetchAndSetData();
  120. });
  121. setupPushWarning();
  122. setupContent();
  123. }
  124. private void setupPushWarning() {
  125. if (snackbar != null) {
  126. if (!snackbar.isShown()) {
  127. snackbar.show();
  128. }
  129. } else {
  130. Context context = getApplicationContext();
  131. String pushUrl = context.getResources().getString(R.string.push_server_url);
  132. if (pushUrl.isEmpty()) {
  133. snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_not_implemented,
  134. Snackbar.LENGTH_INDEFINITE);
  135. } else {
  136. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  137. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
  138. if (account != null) {
  139. boolean usesOldLogin = arbitraryDataProvider.getBooleanValue(account.name,
  140. AccountUtils.ACCOUNT_USES_STANDARD_PASSWORD);
  141. if (usesOldLogin) {
  142. snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_old_login,
  143. Snackbar.LENGTH_INDEFINITE);
  144. } else {
  145. String pushValue = arbitraryDataProvider.getValue(account.name, PushUtils.KEY_PUSH);
  146. if (pushValue == null || pushValue.isEmpty()) {
  147. snackbar = Snackbar.make(emptyContentContainer, R.string.push_notifications_temp_error,
  148. Snackbar.LENGTH_INDEFINITE);
  149. }
  150. }
  151. }
  152. }
  153. if (snackbar != null && !snackbar.isShown()) {
  154. snackbar.show();
  155. }
  156. }
  157. }
  158. @Override
  159. public void openDrawer() {
  160. super.openDrawer();
  161. if (snackbar != null && snackbar.isShown()) {
  162. snackbar.dismiss();
  163. }
  164. }
  165. @Override
  166. public void closeDrawer() {
  167. super.closeDrawer();
  168. setupPushWarning();
  169. }
  170. public void onDestroy() {
  171. super.onDestroy();
  172. unbinder.unbind();
  173. }
  174. @Override
  175. public void showFiles(boolean onDeviceOnly) {
  176. super.showFiles(onDeviceOnly);
  177. Intent i = new Intent(getApplicationContext(), FileDisplayActivity.class);
  178. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  179. startActivity(i);
  180. }
  181. /**
  182. * sets up the UI elements and loads all activity items.
  183. */
  184. private void setupContent() {
  185. emptyContentIcon.setImageResource(R.drawable.ic_notification_light_grey);
  186. emptyContentProgressBar.getIndeterminateDrawable().setColorFilter(ThemeUtils.primaryAccentColor(this),
  187. PorterDuff.Mode.SRC_IN);
  188. setLoadingMessage();
  189. adapter = new NotificationListAdapter(this);
  190. recyclerView.setAdapter(adapter);
  191. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  192. DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
  193. recyclerView.getContext(),
  194. layoutManager.getOrientation()
  195. );
  196. recyclerView.setLayoutManager(layoutManager);
  197. recyclerView.addItemDecoration(dividerItemDecoration);
  198. BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);
  199. if (getResources().getBoolean(R.bool.bottom_toolbar_enabled)) {
  200. bottomNavigationView.setVisibility(View.VISIBLE);
  201. DisplayUtils.setupBottomBar(bottomNavigationView, getResources(), this, -1);
  202. }
  203. fetchAndSetData();
  204. }
  205. private void populateList(List<Notification> notifications) {
  206. adapter.setNotificationItems(notifications);
  207. }
  208. private void fetchAndSetData() {
  209. final Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
  210. final Context context = MainApp.getAppContext();
  211. Thread t = new Thread(() -> {
  212. try {
  213. if (currentAccount != null) {
  214. OwnCloudAccount ocAccount = new OwnCloudAccount(currentAccount, context);
  215. OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
  216. getClientFor(ocAccount, MainApp.getAppContext());
  217. mClient.setOwnCloudVersion(AccountUtils.getServerVersion(currentAccount));
  218. RemoteOperation getRemoteNotificationOperation = new GetRemoteNotificationsOperation();
  219. final RemoteOperationResult result = getRemoteNotificationOperation.execute(mClient);
  220. if (result.isSuccess() && result.getNotificationData() != null) {
  221. final List<Notification> notifications = result.getNotificationData();
  222. runOnUiThread(() -> {
  223. populateList(notifications);
  224. if (notifications.size() > 0) {
  225. swipeEmptyListRefreshLayout.setVisibility(View.GONE);
  226. swipeListRefreshLayout.setVisibility(View.VISIBLE);
  227. } else {
  228. setEmptyContent(noResultsHeadline, noResultsMessage);
  229. swipeListRefreshLayout.setVisibility(View.GONE);
  230. swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
  231. }
  232. });
  233. } else {
  234. Log_OC.d(TAG, result.getLogMessage());
  235. // show error
  236. runOnUiThread(() -> setEmptyContent(noResultsHeadline, result.getLogMessage()));
  237. }
  238. hideRefreshLayoutLoader();
  239. } else {
  240. // show error
  241. runOnUiThread(() -> setEmptyContent(noResultsHeadline, getString(R.string.account_not_found)));
  242. }
  243. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  244. Log_OC.e(TAG, "Account not found", e);
  245. } catch (IOException e) {
  246. Log_OC.e(TAG, "IO error", e);
  247. } catch (OperationCanceledException e) {
  248. Log_OC.e(TAG, "Operation has been canceled", e);
  249. } catch (AuthenticatorException e) {
  250. Log_OC.e(TAG, "Authentication Exception", e);
  251. }
  252. });
  253. t.start();
  254. }
  255. private void hideRefreshLayoutLoader() {
  256. runOnUiThread(() -> {
  257. swipeListRefreshLayout.setRefreshing(false);
  258. swipeEmptyListRefreshLayout.setRefreshing(false);
  259. });
  260. }
  261. @Override
  262. public boolean onOptionsItemSelected(MenuItem item) {
  263. boolean retval = true;
  264. switch (item.getItemId()) {
  265. case android.R.id.home:
  266. if (isDrawerOpen()) {
  267. closeDrawer();
  268. } else {
  269. openDrawer();
  270. }
  271. break;
  272. default:
  273. retval = super.onOptionsItemSelected(item);
  274. break;
  275. }
  276. return retval;
  277. }
  278. private void setLoadingMessage() {
  279. emptyContentHeadline.setText(R.string.notifications_loading_activity);
  280. emptyContentMessage.setText("");
  281. emptyContentIcon.setVisibility(View.GONE);
  282. emptyContentProgressBar.setVisibility(View.VISIBLE);
  283. }
  284. private void setEmptyContent(String headline, String message) {
  285. if (emptyContentContainer != null && emptyContentMessage != null) {
  286. emptyContentHeadline.setText(headline);
  287. emptyContentMessage.setText(message);
  288. emptyContentProgressBar.setVisibility(View.GONE);
  289. emptyContentIcon.setVisibility(View.VISIBLE);
  290. }
  291. }
  292. @Override
  293. protected void onResume() {
  294. super.onResume();
  295. setDrawerMenuItemChecked(R.id.nav_notifications);
  296. }
  297. }