PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

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