From 389d80bd76033b852ca04e8f592a638a6bedb5e6 Mon Sep 17 00:00:00 2001 From: fengyuecanzhu <1021300691@qq.com> Date: Sun, 9 May 2021 15:05:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug=E3=80=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/activity/AdSettingActivity.java | 1 + .../ui/activity/BookDetailedActivity.java | 1 + .../myreader/ui/activity/DonateActivity.java | 1 + .../myreader/ui/activity/SplashActivity.java | 1 + .../xyz/fycz/myreader/util/utils/AdUtils.java | 134 ++++++++++++++++++ 5 files changed, 138 insertions(+) create mode 100644 app/src/main/java/xyz/fycz/myreader/util/utils/AdUtils.java diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/AdSettingActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/activity/AdSettingActivity.java index bea7091..b96a176 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/activity/AdSettingActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/AdSettingActivity.java @@ -16,6 +16,7 @@ import xyz.fycz.myreader.ui.dialog.MyAlertDialog; import xyz.fycz.myreader.util.DateHelper; import xyz.fycz.myreader.util.SharedPreUtils; import xyz.fycz.myreader.util.ToastUtils; +import xyz.fycz.myreader.util.utils.AdUtils; import xyz.fycz.myreader.util.utils.FileUtils; /** diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java index 401a063..b0f86a2 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java @@ -64,6 +64,7 @@ import xyz.fycz.myreader.util.ShareUtils; import xyz.fycz.myreader.util.SharedPreUtils; import xyz.fycz.myreader.util.StringHelper; import xyz.fycz.myreader.util.ToastUtils; +import xyz.fycz.myreader.util.utils.AdUtils; import xyz.fycz.myreader.util.utils.BitmapUtil; import xyz.fycz.myreader.util.utils.BlurTransformation; import xyz.fycz.myreader.util.utils.FileUtils; diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/DonateActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/activity/DonateActivity.java index d5dd479..b80ad1d 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/activity/DonateActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/DonateActivity.java @@ -19,6 +19,7 @@ import xyz.fycz.myreader.base.observer.MySingleObserver; import xyz.fycz.myreader.common.URLCONST; import xyz.fycz.myreader.databinding.ActivityDonateBinding; import xyz.fycz.myreader.util.ToastUtils; +import xyz.fycz.myreader.util.utils.AdUtils; /** * @author fengyue diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/SplashActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/activity/SplashActivity.java index a929908..4bcc397 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/activity/SplashActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/SplashActivity.java @@ -36,6 +36,7 @@ import xyz.fycz.myreader.util.IOUtils; import xyz.fycz.myreader.util.PermissionsChecker; import xyz.fycz.myreader.util.SharedPreUtils; import xyz.fycz.myreader.util.ToastUtils; +import xyz.fycz.myreader.util.utils.AdUtils; import xyz.fycz.myreader.util.utils.ImageLoader; import xyz.fycz.myreader.util.utils.MD5Utils; import xyz.fycz.myreader.util.utils.OkHttpUtils; diff --git a/app/src/main/java/xyz/fycz/myreader/util/utils/AdUtils.java b/app/src/main/java/xyz/fycz/myreader/util/utils/AdUtils.java new file mode 100644 index 0000000..b3da89e --- /dev/null +++ b/app/src/main/java/xyz/fycz/myreader/util/utils/AdUtils.java @@ -0,0 +1,134 @@ +package xyz.fycz.myreader.util.utils; + +import android.util.Log; + + +import com.weaction.ddsdk.base.DdSdkHelper; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import io.reactivex.Single; +import io.reactivex.SingleEmitter; +import io.reactivex.SingleOnSubscribe; +import io.reactivex.annotations.NonNull; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import xyz.fycz.myreader.application.App; +import xyz.fycz.myreader.base.observer.MySingleObserver; +import xyz.fycz.myreader.common.URLCONST; +import xyz.fycz.myreader.model.backup.UserService; +import xyz.fycz.myreader.util.DateHelper; +import xyz.fycz.myreader.util.SharedPreUtils; + +/** + * @author fengyue + * @date 2021/4/22 19:00 + */ +public class AdUtils { + public static final String TAG = AdUtils.class.getSimpleName(); + private static boolean hasInitAd = false; + + public static Single checkHasAd() { + return Single.create((SingleOnSubscribe) emitter -> { + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); + String body = "type=hasAd" + UserService.makeSignalParam(); + RequestBody requestBody = RequestBody.create(mediaType, body); + String jsonStr = OkHttpUtils.getHtml(URLCONST.AD_URL, requestBody, "UTF-8"); + boolean hasAd = false; + try { + JSONObject jsonObject = new JSONObject(jsonStr); + int code = jsonObject.getInt("code"); + if (code > 200) { + Log.e(TAG, "checkHasAd-->errorCode:" + code); + if (code == 213) { + hasAd = true; + } + } else { + hasAd = jsonObject.getBoolean("result"); + } + Log.i(TAG, "hasAd:" + hasAd); + } catch (JSONException e) { + e.printStackTrace(); + } + emitter.onSuccess(hasAd); + }).compose(RxUtils::toSimpleSingle); + } + + public static void adRecord(String type, String name) { + Single.create((SingleOnSubscribe) emitter -> { + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); + String body = "adType=" + type + "&type=" + name + UserService.makeSignalParam(); + RequestBody requestBody = RequestBody.create(mediaType, body); + OkHttpUtils.getHtml(URLCONST.AD_URL, requestBody, "UTF-8"); + emitter.onSuccess(true); + }).compose(RxUtils::toSimpleSingle).subscribe(new MySingleObserver() { + @Override + public void onSuccess(@NonNull Boolean aBoolean) { + Log.i(TAG, name + "上报成功"); + } + + @Override + public void onError(Throwable e) { + Log.e(TAG, name + "上报失败\n" + e.getLocalizedMessage()); + } + }); + } + + public static Single adTimes() { + return Single.create((SingleOnSubscribe) emitter -> { + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); + String body = "type=adTimes" + UserService.makeSignalParam(); + RequestBody requestBody = RequestBody.create(mediaType, body); + String jsonStr = OkHttpUtils.getHtml(URLCONST.AD_URL, requestBody, "UTF-8"); + JSONObject jsonObject = new JSONObject(jsonStr); + int[] adTimes = new int[]{-1, 3, 5}; + try { + int code = jsonObject.getInt("code"); + JSONArray adTimesArr = jsonObject.getJSONArray("result"); + Log.i(TAG, "adTimesArr:" + adTimesArr.toString()); + if (code > 200) { + Log.e(TAG, "adTimes-->errorCode:" + code); + if (code == 213) { + adTimes = new int[]{-1}; + } + } else { + adTimes = new int[adTimesArr.length()]; + for (int i = 0; i < adTimesArr.length(); i++) { + adTimes[i] = adTimesArr.getInt(i); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + emitter.onSuccess(adTimes); + }).compose(RxUtils::toSimpleSingle); + } + + public static boolean checkTodayShowAd() { + SharedPreUtils spu = SharedPreUtils.getInstance(); + String splashAdCount = spu.getString("splashAdCount"); + boolean bookDetailAd = spu.getBoolean("bookDetailAd", true); + int adTimes = spu.getInt("curAdTimes", 3); + String[] splashAdCounts = splashAdCount.split(":"); + String today = DateHelper.getYearMonthDay1(); + int todayAdCount; + if (today.equals(splashAdCounts[0])) { + todayAdCount = Integer.parseInt(splashAdCounts[1]); + } else { + todayAdCount = 0; + } + return adTimes < 0 || todayAdCount < adTimes || bookDetailAd; + } + + public static void initAd() { + if (!hasInitAd) { + hasInitAd = true; + DdSdkHelper.init("1234", "216", "51716a16fbdf50905704b6575b1b3b60", + "142364", "35ce0efe5f3cc960b116db227498e238", + "8167", "85bd159309c3da1b", + App.getApplication(), true); + } + } +}