NotificationBuilderWithProgressBar.java 4.4 KB

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