NotificationUtils.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2015 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.notifications;
  20. import java.util.Random;
  21. import android.app.NotificationManager;
  22. import android.content.Context;
  23. import android.os.Handler;
  24. import android.os.HandlerThread;
  25. import android.os.Process;
  26. import android.support.v4.app.NotificationCompat;
  27. import com.owncloud.android.R;
  28. public class NotificationUtils {
  29. /**
  30. * Factory method for {@link android.support.v4.app.NotificationCompat.Builder} instances.
  31. *
  32. * Not strictly needed from the moment when the minimum API level supported by the app
  33. * was raised to 14 (Android 4.0).
  34. *
  35. * Formerly, returned a customized implementation of {@link android.support.v4.app.NotificationCompat.Builder}
  36. * for Android API levels >= 8 and < 14.
  37. *
  38. * Kept in place for the extra abstraction level; notifications in the app need a review, and they
  39. * change a lot in different Android versions.
  40. *
  41. * @param context Context that will use the builder to create notifications
  42. * @return An instance of the regular {@link NotificationCompat.Builder}.
  43. */
  44. public static NotificationCompat.Builder newNotificationBuilder(Context context) {
  45. return new NotificationCompat.Builder(context).
  46. setColor(context.getResources().getColor(R.color.primary));
  47. }
  48. public static void cancelWithDelay(
  49. final NotificationManager notificationManager,
  50. final int notificationId,
  51. long delayInMillis) {
  52. HandlerThread thread = new HandlerThread(
  53. "NotificationDelayerThread_" + (new Random(System.currentTimeMillis())).nextInt(),
  54. Process.THREAD_PRIORITY_BACKGROUND);
  55. thread.start();
  56. Handler handler = new Handler(thread.getLooper());
  57. handler.postDelayed(new Runnable() {
  58. public void run() {
  59. notificationManager.cancel(notificationId);
  60. ((HandlerThread)Thread.currentThread()).getLooper().quit();
  61. }
  62. }, delayInMillis);
  63. }
  64. }