diff --git a/.idea/misc.xml b/.idea/misc.xml index 997510f..5f24a5e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -15,6 +15,7 @@ + @@ -28,6 +29,7 @@ + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index de41b74..2b2234d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -130,6 +130,7 @@ + diff --git a/app/src/main/java/xyz/fycz/myreader/application/App.java b/app/src/main/java/xyz/fycz/myreader/application/App.java index beeb52f..62ee7ef 100644 --- a/app/src/main/java/xyz/fycz/myreader/application/App.java +++ b/app/src/main/java/xyz/fycz/myreader/application/App.java @@ -88,8 +88,7 @@ public class App extends Application { initNightTheme(); // LLog.init(APPCONST.LOG_DIR); initDialogX(); - if (!debug) - AdUtils.initAd(); + AdUtils.initAd(); } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/RemoveAdActivity.kt b/app/src/main/java/xyz/fycz/myreader/ui/activity/RemoveAdActivity.kt new file mode 100644 index 0000000..49832f8 --- /dev/null +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/RemoveAdActivity.kt @@ -0,0 +1,81 @@ +package xyz.fycz.myreader.ui.activity + +import android.os.Bundle +import android.text.TextUtils +import androidx.appcompat.widget.Toolbar +import xyz.fycz.myreader.R +import xyz.fycz.myreader.application.App +import xyz.fycz.myreader.base.BaseActivity +import xyz.fycz.myreader.base.adapter2.onClick +import xyz.fycz.myreader.databinding.ActivityRemoveAdBinding +import xyz.fycz.myreader.util.help.RelativeDateHelp +import xyz.fycz.myreader.util.utils.AdUtils + +/** + * @author fengyue + * @date 2022/3/3 17:04 + */ +class RemoveAdActivity : BaseActivity() { + private lateinit var binding: ActivityRemoveAdBinding + private var rewardLastTime = 0L + + override fun bindView() { + binding = ActivityRemoveAdBinding.inflate(layoutInflater) + setContentView(binding.root) + } + + override fun setUpToolbar(toolbar: Toolbar?) { + super.setUpToolbar(toolbar) + setStatusBarColor(R.color.colorPrimary, true) + supportActionBar?.title = getString(R.string.remove_ad) + } + + override fun initData(savedInstanceState: Bundle?) { + super.initData(savedInstanceState) + initRewardTime() + } + + fun initRewardTime() { + kotlin.runCatching { + val rewardTime = AdUtils.getSp().getString("rewardLastTime") + if (!TextUtils.isEmpty(rewardTime)) { + rewardLastTime = AdUtils.SDF.parse(rewardTime).time + } + } + } + + override fun initWidget() { + super.initWidget() + countRewardTime() + binding.tvTip.text = getString( + R.string.remove_ad_tip, + AdUtils.getAdConfig().removeAdTime, + AdUtils.getAdConfig().totalRemove, + AdUtils.getAdConfig().maxRemove + ) + } + + private fun countRewardTime() { + val dur = rewardLastTime - System.currentTimeMillis() + if (dur > 0) { + binding.tvCurRemoveAdTime.text = getString( + R.string.cur_remove_ad_time, + RelativeDateHelp.formatDuring(dur) + ) + App.getHandler().postDelayed({ countRewardTime() }, 1000) + } else { + binding.tvCurRemoveAdTime.text = getString(R.string.cur_remove_ad_time, "无记录") + } + } + + override fun initClick() { + super.initClick() + binding.rlRewardVideo.onClick { + AdUtils.showRewardVideoAd(this) { + AdUtils.removeAdReward() + initRewardTime() + countRewardTime() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/fycz/myreader/ui/fragment/MineFragment.java b/app/src/main/java/xyz/fycz/myreader/ui/fragment/MineFragment.java index 6f867bb..0c88d40 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/fragment/MineFragment.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/fragment/MineFragment.java @@ -57,6 +57,7 @@ import xyz.fycz.myreader.ui.activity.LoginActivity; import xyz.fycz.myreader.ui.activity.MainActivity; import xyz.fycz.myreader.ui.activity.MoreSettingActivity; import xyz.fycz.myreader.ui.activity.ReadRecordActivity; +import xyz.fycz.myreader.ui.activity.RemoveAdActivity; import xyz.fycz.myreader.ui.dialog.DialogCreator; import xyz.fycz.myreader.ui.dialog.LoadingDialog; import xyz.fycz.myreader.util.SharedPreUtils; @@ -408,7 +409,7 @@ public class MineFragment extends BaseFragment { }).setCancelButton(R.string.cancel); }); - binding.mineRlAdSetting.setOnClickListener(v -> startActivity(new Intent(getActivity(), AdSettingActivity.class))); + binding.mineRlRemoveAd.setOnClickListener(v -> startActivity(new Intent(getActivity(), RemoveAdActivity.class))); binding.mineRlAbout.setOnClickListener(v -> { Intent aboutIntent = new Intent(getActivity(), AboutActivity.class); 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 index 4cc4888..ba45616 100644 --- a/app/src/main/java/xyz/fycz/myreader/util/utils/AdUtils.java +++ b/app/src/main/java/xyz/fycz/myreader/util/utils/AdUtils.java @@ -1,6 +1,7 @@ package xyz.fycz.myreader.util.utils; import android.app.Activity; +import android.text.TextUtils; import android.util.Log; import android.view.View; @@ -14,6 +15,11 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Locale; + import io.reactivex.Single; import io.reactivex.SingleOnSubscribe; import io.reactivex.annotations.NonNull; @@ -26,6 +32,7 @@ import xyz.fycz.myreader.entity.ad.AdBean; import xyz.fycz.myreader.entity.ad.AdConfig; import xyz.fycz.myreader.model.user.UserService; import xyz.fycz.myreader.util.SharedPreUtils; +import xyz.fycz.myreader.util.ToastUtils; import xyz.fycz.myreader.util.help.DateHelper; /** @@ -36,6 +43,7 @@ public class AdUtils { public static final String TAG = AdUtils.class.getSimpleName(); private static boolean hasInitAd = false; private static AdConfig adConfig; + public static final DateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); static { String config = getSp().getString("adConfig"); @@ -51,6 +59,7 @@ public class AdUtils { } public static Single checkHasAd() { + if (hasRemoveAdReward()) return Single.just(false); initAd(); return Single.create((SingleOnSubscribe) emitter -> { boolean hasAd = false; @@ -175,7 +184,7 @@ public class AdUtils { return currentTime - adConfigTime >= adConfig.getExpireTime() * 60L * 1000; } - public static boolean adTime(String adTag, AdBean adBean){ + public static boolean adTime(String adTag, AdBean adBean) { if (adBean.getStatus() == 0) return false; long adTime = getSp().getLong(adTag + "Time"); long currentTime = System.currentTimeMillis(); @@ -293,6 +302,64 @@ public class AdUtils { } } + public static void removeAdReward() { + if (canAddReward()) { + try { + String rewardTime = getSp().getString("rewardLastTime"); + long current = System.currentTimeMillis(); + long rewardLastTime = System.currentTimeMillis(); + if (!TextUtils.isEmpty(rewardTime)) { + rewardLastTime = SDF.parse(rewardTime).getTime(); + } + if (rewardLastTime < current) rewardLastTime = current; + rewardLastTime += adConfig.getRemoveAdTime() * 60L * 60 * 1000; + getSp().putString("rewardLastTime", SDF.format(rewardLastTime)); + rewardCountPlus(); + } catch (ParseException e) { + e.printStackTrace(); + ToastUtils.showError("" + e.getLocalizedMessage()); + } + ToastUtils.showSuccess("奖励获取成功"); + } else { + ToastUtils.showWarring("已达到单日最大获得奖励次数"); + } + } + + private static void rewardCountPlus() { + String today = DateHelper.getYearMonthDay1(); + String[] rewardCount = getSp().getString("rewardCount").split(":"); + int count; + if (today.equals(rewardCount[0])) { + count = Integer.parseInt(rewardCount[1]); + } else { + count = 0; + } + count++; + getSp().putString("rewardCount", today + ":" + count); + } + + public static boolean canAddReward() { + String today = DateHelper.getYearMonthDay1(); + String[] rewardCount = getSp().getString("rewardCount").split(":"); + if (today.equals(rewardCount[0]) && rewardCount.length > 1) { + return Integer.parseInt(rewardCount[1]) < adConfig.getMaxRemove(); + } + return true; + } + + public static boolean hasRemoveAdReward() { + String rewardTime = getSp().getString("rewardLastTime"); + long rewardLastTime = 0; + try { + if (!TextUtils.isEmpty(rewardTime)) { + rewardLastTime = SDF.parse(rewardTime).getTime(); + } + } catch (ParseException e) { + e.printStackTrace(); + } + return rewardLastTime > System.currentTimeMillis(); + } + public static AdConfig getAdConfig() { return adConfig; } diff --git a/app/src/main/res/layout/activity_remove_ad.xml b/app/src/main/res/layout/activity_remove_ad.xml new file mode 100644 index 0000000..7ac2060 --- /dev/null +++ b/app/src/main/res/layout/activity_remove_ad.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_mine.xml b/app/src/main/res/layout/fragment_mine.xml index 89797bb..f275df7 100644 --- a/app/src/main/res/layout/fragment_mine.xml +++ b/app/src/main/res/layout/fragment_mine.xml @@ -368,8 +368,7 @@ android:textSize="@dimen/text_normal_size" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0fc05c1..1077a74 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -465,6 +465,10 @@ 广告设置 开屏广告 每日广告显示次数 + 免广告奖励 + 去除广告 + 当前免除广告时长:%s + 去除广告规则\n1、观看激励视频即可去除应用内所有广告(包括开屏)\n2、每次观看完成可获得%d小时的免广告时间,最多可累计%d小时\n3、每日最多有%d次获得免广告时长的机会 当前次数:%s,今日已显示次数:%s 广告总开关 详情页广告 @@ -479,6 +483,7 @@ 点击下列广告即可支持作者,建议在WIFI环境下点击 信息流广告(下载的文件可在广告设置中清除) 激励视频 + 观看激励视频 广告下载/缓存 清除广告文件 封面地址