@@ -56,10 +56,6 @@ import com.github.shadowsocks.utils.readableMessage
5656import com.github.shadowsocks.widget.ListHolderListener
5757import com.github.shadowsocks.widget.MainListListener
5858import com.github.shadowsocks.widget.UndoSnackbarManager
59- import com.google.android.gms.ads.VideoOptions
60- import com.google.android.gms.ads.formats.NativeAdOptions
61- import com.google.android.gms.ads.formats.UnifiedNativeAd
62- import com.google.android.gms.ads.formats.UnifiedNativeAdView
6359import com.google.zxing.BarcodeFormat
6460import com.google.zxing.EncodeHintType
6561import com.google.zxing.MultiFormatWriter
@@ -89,32 +85,6 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
8985 private fun isProfileEditable (id : Long ) =
9086 (activity as MainActivity ).state == BaseService .State .Stopped || id !in Core .activeProfileIds
9187
92- private var nativeAd: UnifiedNativeAd ? = null
93- private var nativeAdView: UnifiedNativeAdView ? = null
94- private var adHost: ProfileViewHolder ? = null
95- private fun tryBindAd () = lifecycleScope.launchWhenStarted {
96- val fp = layoutManager.findFirstVisibleItemPosition()
97- if (fp < 0 ) return @launchWhenStarted
98- for (i in object : Iterator <Int > {
99- var first = fp
100- var last = layoutManager.findLastCompletelyVisibleItemPosition()
101- var flipper = false
102- override fun hasNext () = first <= last
103- override fun next (): Int {
104- flipper = ! flipper
105- return if (flipper) first++ else last--
106- }
107- }.asSequence().toList().reversed()) {
108- val viewHolder = profilesList.findViewHolderForAdapterPosition(i) as ? ProfileViewHolder
109- if (viewHolder?.item?.isSponsored == true ) {
110- viewHolder.populateUnifiedNativeAdView(nativeAd!! , nativeAdView!! )
111- // might be in the middle of a layout after scrolling, need to wait
112- withContext(Dispatchers .Main ) { profilesAdapter.notifyItemChanged(i) }
113- break
114- }
115- }
116- }
117-
11888 @SuppressLint(" ValidFragment" )
11989 class QRCodeDialog () : DialogFragment() {
12090 constructor (url: String ) : this () {
@@ -157,7 +127,6 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
157127 private val traffic = itemView.findViewById<TextView >(R .id.traffic)
158128 private val edit = itemView.findViewById<View >(R .id.edit)
159129 private val subscription = itemView.findViewById<View >(R .id.subscription)
160- private val adContainer = itemView.findViewById<LinearLayout >(R .id.ad_container)
161130
162131 init {
163132 edit.setOnClickListener {
@@ -181,92 +150,6 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
181150 TooltipCompat .setTooltipText(share, share.contentDescription)
182151 }
183152
184- fun populateUnifiedNativeAdView (nativeAd : UnifiedNativeAd , adView : UnifiedNativeAdView ) {
185- // Set other ad assets.
186- adView.headlineView = adView.findViewById(R .id.ad_headline)
187- adView.bodyView = adView.findViewById(R .id.ad_body)
188- adView.callToActionView = adView.findViewById(R .id.ad_call_to_action)
189- adView.iconView = adView.findViewById(R .id.ad_app_icon)
190- adView.starRatingView = adView.findViewById(R .id.ad_stars)
191- adView.advertiserView = adView.findViewById(R .id.ad_advertiser)
192-
193- // The headline and media content are guaranteed to be in every UnifiedNativeAd.
194- (adView.headlineView as TextView ).text = nativeAd.headline
195-
196- // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
197- // check before trying to display them.
198- if (nativeAd.body == null ) {
199- adView.bodyView.visibility = View .INVISIBLE
200- } else {
201- adView.bodyView.visibility = View .VISIBLE
202- (adView.bodyView as TextView ).text = nativeAd.body
203- }
204-
205- if (nativeAd.callToAction == null ) {
206- adView.callToActionView.visibility = View .INVISIBLE
207- } else {
208- adView.callToActionView.visibility = View .VISIBLE
209- (adView.callToActionView as Button ).text = nativeAd.callToAction
210- }
211-
212- if (nativeAd.icon == null ) {
213- adView.iconView.visibility = View .GONE
214- } else {
215- (adView.iconView as ImageView ).setImageDrawable(
216- nativeAd.icon.drawable)
217- adView.iconView.visibility = View .VISIBLE
218- }
219-
220- if (nativeAd.starRating == null ) {
221- adView.starRatingView.visibility = View .INVISIBLE
222- } else {
223- (adView.starRatingView as RatingBar ).rating = nativeAd.starRating!! .toFloat()
224- adView.starRatingView.visibility = View .VISIBLE
225- }
226-
227- if (nativeAd.advertiser == null ) {
228- adView.advertiserView.visibility = View .INVISIBLE
229- } else {
230- (adView.advertiserView as TextView ).text = nativeAd.advertiser
231- adView.advertiserView.visibility = View .VISIBLE
232- }
233-
234- // This method tells the Google Mobile Ads SDK that you have finished populating your
235- // native ad view with this native ad.
236- adView.setNativeAd(nativeAd)
237- adContainer.addView(adView)
238- adHost = this
239- }
240-
241- fun attach () {
242- if (adHost != null || ! item.isSponsored) return
243- if (nativeAdView == null ) {
244- nativeAdView = layoutInflater.inflate(R .layout.ad_unified, adContainer, false ) as UnifiedNativeAdView
245- AdsManager .load(context) {
246- forUnifiedNativeAd { unifiedNativeAd ->
247- // You must call destroy on old ads when you are done with them,
248- // otherwise you will have a memory leak.
249- nativeAd?.destroy()
250- nativeAd = unifiedNativeAd
251- tryBindAd()
252- }
253- withNativeAdOptions(NativeAdOptions .Builder ().apply {
254- setVideoOptions(VideoOptions .Builder ().apply {
255- setStartMuted(true )
256- }.build())
257- }.build())
258- }
259- } else if (nativeAd != null ) populateUnifiedNativeAdView(nativeAd!! , nativeAdView!! )
260- }
261-
262- fun detach () {
263- if (adHost == this ) {
264- adHost = null
265- adContainer.removeAllViews()
266- tryBindAd()
267- }
268- }
269-
270153 fun bind (item : Profile ) {
271154 this .item = item
272155 val editable = isProfileEditable(item.id)
@@ -341,8 +224,6 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
341224 setHasStableIds(true ) // see: http://stackoverflow.com/a/32488059/2245107
342225 }
343226
344- override fun onViewAttachedToWindow (holder : ProfileViewHolder ) = holder.attach()
345- override fun onViewDetachedFromWindow (holder : ProfileViewHolder ) = holder.detach()
346227 override fun onBindViewHolder (holder : ProfileViewHolder , position : Int ) = holder.bind(profiles[position])
347228 override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): ProfileViewHolder = ProfileViewHolder (
348229 LayoutInflater .from(parent.context).inflate(R .layout.layout_profile, parent, false ))
@@ -594,7 +475,6 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
594475
595476 override fun onDestroyView () {
596477 undoManager.flush()
597- nativeAd?.destroy()
598478 super .onDestroyView()
599479 }
600480
0 commit comments