fr.shywim.antoinedaniel.utils.NotifUtils.java Source code

Java tutorial

Introduction

Here is the source code for fr.shywim.antoinedaniel.utils.NotifUtils.java

Source

/*
 * Copyright (c) 2015 Matthieu Harl
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package fr.shywim.antoinedaniel.utils;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;
import android.support.v4.app.NotificationManagerCompat;

import com.squareup.picasso.Picasso;

import java.io.IOException;

import fr.shywim.antoinedaniel.R;
import fr.shywim.antoinedaniel.ui.MainActivity;

public class NotifUtils {
    @IntDef({ NOTIFICATION_NEW_VIDEO, NOTIFICATION_NEW_SOUNDS, NOTIFICATION_OTHER })
    public @interface NotificationId {
    }

    public static final int NOTIFICATION_NEW_VIDEO = 1;
    public static final int NOTIFICATION_NEW_SOUNDS = 2;
    public static final int NOTIFICATION_OTHER = 3;

    /**
     * Send a notification to the device. Support for weareable extension.
     *
     * @param id Notification id, must be one of {@link #NOTIFICATION_NEW_VIDEO},
     * {@link #NOTIFICATION_NEW_SOUNDS} or {@link #NOTIFICATION_OTHER}.
     * @param context A {@link android.content.Context}.
     * @param msg Notification's message.
     * @param ytLink A youtube video id (optionnal).
     */
    public static void sendNotification(@NotificationId final int id, final Context context, final String msg,
            @Nullable final String ytLink) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_notifs)
                        .setContentTitle(context.getString(R.string.notifications)).setContentText(msg)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setAutoCancel(true);

                switch (id) {
                case NOTIFICATION_NEW_VIDEO:
                    builder.setContentTitle("Nouvelle vido !").setContentText(msg);
                    break;

                case NOTIFICATION_NEW_SOUNDS:
                    builder.setContentTitle("Nouveaux extraits !").setContentText(msg);
                    break;

                case NOTIFICATION_OTHER:
                    builder.setContentTitle("La Bote  Antoine Daniel").setContentText(msg);
                    break;
                }

                builder.setPriority(NotificationCompat.PRIORITY_LOW);
                if (PrefUtils.getNotifSoundEnabled(context)) {
                    builder.setSound(Uri.parse(PrefUtils.getNotifRingtone(context)));
                }
                if (PrefUtils.getNotifLightsEnabled(context)) {
                    builder.setDefaults(Notification.DEFAULT_LIGHTS);
                }

                Bitmap wearableBitmap;

                PendingIntent contentIntent;
                if (ytLink != null) {
                    String bitmapLink = context.getString(R.string.api_get_yt_thmb) + ytLink;

                    try {
                        wearableBitmap = Picasso.with(context).load(bitmapLink).resize(400, 400).centerCrop().get();
                    } catch (IOException e) {
                        // If we can't download the video image, set the default notification bitmap
                        e.printStackTrace();
                        wearableBitmap = BitmapFactory.decodeResource(context.getResources(),
                                R.drawable.weareable_default_background);
                    }

                    String url = context.getString(R.string.youtube_link_prefix) + ytLink;
                    contentIntent = PendingIntent.getActivity(context, 0,
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)), 0);
                } else {
                    wearableBitmap = BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.weareable_default_background);

                    contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
                            0);
                }

                WearableExtender wearableExtender = new WearableExtender().setHintHideIcon(true)
                        .setBackground(wearableBitmap);

                builder.setContentIntent(contentIntent).setLargeIcon(wearableBitmap).extend(wearableExtender);

                notificationManager.notify(id, builder.build());
            }
        }).start();
    }
}