org.enbyted.android.zseinfo.view.NotificationManager.java Source code

Java tutorial

Introduction

Here is the source code for org.enbyted.android.zseinfo.view.NotificationManager.java

Source

/*
 * This file is part of ZSE Info.
 *
 *     ZSE Info is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 3 of the License, or
 *     any later version.
 *
 *     ZSE Info is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Foobar; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.enbyted.android.zseinfo.view;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import org.enbyted.android.zseinfo.InfoApp;
import org.enbyted.android.zseinfo.R;
import org.enbyted.android.zseinfo.data.configuration.Configuration;
import org.enbyted.android.zseinfo.view.activity.MainActivity;

/**
 * Created by Bartosz Grabias on 24.03.14.
 */
public class NotificationManager {
    public static final int ID_FULL_SYNC = 2;
    public static final int ID_NEW_REPLACEMENT = 3;
    public static final int ID_REPLACEMENTS_DOWNLOADED = 1;

    private static android.app.NotificationManager manager;

    private static NotificationCompat.Builder builderFullSync;
    private static NotificationCompat.Builder builderSync;
    private static NotificationCompat.Builder builderNewReplacements;

    private static Context getContext() {
        return InfoApp.getContext();
    }

    private static android.app.NotificationManager getManager() {
        if (manager == null)
            manager = (android.app.NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

        return manager;
    }

    public static void removeNotify(int type) {
        getManager().cancel(type);
    }

    public static void notifyFullSync() {
        if (!Configuration.getInstance().showDebugNotifications())
            return;

        if (builderFullSync == null) {
            builderFullSync = new NotificationCompat.Builder(getContext());
            builderFullSync.setSmallIcon(R.drawable.ic_launcher);
            builderFullSync.setContentTitle("Pena synchroniozacja!");
            builderFullSync.setContentText("Przeprowadzono pen synchronizacj!");

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());

            Intent resultIntent = new Intent(getContext(), MainActivity.class);
            resultIntent.putExtra("TYPE", ID_FULL_SYNC);

            stackBuilder.addNextIntent(resultIntent);

            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            builderFullSync.setContentIntent(resultPendingIntent);
        }

        getManager().notify(ID_FULL_SYNC, builderFullSync.build());
    }

    public static void notifyNewReplacementForMe() {

        if (builderNewReplacements == null) {
            builderNewReplacements = new NotificationCompat.Builder(getContext());
            builderNewReplacements.setSmallIcon(R.drawable.ic_launcher);
        }

        Intent resultIntent = new Intent(getContext(), MainActivity.class);
        resultIntent.putExtra("tab", "TAB_REPLACEMENTS");
        resultIntent.putExtra("my_class", true);
        resultIntent.putExtra("TYPE", ID_NEW_REPLACEMENT);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());

        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        builderNewReplacements.setContentIntent(resultPendingIntent);

        builderNewReplacements.setContentTitle("Nowe zastpstwo!");
        builderNewReplacements.setContentText("Kliknij aby zobaczy nowe zastpstwa dla twojej klasy.");
        builderNewReplacements.setDefaults(Notification.DEFAULT_VIBRATE);

        getManager().notify(ID_NEW_REPLACEMENT, builderNewReplacements.build());
    }

    public static void notifyNewReplacements() {
        if (builderNewReplacements == null) {
            builderNewReplacements = new NotificationCompat.Builder(getContext());
            builderNewReplacements.setSmallIcon(R.drawable.ic_launcher);
        }

        builderNewReplacements.setContentTitle("Nowe zastpstwa!");
        builderNewReplacements.setContentText("Zastpstwa na jutro s ju dostpne!");
        builderNewReplacements.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);

        Intent resultIntent = new Intent(getContext(), MainActivity.class);
        resultIntent.putExtra("tab", "TAB_REPLACEMENTS");
        resultIntent.putExtra("my_class", false);
        resultIntent.putExtra("TYPE", ID_NEW_REPLACEMENT);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());

        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        builderNewReplacements.setContentIntent(resultPendingIntent);

        getManager().notify(ID_NEW_REPLACEMENT, builderNewReplacements.build());
    }

    public static void notifyReplacementsSync() {
        if (!Configuration.getInstance().showDebugNotifications())
            return;

        if (builderSync == null) {
            builderSync = new NotificationCompat.Builder(getContext());
            builderSync.setSmallIcon(R.drawable.ic_launcher);
            builderSync.setContentTitle("Pobrano zastpstwa!");
            builderSync.setContentText("Lista zastpstw zostaa automatycznie odwierzona.");

            Intent resultIntent = new Intent(getContext(), MainActivity.class);
            resultIntent.putExtra("tab", "TAB_REPLACEMENTS");
            resultIntent.putExtra("TYPE", ID_REPLACEMENTS_DOWNLOADED);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(getContext());

            stackBuilder.addNextIntent(resultIntent);

            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            builderSync.setContentIntent(resultPendingIntent);
        }

        getManager().notify(ID_REPLACEMENTS_DOWNLOADED, builderSync.build());
    }

}