Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inmobi Adapter | Implementation of Mediation Extras #1057

Merged
merged 11 commits into from
Apr 17, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.googlemobileads.mediation.gma_mediation_inmobi">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://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.

package io.flutter.plugins.googlemobileads.mediation.gma_mediation_inmobi

import android.os.Bundle
import android.util.Log
import android.util.Pair
import androidx.core.os.bundleOf
import com.google.ads.mediation.inmobi.InMobiAdapter
import com.google.ads.mediation.inmobi.InMobiNetworkKeys
import com.google.ads.mediation.inmobi.InMobiNetworkValues
import com.google.android.gms.ads.mediation.MediationExtrasReceiver
import io.flutter.plugins.googlemobileads.FlutterMediationExtras

class InMobiFlutterMediationExtras : FlutterMediationExtras() {
private var flutterExtras: Map<String, Any>? = null

override fun setMediationExtras(extras: MutableMap<String, Any>) {
flutterExtras = extras
}

override fun getMediationExtras(): Pair<Class<out MediationExtrasReceiver>, Bundle> {
val extrasMap = flutterExtras
if (extrasMap == null) {
return Pair<Class<out MediationExtrasReceiver>, Bundle>(InMobiAdapter::class.java, bundleOf())
}
val extrasBundle = bundleOf()
val ageGroupValue = extrasMap[AGE_GROUP]
if (ageGroupValue is Int) {
Log.d("InMobiMediationExtras", "ageGroup: $ageGroupValue")
when(ageGroupValue) {
0 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.BELOW_18)
1 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.BETWEEN_18_AND_24)
2 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.BETWEEN_25_AND_29)
3 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.BETWEEN_30_AND_34)
4 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.BETWEEN_35_AND_44)
5 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.BETWEEN_45_AND_54)
6 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.BETWEEN_55_AND_65)
7 -> extrasBundle.putString(InMobiNetworkKeys.AGE_GROUP, InMobiNetworkValues.ABOVE_65)
}
}
val educationValue = extrasMap[EDUCATION]
if (educationValue is Int) {
Log.d("InMobiMediationExtras", "education: $educationValue")
when (educationValue) {
0 -> extrasBundle.putString(InMobiNetworkKeys.EDUCATION, InMobiNetworkValues.EDUCATION_HIGHSCHOOLORLESS)
1 -> extrasBundle.putString(InMobiNetworkKeys.EDUCATION, InMobiNetworkValues.EDUCATION_COLLEGEORGRADUATE)
2 -> extrasBundle.putString(InMobiNetworkKeys.EDUCATION, InMobiNetworkValues.EDUCATION_POSTGRADUATEORABOVE)
}
}
val ageValue = extrasMap[AGE]
if (ageValue is Int) {
Log.d("InMobiMediationExtras", "age: $ageValue")
extrasBundle.putString(InMobiNetworkKeys.AGE, ageValue.toString())
}
val postalCodeValue = extrasMap[POSTAL_CODE]
if (postalCodeValue is String) {
Log.d("InMobiMediationExtras", "postalCode: $postalCodeValue")
extrasBundle.putString(InMobiNetworkKeys.POSTAL_CODE, postalCodeValue)
}
val areaCodeValue = extrasMap[AREA_CODE]
if (areaCodeValue is String) {
Log.d("InMobiMediationExtras", "areaCode: $areaCodeValue")
extrasBundle.putString(InMobiNetworkKeys.AREA_CODE, areaCodeValue)
}
val languageValue = extrasMap[LANGUAGE]
if (languageValue is String) {
Log.d("InMobiMediationExtras", "language: $languageValue")
extrasBundle.putString(InMobiNetworkKeys.LANGUAGE, languageValue)
}
val cityValue = extrasMap[CITY]
if (cityValue is String) {
Log.d("InMobiMediationExtras", "city: $cityValue")
extrasBundle.putString(InMobiNetworkKeys.CITY, cityValue)
}
val stateValue = extrasMap[STATE]
if (stateValue is String) {
Log.d("InMobiMediationExtras", "state: $stateValue")
extrasBundle.putString(InMobiNetworkKeys.STATE, stateValue)
}
val countryValue = extrasMap[COUNTRY]
if (countryValue is String) {
Log.d("InMobiMediationExtras", "country: $countryValue")
extrasBundle.putString(InMobiNetworkKeys.COUNTRY, countryValue)
}
val logLevelValue = extrasMap[LOGLEVEL]
if (logLevelValue is Int) {
Log.d("InMobiMediationExtras", "logLevel: $logLevelValue")
when (logLevelValue) {
0 -> extrasBundle.putString(InMobiNetworkKeys.LOGLEVEL, InMobiNetworkValues.LOGLEVEL_NONE)
1 -> extrasBundle.putString(InMobiNetworkKeys.LOGLEVEL, InMobiNetworkValues.LOGLEVEL_DEBUG)
2 -> extrasBundle.putString(InMobiNetworkKeys.LOGLEVEL, InMobiNetworkValues.LOGLEVEL_ERROR)
}
}
val interestsValue = extrasMap[INTERESTS]
if (interestsValue is String) {
Log.d("InMobiMediationExtras", "interests: $interestsValue")
extrasBundle.putString(InMobiNetworkKeys.INTERESTS, interestsValue)
}
return Pair<Class<out MediationExtrasReceiver>, Bundle>(InMobiAdapter::class.java, extrasBundle)
}

companion object {
private const val AGE_GROUP = "ageGroup"
private const val EDUCATION = "education"
private const val AGE = "age"
private const val POSTAL_CODE = "postalCode"
private const val AREA_CODE = "areaCode"
private const val LANGUAGE = "language"
private const val CITY = "city"
private const val STATE = "state"
private const val COUNTRY = "country"
private const val INTERESTS = "interests"
private const val LOGLEVEL = "logLevel"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://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 Foundation
import InMobiAdapter

@objc protocol FLTMediationExtras {
var extras: NSMutableDictionary { get }
func getMediationExtras() -> GADAdNetworkExtras
}

@objc(InMobiFlutterMediationExtras)
class InMobiFlutterMediationExtras : NSObject, FLTMediationExtras {
var extras: NSMutableDictionary = [:]

func getMediationExtras() -> GADAdNetworkExtras {
let inMobiExtras = GADInMobiExtras()
if let ageGroup = extras["ageGroup"] as? Int {
switch ageGroup {
case 0:
inMobiExtras.ageGroup = IMSDKAgeGroup.below18
case 1:
inMobiExtras.ageGroup = IMSDKAgeGroup.between18And24
case 2:
inMobiExtras.ageGroup = IMSDKAgeGroup.between25And29
case 3:
inMobiExtras.ageGroup = IMSDKAgeGroup.between30And34
case 4:
inMobiExtras.ageGroup = IMSDKAgeGroup.between35And44
case 5:
inMobiExtras.ageGroup = IMSDKAgeGroup.between45And54
case 6:
inMobiExtras.ageGroup = IMSDKAgeGroup.between55And65
case 7:
inMobiExtras.ageGroup = IMSDKAgeGroup.above65
default:
break;
}
}
if let educationType = extras["educationType"] as? Int {
switch educationType {
case 0:
inMobiExtras.educationType = IMSDKEducation.highSchoolOrLess
case 1:
inMobiExtras.educationType = IMSDKEducation.collageOrGraduate
case 2:
inMobiExtras.educationType = IMSDKEducation.postGraduateOrAbove
default:
break;
}
}
if let logLevel = extras["logLevel"] as? Int {
switch logLevel {
case 0:
inMobiExtras.logLevel = IMSDKLogLevel.none
case 1:
inMobiExtras.logLevel = IMSDKLogLevel.debug
case 2:
inMobiExtras.logLevel = IMSDKLogLevel.error
default:
break;
}

}
if let age = extras["age"] as? UInt {
inMobiExtras.age = age
}
if let yearOfBirth = extras["yearOfBirth"] as? Int {
inMobiExtras.yearOfBirth = yearOfBirth
}
if let postalCode = extras["postalCode"] as? String {
inMobiExtras.postalCode = postalCode
}
if let areaCode = extras["areaCode"] as? String {
inMobiExtras.areaCode = areaCode
}
if let language = extras["language"] as? String {
inMobiExtras.language = language
}
if let keywords = extras["keywords"] as? String {
inMobiExtras.keywords = keywords
}
if let interests = extras["interests"] as? String {
inMobiExtras.interests = interests
}
let city = extras["city"] as? String
let state = extras["state"] as? String
let country = extras["country"] as? String
if city != nil && state != nil && country != nil {
inMobiExtras.setLocationWithCity(city, state: state, country: country)
}

return inMobiExtras
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://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 'package:gma_mediation_inmobi/gma_mediation_inmobi.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

/// Class with additional parameters to attach to the [AdRequest] or [AdManagerAdRequest] when using the [GmaMediationInMobi] adapter.
class InMobiMediationExtras implements MediationExtras {
InMobiMediationExtras({
this.ageGroup,
this.education,
this.age,
this.yearOfBirth,
this.postalCode,
this.areaCode,
this.language,
this.city,
this.state,
this.country,
this.keywords,
this.interests,
this.logLevel,
});

InMobiAgeGroups? ageGroup;
InMobiEducation? education;
int? age;
int? yearOfBirth;
String? postalCode;
String? areaCode;
String? language;
String? city;
String? state;
String? country;
String? interests;
String? keywords;
InMobiLogLevel? logLevel;

@override
String getAndroidClassName() {
return "io.flutter.plugins.googlemobileads.mediation.gma_mediation_inmobi.InMobiFlutterMediationExtras";
}

@override
String getIOSClassName() {
return "InMobiFlutterMediationExtras";
}

@override
Map<String, dynamic> getExtras() {
return <String, dynamic>{
"ageGroup": ageGroup?.intValue,
"education": education?.intValue,
"age": age,
"yearOfBirth": yearOfBirth,
"postalCode": postalCode,
"areaCode": areaCode,
"language": language,
"city": city,
"state": state,
"country": country,
"keywords": keywords,
"interests": interests,
"logLevel": logLevel?.intValue,
};
}
}

/// Definition of the age groups.
enum InMobiAgeGroups {
below18, // index = 0
between18And24,
between25And29,
between30And34,
between35And44,
between45And54,
between55And65,
above65;

int get intValue => index;
}

/// Definition of different level of education.
enum InMobiEducation {
highschoolOrLess, // index = 0
collegeOrGraduate,
postgraduateOrAbove;

int get intValue => index;
}

/// Used in the InMobiSDK to set the log level.
enum InMobiLogLevel {
none,
debug,
error;

int get intValue => index;
}