|
|
@@ -19,6 +19,7 @@ import androidx.activity.result.contract.ActivityResultContract;
|
|
|
import androidx.activity.result.contract.ActivityResultContracts;
|
|
|
import androidx.annotation.NonNull;
|
|
|
import androidx.annotation.Nullable;
|
|
|
+import androidx.appcompat.app.AlertDialog;
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
import androidx.viewpager2.widget.ViewPager2;
|
|
|
@@ -42,6 +43,7 @@ import java.util.List;
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
private ActivityMainBinding binding;
|
|
|
private HomeViewModel viewModel;
|
|
|
+ private AlertDialog permDialog;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
@@ -84,36 +86,40 @@ public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
AdRequest adRequest = new AdRequest.Builder().build();
|
|
|
|
|
|
- InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
|
|
|
- @Override
|
|
|
- public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
|
|
|
- // The mInterstitialAd reference will be null until
|
|
|
- // an ad is loaded.
|
|
|
- Log.i("ad", "onAdLoaded");
|
|
|
- interstitialAd.show(MainActivity.this);
|
|
|
- }
|
|
|
+ if (hasAppOpsPermission()) {
|
|
|
+ InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
|
|
|
+ @Override
|
|
|
+ public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
|
|
|
+ // The mInterstitialAd reference will be null until
|
|
|
+ // an ad is loaded.
|
|
|
+ Log.i("ad", "onAdLoaded");
|
|
|
+ interstitialAd.show(MainActivity.this);
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
|
|
|
- // Handle the error
|
|
|
- Log.i("ad", loadAdError.getMessage());
|
|
|
- }
|
|
|
- });
|
|
|
+ @Override
|
|
|
+ public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
|
|
|
+ // Handle the error
|
|
|
+ Log.i("ad", loadAdError.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void onResume() {
|
|
|
super.onResume();
|
|
|
if (!hasAppOpsPermission()) {
|
|
|
- new MaterialAlertDialogBuilder(this)
|
|
|
- .setMessage("need app usage permission")
|
|
|
- .setPositiveButton("open settings", (dialog, which) -> {
|
|
|
- dialog.dismiss();
|
|
|
- dialog.cancel();
|
|
|
- startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
|
|
|
- })
|
|
|
- .setCancelable(false)
|
|
|
- .show();
|
|
|
+ if (permDialog == null || !permDialog.isShowing()) {
|
|
|
+ permDialog = new MaterialAlertDialogBuilder(this)
|
|
|
+ .setMessage(R.string.need_usage_access)
|
|
|
+ .setPositiveButton(R.string.open_settings, (dialog, which) -> {
|
|
|
+ dialog.dismiss();
|
|
|
+ dialog.cancel();
|
|
|
+ startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
|
|
|
+ })
|
|
|
+ .setCancelable(false)
|
|
|
+ .show();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|