Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffa732e

Browse files
authored
refactor(predictions): update deprecated remote config functions (firebase#172)
1 parent 7c72225 commit ffa732e

File tree

8 files changed

+240
-195
lines changed

8 files changed

+240
-195
lines changed

predictions/app/src/main/java/com/google/firebase/example/predictions/ConditionalAdsActivity.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
public class ConditionalAdsActivity extends AppCompatActivity {
1818

19-
private static final long CACHE_EXPIRATION = 60 * 1000;
20-
2119
private FirebaseRemoteConfig mFirebaseRemoteConfig;
2220

2321
@Override
@@ -30,29 +28,39 @@ public void initRemoteConfig() {
3028
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
3129

3230
Map<String, Object> remoteConfigDefaults = new HashMap<>();
33-
remoteConfigDefaults.put("ads_enabled", "true");
34-
mFirebaseRemoteConfig.setDefaults(remoteConfigDefaults);
31+
remoteConfigDefaults.put("ads_enabled", true);
32+
mFirebaseRemoteConfig.setDefaultsAsync(remoteConfigDefaults)
33+
.addOnCompleteListener(new OnCompleteListener<Void>() {
34+
@Override
35+
public void onComplete(@NonNull Task<Void> task) {
36+
if (task.isSuccessful()) {
37+
// Default value successfully set
38+
} else {
39+
// Failed to set default value
40+
}
41+
}
42+
});
3543
// [END pred_conditional_ads_init]
3644
}
3745

3846
public void fetchRemoteConfig() {
3947
// [START pred_conditional_ads_fetch]
40-
mFirebaseRemoteConfig.fetch(CACHE_EXPIRATION)
41-
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
42-
@Override
43-
public void onComplete(@NonNull Task<Void> task) {
44-
if (task.isSuccessful()) {
45-
mFirebaseRemoteConfig.activateFetched();
46-
}
47-
48-
// Act on the retrieved parameters
48+
mFirebaseRemoteConfig.fetchAndActivate()
49+
.addOnCompleteListener(new OnCompleteListener<Boolean>() {
50+
@Override
51+
public void onComplete(@NonNull Task<Boolean> task) {
52+
if (task.isSuccessful()) {
53+
// Act on the retrieved parameters
4954

50-
// Show ads based on the ad policy retrieved with Remote Config
51-
executeAdsPolicy();
55+
// Show ads based on the ad policy retrieved with Remote Config
56+
executeAdsPolicy();
5257

53-
// ...
54-
}
55-
});
58+
// ...
59+
} else {
60+
// Handle errors
61+
}
62+
}
63+
});
5664
// [END pred_conditional_ads_fetch]
5765
}
5866

predictions/app/src/main/java/com/google/firebase/example/predictions/MainActivity.java

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.google.android.gms.ads.AdRequest;
99
import com.google.android.gms.ads.AdView;
10+
import com.google.android.gms.tasks.Continuation;
1011
import com.google.android.gms.tasks.OnCompleteListener;
1112
import com.google.android.gms.tasks.Task;
1213
import com.google.firebase.analytics.FirebaseAnalytics;
@@ -23,31 +24,34 @@ protected void onCreate(Bundle savedInstanceState) {
2324
}
2425

2526
public void configShowAds() {
26-
long cacheExpiration = 60L;
27-
2827
// [START pred_config_show_ads]
2928
final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
3029

31-
Map remoteConfigDefaults = new HashMap<String, Object>();
30+
Map<String, Object> remoteConfigDefaults = new HashMap<>();
3231
remoteConfigDefaults.put("ads_policy", "ads_never");
33-
config.setDefaults(remoteConfigDefaults);
34-
35-
// ...
36-
37-
config.fetch(cacheExpiration)
38-
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
32+
config.setDefaultsAsync(remoteConfigDefaults)
33+
.continueWithTask(new Continuation<Void, Task<Boolean>>() {
3934
@Override
40-
public void onComplete(@NonNull Task<Void> task) {
41-
if (task.isSuccessful()) {
42-
config.activateFetched();
35+
public Task<Boolean> then(@NonNull Task<Void> task) throws Exception {
36+
if (!task.isSuccessful()) {
37+
throw task.getException();
4338
}
39+
return config.fetchAndActivate();
40+
}
41+
})
42+
.addOnCompleteListener(new OnCompleteListener<Boolean>() {
43+
@Override
44+
public void onComplete(@NonNull Task<Boolean> task) {
45+
if (task.isSuccessful()) {
46+
// Act on the retrieved parameters
4447

45-
// Act on the retrieved parameters
46-
47-
// Show ads based on the ad policy retrieved with Remote Config
48-
executeAdsPolicy();
48+
// Show ads based on the ad policy retrieved with Remote Config
49+
executeAdsPolicy();
4950

50-
// ...
51+
// ...
52+
} else {
53+
// Handle errors
54+
}
5155
}
5256
});
5357
// [END pred_config_show_ads]
@@ -74,28 +78,31 @@ public void executeAdsPolicy() {
7478
}
7579

7680
public void configPromoStrategy() {
77-
long cacheExpiration = 60L;
78-
7981
// [START config_promo_strategy]
8082
final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
8183

82-
Map remoteConfigDefaults = new HashMap<String, Object>();
84+
Map<String, Object> remoteConfigDefaults = new HashMap<>();
8385
remoteConfigDefaults.put("promoted_bundle", "basic");
84-
config.setDefaults(remoteConfigDefaults);
85-
86-
// ...
87-
88-
config.fetch(cacheExpiration)
89-
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
86+
config.setDefaultsAsync(remoteConfigDefaults)
87+
.continueWithTask(new Continuation<Void, Task<Boolean>>() {
9088
@Override
91-
public void onComplete(@NonNull Task<Void> task) {
89+
public Task<Boolean> then(@NonNull Task<Void> task) throws Exception {
90+
if (!task.isSuccessful()) {
91+
throw task.getException();
92+
}
93+
return config.fetchAndActivate();
94+
}
95+
})
96+
.addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
97+
@Override
98+
public void onComplete(@NonNull Task<Boolean> task) {
9299
if (task.isSuccessful()) {
93-
config.activateFetched();
100+
// Act on the retrieved parameters
101+
String promotedBundle = getPromotedBundle();
102+
// ...
103+
} else {
104+
// Handle errors
94105
}
95-
96-
// Act on the retrieved parameters
97-
String promotedBundle = getPromotedBundle();
98-
// ...
99106
}
100107
});
101108
// [END config_promo_strategy]
@@ -118,28 +125,31 @@ public String getPromotedBundle() {
118125
// [END pred_get_promoted_bundle]
119126

120127
public void configPreventChurn() {
121-
long cacheExpiration = 60L;
122-
123128
// [START pred_config_prevent_churn]
124129
final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
125130

126-
Map remoteConfigDefaults = new HashMap<String, Object>();
131+
Map<String, Object> remoteConfigDefaults = new HashMap<>();
127132
remoteConfigDefaults.put("gift_policy", "gift_never");
128-
config.setDefaults(remoteConfigDefaults);
129-
130-
// ...
131-
132-
config.fetch(cacheExpiration)
133-
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
133+
config.setDefaultsAsync(remoteConfigDefaults)
134+
.continueWithTask(new Continuation<Void, Task<Boolean>>() {
134135
@Override
135-
public void onComplete(@NonNull Task<Void> task) {
136+
public Task<Boolean> then(@NonNull Task<Void> task) throws Exception {
137+
if (!task.isSuccessful()) {
138+
throw task.getException();
139+
}
140+
return config.fetchAndActivate();
141+
}
142+
})
143+
.addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
144+
@Override
145+
public void onComplete(@NonNull Task<Boolean> task) {
136146
if (task.isSuccessful()) {
137-
config.activateFetched();
147+
// Act on the retrieved parameters
148+
executeGiftPolicy();
149+
// ...
150+
} else {
151+
// Handle errors
138152
}
139-
140-
// Act on the retrieved parameters
141-
executeGiftPolicy();
142-
// ...
143153
}
144154
});
145155
// [END pred_config_prevent_churn]

predictions/app/src/main/java/com/google/firebase/example/predictions/OptimizePromotionsActivity.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
public class OptimizePromotionsActivity extends AppCompatActivity {
1515

16-
private static final long CACHE_EXPIRATION = 60 * 1000;
17-
1816
private FirebaseRemoteConfig mConfig;
1917
private String mPromotedBundle;
2018

@@ -29,28 +27,36 @@ private void initConfig() {
2927

3028
Map<String, Object> remoteConfigDefaults = new HashMap<>();
3129
remoteConfigDefaults.put("promoted_bundle", "basic");
32-
mConfig.setDefaults(remoteConfigDefaults);
30+
mConfig.setDefaultsAsync(remoteConfigDefaults)
31+
.addOnCompleteListener(new OnCompleteListener<Void>() {
32+
@Override
33+
public void onComplete(@NonNull Task<Void> task) {
34+
if (task.isSuccessful()) {
35+
// Default value successfully set
36+
} else {
37+
// Failed to set default value
38+
}
39+
}
40+
});
3341
// [END pred_optimize_promotions_init]
3442
}
3543

3644
private void fetchConfig() {
3745
// [START pred_optimize_promotions_fetch]
38-
mConfig.fetch(CACHE_EXPIRATION)
39-
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
46+
mConfig.fetchAndActivate()
47+
.addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
4048
@Override
41-
public void onComplete(@NonNull Task<Void> task) {
49+
public void onComplete(@NonNull Task<Boolean> task) {
4250
if (task.isSuccessful()) {
43-
mConfig.activateFetched();
44-
}
51+
// Act on the retrieved parameters
4552

46-
// Act on the retrieved parameters
47-
48-
// Set the bundle to promote based on parameters retrieved with
49-
// Remote Config. This depends entirely on your app, but for
50-
// example, you might retrieve and use image assets based on the
51-
// specified bundle name.
52-
mPromotedBundle = mConfig.getString("promoted_bundle");
53-
// ...
53+
// Set the bundle to promote based on parameters retrieved with
54+
// Remote Config. This depends entirely on your app, but for
55+
// example, you might retrieve and use image assets based on the
56+
// specified bundle name.
57+
mPromotedBundle = mConfig.getString("promoted_bundle");
58+
// ...
59+
}
5460
}
5561
});
5662
// [END pred_optimize_promotions_fetch]

predictions/app/src/main/java/com/google/firebase/example/predictions/PreventChurnActivity.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import androidx.annotation.NonNull;
44
import androidx.appcompat.app.AppCompatActivity;
55

6+
import com.google.android.gms.tasks.Continuation;
67
import com.google.android.gms.tasks.OnCompleteListener;
78
import com.google.android.gms.tasks.Task;
89
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
@@ -12,31 +13,32 @@
1213

1314
public class PreventChurnActivity extends AppCompatActivity {
1415

15-
private static final long CACHE_EXPIRATION = 60 * 1000;
16-
1716
private void preventChurn() {
1817
// [START pred_prevent_churn]
1918
final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance();
2019

2120
Map<String, Object> remoteConfigDefaults = new HashMap<>();
2221
remoteConfigDefaults.put("grant_retention_gift", false);
23-
config.setDefaults(remoteConfigDefaults);
24-
25-
// ...
26-
27-
config.fetch(CACHE_EXPIRATION)
28-
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
22+
config.setDefaultsAsync(remoteConfigDefaults)
23+
.continueWithTask(new Continuation<Void, Task<Boolean>>() {
2924
@Override
30-
public void onComplete(@NonNull Task<Void> task) {
31-
if (task.isSuccessful()) {
32-
config.activateFetched();
25+
public Task<Boolean> then(@NonNull Task<Void> task) throws Exception {
26+
if (!task.isSuccessful()) {
27+
throw task.getException();
3328
}
34-
35-
// Act on the retrieved parameters. For example, grant the
36-
// retention gift to players who haven't yet received one.
37-
boolean shouldGrantGift = config.getBoolean("grant_retention_gift");
38-
if (shouldGrantGift && !playerAlreadyReceivedGift()) {
39-
grantGiftToPlayer();
29+
return config.fetchAndActivate();
30+
}
31+
})
32+
.addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
33+
@Override
34+
public void onComplete(@NonNull Task<Boolean> task) {
35+
if (task.isSuccessful()) {
36+
// Act on the retrieved parameters. For example, grant the
37+
// retention gift to players who haven't yet received one.
38+
boolean shouldGrantGift = config.getBoolean("grant_retention_gift");
39+
if (shouldGrantGift && !playerAlreadyReceivedGift()) {
40+
grantGiftToPlayer();
41+
}
4042
}
4143
}
4244
});

predictions/app/src/main/java/com/google/firebase/example/predictions/kotlin/ConditionalAdsActivity.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,34 @@ class ConditionalAdsActivity : AppCompatActivity() {
1616
// [START pred_conditional_ads_init]
1717
firebaseRemoteConfig = FirebaseRemoteConfig.getInstance()
1818

19-
val remoteConfigDefaults = HashMap<String, Any>()
20-
remoteConfigDefaults["ads_enabled"] = "true"
21-
firebaseRemoteConfig.setDefaults(remoteConfigDefaults)
19+
val remoteConfigDefaults = hashMapOf<String, Any>(
20+
"ads_enabled" to true
21+
)
22+
firebaseRemoteConfig.setDefaultsAsync(remoteConfigDefaults)
23+
.addOnCompleteListener { task ->
24+
if (task.isSuccessful) {
25+
// Default value successfully set
26+
} else {
27+
// Failed to set default value
28+
}
29+
}
2230
// [END pred_conditional_ads_init]
2331
}
2432

2533
fun fetchRemoteConfig() {
2634
// [START pred_conditional_ads_fetch]
27-
firebaseRemoteConfig.fetch(CACHE_EXPIRATION)
35+
firebaseRemoteConfig.fetchAndActivate()
2836
.addOnCompleteListener(this) { task ->
2937
if (task.isSuccessful) {
30-
firebaseRemoteConfig.activateFetched()
31-
}
38+
// Act on the retrieved parameters
3239

33-
// Act on the retrieved parameters
40+
// Show ads based on the ad policy retrieved with Remote Config
41+
executeAdsPolicy()
3442

35-
// Show ads based on the ad policy retrieved with Remote Config
36-
executeAdsPolicy()
37-
38-
// ...
43+
// ...
44+
} else {
45+
// Handle errors
46+
}
3947
}
4048
// [END pred_conditional_ads_fetch]
4149
}
@@ -54,8 +62,4 @@ class ConditionalAdsActivity : AppCompatActivity() {
5462
}
5563
}
5664
// [END pred_conditional_ads_policy]
57-
58-
companion object {
59-
private val CACHE_EXPIRATION = 60 * 1000L
60-
}
6165
}

0 commit comments

Comments
 (0)