From ef0350396c18f88bb25079515a83a663bdda14a9 Mon Sep 17 00:00:00 2001 From: Martin Zeitler Date: Tue, 2 Apr 2024 02:37:55 +0200 Subject: [PATCH 1/3] Targeting SDK 34, migrated to version-catalog, few minor fixes. --- .github/workflows/android.yml | 12 +- .gitignore | 14 +- README.md | 2 +- app/build.gradle | 48 +++--- .../hms/ads/sdk/ExampleInstrumentedTest.java | 1 - app/src/main/AndroidManifest.xml | 46 +++--- .../java/com/huawei/hms/ads/sdk/AdFormat.java | 17 +-- .../huawei/hms/ads/sdk/AdSampleAdapter.java | 11 +- .../com/huawei/hms/ads/sdk/AdsConstant.java | 22 +-- .../huawei/hms/ads/sdk/BannerActivity.java | 55 ++----- .../com/huawei/hms/ads/sdk/BaseActivity.java | 5 +- .../huawei/hms/ads/sdk/ConsentActivity.java | 7 +- .../huawei/hms/ads/sdk/InstreamActivity.java | 139 ++++++++---------- .../hms/ads/sdk/InterstitialActivity.java | 13 +- .../com/huawei/hms/ads/sdk/MainActivity.java | 87 +++++------ .../huawei/hms/ads/sdk/NativeActivity.java | 7 +- .../huawei/hms/ads/sdk/NativeViewFactory.java | 29 ++-- .../huawei/hms/ads/sdk/RewardActivity.java | 32 +--- .../huawei/hms/ads/sdk/SplashActivity.java | 28 +--- .../hms/ads/sdk/dialogs/ConsentDialog.java | 15 +- .../hms/ads/sdk/dialogs/ProtocolDialog.java | 4 +- build.gradle | 25 +--- gradle.properties | 2 +- gradle/libs.versions.toml | 31 ++++ gradle/wrapper/gradle-wrapper.properties | 2 +- settings.gradle | 23 ++- 26 files changed, 292 insertions(+), 385 deletions(-) create mode 100644 gradle/libs.versions.toml diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index a4734a9..b0c5551 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -9,11 +9,15 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - name: set up JDK 1.8 - uses: actions/setup-java@v1 + # https://github.com/actions/setup-java + - name: Set up AWS Corretto JDK 17 + uses: actions/setup-java@v4 with: - java-version: 1.8 + distribution: corretto + java-version: '17' + architecture: x64 + - name: Get latest code + uses: actions/checkout@v4 - name: Make Gradle executable run: chmod +x ./gradlew - name: Build with Gradle diff --git a/.gitignore b/.gitignore index b03fe7b..4b20f41 100644 --- a/.gitignore +++ b/.gitignore @@ -37,17 +37,7 @@ captures/ # IntelliJ *.iml -.idea/workspace.xml -.idea/tasks.xml -.idea/gradle.xml -.idea/assetWizardSettings.xml -.idea/dictionaries -.idea/libraries -# Android Studio 3 in .gitignore file. -.idea/caches -.idea/modules.xml -# Comment next line if keeping position of elements in Navigation Editor is relevant for you -.idea/navEditor.xml +/.idea/ # Keystore files # Uncomment the following lines if you do not want to check your keystore files in. @@ -81,5 +71,3 @@ lint/intermediates/ lint/generated/ lint/outputs/ lint/tmp/ -# lint/reports/ - diff --git a/README.md b/README.md index 999d883..67174ca 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Used to obtain users' consent on the privacy statement. ## Running Result Banner ads         Interstitial ads        Native ads - + Reward ads         Roll ads        Splash ads diff --git a/app/build.gradle b/app/build.gradle index 71dcb64..40b72c4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,16 +1,25 @@ -apply plugin: 'com.android.application' +plugins { + alias(libs.plugins.android.application) +} android { - compileSdkVersion 29 - buildToolsVersion "29.0.3" + namespace "com.huawei.hms.ads.sdk" + buildToolsVersion = "34.0.0" + compileSdk 34 defaultConfig { applicationId "com.huawei.hms.ads.sdk" - minSdkVersion 19 - targetSdkVersion 29 + minSdk 19 + targetSdk 34 versionCode 1 - versionName "1.0" + versionName "1.0.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + buildTypes { release { minifyEnabled false @@ -19,21 +28,18 @@ android { } } -repositories { - flatDir { - dirs 'libs' - } -} - dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.3.0' - implementation 'androidx.constraintlayout:constraintlayout:2.0.4' - implementation 'com.huawei.hms:ads-lite:13.4.69.300' - implementation 'com.huawei.hms:ads-consent:3.4.69.300' - implementation 'com.huawei.hms:ads-omsdk:1.3.35' + implementation libs.androidx.appcompat + implementation libs.androidx.constraintlayout + implementation libs.huawei.hms.ads.lite + implementation libs.huawei.hms.ads.consent + implementation libs.huawei.hms.ads.omsdk - testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + testImplementation libs.junit + debugImplementation libs.androidx.test.monitor + androidTestImplementation libs.androidx.test.core + androidTestImplementation libs.androidx.test.junit + androidTestImplementation libs.androidx.test.rules + androidTestImplementation libs.androidx.test.runner + androidTestImplementation libs.androidx.espresso.core } diff --git a/app/src/androidTest/java/com/huawei/hms/ads/sdk/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/huawei/hms/ads/sdk/ExampleInstrumentedTest.java index 3c42349..0e00e66 100644 --- a/app/src/androidTest/java/com/huawei/hms/ads/sdk/ExampleInstrumentedTest.java +++ b/app/src/androidTest/java/com/huawei/hms/ads/sdk/ExampleInstrumentedTest.java @@ -21,7 +21,6 @@ public class ExampleInstrumentedTest { public void useAppContext() { // Context of the app under test. Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); - assertEquals("com.huawei.hms.ads.sdk", appContext.getPackageName()); } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bbf0462..4247f8d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ - @@ -10,56 +11,53 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/AppTheme" - android:usesCleartextTraffic="true"> + android:theme="@style/AppTheme"> + android:theme="@android:style/Theme.NoTitleBar.Fullscreen" + android:exported="true"> + - + android:exported="false"/> + + android:exported="false"/> + + android:exported="false"/> + + android:exported="false"/> + + android:exported="false"/> + + android:exported="false"/> + + android:exported="false"/> + + android:exported="false"/> + \ No newline at end of file diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java b/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java index 5bf6a06..6d9a905 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java @@ -18,15 +18,12 @@ public class AdFormat { - /** - * Item name - */ + + /** The items name */ private String title; - /** - * Target activity - */ - private Class targetClass; + /** Target activity's class */ + private Class targetClass; public AdFormat(String title, Class targetClass) { this.title = title; @@ -34,10 +31,10 @@ public AdFormat(String title, Class targetClass) { } public String getTitle() { - return title; + return this.title; } - public Class getTargetClass() { - return targetClass; + public Class getTargetClass() { + return this.targetClass; } } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java b/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java index 873c0c1..c2ac5e6 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java @@ -23,6 +23,8 @@ import android.widget.ArrayAdapter; import android.widget.TextView; +import androidx.annotation.NonNull; + import java.util.List; public class AdSampleAdapter extends ArrayAdapter { @@ -36,12 +38,15 @@ public AdSampleAdapter(Context context, int resource, List formatList) resourceId = resource; } + @NonNull @Override - public View getView(int position, View convertView, ViewGroup parent) { - AdFormat adFormat = getItem(position); + public View getView(int position, View convertView, @NonNull ViewGroup parent) { View view = LayoutInflater.from(mContext).inflate(resourceId, parent, false); TextView title = view.findViewById(android.R.id.text1); - title.setText(adFormat.getTitle()); + AdFormat adFormat = getItem(position); + if (adFormat != null) { + title.setText(adFormat.getTitle()); + } return view; } } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/AdsConstant.java b/app/src/main/java/com/huawei/hms/ads/sdk/AdsConstant.java index 2c7d49c..a318bb6 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/AdsConstant.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/AdsConstant.java @@ -18,28 +18,18 @@ public class AdsConstant { - /** - * SharedPreferences name. - */ + /** SharedPreferences name. */ public static final String SP_NAME = "HuaweiAdsSdkSharedPreferences"; - /** - * The SP key of protocol. - */ + /** The SP key of protocol. */ public static final String SP_PROTOCOL_KEY = "protocol"; - /** - * The SP key of consent. - */ + /** The SP key of consent. */ public static final String SP_CONSENT_KEY = "consent"; - /** - * The SP default value of protocol. - */ + /** The SP default value of protocol. */ public static final int DEFAULT_SP_PROTOCOL_VALUE = 0; - /** - * The SP default value of consent. - */ + /** The SP default value of consent. */ public static final int DEFAULT_SP_CONSENT_VALUE = -1; -} \ No newline at end of file +} diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/BannerActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/BannerActivity.java index a3f1f95..7135ee7 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/BannerActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/BannerActivity.java @@ -18,6 +18,7 @@ import java.util.Locale; +import android.annotation.SuppressLint; import android.graphics.Color; import android.os.Bundle; import android.view.View; @@ -51,7 +52,7 @@ public class BannerActivity extends BaseActivity { /** * Button tapping event listener. */ - private View.OnClickListener buttonListener = new View.OnClickListener() { + private final View.OnClickListener buttonListener = new View.OnClickListener() { @Override public void onClick(View v) { defaultBannerView.setVisibility(View.INVISIBLE); @@ -82,7 +83,7 @@ public void onClick(View v) { /** * Ad listener. */ - private AdListener adListener = new AdListener() { + private final AdListener adListener = new AdListener() { @Override public void onAdLoaded() { // Called when an ad is loaded successfully. @@ -98,7 +99,7 @@ public void onAdFailed(int errorCode) { @Override public void onAdOpened() { // Called when an ad is opened. - showToast(String.format("Ad opened ")); + showToast("Ad opened"); } @Override @@ -120,6 +121,7 @@ public void onAdClosed() { } }; + @SuppressLint("SetTextI18n") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -162,49 +164,20 @@ private void showToast(String message) { private BannerAdSize getBannerAdSize(int checkedId) { BannerAdSize adSize = null; - switch (checkedId) { - case R.id.size_320_50: - adSize = BannerAdSize.BANNER_SIZE_320_50; - break; - case R.id.size_320_100: - adSize = BannerAdSize.BANNER_SIZE_320_100; - break; - case R.id.size_300_250: - adSize = BannerAdSize.BANNER_SIZE_300_250; - break; - case R.id.size_smart: - adSize = BannerAdSize.BANNER_SIZE_SMART; - break; - case R.id.size_360_57: - adSize = BannerAdSize.BANNER_SIZE_360_57; - break; - case R.id.size_360_144: - adSize = BannerAdSize.BANNER_SIZE_360_144; - break; - default: - break; - } + if (checkedId == R.id.size_320_50) {adSize = BannerAdSize.BANNER_SIZE_320_50;} + else if (checkedId == R.id.size_320_100) {adSize = BannerAdSize.BANNER_SIZE_320_100;} + else if (checkedId == R.id.size_300_250) {adSize = BannerAdSize.BANNER_SIZE_300_250;} + else if (checkedId == R.id.size_smart) {adSize = BannerAdSize.BANNER_SIZE_SMART;} + else if (checkedId == R.id.size_360_57) {adSize = BannerAdSize.BANNER_SIZE_360_57;} + else if (checkedId == R.id.size_360_144) {adSize = BannerAdSize.BANNER_SIZE_360_144;} return adSize; } private int getBannerViewBackground(int checkedId) { int color = Color.TRANSPARENT; - switch (checkedId) { - case R.id.color_white: - color = Color.WHITE; - break; - case R.id.color_black: - color = Color.BLACK; - break; - case R.id.color_red: - color = Color.RED; - break; - case R.id.color_transparent: - color = Color.TRANSPARENT; - break; - default: - break; - } + if (checkedId == R.id.color_white) {color = Color.WHITE;} + else if (checkedId == R.id.color_black) {color = Color.BLACK;} + else if (checkedId == R.id.color_red) {color = Color.RED;} return color; } } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/BaseActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/BaseActivity.java index b8375ae..4581699 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/BaseActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/BaseActivity.java @@ -27,9 +27,10 @@ public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - ActionBar actionBar = getSupportActionBar(); - actionBar.setDisplayHomeAsUpEnabled(true); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + } } @Override diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java index ab942db..00cd10f 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java @@ -37,11 +37,8 @@ import java.util.ArrayList; import java.util.List; -import java.util.Locale; -/** - * Activity for displaying consent. - */ +/** Activity for displaying ad-consent settings. */ public class ConsentActivity extends BaseActivity implements ConsentDialog.ConsentDialogCallback { private static final String TAG = ConsentActivity.class.getSimpleName(); @@ -51,7 +48,7 @@ public class ConsentActivity extends BaseActivity implements ConsentDialog.Conse private RequestOptions requestOptions; - private List mAdProviders = new ArrayList(); + private List mAdProviders = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java index 7fe8760..dc5466d 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java @@ -29,6 +29,8 @@ import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; + import com.huawei.hms.ads.AdParam; import com.huawei.hms.ads.MediaMuteListener; import com.huawei.hms.ads.instreamad.InstreamAd; @@ -80,20 +82,17 @@ protected void onCreate(Bundle savedInstanceState) { configAdLoader(); } - private InstreamMediaChangeListener mediaChangeListener = new InstreamMediaChangeListener() { + private final InstreamMediaChangeListener mediaChangeListener = new InstreamMediaChangeListener() { @Override - public void onSegmentMediaChange(InstreamAd instreamAd) { + public void onSegmentMediaChange(@NonNull InstreamAd instreamAd) { whyThisAdUrl = null; whyThisAdUrl = instreamAd.getWhyThisAd(); Log.i(TAG, "onSegmentMediaChange, whyThisAd: " + whyThisAdUrl); if (!TextUtils.isEmpty(whyThisAdUrl)) { whyThisAd.setVisibility(View.VISIBLE); - whyThisAd.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(whyThisAdUrl))); - } - }); + whyThisAd.setOnClickListener(view -> + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(whyThisAdUrl))) + ); } else { whyThisAd.setVisibility(View.GONE); } @@ -107,7 +106,7 @@ public void onClick(View view) { } }; - private InstreamMediaStateListener mediaStateListener = new InstreamMediaStateListener() { + private final InstreamMediaStateListener mediaStateListener = new InstreamMediaStateListener() { @Override public void onMediaProgress(int per, int playTime) { updateCountDown(playTime); @@ -141,7 +140,7 @@ public void onMediaError(int playTime, int errorCode, int extra) { } }; - private MediaMuteListener mediaMuteListener = new MediaMuteListener() { + private final MediaMuteListener mediaMuteListener = new MediaMuteListener() { @Override public void onMute() { isMuted = true; @@ -161,12 +160,7 @@ private void initInstreamAdView() { instreamContainer.addView(instreamView, 0); videoContent = findViewById(R.id.instream_video_content); skipAd = findViewById(R.id.instream_skip); - skipAd.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - removeInstream(); - } - }); + skipAd.setOnClickListener(view -> removeInstream()); countDown = findViewById(R.id.instream_count_down); callToAction = findViewById(R.id.instream_call_to_action); @@ -175,12 +169,9 @@ public void onClick(View v) { instreamView.setInstreamMediaChangeListener(mediaChangeListener); instreamView.setInstreamMediaStateListener(mediaStateListener); instreamView.setMediaMuteListener(mediaMuteListener); - instreamView.setOnInstreamAdClickListener(new InstreamView.OnInstreamAdClickListener() { - @Override - public void onClick() { - Toast.makeText(context, "instream clicked.", Toast.LENGTH_SHORT).show(); - } - }); + instreamView.setOnInstreamAdClickListener(() -> + Toast.makeText(context, "instream clicked.", Toast.LENGTH_SHORT).show() + ); } private void removeInstream() { @@ -193,48 +184,41 @@ private void removeInstream() { } } - private View.OnClickListener clickListener = new View.OnClickListener() { + private final View.OnClickListener clickListener = new View.OnClickListener() { @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.instream_load: - if (instreamView.isPlaying()) { - Toast.makeText(context, getString(R.string.instream_ads_playing), Toast.LENGTH_SHORT).show(); - } else if (null != adLoader) { - initInstreamAdView(); - loadButton.setText(getString(R.string.instream_loading)); - adLoader.loadAd(new AdParam.Builder().build()); - } - break; - case R.id.instream_register: - if (null == instreamAds || instreamAds.size() == 0) { - playVideo(); - } else if (instreamView.isPlaying()) { - Toast.makeText(context, getString(R.string.instream_ads_playing), Toast.LENGTH_SHORT).show(); - } else { - playInstreamAds(instreamAds); - } - break; - case R.id.instream_mute: - if (isMuted) { - instreamView.unmute(); - muteButton.setText(getString(R.string.instream_mute)); - } else { - instreamView.mute(); - muteButton.setText(getString(R.string.instream_unmute)); - } - break; - case R.id.instream_pause_play: - if (instreamView.isPlaying()) { - instreamView.pause(); - pauseButton.setText(getString(R.string.instream_play)); - } else { - instreamView.play(); - pauseButton.setText(getString(R.string.instream_pause)); - } - break; - default: - break; + public void onClick(@NonNull View view) { + if (view.getId() == R.id.instream_load) { + if (instreamView.isPlaying()) { + Toast.makeText(context, getString(R.string.instream_ads_playing), Toast.LENGTH_SHORT).show(); + } else if (null != adLoader) { + initInstreamAdView(); + loadButton.setText(getString(R.string.instream_loading)); + adLoader.loadAd(new AdParam.Builder().build()); + } + } else if (view.getId() == R.id.instream_register) { + if (null == instreamAds || instreamAds.isEmpty()) { + playVideo(); + } else if (instreamView.isPlaying()) { + Toast.makeText(context, getString(R.string.instream_ads_playing), Toast.LENGTH_SHORT).show(); + } else { + playInstreamAds(instreamAds); + } + } else if (view.getId() == R.id.instream_mute) { + if (isMuted) { + instreamView.unmute(); + muteButton.setText(getString(R.string.instream_mute)); + } else { + instreamView.mute(); + muteButton.setText(getString(R.string.instream_unmute)); + } + } else if (view.getId() == R.id.instream_pause_play) { + if (instreamView.isPlaying()) { + instreamView.pause(); + pauseButton.setText(getString(R.string.instream_play)); + } else { + instreamView.play(); + pauseButton.setText(getString(R.string.instream_pause)); + } } } }; @@ -251,10 +235,10 @@ private void initButtons() { pauseButton.setOnClickListener(clickListener); } - private InstreamAdLoadListener instreamAdLoadListener = new InstreamAdLoadListener() { + private final InstreamAdLoadListener instreamAdLoadListener = new InstreamAdLoadListener() { @Override public void onAdLoaded(final List ads) { - if (null == ads || ads.size() == 0) { + if (null == ads || ads.isEmpty()) { playVideo(); return; } @@ -265,7 +249,7 @@ public void onAdLoaded(final List ads) { it.remove(); } } - if (ads.size() == 0) { + if (ads.isEmpty()) { playVideo(); return; } @@ -283,13 +267,13 @@ public void onAdFailed(int errorCode) { } }; + /** + * If the maximum total duration is 60 seconds and the maximum number of roll ads is eight, + * at most four 15-second roll ads or two 30-second roll ads will be returned. + * If the maximum total duration is 120 seconds and the maximum number of roll ads is four, + * no more roll ads will be returned after whichever is reached. + */ private void configAdLoader() { - /** - * if the maximum total duration is 60 seconds and the maximum number of roll ads is eight, - * at most four 15-second roll ads or two 30-second roll ads will be returned. - * If the maximum total duration is 120 seconds and the maximum number of roll ads is four, - * no more roll ads will be returned after whichever is reached. - */ int totalDuration = 60; int maxCount = 4; InstreamAdLoader.Builder builder = new InstreamAdLoader.Builder(context, getString(R.string.instream_ad_id)); @@ -319,18 +303,13 @@ private void playInstreamAds(List ads) { private void updateCountDown(long playTime) { final String time = String.valueOf(Math.round((maxAdDuration - playTime) / 1000)); - runOnUiThread(new Runnable() { - @Override - public void run() { - countDown.setText(time + "s"); - } - }); + runOnUiThread(() -> countDown.setText(time + "s")); } - private int getMaxInstreamDuration(List ads) { + private int getMaxInstreamDuration(@NonNull List ads) { int duration = 0; for (InstreamAd ad : ads) { - duration += ad.getDuration(); + duration += (int) ad.getDuration(); } return duration; } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java index a2237d5..0e063a0 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java @@ -18,7 +18,6 @@ import android.os.Bundle; import android.util.Log; -import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.Toast; @@ -32,11 +31,10 @@ * Activity for displaying an interstitial ad. */ public class InterstitialActivity extends BaseActivity { - private static final String TAG = InterstitialActivity.class.getSimpleName(); + private static final String TAG = InterstitialActivity.class.getSimpleName(); private RadioGroup displayRadioGroup; private Button loadAdButton; - private InterstitialAd interstitialAd; @Override @@ -50,16 +48,11 @@ protected void onCreate(Bundle savedInstanceState) { displayRadioGroup = findViewById(R.id.display_radio_group); loadAdButton = findViewById(R.id.load_ad); - loadAdButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - loadInterstitialAd(); - } - }); + loadAdButton.setOnClickListener(view -> loadInterstitialAd()); } - private AdListener adListener = new AdListener() { + private final AdListener adListener = new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java index bb6fc47..4ccbbde 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java @@ -19,7 +19,6 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.content.res.Configuration; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -27,8 +26,6 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.View; -import android.widget.AdapterView; import android.widget.ListView; import androidx.annotation.NonNull; @@ -58,21 +55,18 @@ public class MainActivity extends AppCompatActivity { private List adFormats = new ArrayList<>(); - private Handler mHandler = new Handler(new Handler.Callback() { - @Override - public boolean handleMessage(@NonNull Message msg) { - if (MainActivity.this.hasWindowFocus()) { - switch (msg.what) { - case PROTOCOL_MSG_TYPE: - showPrivacyDialog(); - break; - case CONSENT_MSG_TYPE: - checkConsentStatus(); - break; - } + private final Handler mHandler = new Handler(msg -> { + if (MainActivity.this.hasWindowFocus()) { + switch (msg.what) { + case PROTOCOL_MSG_TYPE: + showPrivacyDialog(); + break; + case CONSENT_MSG_TYPE: + checkConsentStatus(); + break; } - return false; } + return false; }); @Override @@ -86,10 +80,9 @@ protected void onCreate(Bundle savedInstanceState) { final AdSampleAdapter adapter = new AdSampleAdapter(MainActivity.this, android.R.layout.simple_list_item_1, adFormats); listView.setAdapter(adapter); - listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - AdFormat adFormat = adapter.getItem(position); + listView.setOnItemClickListener((parent, view, position, id) -> { + AdFormat adFormat = adapter.getItem(position); + if (adFormat != null) { Intent intent = new Intent(getApplicationContext(), adFormat.getTargetClass()); startActivity(intent); } @@ -114,26 +107,32 @@ private void showPrivacyDialog() { // If a user does not agree to the service agreement, the service agreement dialog is displayed. if (getPreferences(AdsConstant.SP_PROTOCOL_KEY, AdsConstant.DEFAULT_SP_PROTOCOL_VALUE) == 0) { Log.i(TAG, "Show protocol dialog."); - ProtocolDialog dialog = new ProtocolDialog(this); - dialog.setCallback(new ProtocolDialog.ProtocolDialogCallback() { - @Override - public void agree() { - sendMessage(CONSENT_MSG_TYPE, MSG_DELAY_MS); - } - - @Override - public void cancel() { - // if the user selects the CANCEL button, exit application. - finish(); - } - }); - dialog.setCanceledOnTouchOutside(false); + ProtocolDialog dialog = getProtocolDialog(); dialog.show(); } else { sendMessage(CONSENT_MSG_TYPE, MSG_DELAY_MS); } } + @NonNull + private ProtocolDialog getProtocolDialog() { + ProtocolDialog dialog = new ProtocolDialog(this); + dialog.setCallback(new ProtocolDialog.ProtocolDialogCallback() { + @Override + public void agree() { + sendMessage(CONSENT_MSG_TYPE, MSG_DELAY_MS); + } + + @Override + public void cancel() { + // if the user selects the CANCEL button, exit application. + finish(); + } + }); + dialog.setCanceledOnTouchOutside(false); + return dialog; + } + /** * If a user has not set consent, the consent dialog box is displayed. */ @@ -165,23 +164,15 @@ public boolean onCreateOptionsMenu(Menu menu) { } @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.privacy_settings: - startActivity(new Intent(getApplicationContext(), ProtocolActivity.class)); - break; - case R.id.consent_settings: - startActivity(new Intent(getApplicationContext(), ConsentActivity.class)); - break; + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == R.id.privacy_settings) { + startActivity(new Intent(getApplicationContext(), ProtocolActivity.class)); + } else if (item.getItemId() == R.id.consent_settings) { + startActivity(new Intent(getApplicationContext(), ConsentActivity.class)); } return super.onOptionsItemSelected(item); } - @Override - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); - } - private void checkConsentStatus() { final List adProviderList = new ArrayList<>(); Consent consentInfo = Consent.getInstance(this); @@ -192,7 +183,7 @@ private void checkConsentStatus() { public void onSuccess(ConsentStatus consentStatus, boolean isNeedConsent, List adProviders) { Log.i(TAG, "ConsentStatus: " + consentStatus + ", isNeedConsent: " + isNeedConsent); if (isNeedConsent && consentStatus == ConsentStatus.UNKNOWN) { - if (adProviders != null && adProviders.size() > 0) { + if (adProviders != null && !adProviders.isEmpty()) { adProviderList.addAll(adProviders); } showConsentDialog(adProviderList); diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/NativeActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/NativeActivity.java index 3596e87..73958a0 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/NativeActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/NativeActivity.java @@ -62,12 +62,7 @@ protected void onCreate(Bundle savedInstanceState) { loadBtn = findViewById(R.id.btn_load); adScrollView = findViewById(R.id.scroll_view_ad); - loadBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - loadAd(getAdId()); - } - }); + loadBtn.setOnClickListener(view -> loadAd(getAdId())); loadAd(getAdId()); } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java b/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java index 6bc0f96..aa8c8e4 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/NativeViewFactory.java @@ -10,6 +10,9 @@ import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.core.content.res.ResourcesCompat; + import com.huawei.hms.ads.AppDownloadButton; import com.huawei.hms.ads.AppDownloadButtonStyle; import com.huawei.hms.ads.VideoOperator; @@ -172,27 +175,12 @@ private static void setSixElements(final Context context, View adRootView, final TextView privacyView = adRootView.findViewById(R.id.ad_privacy); TextView permissionView = adRootView.findViewById(R.id.ad_permission); TextView detailView = adRootView.findViewById(R.id.ad_detail); - permissionView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - nativeAd.getAppInfo().showPermissionPage(context); - } - }); - - privacyView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - nativeAd.getAppInfo().showPrivacyPolicy(context); - } - }); - detailView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - nativeAd.showAppDetailPage(context); - } - }); + permissionView.setOnClickListener(view -> nativeAd.getAppInfo().showPermissionPage(context)); + privacyView.setOnClickListener(view -> nativeAd.getAppInfo().showPrivacyPolicy(context)); + detailView.setOnClickListener(view -> nativeAd.showAppDetailPage(context)); } + @NonNull public static View createAppDownloadButtonAdView(NativeAd nativeAd, final ViewGroup parentView) { LayoutInflater inflater = LayoutInflater.from(parentView.getContext()); View adRootView = inflater.inflate(R.layout.native_ad_with_app_download_btn_template, null); @@ -244,6 +232,7 @@ public static View createAppDownloadButtonAdView(NativeAd nativeAd, final ViewGr return nativeView; } + @NonNull public static View createImageOnlyAdView(NativeAd nativeAd, final ViewGroup parentView) { LayoutInflater inflater = LayoutInflater.from(parentView.getContext()); View adRootView = inflater.inflate(R.layout.native_image_only_template, null); @@ -274,7 +263,7 @@ private static class MyAppDownloadStyle extends AppDownloadButtonStyle { public MyAppDownloadStyle(Context context) { super(context); normalStyle.setTextColor(context.getResources().getColor(R.color.white)); - normalStyle.setBackground(context.getResources().getDrawable(R.drawable.native_button_rounded_corners_shape)); + normalStyle.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.native_button_rounded_corners_shape, context.getTheme())); processingStyle.setTextColor(context.getResources().getColor(R.color.black)); } } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java index 84d5a15..74bf5ed 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java @@ -19,7 +19,6 @@ import java.util.Random; import android.os.Bundle; -import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; @@ -30,9 +29,7 @@ import com.huawei.hms.ads.reward.RewardAdLoadListener; import com.huawei.hms.ads.reward.RewardAdStatusListener; -/** - * Activity for displaying a rewarded ad. - */ +/** Activity for displaying a rewarded ad. */ public class RewardActivity extends BaseActivity { private static final int PLUS_SCORE = 1; @@ -138,18 +135,15 @@ public void onRewarded(Reward reward) { } private void showToast(final String text) { - runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(RewardActivity.this, text, Toast.LENGTH_SHORT).show(); - } - }); + runOnUiThread(() -> + Toast.makeText(RewardActivity.this, text, Toast.LENGTH_SHORT).show() + ); } /** * Set a score. * - * @param score + * @param score the score to set. */ private void setScore(int score) { scoreView.setText("Score:" + score); @@ -160,12 +154,7 @@ private void setScore(int score) { */ private void loadWatchButton() { watchAdButton = findViewById(R.id.show_video_button); - watchAdButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - rewardAdShow(); - } - }); + watchAdButton.setOnClickListener(view -> rewardAdShow()); } /** @@ -173,12 +162,7 @@ public void onClick(View v) { */ private void loadPlayButton() { reStartButton = findViewById(R.id.play_button); - reStartButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - play(); - } - }); + reStartButton.setOnClickListener(view -> play()); } private void loadScoreView() { @@ -204,7 +188,7 @@ private void play() { Toast.makeText(RewardActivity.this, "You win!", Toast.LENGTH_SHORT).show(); } else { score -= MINUS_SCORE; - score = score < 0 ? 0 : score; + score = Math.max(score, 0); Toast.makeText(RewardActivity.this, "You lose!", Toast.LENGTH_SHORT).show(); } setScore(score); diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/SplashActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/SplashActivity.java index 11b06f3..64ee52d 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/SplashActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/SplashActivity.java @@ -16,24 +16,23 @@ package com.huawei.hms.ads.sdk; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.os.Bundle; import android.os.Handler; -import android.os.Message; import android.util.Log; import android.widget.Toast; -import androidx.annotation.NonNull; - import com.huawei.hms.ads.AdParam; import com.huawei.hms.ads.AudioFocusType; import com.huawei.hms.ads.splash.SplashAdDisplayListener; import com.huawei.hms.ads.splash.SplashView; +@SuppressLint("CustomSplashScreen") public class SplashActivity extends Activity { private static final String TAG = SplashActivity.class.getSimpleName(); @@ -52,19 +51,14 @@ public class SplashActivity extends Activity { private boolean hasPaused = false; // Callback handler used when the ad display timeout message is received. - private Handler timeoutHandler = new Handler(new Handler.Callback() { - @Override - public boolean handleMessage(@NonNull Message msg) { - if (SplashActivity.this.hasWindowFocus()) { - jump(); - } - return false; - } + private final Handler timeoutHandler = new Handler(msg -> { + if (SplashActivity.this.hasWindowFocus()) {jump();} + return false; }); private SplashView splashView; - private SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() { + private final SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() { @Override public void onAdLoaded() { // Call this method when an ad is successfully loaded. @@ -89,7 +83,7 @@ public void onAdDismissed() { } }; - private SplashAdDisplayListener adDisplayListener = new SplashAdDisplayListener() { + private final SplashAdDisplayListener adDisplayListener = new SplashAdDisplayListener() { @Override public void onAdShowed() { // Call this method when an ad is displayed. @@ -107,7 +101,6 @@ public void onAdClick() { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); - loadAd(); } @@ -168,12 +161,7 @@ private void jump() { startActivity(new Intent(SplashActivity.this, MainActivity.class)); Handler mainHandler = new Handler(); - mainHandler.postDelayed(new Runnable() { - @Override - public void run() { - finish(); - } - }, 1000); + mainHandler.postDelayed(this::finish, 1000); } } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java index 4f431e6..016b2ed 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java @@ -174,23 +174,20 @@ private void showInitConsentInfo() { * * @param rootView rootView */ - private void addInitButtonAndLinkClick(View rootView) { + private void addInitButtonAndLinkClick(@NonNull View rootView) { consentYesBtn = rootView.findViewById(R.id.btn_consent_init_yes); consentYesBtn.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(View v) { + public void onClick(View view) { dismiss(); updateConsentStatus(ConsentStatus.PERSONALIZED); } }); consentNoBtn = rootView.findViewById(R.id.btn_consent_init_skip); - consentNoBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dismiss(); - updateConsentStatus(ConsentStatus.NON_PERSONALIZED); - } + consentNoBtn.setOnClickListener(view -> { + dismiss(); + updateConsentStatus(ConsentStatus.NON_PERSONALIZED); }); initInfoTv = rootView.findViewById(R.id.consent_center_init_content); @@ -201,7 +198,7 @@ public void onClick(View v) { // Set the listener on the event for tapping some text. ClickableSpan initTouchHere = new ClickableSpan() { @Override - public void onClick(View widget) { + public void onClick(@NonNull View widget) { showTouchHereInfo(); } }; diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java index caf62f8..da4b18d 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java @@ -47,9 +47,7 @@ import java.util.List; -/** - * Control on privacy-related dialog boxes. - */ +/** Control on privacy-related dialog boxes. */ public class ProtocolDialog extends Dialog { private static final String ACTION_SIMPLE_PRIVACY = "com.huawei.hms.ppskit.ACTION.SIMPLE_PRIVACY"; diff --git a/build.gradle b/build.gradle index 1810634..99ec934 100644 --- a/build.gradle +++ b/build.gradle @@ -1,27 +1,10 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. -buildscript { - repositories { - google() - jcenter() - maven { url 'https://developer.huawei.com/repo/' } - } - dependencies { - classpath 'com.android.tools.build:gradle:4.1.1' - - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files - } +plugins { + id "com.android.application" version "8.3.1" apply false } -allprojects { - repositories { - google() - jcenter() - maven { url 'https://developer.huawei.com/repo/' } - } -} -task clean(type: Delete) { - delete rootProject.buildDir +tasks.register('clean', Delete) { + delete rootProject.fileTree("build") } diff --git a/gradle.properties b/gradle.properties index 199d16e..56be180 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,5 +16,5 @@ org.gradle.jvmargs=-Xmx1536m # https://developer.android.com/topic/libraries/support-library/androidx-rn android.useAndroidX=true # Automatically convert third-party libraries to use AndroidX -android.enableJetifier=true +android.enableJetifier=false diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..e309b70 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,31 @@ +[versions] +android_gradle_plugin = '8.3.1' +androidx_appcompat = '1.6.1' +androidx_constraintlayout = '2.1.4' +androidx_test_core = '1.5.0' +androidx_test_junit = '1.1.5' +androidx_test_rules = '1.5.0' +androidx_test_runner = '1.5.2' +androidx_test_monitor = '1.6.1' +androidx_test_espresso = "3.5.1" +huawei_hms_ads_consent = '3.4.69.302' +huawei_hms_ads_lite = '13.4.69.302' +huawei_hms_ads_omsdk = '1.3.35' +junit = '4.13.2' + +[plugins] +android_application = { id = "com.android.application", version.ref = "android_gradle_plugin" } + +[libraries] +androidx_appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx_appcompat" } +androidx_constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "androidx_constraintlayout" } +androidx_test_core = { module = "androidx.test:core", version.ref = "androidx_test_core" } +androidx_test_monitor = { module = "androidx.test:monitor", version.ref = "androidx_test_monitor" } +androidx_test_junit = { module = "androidx.test.ext:junit", version.ref = "androidx_test_junit" } +androidx_test_rules = { module = "androidx.test:rules", version.ref = "androidx_test_rules" } +androidx_test_runner = { module = "androidx.test:runner", version.ref = "androidx_test_runner" } +androidx_espresso_core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx_test_espresso" } +huawei_hms_ads_consent = { module = "com.huawei.hms:ads-consent", version.ref = "huawei_hms_ads_consent" } +huawei_hms_ads_lite = { module = "com.huawei.hms:ads-lite", version.ref = "huawei_hms_ads_lite" } +huawei_hms_ads_omsdk = { module = "com.huawei.hms:ads-omsdk", version.ref = "huawei_hms_ads_omsdk" } +junit = { module = "junit:junit", version.ref = "junit" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8221527..8a80371 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip diff --git a/settings.gradle b/settings.gradle index 33a10e5..62babef 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,23 @@ -include ':app' +import org.gradle.api.initialization.resolve.RepositoriesMode + +pluginManagement { + repositories { + gradlePluginPortal() + google() + mavenCentral() + } +} + +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) + repositories { + google() + mavenCentral() + maven { url "https://developer.huawei.com/repo/" } + flatDir { dirs 'libs' } + } +} + rootProject.name='HuaweiAdsSdk_Demo' + +include ':app' From b17521f5b48138d9f6aa46158e63b90034d8eb38 Mon Sep 17 00:00:00 2001 From: Martin Zeitler Date: Tue, 2 Apr 2024 03:15:36 +0200 Subject: [PATCH 2/3] Targeting SDK 34, migrated to version-catalog, few minor fixes. --- .../java/com/huawei/hms/ads/sdk/AdFormat.java | 27 ++---- .../huawei/hms/ads/sdk/AdSampleAdapter.java | 6 +- .../huawei/hms/ads/sdk/ConsentActivity.java | 7 +- .../huawei/hms/ads/sdk/InstreamActivity.java | 11 +-- .../hms/ads/sdk/InterstitialActivity.java | 3 +- .../com/huawei/hms/ads/sdk/MainActivity.java | 14 ++-- .../huawei/hms/ads/sdk/RewardActivity.java | 12 +-- .../hms/ads/sdk/dialogs/ConsentDialog.java | 83 ++++++------------- .../hms/ads/sdk/dialogs/ProtocolDialog.java | 28 +++---- build.gradle | 3 +- 10 files changed, 63 insertions(+), 131 deletions(-) diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java b/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java index 6d9a905..3f12018 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java @@ -16,25 +16,8 @@ package com.huawei.hms.ads.sdk; - -public class AdFormat { - - /** The items name */ - private String title; - - /** Target activity's class */ - private Class targetClass; - - public AdFormat(String title, Class targetClass) { - this.title = title; - this.targetClass = targetClass; - } - - public String getTitle() { - return this.title; - } - - public Class getTargetClass() { - return this.targetClass; - } -} +/** + * @param title The items name + * @param targetClass Target activity's class + */ +public record AdFormat(String title, Class targetClass) {} diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java b/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java index c2ac5e6..cd6d32a 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java @@ -28,9 +28,9 @@ import java.util.List; public class AdSampleAdapter extends ArrayAdapter { - private Context mContext; + private final Context mContext; - private int resourceId; + private final int resourceId; public AdSampleAdapter(Context context, int resource, List formatList) { super(context, resource, formatList); @@ -45,7 +45,7 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) { TextView title = view.findViewById(android.R.id.text1); AdFormat adFormat = getItem(position); if (adFormat != null) { - title.setText(adFormat.getTitle()); + title.setText(adFormat.title()); } return view; } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java index 00cd10f..1db18cf 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java @@ -46,9 +46,7 @@ public class ConsentActivity extends BaseActivity implements ConsentDialog.Conse private TextView adTypeTv; - private RequestOptions requestOptions; - - private List mAdProviders = new ArrayList<>(); + private final List mAdProviders = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -135,6 +133,7 @@ private void loadBannerAd(int consentStatus) { } // Obtain global ad singleton variables and add personalized ad request parameters. + RequestOptions requestOptions; if (HwAds.getRequestOptions() == null) { requestOptions = new RequestOptions(); } else { @@ -156,7 +155,7 @@ private void loadBannerAd(int consentStatus) { updateTextViewTips(consentStatus); } - private AdListener adListener = new AdListener() { + private final AdListener adListener = new AdListener() { @Override public void onAdLoaded() { // Called when an ad is loaded successfully. diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java index dc5466d..072b1dd 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/InstreamActivity.java @@ -48,19 +48,14 @@ public class InstreamActivity extends BaseActivity { private static final String TAG = InstreamActivity.class.getSimpleName(); private TextView videoContent; - private TextView skipAd; private TextView countDown; private TextView callToAction; - private Button loadButton; - private Button registerButton; private Button muteButton; private Button pauseButton; - private RelativeLayout instreamContainer; private InstreamView instreamView; private ImageView whyThisAd; - private Context context; private int maxAdDuration; private String whyThisAdUrl; @@ -159,7 +154,7 @@ private void initInstreamAdView() { instreamView = new InstreamView(getApplicationContext()); instreamContainer.addView(instreamView, 0); videoContent = findViewById(R.id.instream_video_content); - skipAd = findViewById(R.id.instream_skip); + TextView skipAd = findViewById(R.id.instream_skip); skipAd.setOnClickListener(view -> removeInstream()); countDown = findViewById(R.id.instream_count_down); @@ -225,7 +220,7 @@ public void onClick(@NonNull View view) { private void initButtons() { loadButton = findViewById(R.id.instream_load); - registerButton = findViewById(R.id.instream_register); + Button registerButton = findViewById(R.id.instream_register); muteButton = findViewById(R.id.instream_mute); pauseButton = findViewById(R.id.instream_pause_play); @@ -302,7 +297,7 @@ private void playInstreamAds(List ads) { } private void updateCountDown(long playTime) { - final String time = String.valueOf(Math.round((maxAdDuration - playTime) / 1000)); + final String time = String.valueOf(Math.round((float) (maxAdDuration - playTime) / 1000)); runOnUiThread(() -> countDown.setText(time + "s")); } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java index 0e063a0..a1dcb8a 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java @@ -34,7 +34,6 @@ public class InterstitialActivity extends BaseActivity { private static final String TAG = InterstitialActivity.class.getSimpleName(); private RadioGroup displayRadioGroup; - private Button loadAdButton; private InterstitialAd interstitialAd; @Override @@ -47,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) { HwAds.init(this); displayRadioGroup = findViewById(R.id.display_radio_group); - loadAdButton = findViewById(R.id.load_ad); + Button loadAdButton = findViewById(R.id.load_ad); loadAdButton.setOnClickListener(view -> loadInterstitialAd()); } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java index 4ccbbde..f96534a 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java @@ -43,17 +43,12 @@ import java.util.List; public class MainActivity extends AppCompatActivity { - private static final String TAG = MainActivity.class.getSimpleName(); + private static final String TAG = MainActivity.class.getSimpleName(); private static final int PROTOCOL_MSG_TYPE = 100; - private static final int CONSENT_MSG_TYPE = 200; - private static final int MSG_DELAY_MS = 1000; - - private ListView listView; - - private List adFormats = new ArrayList<>(); + private final List adFormats = new ArrayList<>(); private final Handler mHandler = new Handler(msg -> { if (MainActivity.this.hasWindowFocus()) { @@ -76,14 +71,14 @@ protected void onCreate(Bundle savedInstanceState) { initAdItems(); - listView = findViewById(R.id.item_list_view); + ListView listView = findViewById(R.id.item_list_view); final AdSampleAdapter adapter = new AdSampleAdapter(MainActivity.this, android.R.layout.simple_list_item_1, adFormats); listView.setAdapter(adapter); listView.setOnItemClickListener((parent, view, position, id) -> { AdFormat adFormat = adapter.getItem(position); if (adFormat != null) { - Intent intent = new Intent(getApplicationContext(), adFormat.getTargetClass()); + Intent intent = new Intent(getApplicationContext(), adFormat.targetClass()); startActivity(intent); } }); @@ -150,6 +145,7 @@ private int getPreferences(String key, int defValue) { return value; } + /** @noinspection SameParameterValue */ private void sendMessage(int what, int delayMillis) { Message msg = Message.obtain(); msg.what = what; diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java b/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java index 74bf5ed..8e47858 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java @@ -37,14 +37,8 @@ public class RewardActivity extends BaseActivity { private static final int RANGE = 2; - private TextView rewardedTitle; - private TextView scoreView; - private Button reStartButton; - - private Button watchAdButton; - private RewardAd rewardedAd; private int score = 1; @@ -57,7 +51,7 @@ protected void onCreate(Bundle savedInstanceState) { setTitle(getString(R.string.reward_ad)); setContentView(R.layout.activity_reward); - rewardedTitle = findViewById(R.id.text_reward); + TextView rewardedTitle = findViewById(R.id.text_reward); rewardedTitle.setText(R.string.reward_ad_title); // Load a rewarded ad. @@ -153,7 +147,7 @@ private void setScore(int score) { * Load the button for watching a rewarded ad. */ private void loadWatchButton() { - watchAdButton = findViewById(R.id.show_video_button); + Button watchAdButton = findViewById(R.id.show_video_button); watchAdButton.setOnClickListener(view -> rewardAdShow()); } @@ -161,7 +155,7 @@ private void loadWatchButton() { * Load the button for starting a game. */ private void loadPlayButton() { - reStartButton = findViewById(R.id.play_button); + Button reStartButton = findViewById(R.id.play_button); reStartButton.setOnClickListener(view -> play()); } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java index 016b2ed..7648f84 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ConsentDialog.java @@ -16,6 +16,7 @@ package com.huawei.hms.ads.sdk.dialogs; +import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.content.SharedPreferences; @@ -49,19 +50,7 @@ * Control on consent-related dialog boxes. */ public class ConsentDialog extends Dialog { - private Context mContext; - - private LayoutInflater inflater; - - private LinearLayout contentLayout; - - private TextView titleTv; - - private TextView initInfoTv; - - private TextView moreInfoTv; - - private TextView partnersListTv; + private final Context mContext; private View consentDialogView; @@ -71,15 +60,7 @@ public class ConsentDialog extends Dialog { private View partnersListView; - private Button consentYesBtn; - - private Button consentNoBtn; - - private Button moreInfoBackBtn; - - private Button partnerListBackBtn; - - private List madProviders; + private final List mAdProviders; private ConsentDialogCallback mCallback; @@ -105,7 +86,7 @@ public ConsentDialog(@NonNull Context context, List adProviders) { // Customize a dialog box style. super(context, R.style.dialog); mContext = context; - madProviders = adProviders; + mAdProviders = adProviders; } /** @@ -122,13 +103,15 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window dialogWindow = getWindow(); - dialogWindow.requestFeature(Window.FEATURE_NO_TITLE); + if (dialogWindow != null) { + dialogWindow.requestFeature(Window.FEATURE_NO_TITLE); + } - inflater = LayoutInflater.from(mContext); + LayoutInflater inflater = LayoutInflater.from(mContext); consentDialogView = inflater.inflate(R.layout.dialog_consent, null); setContentView(consentDialogView); - titleTv = findViewById(R.id.consent_dialog_title_text); + TextView titleTv = findViewById(R.id.consent_dialog_title_text); titleTv.setText(mContext.getString(R.string.consent_title)); initView = inflater.inflate(R.layout.dialog_consent_content, null); @@ -144,6 +127,7 @@ protected void onCreate(Bundle savedInstanceState) { * * @param consentStatus ConsentStatus */ + @SuppressLint("ApplySharedPref") private void updateConsentStatus(ConsentStatus consentStatus) { // Update the consent status. Consent.getInstance(mContext).setConsentStatus(consentStatus); @@ -175,22 +159,19 @@ private void showInitConsentInfo() { * @param rootView rootView */ private void addInitButtonAndLinkClick(@NonNull View rootView) { - consentYesBtn = rootView.findViewById(R.id.btn_consent_init_yes); - consentYesBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - dismiss(); - updateConsentStatus(ConsentStatus.PERSONALIZED); - } + Button consentYesBtn = rootView.findViewById(R.id.btn_consent_init_yes); + consentYesBtn.setOnClickListener(view -> { + dismiss(); + updateConsentStatus(ConsentStatus.PERSONALIZED); }); - consentNoBtn = rootView.findViewById(R.id.btn_consent_init_skip); + Button consentNoBtn = rootView.findViewById(R.id.btn_consent_init_skip); consentNoBtn.setOnClickListener(view -> { dismiss(); updateConsentStatus(ConsentStatus.NON_PERSONALIZED); }); - initInfoTv = rootView.findViewById(R.id.consent_center_init_content); + TextView initInfoTv = rootView.findViewById(R.id.consent_center_init_content); initInfoTv.setMovementMethod(ScrollingMovementMethod.getInstance()); String initText = mContext.getString(R.string.consent_init_text); final SpannableStringBuilder spanInitText = new SpannableStringBuilder(initText); @@ -227,15 +208,10 @@ private void showTouchHereInfo() { * * @param rootView rootView */ - private void addMoreInfoButtonAndLinkClick(View rootView) { - moreInfoBackBtn = rootView.findViewById(R.id.btn_consent_more_info_back); - moreInfoBackBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - showInitConsentInfo(); - } - }); - moreInfoTv = rootView.findViewById(R.id.consent_center_more_info_content); + private void addMoreInfoButtonAndLinkClick(@NonNull View rootView) { + Button moreInfoBackBtn = rootView.findViewById(R.id.btn_consent_more_info_back); + moreInfoBackBtn.setOnClickListener(v -> showInitConsentInfo()); + TextView moreInfoTv = rootView.findViewById(R.id.consent_center_more_info_content); moreInfoTv.setMovementMethod(ScrollingMovementMethod.getInstance()); String moreInfoText = mContext.getString(R.string.consent_more_info_text); final SpannableStringBuilder spanMoreInfoText = new SpannableStringBuilder(moreInfoText); @@ -243,7 +219,7 @@ public void onClick(View v) { // Set the listener on the event for tapping some text. ClickableSpan moreInfoTouchHere = new ClickableSpan() { @Override - public void onClick(View widget) { + public void onClick(@NonNull View widget) { showPartnersListInfo(); } }; @@ -262,10 +238,10 @@ public void onClick(View widget) { * Display the partner list page. */ private void showPartnersListInfo() { - partnersListTv = partnersListView.findViewById(R.id.partners_list_content); + TextView partnersListTv = partnersListView.findViewById(R.id.partners_list_content); partnersListTv.setMovementMethod(ScrollingMovementMethod.getInstance()); partnersListTv.setText(""); - List learnAdProviders = madProviders; + List learnAdProviders = mAdProviders; if (learnAdProviders != null) { for (AdProvider learnAdProvider : learnAdProviders) { String link = "" @@ -288,21 +264,16 @@ private void showPartnersListInfo() { * * @param rootView rootView */ - private void addPartnersListButtonAndLinkClick(View rootView) { - partnerListBackBtn = rootView.findViewById(R.id.btn_partners_list_back); - partnerListBackBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - showTouchHereInfo(); - } - }); + private void addPartnersListButtonAndLinkClick(@NonNull View rootView) { + Button partnerListBackBtn = rootView.findViewById(R.id.btn_partners_list_back); + partnerListBackBtn.setOnClickListener(v -> showTouchHereInfo()); } /** * Add layout content in the dialog box that is displayed. */ private void addContentView(View view) { - contentLayout = findViewById(R.id.consent_center_layout); + LinearLayout contentLayout = findViewById(R.id.consent_center_layout); contentLayout.removeAllViews(); contentLayout.addView(view); } diff --git a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java index da4b18d..850a95f 100644 --- a/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java +++ b/app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java @@ -16,6 +16,7 @@ package com.huawei.hms.ads.sdk.dialogs; +import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; @@ -53,18 +54,10 @@ public class ProtocolDialog extends Dialog { private static final String ACTION_OAID_SETTING = "com.huawei.hms.action.OAID_SETTING"; - private Context mContext; - - private TextView titleTv; + private final Context mContext; private TextView protocolTv; - private Button confirmButton; - - private Button cancelButton; - - private LayoutInflater inflater; - private ProtocolDialogCallback mCallback; /** @@ -100,13 +93,15 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window dialogWindow = getWindow(); - dialogWindow.requestFeature(Window.FEATURE_NO_TITLE); + if (dialogWindow != null) { + dialogWindow.requestFeature(Window.FEATURE_NO_TITLE); + } - inflater = LayoutInflater.from(mContext); + LayoutInflater inflater = LayoutInflater.from(mContext); LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.dialog_protocol, null); setContentView(rootView); - titleTv = findViewById(R.id.uniform_dialog_title); + TextView titleTv = findViewById(R.id.uniform_dialog_title); titleTv.setText(mContext.getString(R.string.protocol_title)); protocolTv = findViewById(R.id.protocol_center_content); @@ -119,11 +114,12 @@ protected void onCreate(Bundle savedInstanceState) { * * @param rootView rootView */ - private void initButtonBar(LinearLayout rootView) { - confirmButton = rootView.findViewById(R.id.base_okBtn); + private void initButtonBar(@NonNull LinearLayout rootView) { + Button confirmButton = rootView.findViewById(R.id.base_okBtn); confirmButton.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(View v) { + @SuppressLint("ApplySharedPref") + public void onClick(View view) { SharedPreferences preferences = mContext.getSharedPreferences(AdsConstant.SP_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); @@ -136,7 +132,7 @@ public void onClick(View v) { } }); - cancelButton = rootView.findViewById(R.id.base_cancelBtn); + Button cancelButton = rootView.findViewById(R.id.base_cancelBtn); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/build.gradle b/build.gradle index 99ec934..3071c22 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,9 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id "com.android.application" version "8.3.1" apply false + alias(libs.plugins.android.application) apply false } - tasks.register('clean', Delete) { delete rootProject.fileTree("build") } From 66d634e22220f211e91a705a279125f81a9a59e3 Mon Sep 17 00:00:00 2001 From: Martin Zeitler Date: Tue, 2 Apr 2024 04:33:48 +0200 Subject: [PATCH 3/3] Update strings.xml --- app/src/main/res/values-zh-rCN/strings.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 7f2a5e1..7d326f6 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -116,14 +116,14 @@ Privacy Policy Close Do Not Load Ads - The Ads in HUAWEI X is provided in collaboration with our advertising partners. To provide this service, we need to share certain information about you with these partners, including your city-level location as well as your usage records for the news service.\n\n + The Ads in HUAWEI X are provided in collaboration with our advertising partners. To provide this service, we need to share certain information about you with these partners, including your city-level location as well as your usage records for the news service.\n\n For more information about our partners and how your data is processed, please touch here.\n\n By touching AGREE, you indicate that you agree to share the above personal information with our partners so that they can provide you with personalized advertisements on behalf of their customers, based on interests and preferences identified or predicted through analysis of your personal information. \n\nYou can withdraw your consent at any time by going to [app settings]. \n\nIf you touch SKIP, your data will not be shared with our partners and you will not receive personalized ads. 347 351 - The Ads in HUAWEI X is provided in collaboration with our partners. You can find a full list of our partners for each country/region here.\n\n + The Ads in HUAWEI X are provided in collaboration with our partners. You can find a full list of our partners for each country/region here.\n\n In order to provide you with personalized advertisements, we need to share the following information with our partners:\n\n • User information, including advertising ID, city of residence, country, and language.\n\n • Device information, including device name and model, operating system version, screen size, and network type.\n\n