aboutsummaryrefslogtreecommitdiff
path: root/apps/NotificationStudio/src/com/android/notificationstudio/generator/NotificationGenerator.java
blob: 64b4ece28c8bf5cf852667f849a746bc93b29cad (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
/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.notificationstudio.generator;
import static com.android.notificationstudio.model.EditableItem.ACTION1_ICON;
import static com.android.notificationstudio.model.EditableItem.ACTION1_TEXT;
import static com.android.notificationstudio.model.EditableItem.ACTION2_ICON;
import static com.android.notificationstudio.model.EditableItem.ACTION2_TEXT;
import static com.android.notificationstudio.model.EditableItem.ACTION3_ICON;
import static com.android.notificationstudio.model.EditableItem.ACTION3_TEXT;
import static com.android.notificationstudio.model.EditableItem.BIG_CONTENT_TITLE;
import static com.android.notificationstudio.model.EditableItem.BIG_TEXT;
import static com.android.notificationstudio.model.EditableItem.CONTENT_INFO;
import static com.android.notificationstudio.model.EditableItem.CONTENT_TEXT;
import static com.android.notificationstudio.model.EditableItem.CONTENT_TITLE;
import static com.android.notificationstudio.model.EditableItem.LARGE_ICON;
import static com.android.notificationstudio.model.EditableItem.LINES;
import static com.android.notificationstudio.model.EditableItem.NUMBER;
import static com.android.notificationstudio.model.EditableItem.PICTURE;
import static com.android.notificationstudio.model.EditableItem.PROGRESS;
import static com.android.notificationstudio.model.EditableItem.SMALL_ICON;
import static com.android.notificationstudio.model.EditableItem.STYLE;
import static com.android.notificationstudio.model.EditableItem.SUB_TEXT;
import static com.android.notificationstudio.model.EditableItem.SUMMARY_TEXT;
import static com.android.notificationstudio.model.EditableItem.USES_CHRON;
import static com.android.notificationstudio.model.EditableItem.WHEN;

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.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.InboxStyle;

import com.android.notificationstudio.model.EditableItemConstants;

public class NotificationGenerator implements EditableItemConstants {

    public static Notification build(Context context) {

        PendingIntent noop = PendingIntent.getActivity(context, 0, new Intent(), 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        if (SMALL_ICON.hasValue())
            builder.setSmallIcon(SMALL_ICON.getValueInt());
        if (CONTENT_TITLE.hasValue())
            builder.setContentTitle(CONTENT_TITLE.getValueString());
        if (CONTENT_TEXT.hasValue())
            builder.setContentText(CONTENT_TEXT.getValueString());
        if (SUB_TEXT.hasValue())
            builder.setSubText(SUB_TEXT.getValueString());
        if (LARGE_ICON.hasValue())
            builder.setLargeIcon(LARGE_ICON.getValueBitmap());
        if (CONTENT_INFO.hasValue())
            builder.setContentInfo(CONTENT_INFO.getValueString());
        if (NUMBER.hasValue())
            builder.setNumber(NUMBER.getValueInt());
        if (WHEN.hasValue())
            builder.setWhen(WHEN.getValueLong());
        if (PROGRESS.hasValue() && PROGRESS.getValueBool())
            builder.setProgress(0, 0, true);
        if (USES_CHRON.hasValue())
            builder.setUsesChronometer(USES_CHRON.getValueBool());
        if (ACTION1_ICON.hasValue())
            builder.addAction(ACTION1_ICON.getValueInt(), ACTION1_TEXT.getValueString(), noop);
        if (ACTION2_ICON.hasValue())
            builder.addAction(ACTION2_ICON.getValueInt(), ACTION2_TEXT.getValueString(), noop);
        if (ACTION3_ICON.hasValue())
            builder.addAction(ACTION3_ICON.getValueInt(), ACTION3_TEXT.getValueString(), noop);

        if (STYLE.hasValue())
            generateStyle(builder);

        // for older OSes
        builder.setContentIntent(noop);

        return builder.build();
    }

    private static void generateStyle(NotificationCompat.Builder builder) {
        Integer styleValue = STYLE.getValueInt();

        if (STYLE_BIG_PICTURE.equals(styleValue)) {
            BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle();
            if (PICTURE.hasValue())
                bigPicture.bigPicture(PICTURE.getValueBitmap());
            if (BIG_CONTENT_TITLE.hasValue())
                bigPicture.setBigContentTitle(BIG_CONTENT_TITLE.getValueString());
            if (SUMMARY_TEXT.hasValue())
                bigPicture.setSummaryText(SUMMARY_TEXT.getValueString());
            builder.setStyle(bigPicture);
        } else if (STYLE_BIG_TEXT.equals(styleValue)) {
            BigTextStyle bigText = new NotificationCompat.BigTextStyle();
            if (BIG_TEXT.hasValue())
                bigText.bigText(BIG_TEXT.getValueString());
            if (BIG_CONTENT_TITLE.hasValue())
                bigText.setBigContentTitle(BIG_CONTENT_TITLE.getValueString());
            if (SUMMARY_TEXT.hasValue())
                bigText.setSummaryText(SUMMARY_TEXT.getValueString());
            builder.setStyle(bigText);
        } else if (STYLE_INBOX.equals(styleValue)) {
            InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            if (LINES.hasValue()) {
                for (String line : LINES.getValueString().split("\\n")) {
                    inboxStyle.addLine(line);
                }
            }
            if (BIG_CONTENT_TITLE.hasValue())
                inboxStyle.setBigContentTitle(BIG_CONTENT_TITLE.getValueString());
            if (SUMMARY_TEXT.hasValue())
                inboxStyle.setSummaryText(SUMMARY_TEXT.getValueString());
            builder.setStyle(inboxStyle);
        }
    }

}