aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/miz_hi/smileessence/notification/Notificator.java
blob: 11b693f1c8f2b4722dc9e47d6cfb9da457cb6630 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package net.miz_hi.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.miz_hi.smileessence.model.status.event.EventModel;
import net.miz_hi.smileessence.model.status.event.IAttackEvent;
import net.miz_hi.smileessence.model.status.event.StatusEvent;
import net.miz_hi.smileessence.util.CountUpInteger;
import net.miz_hi.smileessence.util.UiHandler;
import net.miz_hi.smileessence.view.activity.MainActivity;

public class Notificator
{

    private static long lastUserId = -1;
    private static long lastStatusId = -1;
    private static CountUpInteger counterSourceUser = new CountUpInteger(5);
    private static CountUpInteger counterTargetStatus = new CountUpInteger(5);

    public static void toast(final String text)
    {
        final Activity activity = MainActivity.getInstance();
        if (activity == null || activity.isFinishing())
        {
            return;
        }
        new UiHandler()
        {

            @Override
            public void run()
            {
                Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
            }
        }.post();
    }

    public static void info(String text)
    {
        crouton(new Notice(text));
    }

    public static void alert(String text)
    {
        crouton(new Notice(text).setStyle(Style.ALERT));
    }

    private static Style getStyle(Style type)
    {
        Configuration.Builder config = new Configuration.Builder();
        config.setDuration(1000);
        Style.Builder builder = new Style.Builder();
        builder.setHeight(64).setConfiguration(config.build());
        if (type == Style.INFO)
        {
            builder.setBackgroundColorValue(Style.holoBlueLight);
        }
        else if (type == Style.ALERT)
        {
            builder.setBackgroundColorValue(Style.holoRedLight);
        }
        return builder.build();
    }

    public static void crouton(final Notice event)
    {
        final Activity activity = MainActivity.getInstance();
        if (activity == null || activity.isFinishing())
        {
            return;
        }

        new UiHandler()
        {

            @Override
            public void run()
            {
                Crouton.makeText(activity, event.getText(), getStyle(event.getStyle())).show();
            }
        }.post();
    }

    public static Notice buildEvent(final EventModel model)
    {
        if (model instanceof StatusEvent)
        {
            StatusEvent se = (StatusEvent) model;
            if (se instanceof IAttackEvent)
            {
                if (lastUserId != se.source.userId)
                {
                    counterSourceUser.reset();
                    lastUserId = se.source.userId;
                }
                else
                {
                    if (counterSourceUser.isOver())
                    {
                        return Notice.getNullEvent();
                    }

                    if (counterSourceUser.countUp())
                    {
                        return new Notice(se.source.screenName + "から攻撃を受けています");
                    }
                }

                if (lastStatusId != se.tweet.statusId)
                {
                    counterTargetStatus.reset();
                    lastStatusId = se.tweet.statusId;
                }
                else
                {
                    if (counterTargetStatus.isOver())
                    {
                        return Notice.getNullEvent();
                    }
                    if (counterTargetStatus.countUp())
                    {
                        return new Notice("あなたのツイートが攻撃を受けています");
                    }
                }
            }
        }

        return new Notice(model.getTextTop());
    }
}