CodeGenerator.java revision 3e190ede751474c7e1b87b34e36354c0ef9e8af4
1/*
2 * Copyright 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.notificationstudio.generator;
18import static com.android.notificationstudio.model.EditableItem.ACTION1_ICON;
19import static com.android.notificationstudio.model.EditableItem.ACTION1_TEXT;
20import static com.android.notificationstudio.model.EditableItem.ACTION2_ICON;
21import static com.android.notificationstudio.model.EditableItem.ACTION2_TEXT;
22import static com.android.notificationstudio.model.EditableItem.ACTION3_ICON;
23import static com.android.notificationstudio.model.EditableItem.ACTION3_TEXT;
24import static com.android.notificationstudio.model.EditableItem.BIG_CONTENT_TITLE;
25import static com.android.notificationstudio.model.EditableItem.BIG_TEXT;
26import static com.android.notificationstudio.model.EditableItem.CONTENT_INFO;
27import static com.android.notificationstudio.model.EditableItem.CONTENT_TEXT;
28import static com.android.notificationstudio.model.EditableItem.CONTENT_TITLE;
29import static com.android.notificationstudio.model.EditableItem.LARGE_ICON;
30import static com.android.notificationstudio.model.EditableItem.LINES;
31import static com.android.notificationstudio.model.EditableItem.NUMBER;
32import static com.android.notificationstudio.model.EditableItem.PICTURE;
33import static com.android.notificationstudio.model.EditableItem.PROGRESS;
34import static com.android.notificationstudio.model.EditableItem.SMALL_ICON;
35import static com.android.notificationstudio.model.EditableItem.STYLE;
36import static com.android.notificationstudio.model.EditableItem.SUB_TEXT;
37import static com.android.notificationstudio.model.EditableItem.SUMMARY_TEXT;
38import static com.android.notificationstudio.model.EditableItem.USES_CHRON;
39import static com.android.notificationstudio.model.EditableItem.WHEN;
40
41import android.content.Context;
42
43import com.android.notificationstudio.model.EditableItem;
44import com.android.notificationstudio.model.EditableItemConstants;
45
46public class CodeGenerator implements EditableItemConstants {
47
48    private static final String INDENT = "\n    ";
49    private static final String STYLE_INDENT = INDENT + "    ";
50
51    public static String generate(Context context) {
52
53        StringBuilder sb = new StringBuilder("new Notification.Builder(context)");
54
55        if (SMALL_ICON.hasValue())
56            sb.append(INDENT + ".setSmallIcon(" + getResourceVar(context, SMALL_ICON) + ")");
57        if (CONTENT_TITLE.hasValue())
58            sb.append(INDENT + ".setContentTitle(" + quote(CONTENT_TITLE) + ")");
59        if (CONTENT_TEXT.hasValue())
60            sb.append(INDENT + ".setContentText(" + quote(CONTENT_TEXT) + ")");
61        if (SUB_TEXT.hasValue())
62            sb.append(INDENT + ".setSubText(" + quote(SUB_TEXT) + ")");
63        if (LARGE_ICON.hasValue())
64            sb.append(INDENT + ".setLargeIcon(largeIconBitmap)");
65        if (CONTENT_INFO.hasValue())
66            sb.append(INDENT + ".setContentInfo(" + quote(CONTENT_INFO) + ")");
67        if (NUMBER.hasValue())
68            sb.append(INDENT + ".setNumber(" + NUMBER.getValueInt() + ")");
69        if (WHEN.hasValue())
70            sb.append(INDENT + ".setWhen(" + WHEN.getValueLong() + ")");
71        if (PROGRESS.hasValue() && PROGRESS.getValueBool())
72            sb.append(INDENT + ".setProgress(0, 0, true)");
73        if (USES_CHRON.hasValue())
74            sb.append(INDENT + ".setUsesChronometer(" + USES_CHRON.getValueBool() + ")");
75        if (ACTION1_ICON.hasValue())
76            generateAction(sb, ACTION1_ICON, ACTION1_TEXT, "action1PendingIntent");
77        if (ACTION2_ICON.hasValue())
78            generateAction(sb, ACTION2_ICON, ACTION2_TEXT, "action2PendingIntent");
79        if (ACTION3_ICON.hasValue())
80            generateAction(sb, ACTION3_ICON, ACTION3_TEXT, "action3PendingIntent");
81
82        if (STYLE.hasValue())
83            generateStyle(sb);
84
85        sb.append(INDENT + ".build();");
86        return sb.toString();
87    }
88
89    private static void generateStyle(StringBuilder sb) {
90        Integer styleValue = STYLE.getValueInt();
91        if (STYLE_BIG_PICTURE.equals(styleValue)) {
92            sb.append(INDENT + ".setStyle(new Notification.BigPictureStyle()");
93            if (PICTURE.hasValue())
94                sb.append(STYLE_INDENT + ".bigPicture(pictureBitmap)");
95        }
96        if (STYLE_BIG_TEXT.equals(styleValue)) {
97            sb.append(INDENT + ".setStyle(new Notification.BigTextStyle()");
98            if (BIG_TEXT.hasValue())
99                sb.append(STYLE_INDENT + ".bigText(" + quote(BIG_TEXT) + ")");
100        }
101        if (STYLE_INBOX.equals(styleValue)) {
102            sb.append(INDENT + ".setStyle(new Notification.InboxStyle()");
103            if (LINES.hasValue()) {
104                for (String line : LINES.getValueString().split("\\n")) {
105                    sb.append(STYLE_INDENT + ".addLine(" + quote(line) + ")");
106                }
107            }
108        }
109        if (BIG_CONTENT_TITLE.hasValue())
110            sb.append(STYLE_INDENT + ".setBigContentTitle(" + quote(BIG_CONTENT_TITLE) + ")");
111        if (SUMMARY_TEXT.hasValue())
112            sb.append(STYLE_INDENT + ".setSummaryText(" + quote(SUMMARY_TEXT) + ")");
113
114        sb.append(")");
115    }
116
117    private static void generateAction(StringBuilder sb,
118            EditableItem icon, EditableItem text, String intentName) {
119        sb.append(INDENT +
120            ".addAction(" + icon.getValueInt() + ", " + quote(text) + ", " + intentName + ")");
121    }
122
123    private static String quote(EditableItem text) {
124        return quote(text.getValueString());
125    }
126
127    private static String quote(String text) {
128        return text != null ? "\"" + text.replace("\"", "\\\"") + "\"" : "null";
129    }
130
131    private static String getResourceVar(Context context, EditableItem item) {
132        int resId = item.getValueInt();
133        String packageName = context.getResources().getResourcePackageName(resId);
134        String type = context.getResources().getResourceTypeName(resId);
135        String entryName = context.getResources().getResourceEntryName(resId);
136        return packageName + ".R." + type + "." + entryName;
137    }
138
139}
140