1 /*
2  * Copyright (C) 2014 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.inputmethod.compat;
18 
19 import android.app.Notification;
20 import android.os.Build;
21 
22 import java.lang.reflect.Field;
23 import java.lang.reflect.Method;
24 
25 public class NotificationCompatUtils {
26     // Note that TextInfo.getCharSequence() is supposed to be available in API level 21 and later.
27     private static final Method METHOD_setColor =
28             CompatUtils.getMethod(Notification.Builder.class, "setColor", int.class);
29     private static final Method METHOD_setVisibility =
30             CompatUtils.getMethod(Notification.Builder.class, "setVisibility", int.class);
31     private static final Method METHOD_setCategory =
32             CompatUtils.getMethod(Notification.Builder.class, "setCategory", String.class);
33     private static final Method METHOD_setPriority =
34             CompatUtils.getMethod(Notification.Builder.class, "setPriority", int.class);
35     private static final Method METHOD_build =
36             CompatUtils.getMethod(Notification.Builder.class, "build");
37     private static final Field FIELD_VISIBILITY_SECRET =
38             CompatUtils.getField(Notification.class, "VISIBILITY_SECRET");
39     private static final int VISIBILITY_SECRET = null == FIELD_VISIBILITY_SECRET ? 0
40             : (Integer) CompatUtils.getFieldValue(null /* receiver */, null /* defaultValue */,
41                     FIELD_VISIBILITY_SECRET);
42     private static final Field FIELD_CATEGORY_RECOMMENDATION =
43             CompatUtils.getField(Notification.class, "CATEGORY_RECOMMENDATION");
44     private static final String CATEGORY_RECOMMENDATION = null == FIELD_CATEGORY_RECOMMENDATION ? ""
45             : (String) CompatUtils.getFieldValue(null /* receiver */, null /* defaultValue */,
46                     FIELD_CATEGORY_RECOMMENDATION);
47     private static final Field FIELD_PRIORITY_LOW =
48             CompatUtils.getField(Notification.class, "PRIORITY_LOW");
49     private static final int PRIORITY_LOW = null == FIELD_PRIORITY_LOW ? 0
50             : (Integer) CompatUtils.getFieldValue(null /* receiver */, null /* defaultValue */,
51                     FIELD_PRIORITY_LOW);
52 
NotificationCompatUtils()53     private NotificationCompatUtils() {
54         // This class is non-instantiable.
55     }
56 
57     // Sets the accent color
setColor(final Notification.Builder builder, final int color)58     public static void setColor(final Notification.Builder builder, final int color) {
59         CompatUtils.invoke(builder, null, METHOD_setColor, color);
60     }
61 
setVisibilityToSecret(final Notification.Builder builder)62     public static void setVisibilityToSecret(final Notification.Builder builder) {
63         CompatUtils.invoke(builder, null, METHOD_setVisibility, VISIBILITY_SECRET);
64     }
65 
setCategoryToRecommendation(final Notification.Builder builder)66     public static void setCategoryToRecommendation(final Notification.Builder builder) {
67         CompatUtils.invoke(builder, null, METHOD_setCategory, CATEGORY_RECOMMENDATION);
68     }
69 
setPriorityToLow(final Notification.Builder builder)70     public static void setPriorityToLow(final Notification.Builder builder) {
71         CompatUtils.invoke(builder, null, METHOD_setPriority, PRIORITY_LOW);
72     }
73 
74     @SuppressWarnings("deprecation")
build(final Notification.Builder builder)75     public static Notification build(final Notification.Builder builder) {
76         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
77             // #build was added in API level 16, JELLY_BEAN
78             return (Notification) CompatUtils.invoke(builder, null, METHOD_build);
79         }
80         // #getNotification was deprecated in API level 16, JELLY_BEAN
81         return builder.getNotification();
82     }
83 }
84