Skip to content

Instantly share code, notes, and snippets.

View andretietz's full-sized avatar

André Tietz andretietz

View GitHub Profile
class WrappingCallAdapter<RETURN_TYPE> implements CallAdapter<RETURN_TYPE> {
private final CallAdapter<RETURN_TYPE> adapter;
private final Map<Integer, String> registration;
private final String info;
WrappingCallAdapter(CallAdapter<RETURN_TYPE> adapter, Map<Integer, String> reg, String info) {
this.adapter = adapter;
this.registration = reg;
this.info = info;
class CustomAnnotationCallAdapterFactory extends CallAdapter.Factory {
private final List<CallAdapter.Factory> callAdapterFactories;
private final Map<Integer, String> registration;
CustomAnnotationCallAdapterFactory(List<CallAdapter.Factory> callAdapterFactories,
Map<Integer, String> reg) {
this.callAdapterFactories = callAdapterFactories;
this.registration = reg;
}
@Target(METHOD)
@Retention(RUNTIME)
public @interface CustomAnnotation {
String[] value();
}
class CustomAnnotationInterceptor implements Interceptor {
private final Map<Integer, String> registration;
public CustomAnnotationInterceptor(Map<Integer, String> reg) {
this.registration = reg;
}
private Integer identify(Request request) {
// make sure this is the same method you use in the CallAdapter
return (request.url() + request.method()).hashCode();
package retrofit2;
import java.util.concurrent.Executor;
import retrofit2.CallAdapter.Factory;
public final class Retrofit2Platform {
private Retrofit2Platform() {
}
// use SparseArray on android! I just did this to make clear what happens here
Map<Integer, String> annotationRegistration = new HashMap<>();
List<CallAdapter.Factory> factories = new ArrayList<>();
// add all your CallAdapter.Factories here
factories.add(...)
// add default CallAdapter as the last item. This is mandatory!
factories.add(Retrofit2Platform.defaultCallAdapterFactory(null));
CustomAnnotationCallAdapterFactory callAdapterFactory =
new CustomAnnotationCallAdapterFactory(factories, annotationRegistration);
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SomeLibrary.init(this);
SomeOtherLibrary.init(this);
}
}
public final class YourLibraryInitProvider extends ContentProvider {
...
@Override
public void attachInfo(Context context, ProviderInfo providerInfo) {
if (providerInfo == null) {
throw new NullPointerException("YourLibraryInitProvider ProviderInfo cannot be null.");
}
// So if the authorities equal the library internal ones, the developer forgot to set his applicationId
<manifest package="some.library.packagename"
xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<provider
android:authorities="${applicationId}.yourlibraryinitprovider"
android:exported="false"
android:enabled="true"
android:name=".YourLibraryInitProvider" />
</application>
public final class YourLibraryInitProvider extends ContentProvider {
public YourLibraryInitProvider() {
}
@Override
public boolean onCreate() {
// get the context (Application context)
Context context = getContext();
// initialize whatever you need