NotificationBuilderWithProgressBar.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. }
  54. }
  55. /**
  56. * Constructor.
  57. *
  58. * @param context Context that will use the builder to create notifications.
  59. */
  60. private NotificationBuilderWithProgressBar(Context context) {
  61. super(context);
  62. mContentView = new RemoteViews(context.getPackageName(), R.layout.notification_with_progress_bar);
  63. setContent(mContentView);
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. @Override
  69. public NotificationCompat.Builder setProgress(int max, int progress, boolean indeterminate) {
  70. mContentView.setProgressBar(R.id.progress, max, progress, indeterminate);
  71. if (max > 0) {
  72. mContentView.setViewVisibility(R.id.progressHolder, View.VISIBLE);
  73. } else {
  74. mContentView.setViewVisibility(R.id.progressHolder, View.GONE);
  75. }
  76. return this;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. @Override
  82. public NotificationCompat.Builder setSmallIcon(int icon) {
  83. super.setSmallIcon(icon); // necessary
  84. mContentView.setImageViewResource(R.id.icon, icon);
  85. return this;
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. @Override
  91. public NotificationCompat.Builder setContentTitle(CharSequence title) {
  92. super.setContentTitle(title);
  93. mContentView.setTextViewText(R.id.title, title);
  94. return this;
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. @Override
  100. public NotificationCompat.Builder setContentText(CharSequence text) {
  101. super.setContentText(text);
  102. mContentView.setTextViewText(R.id.text, text);
  103. if (text != null && text.length() > 0) {
  104. mContentView.setViewVisibility(R.id.text, View.VISIBLE);
  105. } else {
  106. mContentView.setViewVisibility(R.id.text, View.GONE);
  107. }
  108. return this;
  109. }
  110. @Override
  111. public Notification build() {
  112. Notification result = super.build();
  113. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
  114. // super.build() in Android 2.x totally ruins whatever was made #setContent
  115. result.contentView = mContentView;
  116. }
  117. return result;
  118. }
  119. }