PageRenderTime 24ms CodeModel.GetById 8ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/TalkBack/src/com/google/android/marvin/talkback/NotificationType.java

http://eyes-free.googlecode.com/
Java | 191 lines | 128 code | 34 blank | 29 comment | 50 complexity | 477a26cdeab4930eba8df7575d9ddb78 MD5 | raw file
  1/*
  2 * Copyright (C) 2009 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.google.android.marvin.talkback;
 18
 19import android.content.Context;
 20import android.os.Parcel;
 21import android.os.Parcelable;
 22import android.util.Log;
 23
 24/**
 25 * Type safe enumeration for various notification types.
 26 * 
 27 * @author svetoslavganov@google.com (Svetoslav R. Ganov)
 28 */
 29public enum NotificationType implements Parcelable {
 30    TEXT_MESSAGE(R.string.notification_type_text_message), TEXT_MESSAGE_FAILED(
 31            R.string.notification_type_failed_text_message), MISSED_CALL(
 32            R.string.notification_type_missed_call), USB_CONNECTED(
 33            R.string.notification_type_usb_connected),
 34    MUTE(R.string.notification_type_status_mute), CHAT(R.string.notification_type_status_chat),
 35    ERROR(R.string.notification_type_status_error), MORE(R.string.notification_type_status_more),
 36    SDCARD(R.string.notification_type_status_sdcard), SDCARD_USB(
 37            R.string.notification_type_status_sdcard_usb), SYNC(R.string.notification_status_sync),
 38    SYNC_NOANIM(R.string.notification_type_status_sync_noanim), VOICEMAIL(
 39            R.string.notification_type_status_voicemail), PLAY(R.string.notification_status_play),
 40    EMAIL(R.string.notification_type_new_email);
 41
 42    private int mValue;
 43
 44    private static final int ICON_SMS = loadIcon("com.android.mms", "com.android.mms.R$drawable",
 45            "stat_notify_sms");
 46
 47    private static final int ICON_SMS_FAILED = loadIcon("com.android.mms",
 48            "com.android.mms.R$drawable", "stat_notify_sms_failed");
 49
 50    private static final int ICON_PLAY = loadIcon("com.google.android.music",
 51            "com.android.music.R$drawable", "stat_notify_musicplayer");
 52
 53    private static final int ICON_GMAIL = loadIcon("com.google.android.gm",
 54            "com.google.android.gm.R$drawable", "stat_notify_email");
 55
 56    private static final int ICON_EMAIL = loadIcon("com.google.android.email",
 57            "com.android.email.R$drawable", "stat_notify_email_generic");
 58
 59    // INCLUDED in android.jar (accessible via API)
 60    private static final int ICON_MISSED_CALL = android.R.drawable.stat_notify_missed_call;
 61
 62    private static final int ICON_MUTE = android.R.drawable.stat_notify_call_mute;
 63
 64    private static final int ICON_CHAT = android.R.drawable.stat_notify_chat;
 65
 66    private static final int ICON_ERROR = android.R.drawable.stat_notify_error;
 67
 68    private static final int ICON_MORE = android.R.drawable.stat_notify_more;
 69
 70    private static final int ICON_SDCARD = android.R.drawable.stat_notify_sdcard;
 71
 72    private static final int ICON_SDCARD_USB = android.R.drawable.stat_notify_sdcard_usb;
 73
 74    private static final int ICON_SYNC = android.R.drawable.stat_notify_sync;
 75
 76    private static final int ICON_SYNC_NOANIM = android.R.drawable.stat_notify_sync_noanim;
 77
 78    private static final int ICON_VOICEMAIL = android.R.drawable.stat_notify_voicemail;
 79
 80    // This constant must be public to reference it to screen out phone calls
 81    public static final int ICON_PHONE_CALL = android.R.drawable.stat_sys_phone_call;
 82
 83    private static final int INVALID_ICON = -1;
 84
 85    private NotificationType(int value) {
 86        mValue = value;
 87    }
 88
 89    public int getValue() {
 90        return mValue;
 91    }
 92
 93    @Override
 94    public int describeContents() {
 95        return 0;
 96    }
 97
 98    @Override
 99    public void writeToParcel(Parcel parcel, int flags) {
100        parcel.writeInt(mValue);
101    }
102
103    /**
104     * Gets an enumeration member for value.
105     * 
106     * @param value The value.
107     * @return The enumeration member.
108     */
109    private static NotificationType getMemberForValue(int value) {
110        for (NotificationType type : values()) {
111            if (type.mValue == value) {
112                return type;
113            }
114        }
115        return null;
116    }
117
118    public static final Parcelable.Creator<NotificationType> CREATOR =
119            new Parcelable.Creator<NotificationType>() {
120                @Override
121                public NotificationType createFromParcel(Parcel parcel) {
122                    int value = parcel.readInt();
123                    return getMemberForValue(value);
124                }
125
126                @Override
127                public NotificationType[] newArray(int size) {
128                    return new NotificationType[size];
129                }
130            };
131
132    public static NotificationType getNotificationTypeFromIcon(int icon) {
133
134        // Can't use switch because not all of the icon fields are constant
135        if (icon == ICON_SMS) {
136            return NotificationType.TEXT_MESSAGE;
137        } else if (icon == ICON_SMS_FAILED) {
138            return NotificationType.TEXT_MESSAGE_FAILED;
139        } else if (icon == ICON_MISSED_CALL) {
140            return NotificationType.MISSED_CALL;
141        } else if (icon == ICON_MUTE) {
142            return NotificationType.MUTE;
143        } else if (icon == ICON_CHAT) {
144            return NotificationType.CHAT;
145        } else if (icon == ICON_ERROR) {
146            return NotificationType.ERROR;
147        } else if (icon == ICON_MORE) {
148            return NotificationType.MORE;
149        } else if (icon == ICON_SDCARD) {
150            return NotificationType.SDCARD;
151        } else if (icon == ICON_SDCARD_USB) {
152            return NotificationType.SDCARD_USB;
153        } else if (icon == ICON_SYNC) {
154            return NotificationType.SYNC;
155        } else if (icon == ICON_SYNC_NOANIM) {
156            return NotificationType.SYNC_NOANIM;
157        } else if (icon == ICON_VOICEMAIL) {
158            return NotificationType.VOICEMAIL;
159        } else if (icon == ICON_EMAIL) {
160            return NotificationType.EMAIL;
161        } else if (icon == ICON_GMAIL) {
162            return NotificationType.EMAIL;
163        } else if (icon == ICON_PLAY) {
164            return NotificationType.PLAY;
165        } else {
166            LogUtils.log(NotificationType.class, Log.WARN, "Unknown notification %d", icon);
167            return null;
168        }
169    }
170
171    public static int loadIcon(String packageName, String className, String fieldName) {
172        final ClassLoadingManager clm = ClassLoadingManager.getInstance();
173        final Context ctx = TalkBackService.asContext();
174        final Class<?> drawable = clm.loadOrGetCachedClass(ctx, className, packageName);
175
176        if (drawable == null) {
177            LogUtils.log(NotificationType.class, Log.WARN,
178                    "Can't find class drawable in package: %s", packageName);
179            return INVALID_ICON;
180        }
181
182        try {
183            return drawable.getDeclaredField(fieldName).getInt(null);
184        } catch (Exception e) {
185            LogUtils.log(NotificationType.class, Log.WARN,
186                    "Can't find drawable: %s in package: %s", fieldName, packageName, e.toString());
187
188            return INVALID_ICON;
189        }
190    }
191}