Skip to content

Commit

Permalink
Google AdMob added (FirebaseFree build is AdFree also)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrivoruchko committed Jun 21, 2021
1 parent 805bde9 commit 1ff4120
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 52 deletions.
15 changes: 8 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = "info.dvkr.screenstream"
minSdkVersion(21)
targetSdkVersion(30)
versionCode = 30604
versionName = "3.6.4"
versionCode = 30700
versionName = "3.7.0"
resConfigs("en", "ru", "pt-rBR", "zh-rTW", "fr-rFR", "fa", "it", "pl", "hi", "de", "sk", "es", "ar", "ja", "gl", "ca")

vectorDrawables.useSupportLibrary = true
Expand Down Expand Up @@ -83,7 +83,7 @@ dependencies {

implementation("androidx.core:core-ktx:1.5.0")
implementation("androidx.activity:activity-ktx:1.2.3")
implementation('androidx.fragment:fragment-ktx:1.3.5')
implementation("androidx.fragment:fragment-ktx:1.3.5")
implementation("androidx.appcompat:appcompat:1.3.0")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")
implementation("androidx.recyclerview:recyclerview:1.2.1")
Expand All @@ -102,9 +102,9 @@ dependencies {
implementation(fileTree("libs/bottomsheets-release.aar"))
// implementation("com.afollestad.material-dialogs:bottomsheets:3.3.0")

implementation('io.insert-koin:koin-android:3.1.0')
implementation("io.insert-koin:koin-android:3.1.0")
implementation("com.github.iamironz:binaryprefs:1.0.1")
implementation('com.elvishew:xlog:1.10.0')
implementation("com.elvishew:xlog:1.10.0")

firebaseImplementation("com.google.android.play:core:1.10.0")
firebaseImplementation("com.google.android.play:core-ktx:1.8.1") {
Expand All @@ -113,8 +113,9 @@ dependencies {
exclude group: "androidx.fragment"
exclude group: "androidx.core"
}
firebaseReleaseImplementation("com.google.firebase:firebase-analytics:19.0.0")
firebaseReleaseImplementation('com.google.firebase:firebase-crashlytics:18.0.1')
firebaseImplementation("com.google.firebase:firebase-analytics:19.0.0")
firebaseImplementation("com.google.firebase:firebase-crashlytics:18.0.1")
firebaseImplementation("com.google.android.gms:play-services-ads:20.2.0")

debugImplementation("com.squareup.leakcanary:leakcanary-android:2.7")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package info.dvkr.screenstream.ui.activity

import android.util.DisplayMetrics
import android.view.ViewTreeObserver
import android.widget.FrameLayout
import androidx.annotation.LayoutRes
import com.google.android.gms.ads.*


abstract class AdActivity(@LayoutRes contentLayoutId: Int) : AppUpdateActivity(contentLayoutId) {

private var adView: AdView? = null

fun loadAd(adViewContainer: FrameLayout) {
MobileAds.initialize(this) {}
adView = AdView(this)
adViewContainer.addView(adView)
adViewContainer.viewTreeObserver.addOnGlobalLayoutListener(
object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val outMetrics = DisplayMetrics().also { windowManager.defaultDisplay.getMetrics(it) }
var adWidthPixels = adViewContainer.width.toFloat()
if (adWidthPixels == 0f) adWidthPixels = outMetrics.widthPixels.toFloat()
val adWidth = (adWidthPixels / outMetrics.density).toInt()
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this@AdActivity, adWidth)

// Device MUST be added to test devices
adView!!.adUnitId = "ca-app-pub-3406399667931208/3206361386"
adView!!.adSize = adSize
adView!!.loadAd(AdRequest.Builder().build())

adViewContainer.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
}
)
}

override fun onPause() {
adView?.pause()
super.onPause()
}

override fun onResume() {
super.onResume()
adView?.resume()
}

override fun onDestroy() {
adView?.destroy()
super.onDestroy()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package info.dvkr.screenstream.ui.activity

import android.widget.FrameLayout
import androidx.annotation.LayoutRes


abstract class AdActivity(@LayoutRes contentLayoutId: Int) : AppUpdateActivity(contentLayoutId) {

fun loadAd(adViewContainer: FrameLayout) {}
}
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
android:theme="@style/Theme.App.DayNight"
tools:ignore="GoogleAppIndexingWarning">

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

<activity
android:name=".ui.activity.AppActivity"
android:launchMode="singleTask"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class AppService : Service() {
fun startForeground(context: Context, intent: Intent) = ContextCompat.startForegroundService(context, intent)
}

inner class AppServiceBinder : Binder() {
fun getServiceMessageFlow(): SharedFlow<ServiceMessage> = _serviceMessageSharedFlow.asSharedFlow()
class AppServiceBinder(private val serviceMessageSharedFlow: MutableSharedFlow<ServiceMessage>) : Binder() {
fun getServiceMessageFlow(): SharedFlow<ServiceMessage> = serviceMessageSharedFlow.asSharedFlow()
}

private val appServiceBinder = AppServiceBinder()
private val _serviceMessageSharedFlow =
MutableSharedFlow<ServiceMessage>(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
private var appServiceBinder: AppServiceBinder? = AppServiceBinder(_serviceMessageSharedFlow)

private fun sendMessageToActivities(serviceMessage: ServiceMessage) {
XLog.v(getLog("sendMessageToActivities", "ServiceMessage: $serviceMessage"))
Expand Down Expand Up @@ -203,6 +203,7 @@ class AppService : Service() {
appStateMachine = null
coroutineScope.cancel(CancellationException("AppService.destroy"))
stopForeground(true)
appServiceBinder = null
XLog.d(getLog("onDestroy", "Done"))
super.onDestroy()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class AppActivity : PermissionActivity(R.layout.activity_app) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

loadAd(binding.flActivityAppAdViewContainer)

routeIntentAction(intent)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

abstract class ServiceActivity(@LayoutRes contentLayoutId: Int) : AppUpdateActivity(contentLayoutId) {
abstract class ServiceActivity(@LayoutRes contentLayoutId: Int) : AdActivity(contentLayoutId) {

private val serviceMessageLiveData = MutableLiveData<ServiceMessage>()
private var serviceMessageFlowJob: Job? = null
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/layout/activity_app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
android:background="@drawable/view_shadow_bottom"
app:layout_constraintTop_toBottomOf="@id/ll_activity_app_logs" />

<FrameLayout
android:id="@+id/fl_activity_app_ad_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/ll_activity_app_logs" />

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fr_activity_app_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
Expand All @@ -57,7 +63,7 @@
android:layout_marginBottom="24dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/ll_activity_app_logs"
app:layout_constraintTop_toBottomOf="@id/fl_activity_app_ad_view_container"
app:navGraph="@navigation/nav_graph" />

<info.dvkr.screenstream.ui.view.CurvedBottomNavigationView
Expand Down
4 changes: 2 additions & 2 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion(21)
targetSdkVersion(30)
versionCode = 30604
versionName = "3.6.4"
versionCode = 30700
versionName = "3.7.0"
}

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package info.dvkr.screenstream.data.state

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.media.projection.MediaProjection
import android.media.projection.MediaProjectionManager
import android.os.Handler
import android.os.Looper
import android.view.WindowManager
import androidx.core.content.ContextCompat
import com.elvishew.xlog.XLog
Expand All @@ -15,7 +20,6 @@ import info.dvkr.screenstream.data.settings.Settings
import info.dvkr.screenstream.data.settings.SettingsReadOnly
import info.dvkr.screenstream.data.state.helper.BroadcastHelper
import info.dvkr.screenstream.data.state.helper.ConnectivityHelper
import info.dvkr.screenstream.data.state.helper.MediaProjectionHelper
import info.dvkr.screenstream.data.state.helper.NetworkHelper
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
Expand All @@ -36,7 +40,14 @@ class AppStateMachineImpl(
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default + coroutineExceptionHandler)

private val bitmapStateFlow = MutableStateFlow(Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888))
private val mediaProjectionHelper = MediaProjectionHelper(context) { sendEvent(AppStateMachine.Event.StopStream) }
private val projectionManager = context.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
private val projectionCallback = object : MediaProjection.Callback() {
override fun onStop() {
XLog.i(this@AppStateMachineImpl.getLog("MediaProjection.Callback", "onStop"))
sendEvent(AppStateMachine.Event.StopStream)
}
}

private val broadcastHelper = BroadcastHelper.getInstance(context)
private val connectivityHelper: ConnectivityHelper = ConnectivityHelper.getInstance(context)
private val networkHelper = NetworkHelper(context)
Expand Down Expand Up @@ -217,7 +228,8 @@ class AppStateMachineImpl(
XLog.d(getLog("stopProjection"))
if (streamState.isStreaming()) {
streamState.bitmapCapture?.destroy()
mediaProjectionHelper.stopMediaProjection(streamState.mediaProjection)
streamState.mediaProjection?.unregisterCallback(projectionCallback)
streamState.mediaProjection?.stop()
}

return streamState.copy(mediaProjection = null, bitmapCapture = null)
Expand Down Expand Up @@ -278,7 +290,8 @@ class AppStateMachineImpl(
private fun startProjection(streamState: StreamState, intent: Intent): StreamState {
XLog.d(getLog("startProjection"))

val mediaProjection = mediaProjectionHelper.getMediaProjection(intent)
val mediaProjection = projectionManager.getMediaProjection(Activity.RESULT_OK, intent)
mediaProjection.registerCallback(projectionCallback, Handler(Looper.getMainLooper()))
val display = ContextCompat.getSystemService(applicationContext, WindowManager::class.java)!!.defaultDisplay
val bitmapCapture = BitmapCapture(display, settingsReadOnly, mediaProjection, bitmapStateFlow, ::onError)
bitmapCapture.start()
Expand Down

This file was deleted.

0 comments on commit 1ff4120

Please sign in to comment.