aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/lacolaco/smileessence/notification/Notificator.java
blob: 2f2dce904e8a59777c495a8b0b0655a37a6fa089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package net.lacolaco.smileessence.notification;

import android.app.Activity;
import android.widget.Toast;
import de.keyboardsurfer.android.widget.crouton.Configuration;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
import net.lacolaco.smileessence.logging.Logger;

public class Notificator
{

    private Activity activity;
    private String text;
    private NotificationType type;
    private static boolean isRunning;
    private static final int DURATION = 1000;

    public Notificator(Activity activity, String text)
    {
        this(activity, text, NotificationType.INFO);
    }

    public Notificator(Activity activity, String text, NotificationType type)
    {
        this.activity = activity;
        this.text = text;
        this.type = type;
    }

    public static void startNotification()
    {
        isRunning = true;
    }

    public static void stopNotification()
    {
        isRunning = false;
        Crouton.cancelAllCroutons();
    }

    public void publish()
    {
        if(activity == null || activity.isFinishing())
        {
            return;
        }
        if(isRunning)
        {
            Logger.debug(String.format("notify by crouton %s", text));
            makeCrouton().show();
        }
        else
        {
            Logger.debug(String.format("notify by toast %s", text));
            makeToast().show();
        }
    }

    private Style getStyle()
    {
        Configuration.Builder conf = new Configuration.Builder();
        conf.setDuration(DURATION);
        Style.Builder style = new Style.Builder();
        style.setConfiguration(conf.build());
        switch(type)
        {
            case INFO:
            {
                style.setBackgroundColor(Style.holoBlueLight);
                break;
            }
            case ALERT:
            {
                style.setBackgroundColor(Style.holoRedLight);
                break;
            }
        }
        return style.build();
    }

    public Crouton makeCrouton()
    {
        return Crouton.makeText(activity, text, getStyle());
    }

    public Toast makeToast()
    {
        return Toast.makeText(activity, text, Toast.LENGTH_LONG);
    }
}