Copyright (c) 2012, Snakk! Media Group
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are m...
If you think the Android project snakk-ads-android-sample-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.snakk.advertising.internal;
//fromwww.java2s.comimport android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import java.util.HashMap;
import java.util.Map;
/**
* Convenience class used for passing live objects between intents
* pulled from http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android#7683528
*/publicfinalclass Sharable<T> implements Parcelable {
private T m_object;
/**
* Only used for distinguishing null sharables in the debugger.
* It's not referenced anywhere else, and wont be carried over when
* readFromParcel is called.
*/private String debugTag = null;
publicstaticfinal Parcelable.Creator<Sharable> CREATOR = new Parcelable.Creator<Sharable>() {
public Sharable createFromParcel(Parcel in) {
returnnew Sharable (in);
}
@Override
public Sharable[] newArray(int size) {
returnnew Sharable[size];
}
};
public Sharable(final T obj, final String debugTag) {
this.debugTag = debugTag;
m_object = obj;
}
public Sharable(Parcel in) {
readFromParcel(in);
}
public T obj() {
return m_object;
}
@Override
publicint describeContents() {
return 0;
}
@Override
publicvoid writeToParcel(final Parcel out, int flags) {
finallong val;
if(m_object != null) {
val = SystemClock.elapsedRealtime() + m_object.hashCode();
}
else {
val = 0;
}
out.writeLong(val);
put(val, m_object);
}
@SuppressWarnings("unchecked")
privatevoid readFromParcel(final Parcel in) {
finallong val = in.readLong();
m_object = (T)get(val);
}
/////
//TODO do something about cast safety here...
privatestaticfinal Map<Long, Object> s_sharableMap = new HashMap<Long, Object>(3);
synchronizedprivatestaticvoid put(long key, final Object obj) {
s_sharableMap.put(key, obj);
}
synchronizedprivatestatic Object get(long key) {
return s_sharableMap.remove(key);
}
}