NotificationBuilderWithProgressBar.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.notifications;
  21. import com.owncloud.android.R;
  22. import android.app.Notification;
  23. import android.content.Context;
  24. import android.os.Build;
  25. import android.support.v4.app.NotificationCompat;
  26. import android.view.View;
  27. import android.widget.RemoteViews;
  28. /**
  29. * Extends the support class {@link NotificationCompat.Builder} to grant that
  30. * a progress bar is available in every Android version, because
  31. * {@link NotificationCompat.Builder#setProgress(int, int, boolean)} has no
  32. * real effect for Android < 4.0
  33. */
  34. public class NotificationBuilderWithProgressBar extends NotificationCompat.Builder {
  35. /**
  36. * Custom view to replace the original layout of the notifications
  37. */
  38. private RemoteViews mContentView = null;
  39. /**
  40. * Fatory method.
  41. *
  42. * Instances of this class will be only returned in Android versions needing it.
  43. *
  44. * @param context Context that will use the builder to create notifications
  45. * @return An instance of this class, or of the regular
  46. * {@link NotificationCompat.Builder}, when it is good enough.
  47. */
  48. public static NotificationCompat.Builder newNotificationBuilderWithProgressBar(Context context) {
  49. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  50. return new NotificationBuilderWithProgressBar(context);
  51. } else {
  52. return new NotificationCompat.Builder(context).
  53. setColor(context.getResources().getColor(R.color.primary));
  54. }
  55. }
  56. /**
  57. * Constructor.
  58. *
  59. * @param context Context that will use the builder to create notifications.
  60. */
  61. private NotificationBuilderWithProgressBar(Context context) {
  62. super(context);
  63. mContentView = new RemoteViews(context.getPackageName(), R.layout.notification_with_progress_bar);
  64. setContent(mContentView);
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. @Override
  70. public NotificationCompat.Builder setProgress(int max, int progress, boolean indeterminate) {
  71. mContentView.setProgressBar(R.id.progress, max, progress, indeterminate);
  72. if (max > 0) {
  73. mContentView.setViewVisibility(R.id.progressHolder, View.VISIBLE);
  74. } else {
  75. mContentView.setViewVisibility(R.id.progressHolder, View.GONE);
  76. }
  77. return this;
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. @Override
  83. public NotificationCompat.Builder setSmallIcon(int icon) {
  84. super.setSmallIcon(icon); // necessary
  85. mContentView.setImageViewResource(R.id.icon, icon);
  86. return this;
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. @Override
  92. public NotificationCompat.Builder setContentTitle(CharSequence title) {
  93. super.setContentTitle(title);
  94. mContentView.setTextViewText(R.id.title, title);
  95. return this;
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. @Override
  101. public NotificationCompat.Builder setContentText(CharSequence text) {
  102. super.setContentText(text);
  103. mContentView.setTextViewText(R.id.text, text);
  104. if (text != null && text.length() > 0) {
  105. mContentView.setViewVisibility(R.id.text, View.VISIBLE);
  106. } else {
  107. mContentView.setViewVisibility(R.id.text, View.GONE);
  108. }
  109. return this;
  110. }
  111. @Override
  112. public Notification build() {
  113. Notification result = super.build();
  114. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
  115. // super.build() in Android 2.x totally ruins whatever was made #setContent
  116. result.contentView = mContentView;
  117. }
  118. return result;
  119. }
  120. }