Skip to content

Commit

Permalink
Initial Firebase AdMob plugin (flutter#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Muller authored Jul 13, 2017
1 parent 9ff2659 commit 56ca5fd
Show file tree
Hide file tree
Showing 74 changed files with 2,980 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/firebase_admob/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
.atom/
.idea
.packages
.pub/
build/
ios/.generated/
packages
pubspec.lock
3 changes: 3 additions & 0 deletions packages/firebase_admob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.0.1]

* Initial Release: not ready for production use
27 changes: 27 additions & 0 deletions packages/firebase_admob/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8 changes: 8 additions & 0 deletions packages/firebase_admob/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# firebase_admob
A Flutter plugin based on the [Firebase AdMob API](https://firebase.google.com/docs/admob/).

*Warning*: This plugin is still under development, some AdMob features are not available yet and testing has been limited. [Feedback](https://github.com/flutter/flutter/issues) and [Pull Requests](https://github.com/flutter/plugins/pulls) are welcome.

See https://github.com/flutter/plugins/pull/171 for a brief overview of the current API.

For Flutter plugins for other Firebase products, see [FlutterFire.md](https://github.com/flutter/plugins/blob/master/FlutterFire.md).
12 changes: 12 additions & 0 deletions packages/firebase_admob/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

/gradle
/gradlew
/gradlew.bat
36 changes: 36 additions & 0 deletions packages/firebase_admob/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
group 'io.flutter.plugins.firebaseadmob'
version '1.0-SNAPSHOT'

buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
}
}

allprojects {
repositories {
jcenter()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'

defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
dependencies {
compile 'com.google.firebase:firebase-core:11.0.+'
compile 'com.google.firebase:firebase-ads:11.0.+'
}
}
1 change: 1 addition & 0 deletions packages/firebase_admob/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1536M
1 change: 1 addition & 0 deletions packages/firebase_admob/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'firebase_admob'
9 changes: 9 additions & 0 deletions packages/firebase_admob/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.firebaseadmob"
android:versionCode="1"
android:versionName="0.0.1">

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.firebaseadmob;

import android.app.Activity;
import android.widget.LinearLayout;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.firebase.FirebaseApp;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import java.util.Map;

public class FirebaseAdMobPlugin implements MethodCallHandler {
private static final String TAG = "flutter";

private final Activity activity;
private final MethodChannel channel;

private LinearLayout banner;
InterstitialAd interstitial;

public static void registerWith(Registrar registrar) {
final MethodChannel channel =
new MethodChannel(registrar.messenger(), "plugins.flutter.io/firebase_admob");
channel.setMethodCallHandler(new FirebaseAdMobPlugin(registrar.activity(), channel));
}

private FirebaseAdMobPlugin(Activity activity, MethodChannel channel) {
this.activity = activity;
this.channel = channel;
FirebaseApp.initializeApp(activity);
}

private void callInitialize(MethodCall call, Result result) {
String appId = call.argument("appId");
if (appId == null || appId.isEmpty()) {
result.error("no_app_id", "a non-empty AdMob appId was not provided", null);
return;
}
MobileAds.initialize(activity, appId);
result.success(Boolean.TRUE);
}

private void callLoadAd(MobileAd ad, MethodCall call, Result result) {
if (ad.status != MobileAd.Status.CREATED) {
if (ad.status == MobileAd.Status.FAILED)
result.error("load_failed_ad", "cannot reload a failed ad, id=" + ad.id, null);
else result.success(Boolean.TRUE); // The ad was already loaded.
return;
}

String unitId = call.argument("unitId");
if (unitId == null || unitId.isEmpty()) {
result.error("no_unit_id", "a non-empty unitId was not provided for ad id=" + ad.id, null);
return;
}
Map<String, Object> targetingInfo = call.argument("targetingInfo");
ad.load(unitId, targetingInfo);
result.success(Boolean.TRUE);
}

private void callShowAd(int id, MethodCall call, Result result) {
MobileAd ad = MobileAd.getAdForId(id);
if (ad == null) {
result.error("ad_not_loaded", "show failed, the specified ad was not loaded id=" + id, null);
return;
}
ad.show();
result.success(Boolean.TRUE);
}

private void callDisposeAd(int id, MethodCall call, Result result) {
MobileAd ad = MobileAd.getAdForId(id);
if (ad == null) {
result.error("no_ad_for_id", "dispose failed, no add exists for id=" + id, null);
return;
}

ad.dispose();
result.success(Boolean.TRUE);
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("initialize")) {
callInitialize(call, result);
return;
}

Integer id = call.argument("id");
if (id == null) {
result.error(
"no_id",
"all FirebaseAdMobPlugin method calls must specify an integer mobile ad id",
null);
return;
}

switch (call.method) {
case "loadBannerAd":
callLoadAd(MobileAd.createBanner(id, activity, channel), call, result);
break;
case "loadInterstitialAd":
callLoadAd(MobileAd.createInterstitial(id, activity, channel), call, result);
break;
case "showAd":
callShowAd(id, call, result);
break;
case "disposeAd":
callDisposeAd(id, call, result);
break;
default:
result.notImplemented();
}
}
}
Loading

0 comments on commit 56ca5fd

Please sign in to comment.