Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
66 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,87 @@ | ||
package com.plugin.component; | ||
|
||
import android.util.ArrayMap; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
public class SdkManager { | ||
|
||
private static ArrayMap<Class, Object> sServiceArrayMap; | ||
private static ArrayMap<Class, WeakReference<Object>> sWeekServiceArrayMap; | ||
private static final String TAG = "component-core"; | ||
|
||
public static void register(@NonNull Class serviceKey, @NonNull Object serviceObjectOrClass) { | ||
if (!serviceKey.isInterface()) { | ||
public static void register(@NonNull Object componentObjectOrClass, @NonNull Class sdkKey, @NonNull Object sdkImplObjectOrClass) { | ||
ComponentInfo componentInfo = ComponentManager.hasRegister(componentObjectOrClass); | ||
if (componentInfo == null) { | ||
componentInfo = ComponentManager.registerComponent(componentObjectOrClass); | ||
} | ||
if (!sdkKey.isInterface()) { | ||
throw new IllegalArgumentException("register service key must be interface class."); | ||
} | ||
if (serviceObjectOrClass.getClass().isInterface()) { | ||
if (sdkImplObjectOrClass.getClass().isInterface()) { | ||
throw new IllegalArgumentException("register service object must not be interface."); | ||
} | ||
|
||
Class realClass = serviceObjectOrClass instanceof Class ? (Class) serviceObjectOrClass : serviceObjectOrClass.getClass(); | ||
|
||
if (!serviceKey.isAssignableFrom(realClass)) { | ||
throw new IllegalArgumentException(String.format("register service object must implement interface %s.", serviceKey)); | ||
Class realComponentClass = sdkImplObjectOrClass instanceof Class ? (Class) sdkImplObjectOrClass : sdkImplObjectOrClass.getClass(); | ||
Class realImplClass = sdkImplObjectOrClass instanceof Class ? (Class) sdkImplObjectOrClass : sdkImplObjectOrClass.getClass(); | ||
if (!sdkKey.isAssignableFrom(realImplClass)) { | ||
throw new IllegalArgumentException(String.format("register service object must implement interface %s.", sdkKey)); | ||
} | ||
|
||
if (sServiceArrayMap == null) { | ||
sServiceArrayMap = new ArrayMap<>(); | ||
} | ||
sServiceArrayMap.put(serviceKey, serviceObjectOrClass); | ||
Log.d(TAG, String.format("register sdk[ %s ] in component[ %s ] success, with %s", sdkKey, realComponentClass, realImplClass)); | ||
componentInfo.registerSdk(sdkKey, sdkImplObjectOrClass); | ||
} | ||
|
||
public static void unregister(Class serviceKey) { | ||
if (serviceKey == null || sServiceArrayMap == null) return; | ||
sServiceArrayMap.remove(serviceKey); | ||
public static void unregister(Class sdkKey) { | ||
ComponentInfo componentInfo = ComponentManager.findComponentInfoBySdk(sdkKey); | ||
if (componentInfo != null) { | ||
componentInfo.unregisterSdk(sdkKey); | ||
Log.d(TAG, String.format("unregister sdk[ %s ] success, component[ %s ] remove it.", sdkKey, componentInfo.componentClass)); | ||
return; | ||
} | ||
Log.d(TAG, String.format("unregister sdk[ %s ] fail, no component has it.", sdkKey)); | ||
} | ||
|
||
@Nullable | ||
public static <T> T getSdk(Class<T> sdkKey) { | ||
ComponentInfo componentInfo = ComponentManager.findComponentInfoBySdk(sdkKey); | ||
if (componentInfo != null) { | ||
Object result; | ||
if (componentInfo.componentImpl == null) { | ||
try { | ||
componentInfo.componentImpl = (IComponent) componentInfo.componentClass.newInstance(); | ||
componentInfo.componentImpl.attachComponent(ComponentManager.getApplication()); | ||
Log.d(TAG, String.format("before getting sdk[ %s ] , component[ %s ] doing attachComponent.", sdkKey, componentInfo.componentClass)); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} catch (InstantiationException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static <T> T getService(Class<T> serviceKey) { | ||
if (sServiceArrayMap == null) return null; | ||
|
||
Object object = sServiceArrayMap.get(serviceKey); | ||
if (object == null) return null; | ||
|
||
if (object instanceof Class) { | ||
Object result = null; | ||
if (sWeekServiceArrayMap == null) { | ||
sWeekServiceArrayMap = new ArrayMap<>(); | ||
if (componentInfo.componentImpl == null) { | ||
Log.d(TAG, String.format("before getting sdk[ %s ] , component[ %s ] init fail.", sdkKey, componentInfo.componentClass)); | ||
return null; | ||
} | ||
WeakReference<Object> cachedObject = sWeekServiceArrayMap.get(serviceKey); | ||
if (cachedObject != null && cachedObject.get() != null) { | ||
result = cachedObject.get(); | ||
} else { | ||
|
||
result = componentInfo.getSdk(sdkKey); | ||
if (result instanceof Class) { | ||
try { | ||
result = ((Class) object).newInstance(); | ||
sWeekServiceArrayMap.put(serviceKey, new WeakReference<>(result)); | ||
result = ((Class) result).newInstance(); | ||
componentInfo.registerSdk(sdkKey, result); | ||
Log.d(TAG, String.format("before getting sdk[ %s ] , newInstance for sdk success.", sdkKey)); | ||
Log.d(TAG, String.format("get sdk[ %s ] success. ", result.getClass())); | ||
return (T) result; | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} catch (InstantiationException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return (T) result; | ||
} else { | ||
return (T) object; | ||
if (result == null || result instanceof Class) { | ||
Log.d(TAG, String.format("get sdk[ %s ] fail. ", sdkKey)); | ||
} else { | ||
Log.d(TAG, String.format("get sdk[ %s ] success. ", result.getClass())); | ||
} | ||
} | ||
Log.d(TAG, String.format("get sdk[ %s ] fail, no component has it.", sdkKey)); | ||
return null; | ||
} | ||
} |