-
-
Save c3ph3us/496d82751de74cae46f6d2aad2f11e71 to your computer and use it in GitHub Desktop.
example implementation of abstract parcelable
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.os.Parcel; | |
import android.os.Parcelable; | |
import pl.ceph3us.base.common.interfaces.annotations.NonNull; | |
import pl.ceph3us.base.common.interfaces.annotations.Nullable; | |
import java.util.*; | |
/** | |
* Created by ceph3us on 18.07.16. | |
* file belong to $pl.ceph3us.projects.android.datezone.services.user_panel.notifications | |
*/ | |
public abstract class ExtendedNotification extends Notification implements Parcelable { | |
public static final int ID_UNSET = -999; | |
public static final String INTENT_KEY = "intentKey"; | |
private int VAL_NULL = 0 ; | |
private static final String INJECTED_ID = "injected_id"; | |
public static final String TAG_UNDEFINED = "undefinedTag"; | |
private boolean _wasRead; | |
private boolean _isMarkedToDelete; | |
/** this could be set only at creation process other way you need use injected extras id */ | |
private int _id = ID_UNSET; | |
private int title; | |
public int getUntouchedId() { | |
return _id; | |
} | |
public int getId() { | |
if(getUntouchedId() == ID_UNSET) return checkAdrReturnInjectedId(); | |
return getUntouchedId(); | |
} | |
private int checkAdrReturnInjectedId() { | |
int injectedId = extras.getInt(INJECTED_ID); | |
return injectedId != VAL_NULL ? injectedId : ID_UNSET; | |
} | |
public void injectIdToExtras(int injectedId) { | |
extras.putInt(INJECTED_ID,injectedId); | |
} | |
@Nullable | |
public abstract String getTitle(); | |
@Nullable | |
public abstract String getMessage(); | |
@Nullable | |
public abstract String getSubMessage(); | |
public abstract String getTag(); | |
public abstract boolean hasProperty(String propertyName); | |
public abstract String getPropertyValue(String propertyName); | |
public boolean wasRead() { | |
return _wasRead; | |
} | |
public void markAsRead() { | |
_wasRead = true; | |
} | |
public void markToDelete() { _isMarkedToDelete = true; } | |
public boolean isMarkedToDelete() { return _isMarkedToDelete;} | |
public ExtendedNotification() { | |
} | |
public ExtendedNotification(int id) { | |
_id = id; | |
} | |
public ExtendedNotification(Parcel parcel) { | |
super(parcel); | |
_id = parcel.readInt(); | |
_wasRead = parcel.readInt() != 0; | |
} | |
@Override | |
public void writeToParcel(Parcel parcel, int flags) { | |
super.writeToParcel(parcel, flags); | |
parcel.writeInt(_id); | |
parcel.writeInt(_wasRead ? 1 : 0); | |
} | |
public String constructNotificationServiceTag(String requestedTag, int requestedCode) { | |
return "t=" + requestedTag + ":rq=" + requestedCode; | |
} | |
public void writeMapToParcel(Map<String, String> map, Parcel parcel) { | |
/** create set */ | |
Set<Map.Entry<String, String>> entries = map.entrySet(); | |
/**( getStatic size */ | |
int size = entries.size(); | |
/** write size */ | |
parcel.writeInt(size); | |
/** getStatic iterator */ | |
Iterator<Map.Entry<String, String>> iterator = entries.iterator(); | |
/** iterate through elements */ | |
while (iterator.hasNext()) { | |
/** write element as K,V */ | |
Map.Entry<String, String> next = iterator.next(); | |
parcel.writeString(next.getKey()); | |
parcel.writeString(next.getValue()); | |
} | |
} | |
public Map<String, String> readMapFromParcel(Parcel parcel) { | |
Map<String, String> map = new HashMap<>(); | |
/** read size */ | |
int size = parcel.readInt(); | |
/** loop to getStatic content of map K,V*/ | |
for (int s = 0; s < size; s++) { | |
String key = parcel.readString(); | |
String val = parcel.readString(); | |
/** put in map */ | |
map.put(key, val); | |
} | |
return map; | |
} | |
public static List<ExtendedNotification> getAsList(@NonNull List<Parcelable> parcelables) { | |
int size = parcelables.size(); | |
Parcelable[] parcelablesPrimList = parcelables.toArray(new Parcelable[size]); | |
if(parcelablesPrimList.length ==0) return new ArrayList<ExtendedNotification>(); | |
return getAsList(parcelablesPrimList); | |
} | |
public static ExtendedNotification[] getAsPrimList(Parcelable[] parcelables) throws UnsupportedOperationException { | |
int length = parcelables.length; | |
if (length > 0 && ExtendedNotification.class.isAssignableFrom(parcelables[0].getClass())) { | |
ExtendedNotification[] objects = new ExtendedNotification[length]; | |
System.arraycopy(parcelables, 0, objects, 0, length); | |
return objects; | |
} | |
throw new UnsupportedOperationException("Parcelable empty or not ExtendedNotificationType!"); | |
} | |
public static List<ExtendedNotification> getAsList(Parcelable[] parcelables) throws UnsupportedOperationException { | |
ExtendedNotification[] asPrimList = DatezoneNotification.getAsPrimList(parcelables); | |
return pl.ceph3us.dat3us.utils.UtilsArrays.asListImpl(asPrimList); | |
} | |
public abstract boolean isApplicableToAdd(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment