Skip to content

Commit 8b199a7

Browse files
Adam ComellaFacebook Github Bot
Adam Comella
authored and
Facebook Github Bot
committed
Android: Enable apps to provide a custom configuration to Fresco
Summary: The `FrescoModule` supports providing a custom image pipeline configuration. This module is created by `MainReactPackage` but `MainReactPackage` doesn't expose any way to customize the Fresco configuration. This change adds a parameter to `MainReactPackage`'s constructor so that the `FrescoModule`'s configuration can be customized by the app. A couple of design choices were made in this change: - `MainReactPackage`'s new constructor parameter is a `MainPackageConfig`. Introducing `MainPackageConfig` enables `MainReactPackage` to nicely support new optional configuration options in the future. Imagine the alternative of each optional configuration being a separate parameter to the `MainReactPackage` constructor. - `FrescoModule` exposes its default configuration as a builder object through the `getDefaultConfigBuilder` method. This enables app's to start with `FrescoModule`'s default configuration and then modify it. **Test plan (required)** Verified that passing a custom config based on React Nati Closes #10906 Differential Revision: D4237054 Pulled By: mkonicek fbshipit-source-id: 8a62a6f0e77ca5f6d35238950094686756262196
1 parent fde4fb1 commit 8b199a7

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,23 @@ public static boolean hasBeenInitialized() {
111111
}
112112

113113
private static ImagePipelineConfig getDefaultConfig(Context context) {
114+
return getDefaultConfigBuilder(context).build();
115+
}
116+
117+
/**
118+
* Get the default Fresco configuration builder.
119+
* Allows adding of configuration options in addition to the default values.
120+
*
121+
* @return {@link ImagePipelineConfig.Builder} that has been initialized with default values
122+
*/
123+
public static ImagePipelineConfig.Builder getDefaultConfigBuilder(Context context) {
114124
HashSet<RequestListener> requestListeners = new HashSet<>();
115125
requestListeners.add(new SystraceRequestListener());
116126

117127
return OkHttpImagePipelineConfigFactory
118128
.newBuilder(context.getApplicationContext(), OkHttpClientProvider.getOkHttpClient())
119129
.setDownsampleEnabled(false)
120-
.setRequestListeners(requestListeners)
121-
.build();
130+
.setRequestListeners(requestListeners);
122131
}
123132

124133
private static class FrescoHandler implements SoLoaderShim.Handler {

ReactAndroid/src/main/java/com/facebook/react/shell/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ android_library(
44
name = 'shell',
55
srcs = glob(['**/*.java']),
66
deps = [
7+
react_native_dep('libraries/fresco/fresco-react-native:imagepipeline'),
78
react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'),
89
react_native_dep('third-party/android/support/v4:lib-support-v4'),
910
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
package com.facebook.react.shell;
11+
12+
import com.facebook.imagepipeline.core.ImagePipelineConfig;
13+
14+
/**
15+
* Configuration for {@link MainReactPackage}
16+
*/
17+
public class MainPackageConfig {
18+
19+
private ImagePipelineConfig mFrescoConfig;
20+
21+
private MainPackageConfig(Builder builder) {
22+
mFrescoConfig = builder.mFrescoConfig;
23+
}
24+
25+
public ImagePipelineConfig getFrescoConfig() {
26+
return mFrescoConfig;
27+
}
28+
29+
public static class Builder {
30+
31+
private ImagePipelineConfig mFrescoConfig;
32+
33+
public Builder setFrescoConfig(ImagePipelineConfig frescoConfig) {
34+
mFrescoConfig = frescoConfig;
35+
return this;
36+
}
37+
38+
public MainPackageConfig build() {
39+
return new MainPackageConfig(this);
40+
}
41+
}
42+
}

ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@
7474
*/
7575
public class MainReactPackage extends LazyReactPackage {
7676

77+
private MainPackageConfig mConfig;
78+
79+
public MainReactPackage() {
80+
}
81+
82+
/**
83+
* Create a new package with configuration
84+
*/
85+
public MainReactPackage(MainPackageConfig config) {
86+
mConfig = config;
87+
}
88+
7789
@Override
7890
public List<ModuleSpec> getNativeModules(final ReactApplicationContext context) {
7991
return Arrays.asList(
@@ -116,7 +128,7 @@ public NativeModule get() {
116128
new ModuleSpec(FrescoModule.class, new Provider<NativeModule>() {
117129
@Override
118130
public NativeModule get() {
119-
return new FrescoModule(context);
131+
return new FrescoModule(context, mConfig != null ? mConfig.getFrescoConfig() : null);
120132
}
121133
}),
122134
new ModuleSpec(I18nManagerModule.class, new Provider<NativeModule>() {

0 commit comments

Comments
 (0)