Home | History | Annotate | Download | only in generator
      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 
     17 package com.android.notificationstudio.generator;
     18 import static com.android.notificationstudio.model.EditableItem.ACTION1_ICON;
     19 import static com.android.notificationstudio.model.EditableItem.ACTION1_TEXT;
     20 import static com.android.notificationstudio.model.EditableItem.ACTION2_ICON;
     21 import static com.android.notificationstudio.model.EditableItem.ACTION2_TEXT;
     22 import static com.android.notificationstudio.model.EditableItem.ACTION3_ICON;
     23 import static com.android.notificationstudio.model.EditableItem.ACTION3_TEXT;
     24 import static com.android.notificationstudio.model.EditableItem.BIG_CONTENT_TITLE;
     25 import static com.android.notificationstudio.model.EditableItem.BIG_TEXT;
     26 import static com.android.notificationstudio.model.EditableItem.CONTENT_INFO;
     27 import static com.android.notificationstudio.model.EditableItem.CONTENT_TEXT;
     28 import static com.android.notificationstudio.model.EditableItem.CONTENT_TITLE;
     29 import static com.android.notificationstudio.model.EditableItem.LARGE_ICON;
     30 import static com.android.notificationstudio.model.EditableItem.LINES;
     31 import static com.android.notificationstudio.model.EditableItem.NUMBER;
     32 import static com.android.notificationstudio.model.EditableItem.PICTURE;
     33 import static com.android.notificationstudio.model.EditableItem.PROGRESS;
     34 import static com.android.notificationstudio.model.EditableItem.SMALL_ICON;
     35 import static com.android.notificationstudio.model.EditableItem.STYLE;
     36 import static com.android.notificationstudio.model.EditableItem.SUB_TEXT;
     37 import static com.android.notificationstudio.model.EditableItem.SUMMARY_TEXT;
     38 import static com.android.notificationstudio.model.EditableItem.USES_CHRON;
     39 import static com.android.notificationstudio.model.EditableItem.WHEN;
     40 
     41 import android.app.Notification;
     42 import android.app.PendingIntent;
     43 import android.content.Context;
     44 import android.content.Intent;
     45 import android.support.v4.app.NotificationCompat;
     46 import android.support.v4.app.NotificationCompat.BigPictureStyle;
     47 import android.support.v4.app.NotificationCompat.BigTextStyle;
     48 import android.support.v4.app.NotificationCompat.InboxStyle;
     49 
     50 import com.android.notificationstudio.model.EditableItemConstants;
     51 
     52 public class NotificationGenerator implements EditableItemConstants {
     53 
     54     public static Notification build(Context context) {
     55 
     56         PendingIntent noop = PendingIntent.getActivity(context, 0, new Intent(), 0);
     57 
     58         NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
     59         if (SMALL_ICON.hasValue())
     60             builder.setSmallIcon(SMALL_ICON.getValueInt());
     61         if (CONTENT_TITLE.hasValue())
     62             builder.setContentTitle(CONTENT_TITLE.getValueString());
     63         if (CONTENT_TEXT.hasValue())
     64             builder.setContentText(CONTENT_TEXT.getValueString());
     65         if (SUB_TEXT.hasValue())
     66             builder.setSubText(SUB_TEXT.getValueString());
     67         if (LARGE_ICON.hasValue())
     68             builder.setLargeIcon(LARGE_ICON.getValueBitmap());
     69         if (CONTENT_INFO.hasValue())
     70             builder.setContentInfo(CONTENT_INFO.getValueString());
     71         if (NUMBER.hasValue())
     72             builder.setNumber(NUMBER.getValueInt());
     73         if (WHEN.hasValue())
     74             builder.setWhen(WHEN.getValueLong());
     75         if (PROGRESS.hasValue() && PROGRESS.getValueBool())
     76             builder.setProgress(0, 0, true);
     77         if (USES_CHRON.hasValue())
     78             builder.setUsesChronometer(USES_CHRON.getValueBool());
     79         if (ACTION1_ICON.hasValue())
     80             builder.addAction(ACTION1_ICON.getValueInt(), ACTION1_TEXT.getValueString(), noop);
     81         if (ACTION2_ICON.hasValue())
     82             builder.addAction(ACTION2_ICON.getValueInt(), ACTION2_TEXT.getValueString(), noop);
     83         if (ACTION3_ICON.hasValue())
     84             builder.addAction(ACTION3_ICON.getValueInt(), ACTION3_TEXT.getValueString(), noop);
     85 
     86         if (STYLE.hasValue())
     87             generateStyle(builder);
     88 
     89         // for older OSes
     90         builder.setContentIntent(noop);
     91 
     92         return builder.build();
     93     }
     94 
     95     private static void generateStyle(NotificationCompat.Builder builder) {
     96         Integer styleValue = STYLE.getValueInt();
     97 
     98         if (STYLE_BIG_PICTURE.equals(styleValue)) {
     99             BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle();
    100             if (PICTURE.hasValue())
    101                 bigPicture.bigPicture(PICTURE.getValueBitmap());
    102             if (BIG_CONTENT_TITLE.hasValue())
    103                 bigPicture.setBigContentTitle(BIG_CONTENT_TITLE.getValueString());
    104             if (SUMMARY_TEXT.hasValue())
    105                 bigPicture.setSummaryText(SUMMARY_TEXT.getValueString());
    106             builder.setStyle(bigPicture);
    107         } else if (STYLE_BIG_TEXT.equals(styleValue)) {
    108             BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    109             if (BIG_TEXT.hasValue())
    110                 bigText.bigText(BIG_TEXT.getValueString());
    111             if (BIG_CONTENT_TITLE.hasValue())
    112                 bigText.setBigContentTitle(BIG_CONTENT_TITLE.getValueString());
    113             if (SUMMARY_TEXT.hasValue())
    114                 bigText.setSummaryText(SUMMARY_TEXT.getValueString());
    115             builder.setStyle(bigText);
    116         } else if (STYLE_INBOX.equals(styleValue)) {
    117             InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    118             if (LINES.hasValue()) {
    119                 for (String line : LINES.getValueString().split("\\n")) {
    120                     inboxStyle.addLine(line);
    121                 }
    122             }
    123             if (BIG_CONTENT_TITLE.hasValue())
    124                 inboxStyle.setBigContentTitle(BIG_CONTENT_TITLE.getValueString());
    125             if (SUMMARY_TEXT.hasValue())
    126                 inboxStyle.setSummaryText(SUMMARY_TEXT.getValueString());
    127             builder.setStyle(inboxStyle);
    128         }
    129     }
    130 
    131 }
    132