-
-
Save c3ph3us/1ca07c497492b322bf02d01906796471 to your computer and use it in GitHub Desktop.
implementation of ExtendedNotification as parcelable with creator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2015-2016. by ceph3us | |
* All Rights Reserved. | |
* www.ceph3us.pl | |
*/ | |
package pl.ceph3us.projects.android.datezone.services.user_panel.notifications; | |
import android.app.Notification; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.Parcel; | |
import pl.ceph3us.base.android.utils.intents.UtilsPendingIntent; | |
import pl.ceph3us.base.common.interfaces.annotations.Nullable; | |
/** | |
* Created by ceph3us on 28.07.16. | |
* file belong to $pl.ceph3us.projects.android.datezone.services.user_panel.notifications | |
*/ | |
public class ExtendedNotificationWrapper extends ExtendedNotification { | |
@Nullable | |
private final String _tag; | |
@Nullable | |
private final Bundle _wrappedNotificationBundle; | |
@Nullable | |
private final Intent _wrappedIntent; | |
private final int _wrappedIntentForType; | |
private final int _flags; | |
@Nullable | |
transient PendingIntent _wrappedPendingIntent; | |
public PendingIntent getWrappedNotificationPendingIntent(Context context, int requestCode) { | |
return getWrappedNotificationPendingIntent(context, requestCode, false); | |
} | |
public PendingIntent getWrappedNotificationPendingIntent(Context context, int requestCode, boolean forceCreateNew) { | |
if (getWrappedExistingNotificationPendingIntent() == null || forceCreateNew) | |
_wrappedPendingIntent = tryBuildPendingIntent(context, requestCode); | |
return _wrappedPendingIntent; | |
} | |
@Nullable | |
public PendingIntent getWrappedExistingNotificationPendingIntent() { | |
return _wrappedPendingIntent; | |
} | |
@Nullable | |
public Intent getWrappedNotificationIntent() { | |
return _wrappedIntent; | |
} | |
@Nullable | |
private PendingIntent tryBuildPendingIntent(Context context, int requestCode) throws UnsupportedOperationException { | |
Intent wrappedNotificationIntent = getWrappedNotificationIntent(); | |
return wrappedNotificationIntent != null ? UtilsPendingIntent.createPendingIntent(context, wrappedNotificationIntent, _wrappedIntentForType, requestCode, flags) : null; | |
} | |
@Nullable | |
public Bundle getWrappedNotificationBundle() { | |
return _wrappedNotificationBundle; | |
} | |
public static final Creator<ExtendedNotificationWrapper> CREATOR = new Creator<ExtendedNotificationWrapper>() { | |
@Override | |
public ExtendedNotificationWrapper createFromParcel(Parcel in) { | |
return new ExtendedNotificationWrapper(in); | |
} | |
@Override | |
public ExtendedNotificationWrapper[] newArray(int size) { | |
return new ExtendedNotificationWrapper[size]; | |
} | |
}; | |
@Override | |
public void writeToParcel(Parcel parcel, int flags) { | |
super.writeToParcel(parcel, flags); | |
parcel.writeString(_tag); | |
parcel.writeBundle(_wrappedNotificationBundle); | |
parcel.writeParcelable(_wrappedIntent, flags); | |
parcel.writeInt(_wrappedIntentForType); | |
parcel.writeInt(_flags); | |
//PendingIntent.writePendingIntentOrNullToParcel(_wrappedPendingIntent,parcel); | |
} | |
@Override | |
public boolean isApplicableToAdd() { | |
// for now wrapped notifications are all applicable to add ! | |
return true; | |
} | |
public ExtendedNotificationWrapper(Parcel parcel) { | |
super(parcel); | |
_tag = parcel.readString(); | |
_wrappedNotificationBundle = parcel.readBundle(); | |
_wrappedIntent = parcel.readParcelable(Intent.class.getClassLoader()); | |
_wrappedIntentForType = parcel.readInt(); | |
_flags = parcel.readInt(); | |
//_wrappedPendingIntent = PendingIntent.readPendingIntentOrNullFromParcel(parcel); | |
} | |
public ExtendedNotificationWrapper(Notification notification, String tag, int code, Intent laterConvertedToPending, int intentForType, int requestCode) { | |
_tag = tag; | |
_wrappedNotificationBundle = notification.extras; | |
_wrappedIntent = laterConvertedToPending; | |
_wrappedIntentForType = intentForType; | |
_flags = flags; | |
} | |
// @Nullable public Intent getWrappedNotificationIntent() { return (Intent) (getWrappedNotificationBundle() != null ? _wrappedNotificationBundle.getParcelable(ExtendedNotification.INTENT_KEY) : "No wrapped notification instance was passed to this wrapper!");} | |
@Override | |
public String getMessage() { | |
return getWrappedNotificationBundle() != null ? _wrappedNotificationBundle.getString(Notification.EXTRA_TEXT) : "No wrapped notification instance was passed to this wrapper!"; | |
} | |
@Override | |
public String getSubMessage() { | |
return getWrappedNotificationBundle() != null ? _wrappedNotificationBundle.getString(Notification.EXTRA_SUB_TEXT) : "No wrapped notification instance was passed to this wrapper!"; | |
} | |
@Override | |
public String getTitle() { | |
return getWrappedNotificationBundle() != null ? _wrappedNotificationBundle.getString(Notification.EXTRA_TITLE) : "No wrapped notification instance was passed to this wrapper!"; | |
} | |
@Override | |
public String getTag() { | |
if (_tag == null) return TAG_UNDEFINED; | |
return _tag; | |
} | |
@Override | |
public boolean hasProperty(String propertyName) { | |
return false; | |
} | |
@Override | |
public String getPropertyValue(String propertyName) { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment