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.model;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 
24 import com.android.notificationstudio.R;
25 
26 public enum EditableItem implements EditableItemConstants {
27 
28     PRESET(R.string.preset, TYPE_DROP_DOWN, CATEGORY_MAIN,
29             PRESET_BASIC, PRESET_EMAIL, PRESET_PHOTO, PRESET_CUSTOM),
30     SMALL_ICON(R.string.small_icon, TYPE_RESOURCE_ID, CATEGORY_MAIN,
31             SMALL_ICONS),
32     CONTENT_TITLE(R.string.content_title, TYPE_TEXT, CATEGORY_MAIN),
33     CONTENT_TEXT(R.string.content_text, TYPE_TEXT, CATEGORY_MAIN),
34     SUB_TEXT(R.string.sub_text, TYPE_TEXT, CATEGORY_MAIN),
35     LARGE_ICON(R.string.large_icon, TYPE_BITMAP, CATEGORY_MAIN),
36     CONTENT_INFO(R.string.content_info, TYPE_TEXT, CATEGORY_MAIN),
37     NUMBER(R.string.number, TYPE_INT, CATEGORY_MAIN),
38     WHEN(R.string.when, TYPE_DATETIME, CATEGORY_MAIN),
39     PROGRESS(R.string.progress, TYPE_BOOLEAN, CATEGORY_MAIN),
40     USES_CHRON(R.string.uses_chron, TYPE_BOOLEAN, CATEGORY_MAIN),
41     STYLE(R.string.style, TYPE_DROP_DOWN, CATEGORY_STYLE,
42             STYLE_NONE, STYLE_BIG_PICTURE, STYLE_BIG_TEXT, STYLE_INBOX),
43     PICTURE(R.string.picture, TYPE_BITMAP, CATEGORY_STYLE),
44     BIG_TEXT(R.string.big_text, TYPE_TEXT, CATEGORY_STYLE),
45     LINES(R.string.lines, TYPE_TEXT_LINES, CATEGORY_STYLE),
46     BIG_CONTENT_TITLE(R.string.big_content_title, TYPE_TEXT, CATEGORY_STYLE),
47     SUMMARY_TEXT(R.string.summary_text, TYPE_TEXT, CATEGORY_STYLE),
48     ACTION1_ICON(R.string.icon, TYPE_RESOURCE_ID, CATEGORY_ACTION1,
49             ACTION_ICONS),
50     ACTION1_TEXT(R.string.text, TYPE_TEXT, CATEGORY_ACTION1),
51     ACTION2_ICON(R.string.icon, TYPE_RESOURCE_ID, CATEGORY_ACTION2,
52             ACTION_ICONS),
53     ACTION2_TEXT(R.string.text, TYPE_TEXT, CATEGORY_ACTION2),
54     ACTION3_ICON(R.string.icon, TYPE_RESOURCE_ID, CATEGORY_ACTION3,
55             ACTION_ICONS),
56     ACTION3_TEXT(R.string.text, TYPE_TEXT, CATEGORY_ACTION3),
57     ;
58 
59     private final int mCaptionId;
60     private final int mType;
61     private final int mCategoryId;
62 
63     private Object[] mAvailableValues;
64     private Object mValue;
65     private boolean mVisible = true;
66     private Runnable mVisibilityListener;
67 
EditableItem(int captionId, int type, int categoryId, Object... availableValues)68     private EditableItem(int captionId, int type, int categoryId, Object... availableValues) {
69         mCaptionId = captionId;
70         mType = type;
71         mCategoryId = categoryId;
72         mAvailableValues = availableValues;
73     }
74 
75     // init
initIfNecessary(Context context)76     public static void initIfNecessary(Context context) {
77         if (PRESET.hasValue())
78             return;
79         loadBitmaps(context, LARGE_ICON, LARGE_ICONS);
80         loadBitmaps(context, PICTURE, PICTURES);
81         PRESET.setValue(PRESET_BASIC);
82     }
83 
loadBitmaps(Context context, EditableItem item, int[] bitmapResIds)84     private static void loadBitmaps(Context context, EditableItem item, int[] bitmapResIds) {
85         Object[] largeIconBitmaps = new Object[bitmapResIds.length];
86         Resources res = context.getResources();
87         for (int i = 0; i < bitmapResIds.length; i++)
88             largeIconBitmaps[i] = BitmapFactory.decodeResource(res, bitmapResIds[i]);
89         item.setAvailableValues(largeIconBitmaps);
90     }
91 
92     // visibility
isVisible()93     public boolean isVisible() {
94         return mVisible;
95     }
96 
setVisible(boolean visible)97     public void setVisible(boolean visible) {
98         if (mVisible == visible)
99             return;
100         mVisible = visible;
101         if (mVisibilityListener != null)
102             mVisibilityListener.run();
103     }
104 
setVisibilityListener(Runnable listener)105     public void setVisibilityListener(Runnable listener) {
106         mVisibilityListener = listener;
107     }
108 
109     // value
110 
hasValue()111     public boolean hasValue() {
112         return mValue != null;
113     }
114 
setValue(Object value)115     public void setValue(Object value) {
116         if (mValue == value)
117             return;
118         mValue = value;
119         if (this == STYLE)
120             applyStyle();
121         if (this == PRESET && !PRESET_CUSTOM.equals(value))
122             applyPreset();
123     }
124 
applyStyle()125     private void applyStyle() {
126         PICTURE.setVisible(STYLE_BIG_PICTURE.equals(mValue));
127         BIG_TEXT.setVisible(STYLE_BIG_TEXT.equals(mValue));
128         LINES.setVisible(STYLE_INBOX.equals(mValue));
129         BIG_CONTENT_TITLE.setVisible(!STYLE_NONE.equals(mValue));
130         SUMMARY_TEXT.setVisible(!STYLE_NONE.equals(mValue));
131     }
132 
applyPreset()133     private void applyPreset() {
134         for (EditableItem item : values())
135             if (item != PRESET)
136                 item.setValue(null);
137         STYLE.setValue(STYLE_NONE);
138         if (PRESET_BASIC.equals(mValue)) {
139             SMALL_ICON.setValue(android.R.drawable.stat_notify_chat);
140             CONTENT_TITLE.setValue("Basic title");
141             CONTENT_TEXT.setValue("Basic text");
142         } else if (PRESET_EMAIL.equals(mValue)) {
143             SMALL_ICON.setValue(R.drawable.ic_notification_multiple_mail_holo_dark);
144             LARGE_ICON.setValue(LARGE_ICON.getAvailableValues()[3]);
145             CONTENT_TITLE.setValue("3 new messages");
146             CONTENT_TEXT.setValue("Alice, Bob, Chuck");
147             STYLE.setValue(STYLE_INBOX);
148             LINES.setValue("Alice: Re: Something\n" +
149                            "Bob: Did you get the memo?\n" +
150                            "Chuck: Limited time offer!");
151         } else if (PRESET_PHOTO.equals(mValue)) {
152             SMALL_ICON.setValue(android.R.drawable.ic_menu_camera);
153             LARGE_ICON.setValue(LARGE_ICON.getAvailableValues()[2]);
154             CONTENT_TITLE.setValue("Sunset on the rocks");
155             CONTENT_TEXT.setValue("800x534 | 405.1K");
156             SUMMARY_TEXT.setValue(CONTENT_TEXT.getValueString());
157             STYLE.setValue(STYLE_BIG_PICTURE);
158             PICTURE.setValue(PICTURE.getAvailableValues()[0]);
159             ACTION1_ICON.setValue(android.R.drawable.ic_menu_share);
160             ACTION1_TEXT.setValue("Share");
161         }
162     }
163 
getValue()164     public Object getValue() {
165         return mValue;
166     }
167 
getValueString()168     public String getValueString() {
169         return (String) mValue;
170     }
171 
getValueInt()172     public int getValueInt() {
173         return (Integer) mValue;
174     }
175 
getValueLong()176     public long getValueLong() {
177         return (Long) mValue;
178     }
179 
getValueBool()180     public boolean getValueBool() {
181         return (Boolean) mValue;
182     }
183 
getValueBitmap()184     public Bitmap getValueBitmap() {
185         return (Bitmap) mValue;
186     }
187 
188     // available values
189 
getAvailableValues()190     public Object[] getAvailableValues() {
191         return mAvailableValues;
192     }
193 
getAvailableValuesInteger()194     public Integer[] getAvailableValuesInteger() {
195         Integer[] integers = new Integer[mAvailableValues.length];
196         System.arraycopy(mAvailableValues, 0, integers, 0, integers.length);
197         return integers;
198     }
199 
setAvailableValues(T... values)200     public <T> void setAvailableValues(T... values) {
201         mAvailableValues = values;
202     }
203 
getCaption(Context context)204     public String getCaption(Context context) {
205         return context.getString(mCaptionId);
206     }
207 
getCategory(Context context)208     public String getCategory(Context context) {
209         return context.getString(mCategoryId);
210     }
211 
getType()212     public int getType() {
213         return mType;
214     }
215 
216 }
217