-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: interstitials and rewarded ads (#18)
* fix: wrong event naming * fix: RN prefix * fix: add module suffix * fix: make android work * fix: module name * fix: missing rctconvert * Update android/src/main/java/io/invertase/googleads/common/RCTConvert.java Co-authored-by: Mike Hardy <[email protected]>
- Loading branch information
Showing
13 changed files
with
355 additions
and
23 deletions.
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
android/src/main/java/io/invertase/googleads/ReactNativeAppModule.java
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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package io.invertase.googleads; | ||
|
||
/* | ||
* Copyright (c) 2016-present Invertase Limited & Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this library except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.WritableMap; | ||
import io.invertase.googleads.common.ReactNativeApp; | ||
import io.invertase.googleads.common.RCTConvert; | ||
import io.invertase.googleads.common.ReactNativeEvent; | ||
import io.invertase.googleads.common.ReactNativeEventEmitter; | ||
import io.invertase.googleads.common.ReactNativeJSON; | ||
import io.invertase.googleads.common.ReactNativeMeta; | ||
import io.invertase.googleads.common.ReactNativePreferences; | ||
import io.invertase.googleads.common.ReactNativeModule; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ReactNativeAppModule extends ReactNativeModule { | ||
private static final String TAG = "RNAppModule"; | ||
|
||
ReactNativeAppModule(ReactApplicationContext reactContext) { | ||
super(reactContext, TAG); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
super.initialize(); | ||
ReactNativeEventEmitter.getSharedInstance().attachReactContext(getContext()); | ||
} | ||
|
||
@ReactMethod | ||
public void initializeApp(ReadableMap options, ReadableMap appConfig, Promise promise) { | ||
// ReactNativeApp reactNativeApp = | ||
// RCTConvertFirebase.readableMapToFirebaseApp(options, appConfig, getContext()); | ||
|
||
// WritableMap reactNativeAppMap = RCTConvertFirebase.reactNativeAppToWritableMap(reactNativeApp); | ||
// promise.resolve(reactNativeAppMap); | ||
promise.resolve(options); | ||
} | ||
|
||
@ReactMethod | ||
public void setAutomaticDataCollectionEnabled(String appName, Boolean enabled) { | ||
// ReactNativeApp reactNativeApp = ReactNativeApp.getInstance(appName); | ||
// reactNativeApp.setDataCollectionDefaultEnabled(enabled); | ||
} | ||
|
||
@ReactMethod | ||
public void deleteApp(String appName, Promise promise) { | ||
// ReactNativeApp reactNativeApp = ReactNativeApp.getInstance(appName); | ||
|
||
// if (reactNativeApp != null) { | ||
// reactNativeApp.delete(); | ||
// } | ||
|
||
promise.resolve(null); | ||
} | ||
|
||
@ReactMethod | ||
public void eventsNotifyReady(Boolean ready) { | ||
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance(); | ||
emitter.notifyJsReady(ready); | ||
} | ||
|
||
@ReactMethod | ||
public void eventsGetListeners(Promise promise) { | ||
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance(); | ||
promise.resolve(emitter.getListenersMap()); | ||
} | ||
|
||
@ReactMethod | ||
public void eventsPing(String eventName, ReadableMap eventBody, Promise promise) { | ||
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance(); | ||
emitter.sendEvent( | ||
new ReactNativeEvent( | ||
eventName, RCTConvert.readableMapToWritableMap(eventBody))); | ||
promise.resolve(RCTConvert.readableMapToWritableMap(eventBody)); | ||
} | ||
|
||
@ReactMethod | ||
public void eventsAddListener(String eventName) { | ||
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance(); | ||
emitter.addListener(eventName); | ||
} | ||
|
||
@ReactMethod | ||
public void eventsRemoveListener(String eventName, Boolean all) { | ||
ReactNativeEventEmitter emitter = ReactNativeEventEmitter.getSharedInstance(); | ||
emitter.removeListener(eventName, all); | ||
} | ||
|
||
@ReactMethod | ||
public void addListener(String eventName) { | ||
// Keep: Required for RN built in Event Emitter Calls. | ||
} | ||
|
||
@ReactMethod | ||
public void removeListeners(Integer count) { | ||
// Keep: Required for RN built in Event Emitter Calls. | ||
} | ||
|
||
/** ------------------ META ------------------ */ | ||
@ReactMethod | ||
public void metaGetAll(Promise promise) { | ||
promise.resolve(ReactNativeMeta.getSharedInstance().getAll()); | ||
} | ||
|
||
/** ------------------ JSON ------------------ */ | ||
@ReactMethod | ||
public void jsonGetAll(Promise promise) { | ||
promise.resolve(ReactNativeJSON.getSharedInstance().getAll()); | ||
} | ||
|
||
/** ------------------ PREFERENCES ------------------ */ | ||
@ReactMethod | ||
public void preferencesSetBool(String key, boolean value, Promise promise) { | ||
ReactNativePreferences.getSharedInstance().setBooleanValue(key, value); | ||
promise.resolve(null); | ||
} | ||
|
||
@ReactMethod | ||
public void preferencesSetString(String key, String value, Promise promise) { | ||
ReactNativePreferences.getSharedInstance().setStringValue(key, value); | ||
promise.resolve(null); | ||
} | ||
|
||
@ReactMethod | ||
public void preferencesGetAll(Promise promise) { | ||
promise.resolve(ReactNativePreferences.getSharedInstance().getAll()); | ||
} | ||
|
||
@ReactMethod | ||
public void preferencesClearAll(Promise promise) { | ||
ReactNativePreferences.getSharedInstance().clearAll(); | ||
promise.resolve(null); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getConstants() { | ||
Map<String, Object> constants = new HashMap<>(); | ||
// List<Map<String, Object>> appsList = new ArrayList<>(); | ||
// List<ReactNativeApp> reactNativeApps = ReactNativeApp.getApps(getReactApplicationContext()); | ||
|
||
// for (ReactNativeApp app : reactNativeApps) { | ||
// appsList.add(RCTConvertFirebase.reactNativeAppToMap(app)); | ||
// } | ||
|
||
// constants.put("NATIVE_FIREBASE_APPS", appsList); | ||
|
||
// constants.put("FIREBASE_RAW_JSON", ReactNativeJSON.getSharedInstance().getRawJSON()); | ||
|
||
return constants; | ||
} | ||
} |
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
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
118 changes: 118 additions & 0 deletions
118
android/src/main/java/io/invertase/googleads/common/RCTConvert.java
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package io.invertase.googleads.common; | ||
|
||
/* | ||
* Copyright (c) 2016-present Invertase Limited & Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this library except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.WritableMap; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Utilities to convert to and from React Native bridge formats. | ||
*/ | ||
public class RCTConvert { | ||
private static String TAG = "RCTConvert"; | ||
|
||
/** | ||
* Takes a value and calls the appropriate setter for its type on the target map + key | ||
* | ||
* @param key String key to set on target map | ||
* @param value Object value to set on target map | ||
* @param map WritableMap target map to write the value to | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static WritableMap mapPutValue(String key, @Nullable Object value, WritableMap map) { | ||
if (value == null) { | ||
map.putNull(key); | ||
return map; | ||
} | ||
|
||
String type = value.getClass().getName(); | ||
|
||
switch (type) { | ||
case "java.lang.Boolean": | ||
map.putBoolean(key, (Boolean) value); | ||
break; | ||
case "java.lang.Long": | ||
Long longVal = (Long) value; | ||
map.putDouble(key, (double) longVal); | ||
break; | ||
case "java.lang.Float": | ||
float floatVal = (float) value; | ||
map.putDouble(key, (double) floatVal); | ||
break; | ||
case "java.lang.Double": | ||
map.putDouble(key, (Double) value); | ||
break; | ||
case "java.lang.Integer": | ||
map.putInt(key, (int) value); | ||
break; | ||
case "java.lang.String": | ||
map.putString(key, (String) value); | ||
break; | ||
case "org.json.JSONObject$1": | ||
map.putString(key, value.toString()); | ||
break; | ||
default: | ||
if (List.class.isAssignableFrom(value.getClass())) { | ||
map.putArray(key, Arguments.makeNativeArray((List<Object>) value)); | ||
} else if (Map.class.isAssignableFrom(value.getClass())) { | ||
WritableMap childMap = Arguments.createMap(); | ||
Map<String, Object> valueMap = (Map<String, Object>) value; | ||
|
||
for (Map.Entry<String, Object> entry : valueMap.entrySet()) { | ||
mapPutValue(entry.getKey(), entry.getValue(), childMap); | ||
} | ||
|
||
map.putMap(key, childMap); | ||
} else { | ||
Log.d(TAG, "utils:mapPutValue:unknownType:" + type); | ||
map.putNull(key); | ||
} | ||
} | ||
|
||
return map; | ||
} | ||
|
||
// TODO Remove me - also in SharedUtils | ||
public static WritableMap readableMapToWritableMap(ReadableMap map) { | ||
WritableMap writableMap = Arguments.createMap(); | ||
// https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.java#L58 | ||
writableMap.merge(map); | ||
return writableMap; | ||
} | ||
|
||
public static Map<String, Object> toHashMap(ReadableMap readableMap) { | ||
// https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java#L263 | ||
return readableMap.toHashMap(); | ||
} | ||
|
||
public static List<Object> toArrayList(ReadableArray readableArray) { | ||
// https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java#L140 | ||
return readableArray.toArrayList(); | ||
} | ||
} |
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
Oops, something went wrong.