Skip to content

Commit

Permalink
backport: 20230629
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Jun 30, 2023
1 parent 7888deb commit 5f129f6
Show file tree
Hide file tree
Showing 19 changed files with 355 additions and 205 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ android {
namespace = "io.nekohasekai.sagernet"
}

val ossImplementation by configurations
val playImplementation by configurations

dependencies {

implementation(fileTree("libs"))
Expand Down Expand Up @@ -76,4 +79,7 @@ dependencies {
kapt("com.github.MatrixDev.Roomigrant:RoomigrantCompiler:0.3.4")

coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")

ossImplementation("com.google.android.gms:play-services-ads:22.1.0")
playImplementation("com.google.android.gms:play-services-ads:22.1.0")
}
12 changes: 12 additions & 0 deletions app/src/foss/java/moe/matsuri/MoeAd.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package moe.matsuri

import android.content.Context
import android.view.ViewGroup

object MoeAd {
fun initialize(ctx: Context) {
}

fun showBannerAd(parentLayout: ViewGroup) {
}
}
104 changes: 104 additions & 0 deletions app/src/google/java/moe/matsuri/MoeAd.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package moe.matsuri

import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.LinearLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.appopen.AppOpenAd
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.ktx.dp2pxf
import io.nekohasekai.sagernet.ktx.isOss
import io.nekohasekai.sagernet.ktx.isPlay

object MoeAd {
private const val LOG_TAG = "MoeAd"

private const val APP_OPEN_TEST = "ca-app-pub-3940256099942544/3419835294"
private const val APP_OPEN_RELEASE = "ca-app-pub-3940256099942544/3419835294"
private val AD_APPOPEN_UNIT_ID = if (isOss || isPlay) APP_OPEN_RELEASE else APP_OPEN_TEST

private const val BANNER_TEST = "ca-app-pub-3940256099942544/6300978111"
private const val BANNER_RELEASE = "ca-app-pub-2318700711963667/2682266712"
private val AD_BANNER_UNIT_ID = if (isOss || isPlay) BANNER_RELEASE else BANNER_TEST

fun initialize(ctx: Context) {
MobileAds.initialize(ctx)
}

fun showBannerAd(parentLayout: ViewGroup) {
val ctx = parentLayout.context
val req = AdRequest.Builder().build()
val adview = AdView(ctx).apply {
setAdSize(AdSize.BANNER)
adUnitId = AD_BANNER_UNIT_ID
}
val layout = LinearLayout(ctx)
val close = ImageButton(ctx, null, R.style.Widget_AppCompat_Button_Borderless).apply {
setOnClickListener {
layout.visibility = View.GONE
}
setImageResource(R.drawable.ic_navigation_close)
visibility = View.GONE
}
layout.apply {
layoutParams = CoordinatorLayout.LayoutParams(
CoordinatorLayout.LayoutParams.WRAP_CONTENT,
CoordinatorLayout.LayoutParams.WRAP_CONTENT
).apply {
anchorId = R.id.fab
anchorGravity = Gravity.CENTER_HORIZONTAL or Gravity.TOP
translationY = -dp2pxf(32)
}
addView(adview.apply {
adListener = BannerAdListener(close)
loadAd(req)
})
addView(close)
}
parentLayout.addView(layout)
}

internal class BannerAdListener(val close: ImageButton) : AdListener() {
override fun onAdFailedToLoad(p0: LoadAdError) {
super.onAdFailedToLoad(p0)
Log.d(LOG_TAG, "onAdFailedToLoad")
}

override fun onAdLoaded() {
super.onAdLoaded()
close.visibility = View.VISIBLE
}
}

fun showAppOpenAd(activity: Activity) {
val req = AdRequest.Builder().build()
val callback = AppOpenAdCallback(activity)
AppOpenAd.load(activity, AD_APPOPEN_UNIT_ID, req, callback)
}

internal class AppOpenAdCallback(
val activity: Activity
) : AppOpenAd.AppOpenAdLoadCallback() {
override fun onAdFailedToLoad(p0: LoadAdError) {
super.onAdFailedToLoad(p0)
Log.d(LOG_TAG, "onAdFailedToLoad")
}

override fun onAdLoaded(ad: AppOpenAd) {
super.onAdLoaded(ad)
// Log.d(LOG_TAG, "onAdLoaded")
ad.show(activity)
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/Theme.Start">

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-2318700711963667~7062247500" />

<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ oneway interface ISagerNetServiceCallback {
void trafficUpdated(long profileId, in TrafficStats stats, boolean isCurrent);
void statsUpdated(in AppStatsList statsList);
void missingPlugin(String profileName, String pluginName);
void routeAlert(int type, String routeName);
void updateWakeLockStatus(boolean acquired);
}
6 changes: 0 additions & 6 deletions app/src/main/java/io/nekohasekai/sagernet/bg/BaseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,6 @@ class BaseService {
startProcesses()
data.changeState(State.Connected)

for ((type, routeName) in proxy.config.alerts) {
data.binder.broadcast {
it.routeAlert(type, routeName)
}
}

lateInit()
} catch (_: CancellationException) { // if the job was cancelled, it is canceller's responsibility to call stopRunner
} catch (_: UnknownHostException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class SagerConnection(private var listenForDeath: Boolean = false) : ServiceConn
fun statsUpdated(stats: List<AppStats>) {}

fun missingPlugin(profileName: String, pluginName: String) {}
fun routeAlert(type: Int, routeName: String) {}

fun onServiceConnected(service: ISagerNetService)

Expand Down Expand Up @@ -93,13 +92,6 @@ class SagerConnection(private var listenForDeath: Boolean = false) : ServiceConn
callback.statsUpdated(statsList.data)
}

override fun routeAlert(type: Int, routeName: String) {
val callback = callback ?: return
runOnMainDispatcher {
callback.routeAlert(type, routeName)
}
}

override fun updateWakeLockStatus(acquired: Boolean) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ class ServiceNotification(
override fun missingPlugin(profileName: String?, pluginName: String?) {
}

override fun routeAlert(type: Int, routeName: String?) {
}

override fun updateWakeLockStatus(acquired: Boolean) {
updateActions()
builder.priority =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ data class RuleEntity(
-1L -> app.getString(R.string.route_bypass)
-2L -> app.getString(R.string.route_block)
else -> ProfileManager.getProfile(outbound)?.displayName()
?: app.getString(R.string.route_proxy)
?: app.getString(R.string.error_title)
}
}

Expand Down
Loading

0 comments on commit 5f129f6

Please sign in to comment.