Skip to content

Commit

Permalink
缓存资源已全部加载的Resources,以有减少不必要的资源检查,从而提高运行速度。
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Apr 15, 2021
1 parent b2e1b29 commit 364b64e
Showing 1 changed file with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,27 @@
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;

import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;

import com.iqiyi.android.qigsaw.core.common.SplitBaseInfoProvider;
import com.iqiyi.android.qigsaw.core.common.SplitLog;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

Expand All @@ -71,7 +74,7 @@ public class SplitCompatResourcesLoader {
* After Android 7.0, WebView.apk resources is added dynamically.
*/
public static void loadResources(Context context, Resources resources) throws Throwable {
checkOrUpdateResources(context, resources);
if (!fullyLoadedRes.containsKey(resources)) checkOrUpdateResources(context, resources);
}

static void loadResources(Context context, Resources preResources, String splitApkPath) throws Throwable {
Expand Down Expand Up @@ -100,6 +103,7 @@ private static void checkOrUpdateResources(Context context, Resources resources)
}
try {
installSplitResDirs(context, resources, unloadedSplitPaths);
if (isFullyLoadedRes()) fullyLoadedRes.put(resources, null);
} catch (Throwable e) {
throw new SplitCompatResourcesException("Failed to install resources " + unloadedSplitPaths.toString() + " for " + context.getClass().getName(), e);
}
Expand Down Expand Up @@ -557,4 +561,33 @@ static Method getGetApkAssetsMethod() throws NoSuchMethodException {
return getApkAssetsMethod;
}
}

//region fullyLoadedRes
private static final Map<Resources, Void> fullyLoadedRes = new WeakHashMap<>();
private static List<String> dynamicFeatures;

private static boolean isFullyLoadedRes() {
Collection<String> dynamicFeatures = getDynamicFeatures();
Collection<String> loadedSplitNames = getLoadedSplitNames();
if (dynamicFeatures != null && loadedSplitNames != null) {
return loadedSplitNames.containsAll(dynamicFeatures);
} else return false;
}

@Nullable
private static Collection<String> getDynamicFeatures() {
if (dynamicFeatures != null && dynamicFeatures.size() > 0) return dynamicFeatures;
String[] features = SplitBaseInfoProvider.getDynamicFeatures();
dynamicFeatures = features != null ? Arrays.asList(features) : null;
return dynamicFeatures;
}

private static Collection<String> getLoadedSplitNames() {
SplitLoadManager loadManager = SplitLoadManagerService.getInstance();
if (loadManager != null) {
return loadManager.getLoadedSplitNames();
}
return null;
}
//endregion
}

0 comments on commit 364b64e

Please sign in to comment.