UNITY ADS


SUBMITTED BY: marco212

DATE: April 1, 2022, 9:51 p.m.

FORMAT: Text only

SIZE: 18.4 kB

HITS: 274

  1. BUILD GRADE
  2. compile 'com.unity3d.ads:unity-ads:3.7.+'
  3. compile 'cn.pedant.sweetalert:library:1.3'
  4. compile 'com.sdsmdg.tastytoast:tastytoast:0.1.1'
  5. //////////////////////
  6. ANDROIDMANIESTO
  7. <!--- Meta Data -->
  8. <activity
  9. android:name="com.unity3d.services.ads.adunit.AdUnitActivity"
  10. android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
  11. android:hardwareAccelerated="true"
  12. android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
  13. <activity
  14. android:name="com.unity3d.services.ads.adunit.AdUnitTransparentActivity"
  15. android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
  16. android:hardwareAccelerated="true"
  17. android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
  18. <activity
  19. android:name="com.unity3d.services.ads.adunit.AdUnitTransparentSoftwareActivity"
  20. android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
  21. android:hardwareAccelerated="false"
  22. android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
  23. <activity
  24. android:name="com.unity3d.services.ads.adunit.AdUnitSoftwareActivity"
  25. android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
  26. android:hardwareAccelerated="false"
  27. android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
  28. ////////////////////////////
  29. SOCKSHTTPACTIVITY OR MAINACTIVITY
  30. /ads unity
  31. import com.unity3d.ads.IUnityAdsInitializationListener;
  32. import com.unity3d.ads.IUnityAdsLoadListener;
  33. import com.unity3d.ads.IUnityAdsShowListener;
  34. import com.unity3d.ads.UnityAds;
  35. import com.unity3d.ads.UnityAdsShowOptions;
  36. import com.unity3d.services.banners.BannerErrorInfo;
  37. import com.unity3d.services.banners.BannerView;
  38. import com.unity3d.services.banners.UnityBannerSize;
  39. import com.unity3d.ads.IUnityAdsListener;
  40. import cn.pedant.SweetAlert.SweetAlertDialog;
  41. import android.os.CountDownTimer;
  42. import com.jaxtunnel.phtunnel.service.SocksDNSService;
  43. import java.util.concurrent.TimeUnit;
  44. import java.util.Locale;
  45. //end
  46. /////////////////////////
  47. public class MainActivity ir SocksHttpActivity
  48. IUnityAdsInitializationListener
  49. ////////////////////////
  50. //private unity ads
  51. private String unityGameID = "4087375";
  52. Boolean testMode = true;
  53. private String bottomAdUnitId = "Banner_Android";
  54. private String InterstitialadUnitId = "Interstitial_Android";
  55. private String RewardadUnitId = "Rewarded_Android";
  56. private UnityAds interstitialAds;
  57. private Button mReward;
  58. //end
  59. ////////////////////
  60. //Under the deprive of unity
  61. private BannerView.IListener bannerListener =
  62. new BannerView.IListener() {
  63. @Override
  64. public void onBannerLoaded(BannerView bannerAdView) {
  65. // Called when the banner is loaded.
  66. Log.v("UnityAdsExample", "onBannerLoaded: " + bannerAdView.getPlacementId());
  67. // Enable the correct button to hide the ad
  68. }
  69. @Override
  70. public void onBannerFailedToLoad(
  71. BannerView bannerAdView, BannerErrorInfo errorInfo) {
  72. Log.e(
  73. "UnityAdsExample",
  74. "Unity Ads failed to load banner for "
  75. + bannerAdView.getPlacementId()
  76. + " with error: ["
  77. + errorInfo.errorCode
  78. + "] "
  79. + errorInfo.errorMessage);
  80. // Note that the BannerErrorInfo object can indicate a no fill (see API
  81. // documentation).
  82. }
  83. @Override
  84. public void onBannerClick(BannerView bannerAdView) {
  85. // Called when a banner is clicked.
  86. Log.v("UnityAdsExample", "onBannerClick: " + bannerAdView.getPlacementId());
  87. }
  88. @Override
  89. public void onBannerLeftApplication(BannerView bannerAdView) {
  90. // Called when the banner links out of the application.
  91. Log.v(
  92. "UnityAdsExample",
  93. "onBannerLeftApplication: " + bannerAdView.getPlacementId());
  94. }
  95. };
  96. private IUnityAdsLoadListener loadListener =new IUnityAdsLoadListener() {
  97. @Override
  98. public void onUnityAdsAdLoaded(String placementId) {
  99. UnityAds.show(MainActivity.this,RewardadUnitId,new UnityAdsShowOptions(),showListener);
  100. //UnityAds.show(MainActivity.this,InterstitialadUnitId,new UnityAdsShowOptions(),showListener);
  101. }
  102. @Override
  103. public void onUnityAdsFailedToLoad(
  104. String placementId, UnityAds.UnityAdsLoadError error, String message) {
  105. Log.e(
  106. "UnityAdsExample",
  107. "Unity Ads failed to load ad for "
  108. + placementId
  109. + " with error: ["
  110. + error
  111. + "] "
  112. + message);
  113. }
  114. };
  115. private IUnityAdsShowListener showListener =
  116. new IUnityAdsShowListener() {
  117. private SweetAlertDialog pDialog;
  118. @Override
  119. public void onUnityAdsShowFailure(
  120. String placementId, UnityAds.UnityAdsShowError error, String message) {
  121. Log.e(
  122. "UnityAdsExample",
  123. "Unity Ads failed to show ad for "
  124. + placementId
  125. + " with error: ["
  126. + error
  127. + "] "
  128. + message);
  129. SweetAlertDialog nops =
  130. new SweetAlertDialog(
  131. MainActivity.this, SweetAlertDialog.ERROR_TYPE);
  132. nops.setTitleText("WARNING");
  133. nops.setContentText(
  134. "Failed to Load Ad, Please check your internet connection !!");
  135. nops.setConfirmText("Ok");
  136. nops.show();
  137. }
  138. @Override
  139. public void onUnityAdsShowStart(String placementId) {
  140. Log.v("UnityAdsExample", "onUnityAdsShowStart: " + placementId);
  141. //progressDialog.dismiss();
  142. }
  143. @Override
  144. public void onUnityAdsShowClick(String placementId) {
  145. Log.v("UnityAdsExample", "onUnityAdsShowClick: " + placementId);
  146. }
  147. @Override
  148. public void onUnityAdsShowComplete(
  149. String placementId, UnityAds.UnityAdsShowCompletionState state) {
  150. Log.v("UnityAdsExample", "onUnityAdsShowComplete: " + placementId);
  151. if (state.equals(UnityAds.UnityAdsShowCompletionState.COMPLETED)) {
  152. // doo
  153. btnTimer(); // Set button
  154. long ads_time = 6000 * 10 * 60 * 2; // add time
  155. addTime(ads_time); // set the time
  156. /*pDialog =
  157. new SweetAlertDialog(MainActivity.this, SweetAlertDialog.CUSTOM_IMAGE_TYPE);
  158. pDialog.setTitleText("Reward Time!.");
  159. pDialog.setContentText("Watch the whole video to get + 2 hours");
  160. pDialog.setTitle("Gracias por apoyar la aplicacion");
  161. pDialog.setCancelable(false);
  162. pDialog.setCustomImage(R.drawable.modojuegos);
  163. pDialog.show();*/
  164. Toast.makeText(MainActivity.this, " 2 hours successfully added to your time!", Toast.LENGTH_LONG).show();
  165. // Toast.makeText(SocksHttpMainActivity.this, "Mode Listener: 2 hours
  166. // successfully added to your time!", Toast.LENGTH_LONG).show();
  167. // Reward the user for watching the ad to completion
  168. } else {
  169. // Do not reward the user for skipping the ad
  170. }
  171. }
  172. };
  173. // This banner view object will be placed at the bottom of the screen:
  174. BannerView bottomBanner;
  175. // View objects to display banners:
  176. LinearLayout bottomBannerView;
  177. ///////////////////////////
  178. protected void onCreate
  179. //ads unity
  180. UnityAds.initialize(getApplicationContext(), unityGameID, testMode);
  181. showAds();
  182. // load reward ads
  183. loadRewardAds();
  184. mTextViewCountDown = (TextView) findViewById(R.id.tvTimeRemaining);
  185. timer_layout = (LinearLayout) findViewById(R.id.timerLayout);
  186. mReward = (Button) findViewById(R.id.btnAddTime);
  187. mReward.setOnClickListener(new View.OnClickListener(){
  188. @Override
  189. public void onClick(View p1) {
  190. DisplayRewardedVideoAd();
  191. btnTimer();
  192. long ads_time = 6000 * 10 * 60 * 2; // add time
  193. addTime(ads_time); // set the time
  194. Toast.makeText(MainActivity.this, "Espere mientras se carga el Video", Toast.LENGTH_LONG).show();
  195. Toast.makeText(MainActivity.this, " 2 hours successfully added to your time!", Toast.LENGTH_LONG).show();
  196. }
  197. });
  198. //////////////////////////////
  199. private void loadRewardAds() {
  200. IUnityAdsListener RewardListener = new IUnityAdsListener() {
  201. @Override
  202. public void onUnityAdsReady(String adUnitId) {
  203. // Implement functionality for an ad being ready to show.
  204. //Toast.makeText(getApplicationContext(),"Video ads loaded",Toast.LENGTH_SHORT).show();
  205. }
  206. @Override
  207. public void onUnityAdsStart(String adUnitId) {
  208. // Implement functionality for a user starting to watch an ad.
  209. }
  210. @Override
  211. public void onUnityAdsFinish(String adUnitId, UnityAds.FinishState finishState) {
  212. // Implement functionality for a user finishing an ad.
  213. loadRewardAds();
  214. }
  215. @Override
  216. public void onUnityAdsError(UnityAds.UnityAdsError error, String message) {
  217. // Implement functionality for a Unity Ads service error occurring.
  218. Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
  219. loadRewardAds();
  220. }
  221. };
  222. UnityAds.setListener(RewardListener);
  223. UnityAds.load(RewardadUnitId);
  224. }
  225. private void showAds() {
  226. // Create the bottom banner view object:
  227. bottomBanner = new BannerView(this, bottomAdUnitId, new UnityBannerSize(320, 50));
  228. // Set the listener for banner lifecycle events:
  229. bottomBanner.setListener(bannerListener);
  230. bottomBannerView = (LinearLayout) findViewById(R.id.bottomBanner);
  231. LoadBannerAd(bottomBanner, bottomBannerView);
  232. }
  233. public void LoadBannerAd(BannerView bannerView, LinearLayout bannerLayout) {
  234. // Request a banner ad:
  235. bannerView.load();
  236. // Associate the banner view object with the banner view:
  237. bannerLayout.addView(bannerView);
  238. }
  239. @Override
  240. public void onInitializationComplete() {
  241. // Enable the show ad buttons since ads can now be loaded
  242. // showAds();
  243. DisplayRewardedVideoAd();
  244. }
  245. @Override
  246. public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {
  247. Log.e(
  248. "UnityAdsExample",
  249. "Unity Ads initialization failed with error: [" + error + "] " + message);
  250. }
  251. // Implement a function to load a rewarded ad. The ad will start to show once the ad has been
  252. // loaded.
  253. public void DisplayRewardedAd() {
  254. UnityAds.load(RewardadUnitId, loadListener);
  255. }
  256. // to show reward Ads
  257. public void DisplayRewardedVideoAd() {
  258. if (UnityAds.isReady(RewardadUnitId)) {
  259. UnityAds.show(this, RewardadUnitId);
  260. }else{
  261. // Toast.makeText(getApplicationContext(),"ads not loaded",Toast.LENGTH_SHORT).show();
  262. }
  263. }
  264. public void DisplayInterstitialAd() {
  265. UnityAds.load(InterstitialadUnitId, loadListener);
  266. }
  267. //////////////////////////////
  268. //time add
  269. private CountDownTimer mCountDownTimer;
  270. private boolean mTimerRunning;
  271. private long saved_ads_time;
  272. private long mTimeLeftInMillis;
  273. private long mEndTime;
  274. //private SweetAlertDialog pDialog;
  275. private long mTimeLeftBtn;
  276. private TextView mTextViewCountDown;
  277. private LinearLayout timer_layout;
  278. private boolean mTimerEnabled;
  279. private
  280. CountDownTimer mBtnCountDown;
  281. //add time
  282. private void start(){
  283. if (saved_ads_time == 0){
  284. Toast.makeText(MainActivity.this, "Su tiempo expirará pronto, haga clic en AGREGAR TIEMPO para renovar el acceso.", Toast.LENGTH_LONG).show();
  285. long millisInput = 1000 * 500;
  286. setTime(millisInput);
  287. }
  288. if (!mTimerRunning){
  289. startTimer();
  290. }
  291. }
  292. private void stop(){
  293. if (mTimerRunning){
  294. pauseTimer();
  295. }
  296. }
  297. private void addTime(long time){
  298. setTime(time);
  299. if (mTimerRunning){
  300. pauseTimer();
  301. }
  302. startTimer();
  303. }
  304. private void pauseTimer() {
  305. mCountDownTimer.cancel();
  306. mTimerRunning = false;
  307. }
  308. private void updateCountDownText(){
  309. long days = TimeUnit.MILLISECONDS.toDays(mTimeLeftInMillis);
  310. long daysMillis = TimeUnit.DAYS.toMillis(days);
  311. long hours = TimeUnit.MILLISECONDS.toHours(mTimeLeftInMillis - daysMillis);
  312. long hoursMillis = TimeUnit.HOURS.toMillis(hours);
  313. long minutes = TimeUnit.MILLISECONDS.toMinutes(mTimeLeftInMillis - daysMillis - hoursMillis);
  314. long minutesMillis = TimeUnit.MINUTES.toMillis(minutes);
  315. long seconds = TimeUnit.MILLISECONDS.toSeconds(mTimeLeftInMillis - daysMillis - hoursMillis - minutesMillis);
  316. String resultString = days + "d:" + hours + "h:" + minutes + "m:" + seconds + "s";
  317. mTextViewCountDown.setText(resultString);
  318. }
  319. private void setTime(long milliseconds) {
  320. saved_ads_time = mTimeLeftInMillis + milliseconds;
  321. mTimeLeftInMillis = saved_ads_time;
  322. updateCountDownText();
  323. }
  324. private void saveTime(){
  325. SharedPreferences saved_current_time = getSharedPreferences("time", Context.MODE_PRIVATE);
  326. SharedPreferences.Editor time_edit = saved_current_time.edit();
  327. time_edit.putLong("SAVED_TIME", mTimeLeftInMillis);
  328. time_edit.apply();
  329. }
  330. private void resumeTime(){
  331. SharedPreferences time = getSharedPreferences("time", Context.MODE_PRIVATE);
  332. long saved_time = time.getLong("SAVED_TIME", 0);
  333. setTime(saved_time);
  334. // Use this code to continue time if app close accidentally while connected
  335. String state = SkStatus.getLastState();
  336. if (SkStatus.SSH_CONNECTED.equals(state)) {
  337. if (!mTimerRunning){
  338. startTimer();
  339. }
  340. }
  341. mTimerEnabled = true;
  342. }
  343. private void startTimer() {
  344. mEndTime = System.currentTimeMillis() + mTimeLeftInMillis;
  345. mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
  346. @Override
  347. public void onTick(long millisUntilFinished) {
  348. mTimeLeftInMillis = millisUntilFinished;
  349. saveTime();
  350. updateCountDownText();
  351. }
  352. @Override
  353. public void onFinish() {
  354. mTimerRunning = false;
  355. pauseTimer();
  356. saved_ads_time = 0;
  357. // Code for auto stop vpn (sockshtttp)
  358. Intent stopVPN = new Intent(SocksDNSService.TUNNEL_SSH_STOP_SERVICE);
  359. LocalBroadcastManager.getInstance(MainActivity.this)
  360. .sendBroadcast(stopVPN);
  361. Toast.makeText(MainActivity.this,"¡Se acabó el tiempo! Haga clic en Agregar + Hora para renovar el acceso.", Toast.LENGTH_LONG).show();
  362. }
  363. }.start();
  364. mTimerRunning = true;
  365. }
  366. private void btnTimer() {
  367. mBtnCountDown = new CountDownTimer(20000, 1000) {
  368. @Override
  369. public void onTick(long millisUntilFinished) {
  370. mTimeLeftBtn = millisUntilFinished;
  371. mReward.setEnabled(false);
  372. updateBtnText();
  373. }
  374. @Override
  375. public void onFinish() {
  376. mReward.setEnabled(true);
  377. mReward.setText("ADD + TIME");
  378. }
  379. }.start();
  380. }
  381. private void updateBtnText() {
  382. int seconds = (int) (mTimeLeftBtn / 1000) % 60;
  383. String timeLeftFormatted;
  384. if (seconds > 0) {
  385. timeLeftFormatted = String.format(Locale.getDefault(),
  386. "%02d", seconds);
  387. mReward.setText("Refresh in " + timeLeftFormatted);
  388. }
  389. }
  390. //end
  391. //////////////////////////
  392. Add optiones aditionales
  393. timer_layout.setVisibility(View.VISIBLE);
  394. start();
  395. stop();
  396. if (!mTimerEnabled){
  397. resumeTime(); // resume time
  398. }
  399. showInterstitial();
  400. ///////////////////////////
  401. Layout time
  402. <android.support.v7.widget.CardView
  403. android:layout_width="fill_parent"
  404. android:layout_height="wrap_content"
  405. app:cardCornerRadius="2.0dip"
  406. app:cardElevation="2.0dip"
  407. app:contentPadding="10dip"
  408. android:id="@+id/connection_cardView"
  409. android:layout_marginLeft="8dp"
  410. android:layout_marginRight="8dp"
  411. android:layout_marginTop="10dp">
  412. <LinearLayout
  413. android:orientation="horizontal"
  414. android:visibility="gone"
  415. android:layout_width="match_parent"
  416. android:layout_height="wrap_content"
  417. android:gravity="center"
  418. android:layout_marginTop="7dp"
  419. android:id="@+id/timerLayout">
  420. <ImageView
  421. android:src="@drawable/image_1"
  422. android:layout_width="30dp"
  423. android:layout_height="30dp"
  424. android:layout_marginLeft="3dp"/>
  425. <LinearLayout
  426. android:orientation="vertical"
  427. android:layout_width="match_parent"
  428. android:layout_height="wrap_content"
  429. android:layout_margin="5dp"
  430. android:layout_weight="1.0">
  431. <TextView
  432. android:layout_width="wrap_content"
  433. android:layout_height="wrap_content"
  434. android:text="Time Left:"
  435. android:textAppearance="?android:attr/textAppearanceSmall"
  436. android:textStyle="bold"/>
  437. <TextView
  438. android:layout_width="wrap_content"
  439. android:layout_height="wrap_content"
  440. android:text="0d:0h:0m:0s"
  441. android:textSize="13.0sp"
  442. android:textStyle="bold"
  443. android:textAppearance="?android:attr/textAppearanceMedium"
  444. android:id="@+id/tvTimeRemaining"/>
  445. </LinearLayout>
  446. <Button
  447. android:foreground="?android:attr/selectableItemBackground"
  448. android:clickable="true"
  449. android:layout_width="150dp"
  450. android:layout_height="35dp"
  451. android:text="ADD + TIME"
  452. android:id="@+id/btnAddTime"
  453. android:textColor="#FFFFFFFF"
  454. android:background="?colorPrimary"
  455. style="?android:attr/buttonStyleSmall"/>
  456. </LinearLayout>
  457. </android.support.v7.widget.CardView>
  458. ////////////////////////////
  459. Layout banner
  460. <LinearLayout
  461. android:id="@+id/bottomBanner"
  462. android:layout_width="match_parent"
  463. android:layout_height="wrap_content"
  464. android:layout_marginLeft="15dp"
  465. android:layout_marginRight="15dp"
  466. android:orientation="vertical"
  467. android:layout_gravity="center"
  468. android:gravity="center"
  469. android:layout_margin="5dp">
  470. </LinearLayout>

comments powered by Disqus