-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AppStateEventNotifier. This replaces OnApplicationPause for App…
…OpenAds. PiperOrigin-RevId: 455262379
- Loading branch information
1 parent
6613d76
commit 95182ef
Showing
16 changed files
with
454 additions
and
42 deletions.
There are no files selected for viewing
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
25 changes: 25 additions & 0 deletions
25
...ce/android-library/app/src/main/java/com/google/unity/ads/UnityAppStateEventCallback.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,25 @@ | ||
/* | ||
* Copyright (C) 2022 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.google.unity.ads; | ||
|
||
/** | ||
* An interface form of {@link UnityAppStateEventCallback} that can be implemented via {@code | ||
* AndroidJavaProxy} in Unity to receive ad events synchronously. | ||
*/ | ||
public interface UnityAppStateEventCallback { | ||
void onAppStateChanged(boolean isBackground); | ||
} |
99 changes: 99 additions & 0 deletions
99
...ce/android-library/app/src/main/java/com/google/unity/ads/UnityAppStateEventNotifier.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,99 @@ | ||
/* | ||
* Copyright (C) 2022 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.google.unity.ads; | ||
|
||
import android.app.Activity; | ||
import androidx.annotation.NonNull; | ||
import androidx.lifecycle.DefaultLifecycleObserver; | ||
import androidx.lifecycle.LifecycleOwner; | ||
import androidx.lifecycle.ProcessLifecycleOwner; | ||
|
||
/** | ||
* Proxy for the ProcessLifecycleOwner for the Unity AppStateEventNotifier. | ||
*/ | ||
public class UnityAppStateEventNotifier implements DefaultLifecycleObserver { | ||
|
||
/** | ||
* The {@code Activity} on which the app open ad will display. | ||
*/ | ||
private final Activity activity; | ||
|
||
/** | ||
* A callback implemented in Unity via {@code AndroidJavaProxy} to receive lifecycle events. | ||
*/ | ||
private final UnityAppStateEventCallback callback; | ||
|
||
public UnityAppStateEventNotifier(Activity activity, UnityAppStateEventCallback callback) { | ||
this.activity = activity; | ||
this.callback = callback; | ||
} | ||
|
||
/** Attaches as an observer of the ProcessLifecycleOwner. */ | ||
public void startListening() { | ||
activity.runOnUiThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
ProcessLifecycleOwner.get() | ||
.getLifecycle() | ||
.addObserver(UnityAppStateEventNotifier.this); | ||
} | ||
}); | ||
} | ||
|
||
/** Detatches as an observer of the ProcessLifecycleOwner. */ | ||
public void stopListening() { | ||
activity.runOnUiThread( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
ProcessLifecycleOwner.get() | ||
.getLifecycle() | ||
.removeObserver(UnityAppStateEventNotifier.this); | ||
} | ||
}); | ||
} | ||
|
||
/** DefaultLifecycleObserver implementation. */ | ||
|
||
@Override | ||
public void onStart(@NonNull LifecycleOwner owner) { | ||
callback.onAppStateChanged(/*isBackground=*/ false); | ||
} | ||
|
||
@Override | ||
public void onStop(@NonNull LifecycleOwner owner) { | ||
callback.onAppStateChanged(/*isBackground=*/ true); | ||
} | ||
|
||
@Override | ||
public void onCreate(@NonNull LifecycleOwner owner) { | ||
} | ||
|
||
@Override | ||
public void onDestroy(@NonNull LifecycleOwner owner) { | ||
} | ||
|
||
@Override | ||
public void onResume(@NonNull LifecycleOwner owner) { | ||
} | ||
|
||
@Override | ||
public void onPause(@NonNull LifecycleOwner owner) { | ||
} | ||
|
||
} |
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,6 @@ | ||
# AndroidX package structure to make it clearer which packages are bundled with the | ||
# Android operating system, and which are packaged with your app"s APK | ||
# https://developer.android.com/topic/libraries/support-library/androidx-rn | ||
android.useAndroidX=true | ||
# Automatically convert third-party libraries to use AndroidX | ||
android.enableJetifier=true |
48 changes: 48 additions & 0 deletions
48
source/plugin/Assets/GoogleMobileAds/Api/AppStateEventNotifier.cs
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,48 @@ | ||
// Copyright (C) 2022 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 | ||
// | ||
// 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. | ||
|
||
using System; | ||
using GoogleMobileAds.Common; | ||
|
||
namespace GoogleMobileAds.Api | ||
{ | ||
/// <summary> | ||
/// Notifies changes in app foreground/background. | ||
/// Subscribe to AppStateChanged to get notified of changes in app state. | ||
/// </summary> | ||
public class AppStateEventNotifier | ||
{ | ||
/// <summary> | ||
/// Raised when the app enters the background/foreground. | ||
/// </summary> | ||
public static event Action<AppState> AppStateChanged | ||
{ | ||
add | ||
{ | ||
client.AppStateChanged += value; | ||
} | ||
remove | ||
{ | ||
client.AppStateChanged -= value; | ||
} | ||
} | ||
|
||
private static IAppStateEventClient client; | ||
|
||
static AppStateEventNotifier() | ||
{ | ||
client = MobileAds.GetClientFactory().BuildAppStateEventClient(); | ||
} | ||
} | ||
} |
Oops, something went wrong.