├── .github └── workflows │ └── android.yml ├── .gitignore ├── LICENSE ├── README.md ├── README_ZH.md ├── app ├── build.gradle ├── proguard-rules.pro ├── release │ └── HuaweiAdsSdk_Demo_13.4.56.300_release.apk └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── huawei │ │ └── hms │ │ └── ads │ │ └── sdk │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── ic_launcher-playstore.png │ ├── java │ │ └── com │ │ │ └── huawei │ │ │ └── hms │ │ │ └── ads │ │ │ └── sdk │ │ │ ├── AdFormat.java │ │ │ ├── AdSampleAdapter.java │ │ │ ├── AdSampleApplication.java │ │ │ ├── AdsConstant.java │ │ │ ├── BannerActivity.java │ │ │ ├── BaseActivity.java │ │ │ ├── BiddingSplashActivity.java │ │ │ ├── ConsentActivity.java │ │ │ ├── InstreamActivity.java │ │ │ ├── InterstitialActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── NativeActivity.java │ │ │ ├── NativeViewFactory.java │ │ │ ├── ProtocolActivity.java │ │ │ ├── RewardActivity.java │ │ │ ├── SplashActivity.java │ │ │ └── dialogs │ │ │ ├── ConsentDialog.java │ │ │ └── ProtocolDialog.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable-xhdpi │ │ ├── btn_check_off_emui.png │ │ ├── btn_check_on_emui.png │ │ ├── btn_check_on_pressed_emui.png │ │ ├── ic_sb_cancel_gray.png │ │ └── popup_full_bright_emui.9.png │ │ ├── drawable-xxhdpi │ │ ├── btn_default_disabled_emui.9.png │ │ ├── btn_default_normal_emui.9.png │ │ └── btn_default_pressed_emui.9.png │ │ ├── drawable │ │ ├── app_whythisad_info.xml │ │ ├── btn_check_off_emui.png │ │ ├── btn_check_on_emui.png │ │ ├── btn_check_on_pressed_emui.png │ │ ├── default_slogan.png │ │ ├── default_slogan_landscape.png │ │ ├── detail_checkbox_selector.xml │ │ ├── emui_button_select.xml │ │ ├── ic_launcher_background.xml │ │ ├── native_button_rounded_corners_shape.xml │ │ └── native_flag_rounded_corners_shape.xml │ │ ├── layout │ │ ├── activity_banner.xml │ │ ├── activity_bidding_splash.xml │ │ ├── activity_consent.xml │ │ ├── activity_instream.xml │ │ ├── activity_interstitial.xml │ │ ├── activity_main.xml │ │ ├── activity_native.xml │ │ ├── activity_reward.xml │ │ ├── activity_splash.xml │ │ ├── dialog_consent.xml │ │ ├── dialog_consent_content.xml │ │ ├── dialog_consent_moreinfo.xml │ │ ├── dialog_consent_partner_list.xml │ │ ├── dialog_protocol.xml │ │ ├── native_ad_with_app_download_btn_template.xml │ │ ├── native_common_medium_template.xml │ │ ├── native_image_only_template.xml │ │ ├── native_small_image_template.xml │ │ ├── native_small_template.xml │ │ ├── native_three_images_template.xml │ │ └── native_video_template.xml │ │ ├── menu │ │ └── ad_sample_menu.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── values-zh-rCN │ │ └── strings.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── ic_launcher_background.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── huawei │ └── hms │ └── ads │ └── sdk │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── result ├── Banner.gif ├── Interstitial.gif ├── Native.gif ├── Reward.gif ├── Roll.gif └── Splash.gif └── settings.gradle /.github/workflows/android.yml: -------------------------------------------------------------------------------- 1 | name: Build & Publish Debug APK 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v1 13 | - name: set up JDK 1.8 14 | uses: actions/setup-java@v1 15 | with: 16 | java-version: 1.8 17 | - name: Make Gradle executable 18 | run: chmod +x ./gradlew 19 | - name: Build with Gradle 20 | run: ./gradlew build 21 | - name: Build Debug APK 22 | run: ./gradlew assembleDebug 23 | - name: Releasing using Hub 24 | uses: ShaunLWM/action-release-debugapk@master 25 | env: 26 | GITHUB_TOKEN: ${{ secrets.TOKEN }} 27 | APP_FOLDER: app 28 | RELEASE_TITLE: New Build 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Android template 3 | # Built application files 4 | *.aar 5 | *.ap_ 6 | *.aab 7 | 8 | # Files for the ART/Dalvik VM 9 | *.dex 10 | 11 | # Java class files 12 | *.class 13 | 14 | # Generated files 15 | bin/ 16 | gen/ 17 | out/ 18 | 19 | # Gradle files 20 | .gradle/ 21 | build/ 22 | 23 | # Local configuration file (sdk path, etc) 24 | local.properties 25 | 26 | # Proguard folder generated by Eclipse 27 | proguard/ 28 | 29 | # Log Files 30 | *.log 31 | 32 | # Android Studio Navigation editor temp files 33 | .navigation/ 34 | 35 | # Android Studio captures folder 36 | captures/ 37 | 38 | # IntelliJ 39 | *.iml 40 | .idea/workspace.xml 41 | .idea/tasks.xml 42 | .idea/gradle.xml 43 | .idea/assetWizardSettings.xml 44 | .idea/dictionaries 45 | .idea/libraries 46 | # Android Studio 3 in .gitignore file. 47 | .idea/caches 48 | .idea/modules.xml 49 | # Comment next line if keeping position of elements in Navigation Editor is relevant for you 50 | .idea/navEditor.xml 51 | 52 | # Keystore files 53 | # Uncomment the following lines if you do not want to check your keystore files in. 54 | #*.jks 55 | #*.keystore 56 | 57 | # External native build folder generated in Android Studio 2.2 and later 58 | .externalNativeBuild 59 | .cxx/ 60 | 61 | # Google Services (e.g. APIs or Firebase) 62 | # google-services.json 63 | 64 | # Freeline 65 | freeline.py 66 | freeline/ 67 | freeline_project_description.json 68 | 69 | # fastlane 70 | fastlane/report.xml 71 | fastlane/Preview.html 72 | fastlane/screenshots 73 | fastlane/test_output 74 | fastlane/readme.md 75 | 76 | # Version control 77 | vcs.xml 78 | 79 | # lint 80 | lint/intermediates/ 81 | lint/generated/ 82 | lint/outputs/ 83 | lint/tmp/ 84 | # lint/reports/ 85 | 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Petal Ads Sample Code (Java) 2 | English | [中文](https://github.com/HMS-Core/hms-ads-demo-java/blob/master/README_ZH.md) 3 | ## Contents 4 | 5 | * [Introduction](#Introduction) 6 | * [Installation](#Installation) 7 | * [Configuration](#Configuration) 8 | * [Environment Requirements](#Environment-Requirements) 9 | * [Sample Code](#Sample-Code) 10 | * [Running Result](#Running-Result) 11 | * [License](#License) 12 | 13 | 14 | ## Introduction 15 | The Java sample code provided by Ads Kit describes how to display ads by integrating the Petal Ads SDK into your Android app. 16 | 17 | ## Installation 18 | Install the sample code on a Huawei phone. 19 |
You can use HMS Toolkit to quickly run the sample code. HMS Toolkit supports one-stop kit integration, and provides functions such as free app debugging on remote real devices. 20 | To learn more about HMS Toolkit, please refer to the [HMS Toolkit documentation](https://developer.huawei.com/consumer/en/doc/development/Tools-Guides/getting-started-0000001077381096?ha_source=hms1).
21 | 22 | ## Configuration Management 23 | N/A 24 | 25 | ## Environment Requirements 26 | Your Huawei phone has HMS Core (APK) 4.0.0 or later installed. 27 | 28 | ## Sample Code 29 | The Java sample code provided by Ads Kit integrates the Petal Ads SDK, which offers a range of ad formats including banner, native, rewarded, interstitial, splash, and roll ads. The sample code contains the following files for you to load and display ads and obtain user consent: 30 | 31 | (1) BannerActivity.java 32 | Used to load and display banner ads. 33 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\BannerActivity.java
34 | 35 | (2) NativeActivity.java 36 | Used to load and display native ads. 37 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\NativeActivity.java
38 | 39 | (3) RewardActivity.java 40 | Used to load and display rewarded ads. 41 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\RewardActivity.java
42 | 43 | (4) InterstitialActivity.java 44 | Used to load and display interstitial ads. 45 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\InterstitialActivity.java
46 | 47 | (5) SplashActivity.java 48 | Used to load and display splash ads. 49 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\SplashActivity.java
50 | 51 | (6) InstreamActivity.java 52 | Used to load and display roll ads. 53 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\InstreamActivity.java
54 | 55 | (7) ConsentActivity.java 56 | Used to obtain user consent. 57 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\ConsentActivity.java
58 | 59 | (8) ProtocolActivity.java 60 | Used to obtain users' consent on the privacy statement. 61 |
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\ProtocolActivity.java
62 | 63 | ## Running Result 64 | Banner ads         Interstitial ads        Native ads 65 | 66 | 67 | 68 | Reward ads         Roll ads        Splash ads 69 | 70 | 71 | 72 | ## Technical Support 73 | You can visit the [Reddit community](https://www.reddit.com/r/HuaweiDevelopers/) to obtain the latest information about HMS Core and communicate with other developers. 74 | 75 | If you have any questions about the sample code, try the following: 76 | - Visit [Stack Overflow](https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Votes), submit your questions, and tag them with `huawei-mobile-services`. Huawei experts will answer your questions. 77 | - Visit the HMS Core section in the [HUAWEI Developer Forum](https://forums.developer.huawei.com/forumPortal/en/home?fid=0101187876626530001?ha_source=hms1) and communicate with other developers. 78 | 79 | If you encounter any issues when using the sample code, submit your [issues](https://github.com/HMS-Core/hms-ads-demo-java/issues) or submit a [pull request](https://github.com/HMS-Core/hms-ads-demo-java/pulls). 80 | 81 | ## License 82 | The sample code is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0). 83 | -------------------------------------------------------------------------------- /README_ZH.md: -------------------------------------------------------------------------------- 1 | # 鲸鸿动能广告服务Java示例代码 2 | 中文 | [English](https://github.com/HMS-Core/hms-ads-demo-java/blob/master/README.md) 3 | ## 目录 4 | 5 | * [简介](#简介) 6 | * [安装](#安装) 7 | * [配置](#配置) 8 | * [环境要求](#环境要求) 9 | * [示例代码](#示例代码) 10 | * [运行结果](#运行结果) 11 | * [授权许可](#授权许可) 12 | 13 | 14 | ## 简介 15 | 鲸鸿动能广告服务Java示例代码向您介绍如何将鲸鸿动能SDK集成到安卓应用中,实现广告展示。 16 | 17 | ## 安装 18 | 在华为手机上安装该示例代码项目。 19 |
该示例也可以通过HMS Toolkit快速启动运行,且支持各Kit一站式集成,并提供远程真机免费调测等功能。了解更多信息,请参见[HMS Toolkit](https://developer.huawei.com/consumer/cn/doc/development/Tools-Guides/getting-started-0000001077381096?ha_source=hms1)。
20 | 21 | ## 配置 22 | 无需配置。 23 | 24 | ## 环境要求 25 | 在华为手机上安装HMS Core (APK) 4.0.0及以上版本。 26 | 27 | ## 示例代码 28 | 鲸鸿动能广告服务Java示例代码集成了HMS Core Ads SDK,为您提供横幅广告、原生广告、激励广告、插屏广告、开屏广告和贴片广告的展示页面。 29 | 本示例代码包括以下六个文件,便于您进行广告加载、展示和用户意见征集: 30 | 31 | 1). BannerActivity.java 32 | 用于加载、展示横幅广告。 33 |
代码位置:app\src\main\java\com\huawei\hms\ads\sdk\BannerActivity.java
34 | 35 | 2). NativeActivity.java 36 | 用于加载、展示原生广告。 37 |
代码位置: app\src\main\java\com\huawei\hms\ads\sdk\NativeActivity.java
38 | 39 | 3). RewardActivity.java 40 | 用于加载、展示激励广告。 41 |
代码位置:app\src\main\java\com\huawei\hms\ads\sdk\RewardActivity.java
42 | 43 | 4). InterstitialActivity.java 44 | 用于加载、展示插屏广告。 45 |
代码位置: app\src\main\java\com\huawei\hms\ads\sdk\InterstitialActivity.java
46 | 47 | 5). SplashActivity.java 48 | 用于加载、展示开屏广告。 49 |
代码位置: app\src\main\java\com\huawei\hms\ads\sdk\SplashActivity.java
50 | 51 | 6). InstreamActivity.java 52 | 用于加载、展示贴片广告。 53 |
代码位置:app\src\main\java\com\huawei\hms\ads\sdk\InstreamActivity.java
54 | 55 | 7). ConsentActivity.java 56 | 用于征求用户意见。 57 |
代码位置:app\src\main\java\com\huawei\hms\ads\sdk\ConsentActivity.java
58 | 59 | 8). ProtocolActivity.java 60 | 用于收集用户对隐私条款的意见。 61 |
代码位置:app\src\main\java\com\huawei\hms\ads\sdk\ProtocolActivity.java
62 | 63 | 64 | ## 运行结果 65 | Banner Ads         Interstitial Ads        Native Ads 66 | 67 | 68 | 69 | Reward Ads         Instream Ads        Splash Ads 70 | 71 | 72 | 73 | ## 技术支持 74 | 如果您对HMS Core还处于评估阶段,可在[Reddit社区](https://www.reddit.com/r/HuaweiDevelopers/)获取关于HMS Core的最新讯息,并与其他开发者交流见解。 75 | 76 | 如果您对使用HMS示例代码有疑问,请尝试: 77 | - 开发过程遇到问题上[Stack Overflow](https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Votes),在`huawei-mobile-services`标签下提问,有华为研发专家在线一对一解决您的问题。 78 | - 到[华为开发者论坛](https://forums.developer.huawei.com/forumPortal/en/home?fid=0101187876626530001?ha_source=hms1) HMS Core板块与其他开发者进行交流。 79 | 80 | 如果您在尝试示例代码中遇到问题,请向仓库提交[issue](https://github.com/HMS-Core/hms-ads-demo-java/issues),也欢迎您提交[Pull Request](https://github.com/HMS-Core/hms-ads-demo-java/pulls)。 81 | 82 | ## 授权许可 83 | 鲸鸿动能广告服务Java示例代码经过 [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0)授权许可。 84 | 85 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.3" 6 | defaultConfig { 7 | applicationId "com.huawei.hms.ads.sdk" 8 | minSdkVersion 19 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | repositories { 23 | flatDir { 24 | dirs 'libs' 25 | } 26 | } 27 | 28 | dependencies { 29 | implementation fileTree(dir: 'libs', include: ['*.jar']) 30 | implementation 'androidx.appcompat:appcompat:1.3.0' 31 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 32 | implementation 'com.huawei.hms:ads-lite:13.4.73.301' 33 | implementation 'com.huawei.hms:ads-consent:3.4.73.301' 34 | implementation 'com.huawei.hms:ads-omsdk:1.3.35' 35 | 36 | testImplementation 'junit:junit:4.13.2' 37 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 38 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 39 | } 40 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -keep class com.huawei.openalliance.ad.** { *; } 23 | -keep class com.huawei.hms.ads.** { *; } 24 | -------------------------------------------------------------------------------- /app/release/HuaweiAdsSdk_Demo_13.4.56.300_release.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/release/HuaweiAdsSdk_Demo_13.4.56.300_release.apk -------------------------------------------------------------------------------- /app/src/androidTest/java/com/huawei/hms/ads/sdk/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.huawei.hms.ads.sdk; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.huawei.hms.ads.sdk", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 35 | 36 | 40 | 44 | 48 | 52 | 57 | 62 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-playstore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/ic_launcher-playstore.png -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/AdFormat.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | 20 | public class AdFormat { 21 | /** 22 | * Item name 23 | */ 24 | private String title; 25 | 26 | /** 27 | * Target activity 28 | */ 29 | private Class targetClass; 30 | 31 | public AdFormat(String title, Class targetClass) { 32 | this.title = title; 33 | this.targetClass = targetClass; 34 | } 35 | 36 | public String getTitle() { 37 | return title; 38 | } 39 | 40 | public Class getTargetClass() { 41 | return targetClass; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/AdSampleAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.content.Context; 20 | import android.view.LayoutInflater; 21 | import android.view.View; 22 | import android.view.ViewGroup; 23 | import android.widget.ArrayAdapter; 24 | import android.widget.TextView; 25 | 26 | import java.util.List; 27 | 28 | public class AdSampleAdapter extends ArrayAdapter { 29 | private Context mContext; 30 | 31 | private int resourceId; 32 | 33 | public AdSampleAdapter(Context context, int resource, List formatList) { 34 | super(context, resource, formatList); 35 | mContext = context; 36 | resourceId = resource; 37 | } 38 | 39 | @Override 40 | public View getView(int position, View convertView, ViewGroup parent) { 41 | AdFormat adFormat = getItem(position); 42 | View view = LayoutInflater.from(mContext).inflate(resourceId, parent, false); 43 | TextView title = view.findViewById(android.R.id.text1); 44 | title.setText(adFormat.getTitle()); 45 | return view; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/AdSampleApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.app.Application; 20 | 21 | import com.huawei.hms.ads.HwAds; 22 | 23 | public class AdSampleApplication extends Application { 24 | 25 | @Override 26 | public void onCreate() { 27 | super.onCreate(); 28 | 29 | // Initialize the Petal Ads SDK. 30 | HwAds.init(this); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/AdsConstant.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | public class AdsConstant { 20 | 21 | /** 22 | * SharedPreferences name. 23 | */ 24 | public static final String SP_NAME = "HuaweiAdsSdkSharedPreferences"; 25 | 26 | /** 27 | * The SP key of protocol. 28 | */ 29 | public static final String SP_PROTOCOL_KEY = "protocol"; 30 | 31 | /** 32 | * The SP key of consent. 33 | */ 34 | public static final String SP_CONSENT_KEY = "consent"; 35 | 36 | /** 37 | * The SP default value of protocol. 38 | */ 39 | public static final int DEFAULT_SP_PROTOCOL_VALUE = 0; 40 | 41 | /** 42 | * The SP default value of consent. 43 | */ 44 | public static final int DEFAULT_SP_CONSENT_VALUE = -1; 45 | } -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/BannerActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import java.util.Locale; 20 | 21 | import android.graphics.Color; 22 | import android.os.Bundle; 23 | import android.view.View; 24 | import android.widget.Button; 25 | import android.widget.FrameLayout; 26 | import android.widget.RadioGroup; 27 | import android.widget.TextView; 28 | import android.widget.Toast; 29 | 30 | import com.huawei.hms.ads.AdListener; 31 | import com.huawei.hms.ads.AdParam; 32 | import com.huawei.hms.ads.BannerAdSize; 33 | import com.huawei.hms.ads.banner.BannerView; 34 | 35 | /** 36 | * Activity for displaying a banner ad. 37 | */ 38 | public class BannerActivity extends BaseActivity { 39 | private static final int REFRESH_TIME = 60; 40 | 41 | private BannerView bannerView; 42 | 43 | private BannerView defaultBannerView; 44 | 45 | private FrameLayout adFrameLayout; 46 | 47 | private RadioGroup sizeRadioGroup; 48 | 49 | private RadioGroup colorRadioGroup; 50 | 51 | /** 52 | * Button tapping event listener. 53 | */ 54 | private View.OnClickListener buttonListener = new View.OnClickListener() { 55 | @Override 56 | public void onClick(View v) { 57 | defaultBannerView.setVisibility(View.INVISIBLE); 58 | if (bannerView != null) { 59 | adFrameLayout.removeView(bannerView); 60 | bannerView.destroy(); 61 | } 62 | 63 | // Call new BannerView(Context context) to create a BannerView class. 64 | bannerView = new BannerView(v.getContext()); 65 | 66 | // Set an ad slot ID. 67 | bannerView.setAdId(getString(R.string.banner_ad_id)); 68 | 69 | // Set the background color and size based on user selection. 70 | BannerAdSize adSize = getBannerAdSize(sizeRadioGroup.getCheckedRadioButtonId()); 71 | bannerView.setBannerAdSize(adSize); 72 | 73 | int color = getBannerViewBackground(colorRadioGroup.getCheckedRadioButtonId()); 74 | bannerView.setBackgroundColor(color); 75 | 76 | adFrameLayout.addView(bannerView); 77 | bannerView.setAdListener(adListener); 78 | bannerView.loadAd(new AdParam.Builder().build()); 79 | } 80 | }; 81 | 82 | /** 83 | * Ad listener. 84 | */ 85 | private AdListener adListener = new AdListener() { 86 | @Override 87 | public void onAdLoaded() { 88 | // Called when an ad is loaded successfully. 89 | showToast("Ad loaded."); 90 | } 91 | 92 | @Override 93 | public void onAdFailed(int errorCode) { 94 | // Called when an ad fails to be loaded. 95 | showToast(String.format(Locale.ROOT, "Ad failed to load with error code %d.", errorCode)); 96 | } 97 | 98 | @Override 99 | public void onAdOpened() { 100 | // Called when an ad is opened. 101 | showToast(String.format("Ad opened ")); 102 | } 103 | 104 | @Override 105 | public void onAdClicked() { 106 | // Called when a user taps an ad. 107 | showToast("Ad clicked"); 108 | } 109 | 110 | @Override 111 | public void onAdLeave() { 112 | // Called when a user has left the app. 113 | showToast("Ad Leave"); 114 | } 115 | 116 | @Override 117 | public void onAdClosed() { 118 | // Called when an ad is closed. 119 | showToast("Ad closed"); 120 | } 121 | }; 122 | 123 | @Override 124 | protected void onCreate(Bundle savedInstanceState) { 125 | super.onCreate(savedInstanceState); 126 | 127 | setTitle(getString(R.string.banner_ad)); 128 | setContentView(R.layout.activity_banner); 129 | 130 | TextView bannerTitle = findViewById(R.id.text_banner); 131 | bannerTitle.setText("This is banner ads sample."); 132 | 133 | sizeRadioGroup = findViewById(R.id.size_radioGroup); 134 | colorRadioGroup = findViewById(R.id.color_radioGroup); 135 | 136 | // Load the default banner ad. 137 | loadDefaultBannerAd(); 138 | 139 | // Set the button for loading an ad. 140 | Button loadButton = findViewById(R.id.refreshButton); 141 | loadButton.setOnClickListener(buttonListener); 142 | 143 | adFrameLayout = findViewById(R.id.ad_frame); 144 | } 145 | 146 | /** 147 | * Load the default banner ad. 148 | */ 149 | private void loadDefaultBannerAd() { 150 | // Obtain BannerView based on the configuration in layout/activity_main.xml. 151 | defaultBannerView = findViewById(R.id.hw_banner_view); 152 | defaultBannerView.setAdListener(adListener); 153 | defaultBannerView.setBannerRefresh(REFRESH_TIME); 154 | 155 | AdParam adParam = new AdParam.Builder().build(); 156 | defaultBannerView.loadAd(adParam); 157 | } 158 | 159 | private void showToast(String message) { 160 | Toast.makeText(BannerActivity.this, message, Toast.LENGTH_SHORT).show(); 161 | } 162 | 163 | private BannerAdSize getBannerAdSize(int checkedId) { 164 | BannerAdSize adSize = null; 165 | switch (checkedId) { 166 | case R.id.size_320_50: 167 | adSize = BannerAdSize.BANNER_SIZE_320_50; 168 | break; 169 | case R.id.size_320_100: 170 | adSize = BannerAdSize.BANNER_SIZE_320_100; 171 | break; 172 | case R.id.size_300_250: 173 | adSize = BannerAdSize.BANNER_SIZE_300_250; 174 | break; 175 | case R.id.size_smart: 176 | adSize = BannerAdSize.BANNER_SIZE_SMART; 177 | break; 178 | case R.id.size_360_57: 179 | adSize = BannerAdSize.BANNER_SIZE_360_57; 180 | break; 181 | case R.id.size_360_144: 182 | adSize = BannerAdSize.BANNER_SIZE_360_144; 183 | break; 184 | default: 185 | break; 186 | } 187 | return adSize; 188 | } 189 | 190 | private int getBannerViewBackground(int checkedId) { 191 | int color = Color.TRANSPARENT; 192 | switch (checkedId) { 193 | case R.id.color_white: 194 | color = Color.WHITE; 195 | break; 196 | case R.id.color_black: 197 | color = Color.BLACK; 198 | break; 199 | case R.id.color_red: 200 | color = Color.RED; 201 | break; 202 | case R.id.color_transparent: 203 | color = Color.TRANSPARENT; 204 | break; 205 | default: 206 | break; 207 | } 208 | return color; 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/BaseActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package com.huawei.hms.ads.sdk; 17 | 18 | import android.os.Bundle; 19 | 20 | import androidx.appcompat.app.ActionBar; 21 | import androidx.appcompat.app.AppCompatActivity; 22 | 23 | /** 24 | * This base activity for support navigate up within activity hierarchy from the action bar. 25 | */ 26 | public class BaseActivity extends AppCompatActivity { 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | 31 | ActionBar actionBar = getSupportActionBar(); 32 | actionBar.setDisplayHomeAsUpEnabled(true); 33 | } 34 | 35 | @Override 36 | public boolean onSupportNavigateUp() { 37 | finish(); 38 | return super.onSupportNavigateUp(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/BiddingSplashActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.content.Intent; 20 | import android.content.pm.ActivityInfo; 21 | import android.content.res.Configuration; 22 | import android.os.Build; 23 | import android.os.Bundle; 24 | import android.util.Log; 25 | import android.view.View; 26 | import android.view.ViewGroup; 27 | import android.view.WindowManager; 28 | import android.widget.RelativeLayout; 29 | import android.widget.Toast; 30 | 31 | import com.huawei.hms.ads.AdParam; 32 | import com.huawei.hms.ads.AudioFocusType; 33 | import com.huawei.hms.ads.BiddingInfo; 34 | import com.huawei.hms.ads.BiddingParam; 35 | import com.huawei.hms.ads.splash.SplashAd; 36 | import com.huawei.hms.ads.splash.SplashView; 37 | import com.huawei.hms.ads.splash.listener.SplashListener; 38 | import com.huawei.hms.ads.splash.listener.SplashLoadListener; 39 | import com.huawei.openalliance.ad.inter.HiAd; 40 | import com.huawei.openalliance.ad.inter.IHiAd; 41 | 42 | public class BiddingSplashActivity extends BaseActivity { 43 | private static final String TAG = BiddingSplashActivity.class.getSimpleName(); 44 | 45 | private RelativeLayout splashContainer; 46 | 47 | private SplashAd splashAd; 48 | 49 | // 返回键标志位 按返回键退出时应用不被重新拉起,但是在展示广告时下拉通知栏仍然需要正常跳转 50 | private boolean hasPaused = false; 51 | 52 | @Override 53 | protected void onCreate(Bundle savedInstanceState) { 54 | // set full screen 55 | getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 56 | // set translucent navigation 57 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 58 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 59 | getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 60 | } 61 | 62 | try { 63 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { 64 | if (Configuration.ORIENTATION_PORTRAIT == getResources().getConfiguration().orientation) { 65 | WindowManager.LayoutParams lp = getWindow().getAttributes(); 66 | lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; 67 | getWindow().setAttributes(lp); 68 | } 69 | } 70 | } catch (Throwable e) { 71 | Log.w(TAG, "set CutoutMode error:" + e.getClass().getSimpleName()); 72 | } 73 | super.onCreate(savedInstanceState); 74 | setContentView(R.layout.activity_bidding_splash); 75 | splashContainer = findViewById(R.id.hw_bidding_splash_container); 76 | splashAd = new SplashAd(this); 77 | configSplashParam(); 78 | // 加载开屏广告 79 | loadSplashAd(); 80 | } 81 | 82 | 83 | private void loadSplashAd() { 84 | Log.i(TAG, "loadSplashAd"); 85 | splashAd.loadAd(new SplashLoadListener() { 86 | @Override 87 | public void onAdLoaded() { 88 | Log.i(TAG, "onAdLoaded"); 89 | if (splashAd.isLoaded()) { 90 | BiddingInfo biddingInfo = splashAd.getBiddingInfo(); 91 | if (biddingInfo != null) { 92 | Log.d(TAG, "AdxServer Return BiddingInfo, price is " + biddingInfo.getPrice() + 93 | ", cur is " + biddingInfo.getCur() + 94 | ", nurl is " + biddingInfo.getNurl() + 95 | ", lur is " + biddingInfo.getLurl()); 96 | } else { 97 | Log.d(TAG, "AdxServer Return BiddingInfo is null. "); 98 | } 99 | // 展示开屏广告 100 | showSplashAd(); 101 | } 102 | showMsg("onAdLoaded"); 103 | } 104 | 105 | @Override 106 | public void onAdFailed(int errorCode) { 107 | Log.i(TAG, "onAdFailed, errorCode: " + errorCode); 108 | } 109 | }); 110 | } 111 | 112 | private void showSplashAd() { 113 | Log.i(TAG, "showSplashAd"); 114 | SplashView splashView = getSplashView(); 115 | splashContainer.removeAllViews(); 116 | RelativeLayout.LayoutParams splashParam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 117 | splashContainer.addView(splashView, splashParam); 118 | splashContainer.bringToFront(); 119 | if (splashAd.isLoaded()) { 120 | Log.i(TAG, "Ad is Loaded"); 121 | splashAd.showAd(true); 122 | } else { 123 | Log.i(TAG, "Ad is Empty"); 124 | } 125 | } 126 | 127 | private SplashView getSplashView() { 128 | SplashView splashView = splashAd.getSplashView(); 129 | BiddingSplashActivity BiddingSplashActivity = new BiddingSplashActivity(); 130 | splashView.setAdDisplayListener(new SplashAdDisplayListener(){ 131 | private WeakReference activityRef = new WeakReference<>(BiddingSplashActivity); 132 | @Override 133 | public void onAdClick() { 134 | BiddingSplashActivity splashActivity = activityRef.get(); 135 | if (null == splashActivity) { 136 | return; 137 | } 138 | super.onAdClick(); 139 | Log.d(TAG, "onAdClick"); 140 | Toast.makeText(splashActivity.getApplicationContext(), "onAdClick", Toast.LENGTH_LONG).show(); 141 | } 142 | }); 143 | return splashView; 144 | } 145 | 146 | @Override 147 | // 从其他页面回到开屏页面时调用,进入应用 148 | protected void onRestart() { 149 | super.onRestart(); 150 | Log.d(TAG, "onRestart"); 151 | hasPaused = false; 152 | jump(); 153 | } 154 | 155 | @Override 156 | protected void onStop() { 157 | Log.d(TAG, "onStop"); 158 | hasPaused = true; 159 | super.onStop(); 160 | } 161 | 162 | private void configSplashParam() { 163 | IHiAd hiad = HiAd.getInstance(getApplicationContext()); 164 | hiad.initLog(true, Log.DEBUG); 165 | if (splashAd == null) { 166 | splashAd = new SplashAd(this); 167 | } 168 | // Lock the screen orientation on the device. Your app will automatically adapt to the screen orientation. 169 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED); 170 | int orientation = getScreenOrientation(); 171 | String slotId; 172 | // Set the default slogan and the splash ad unit ID based on the screen orientation on the device. 173 | if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { 174 | splashAd.setSloganResId(R.drawable.default_slogan); 175 | slotId = getString(R.string.ad_id_splash); 176 | } else { 177 | splashAd.setSloganResId(R.drawable.default_slogan_landscape); 178 | slotId = getString(R.string.ad_id_splash_landscape); 179 | } 180 | AdParam.Builder adParam = new AdParam.Builder(); 181 | // 设置广告位参与实时bidding参数 182 | BiddingParam biddingParam = new BiddingParam(); 183 | adParam.addBiddingParamMap(slotId, biddingParam); 184 | adParam.setTMax(500); 185 | 186 | // Set app icon. 187 | splashAd.setLogoResId(R.mipmap.ic_launcher); 188 | // Set app name. 189 | splashAd.setMediaNameResId(R.string.media_name); 190 | // Set the audio focus type for a video splash ad. 191 | splashAd.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE); 192 | splashAd.setAdParam(slotId, orientation, adParam.build()); 193 | splashAd.setSplashListener(new SplashListener() { 194 | @Override 195 | public void onAdShowStart() { 196 | Log.i(TAG, "onAdShowStart"); 197 | } 198 | 199 | @Override 200 | public void onAdError(int errorCode) { 201 | String errorMsg = "onAdError, errorCode: " + errorCode; 202 | Log.i(TAG, errorMsg); 203 | showMsg(errorMsg); 204 | } 205 | 206 | @Override 207 | public void onAdDismissed() { 208 | showMsg("onAdDismissed"); 209 | Log.i(TAG, "onAdDismissed"); 210 | jump(); 211 | } 212 | }); 213 | } 214 | 215 | private int getScreenOrientation() { 216 | Configuration config = getResources().getConfiguration(); 217 | if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 218 | return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 219 | } else { 220 | return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 221 | } 222 | } 223 | 224 | private void jump() { 225 | Log.d(TAG, "jump hasPaused: " + hasPaused); 226 | if(!hasPaused) { 227 | hasPaused = true; 228 | Log.d(TAG, "jump into application"); 229 | startActivity(new Intent(BiddingSplashActivity.this, MainActivity.class)); 230 | finish(); 231 | } 232 | } 233 | 234 | private void showMsg(String msg) { 235 | Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); 236 | } 237 | } 238 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/ConsentActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.widget.TextView; 22 | import android.widget.Toast; 23 | 24 | import com.huawei.hms.ads.AdListener; 25 | import com.huawei.hms.ads.AdParam; 26 | import com.huawei.hms.ads.BannerAdSize; 27 | import com.huawei.hms.ads.HwAds; 28 | import com.huawei.hms.ads.RequestOptions; 29 | import com.huawei.hms.ads.UnderAge; 30 | import com.huawei.hms.ads.banner.BannerView; 31 | import com.huawei.hms.ads.consent.bean.AdProvider; 32 | import com.huawei.hms.ads.consent.constant.ConsentStatus; 33 | import com.huawei.hms.ads.consent.constant.DebugNeedConsent; 34 | import com.huawei.hms.ads.consent.inter.Consent; 35 | import com.huawei.hms.ads.consent.inter.ConsentUpdateListener; 36 | import com.huawei.hms.ads.sdk.dialogs.ConsentDialog; 37 | 38 | import java.util.ArrayList; 39 | import java.util.List; 40 | import java.util.Locale; 41 | 42 | /** 43 | * Activity for displaying consent. 44 | */ 45 | public class ConsentActivity extends BaseActivity implements ConsentDialog.ConsentDialogCallback { 46 | private static final String TAG = ConsentActivity.class.getSimpleName(); 47 | 48 | private BannerView adView; 49 | 50 | private TextView adTypeTv; 51 | 52 | private RequestOptions requestOptions; 53 | 54 | private List mAdProviders = new ArrayList(); 55 | 56 | @Override 57 | protected void onCreate(Bundle savedInstanceState) { 58 | super.onCreate(savedInstanceState); 59 | setTitle(getString(R.string.consent_settings)); 60 | setContentView(R.layout.activity_consent); 61 | adTypeTv = findViewById(R.id.ad_load_tip); 62 | adView = findViewById(R.id.consent_ad_view); 63 | 64 | // Check consent status. 65 | checkConsentStatus(); 66 | } 67 | 68 | /** 69 | * Check consent status. 70 | */ 71 | private void checkConsentStatus() { 72 | Consent consentInfo = Consent.getInstance(this); 73 | 74 | // To ensure that a dialog box is displayed each time you access the code demo, set ConsentStatus to UNKNOWN. In normal cases, the code does not need to be added. 75 | consentInfo.setConsentStatus(ConsentStatus.UNKNOWN); 76 | String testDeviceId = consentInfo.getTestDeviceId(); 77 | consentInfo.addTestDeviceId(testDeviceId); 78 | 79 | // After DEBUG_NEED_CONSENT is set, ensure that the consent is required even if a device is not located in a specified area. 80 | consentInfo.setDebugNeedConsent(DebugNeedConsent.DEBUG_NEED_CONSENT); 81 | consentInfo.requestConsentUpdate(new ConsentUpdateListener() { 82 | @Override 83 | public void onSuccess(ConsentStatus consentStatus, boolean isNeedConsent, List adProviders) { 84 | Log.d(TAG, "ConsentStatus: " + consentStatus + ", isNeedConsent: " + isNeedConsent); 85 | 86 | // The parameter indicating whether the consent is required is returned. 87 | if (isNeedConsent) { 88 | // If ConsentStatus is set to UNKNOWN, re-collect user consent. 89 | if (consentStatus == ConsentStatus.UNKNOWN) { 90 | mAdProviders.clear(); 91 | mAdProviders.addAll(adProviders); 92 | showConsentDialog(); 93 | } else { 94 | // If ConsentStatus is set to PERSONALIZED or NON_PERSONALIZED, no dialog box is displayed to collect user consent. 95 | loadBannerAd(consentStatus.getValue()); 96 | } 97 | } else { 98 | // If a country does not require your app to collect user consent before displaying ads, your app can request a personalized ad directly. 99 | Log.d(TAG, "User is NOT need Consent"); 100 | loadBannerAd(ConsentStatus.PERSONALIZED.getValue()); 101 | } 102 | } 103 | 104 | @Override 105 | public void onFail(String errorDescription) { 106 | Log.e(TAG, "User's consent status failed to update: " + errorDescription); 107 | Toast 108 | .makeText(ConsentActivity.this, "User's consent status failed to update: " + errorDescription, 109 | Toast.LENGTH_LONG) 110 | .show(); 111 | 112 | // In this demo,if the request fails ,you can load a non-personalized ad by default. 113 | loadBannerAd(ConsentStatus.NON_PERSONALIZED.getValue()); 114 | } 115 | }); 116 | } 117 | 118 | /** 119 | * Display the consent dialog box. 120 | */ 121 | private void showConsentDialog() { 122 | // Start to process the consent dialog box. 123 | ConsentDialog dialog = new ConsentDialog(this, mAdProviders); 124 | dialog.setCallback(this); 125 | dialog.setCanceledOnTouchOutside(false); 126 | dialog.show(); 127 | } 128 | 129 | @Override 130 | public void updateConsentStatus(ConsentStatus consentStatus) { 131 | loadBannerAd(consentStatus.getValue()); 132 | } 133 | 134 | private void loadBannerAd(int consentStatus) { 135 | Log.d(TAG, "Load banner ad, consent status: " + consentStatus); 136 | if (consentStatus == ConsentStatus.UNKNOWN.getValue()) { 137 | removeBannerAd(); 138 | } 139 | 140 | // Obtain global ad singleton variables and add personalized ad request parameters. 141 | if (HwAds.getRequestOptions() == null) { 142 | requestOptions = new RequestOptions(); 143 | } else { 144 | requestOptions = HwAds.getRequestOptions(); 145 | } 146 | 147 | // For non-personalized ads, reset this parameter. 148 | requestOptions = requestOptions.toBuilder() 149 | .setTagForUnderAgeOfPromise(UnderAge.PROMISE_TRUE) 150 | .setNonPersonalizedAd(consentStatus) 151 | .build(); 152 | HwAds.setRequestOptions(requestOptions); 153 | AdParam adParam = new AdParam.Builder().build(); 154 | adView.setAdId(getString(R.string.banner_ad_id)); 155 | adView.setBannerAdSize(BannerAdSize.BANNER_SIZE_SMART); 156 | adView.setAdListener(adListener); 157 | adView.loadAd(adParam); 158 | 159 | updateTextViewTips(consentStatus); 160 | } 161 | 162 | private AdListener adListener = new AdListener() { 163 | @Override 164 | public void onAdLoaded() { 165 | // Called when an ad is loaded successfully. 166 | Toast.makeText(ConsentActivity.this, "Ad loaded successfully", Toast.LENGTH_SHORT).show(); 167 | } 168 | 169 | @Override 170 | public void onAdFailed(int errorCode) { 171 | // Called when an ad fails to be loaded. 172 | Toast.makeText(ConsentActivity.this, "Ad failed to load", Toast.LENGTH_SHORT).show(); 173 | } 174 | }; 175 | 176 | private void removeBannerAd() { 177 | if (adView != null) { 178 | adView.removeAllViews(); 179 | } 180 | updateTextViewTips(ConsentStatus.UNKNOWN.getValue()); 181 | } 182 | 183 | private void updateTextViewTips(int consentStatus) { 184 | if (ConsentStatus.NON_PERSONALIZED.getValue() == consentStatus) { 185 | adTypeTv.setText(getString(R.string.load_non_personalized_text)); 186 | } else if (ConsentStatus.PERSONALIZED.getValue() == consentStatus) { 187 | adTypeTv.setText(getString(R.string.load_personalized_text)); 188 | } else { // ConsentStatus.UNKNOWN 189 | adTypeTv.setText(getString(R.string.no_ads_text)); 190 | } 191 | } 192 | } -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.widget.Button; 23 | import android.widget.RadioGroup; 24 | import android.widget.Toast; 25 | 26 | import com.huawei.hms.ads.AdListener; 27 | import com.huawei.hms.ads.AdParam; 28 | import com.huawei.hms.ads.HwAds; 29 | import com.huawei.hms.ads.InterstitialAd; 30 | 31 | /** 32 | * Activity for displaying an interstitial ad. 33 | */ 34 | public class InterstitialActivity extends BaseActivity { 35 | private static final String TAG = InterstitialActivity.class.getSimpleName(); 36 | 37 | private RadioGroup displayRadioGroup; 38 | private Button loadAdButton; 39 | 40 | private InterstitialAd interstitialAd; 41 | 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setTitle(getString(R.string.interstitial_ad)); 46 | setContentView(R.layout.activity_interstitial); 47 | 48 | // Initialize the Petal Ads SDK. 49 | HwAds.init(this); 50 | 51 | displayRadioGroup = findViewById(R.id.display_radio_group); 52 | loadAdButton = findViewById(R.id.load_ad); 53 | loadAdButton.setOnClickListener(new View.OnClickListener() { 54 | @Override 55 | public void onClick(View v) { 56 | loadInterstitialAd(); 57 | } 58 | }); 59 | 60 | } 61 | 62 | private AdListener adListener = new AdListener() { 63 | @Override 64 | public void onAdLoaded() { 65 | super.onAdLoaded(); 66 | showToast("Ad loaded"); 67 | 68 | // Display an interstitial ad. 69 | showInterstitial(); 70 | } 71 | 72 | @Override 73 | public void onAdFailed(int errorCode) { 74 | showToast("Ad load failed with error code: " + errorCode); 75 | Log.d(TAG, "Ad load failed with error code: " + errorCode); 76 | } 77 | 78 | @Override 79 | public void onAdClosed() { 80 | super.onAdClosed(); 81 | showToast("Ad closed"); 82 | Log.d(TAG, "onAdClosed"); 83 | } 84 | 85 | @Override 86 | public void onAdClicked() { 87 | Log.d(TAG, "onAdClicked"); 88 | super.onAdClicked(); 89 | } 90 | 91 | @Override 92 | public void onAdOpened() { 93 | Log.d(TAG, "onAdOpened"); 94 | super.onAdOpened(); 95 | } 96 | }; 97 | 98 | private void loadInterstitialAd() { 99 | interstitialAd = new InterstitialAd(this); 100 | interstitialAd.setAdId(getAdId()); 101 | interstitialAd.setAdListener(adListener); 102 | 103 | AdParam adParam = new AdParam.Builder().build(); 104 | interstitialAd.loadAd(adParam); 105 | } 106 | 107 | private String getAdId() { 108 | if (displayRadioGroup.getCheckedRadioButtonId() == R.id.display_image) { 109 | return getString(R.string.image_ad_id); 110 | } else { 111 | return getString(R.string.video_ad_id); 112 | } 113 | } 114 | 115 | private void showInterstitial() { 116 | // Display an interstitial ad. 117 | if (interstitialAd != null && interstitialAd.isLoaded()) { 118 | interstitialAd.show(this); 119 | } else { 120 | showToast("Ad did not load"); 121 | } 122 | } 123 | 124 | private void showToast(final String text) { 125 | runOnUiThread(new Runnable() { 126 | @Override 127 | public void run() { 128 | Toast.makeText(InterstitialActivity.this, text, Toast.LENGTH_SHORT).show(); 129 | } 130 | }); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.content.Context; 20 | import android.content.Intent; 21 | import android.content.SharedPreferences; 22 | import android.content.res.Configuration; 23 | import android.os.Bundle; 24 | import android.os.Handler; 25 | import android.os.Message; 26 | import android.util.Log; 27 | import android.view.Menu; 28 | import android.view.MenuInflater; 29 | import android.view.MenuItem; 30 | import android.view.View; 31 | import android.widget.AdapterView; 32 | import android.widget.ListView; 33 | 34 | import androidx.annotation.NonNull; 35 | import androidx.appcompat.app.AppCompatActivity; 36 | 37 | import com.huawei.hms.ads.consent.bean.AdProvider; 38 | import com.huawei.hms.ads.consent.constant.ConsentStatus; 39 | import com.huawei.hms.ads.consent.constant.DebugNeedConsent; 40 | import com.huawei.hms.ads.consent.inter.Consent; 41 | import com.huawei.hms.ads.consent.inter.ConsentUpdateListener; 42 | import com.huawei.hms.ads.sdk.dialogs.ConsentDialog; 43 | import com.huawei.hms.ads.sdk.dialogs.ProtocolDialog; 44 | 45 | import java.util.ArrayList; 46 | import java.util.List; 47 | 48 | public class MainActivity extends AppCompatActivity { 49 | private static final String TAG = MainActivity.class.getSimpleName(); 50 | 51 | private static final int PROTOCOL_MSG_TYPE = 100; 52 | 53 | private static final int CONSENT_MSG_TYPE = 200; 54 | 55 | private static final int MSG_DELAY_MS = 1000; 56 | 57 | private ListView listView; 58 | 59 | private List adFormats = new ArrayList<>(); 60 | 61 | private Handler mHandler = new Handler(new Handler.Callback() { 62 | @Override 63 | public boolean handleMessage(@NonNull Message msg) { 64 | if (MainActivity.this.hasWindowFocus()) { 65 | switch (msg.what) { 66 | case PROTOCOL_MSG_TYPE: 67 | showPrivacyDialog(); 68 | break; 69 | case CONSENT_MSG_TYPE: 70 | checkConsentStatus(); 71 | break; 72 | } 73 | } 74 | return false; 75 | } 76 | }); 77 | 78 | @Override 79 | protected void onCreate(Bundle savedInstanceState) { 80 | super.onCreate(savedInstanceState); 81 | setContentView(R.layout.activity_main); 82 | 83 | initAdItems(); 84 | 85 | listView = findViewById(R.id.item_list_view); 86 | final AdSampleAdapter adapter = 87 | new AdSampleAdapter(MainActivity.this, android.R.layout.simple_list_item_1, adFormats); 88 | listView.setAdapter(adapter); 89 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 90 | @Override 91 | public void onItemClick(AdapterView parent, View view, int position, long id) { 92 | AdFormat adFormat = adapter.getItem(position); 93 | Intent intent = new Intent(getApplicationContext(), adFormat.getTargetClass()); 94 | startActivity(intent); 95 | } 96 | }); 97 | 98 | // Show the dialog for setting user privacy. 99 | sendMessage(PROTOCOL_MSG_TYPE, MSG_DELAY_MS); 100 | } 101 | 102 | private void initAdItems() { 103 | adFormats.add(new AdFormat(getString(R.string.banner_ad), BannerActivity.class)); 104 | adFormats.add(new AdFormat(getString(R.string.native_ad), NativeActivity.class)); 105 | adFormats.add(new AdFormat(getString(R.string.reward_ad), RewardActivity.class)); 106 | adFormats.add(new AdFormat(getString(R.string.interstitial_ad), InterstitialActivity.class)); 107 | adFormats.add(new AdFormat(getString(R.string.instream_ad), InstreamActivity.class)); 108 | adFormats.add(new AdFormat(getString(R.string.bidding_splash_ad), BiddingSplashActivity.class)); 109 | } 110 | 111 | /** 112 | * Display the app privacy protocol dialog box. 113 | */ 114 | private void showPrivacyDialog() { 115 | // If a user does not agree to the service agreement, the service agreement dialog is displayed. 116 | if (getPreferences(AdsConstant.SP_PROTOCOL_KEY, AdsConstant.DEFAULT_SP_PROTOCOL_VALUE) == 0) { 117 | Log.i(TAG, "Show protocol dialog."); 118 | ProtocolDialog dialog = new ProtocolDialog(this); 119 | dialog.setCallback(new ProtocolDialog.ProtocolDialogCallback() { 120 | @Override 121 | public void agree() { 122 | sendMessage(CONSENT_MSG_TYPE, MSG_DELAY_MS); 123 | } 124 | 125 | @Override 126 | public void cancel() { 127 | // if the user selects the CANCEL button, exit application. 128 | finish(); 129 | } 130 | }); 131 | dialog.setCanceledOnTouchOutside(false); 132 | dialog.show(); 133 | } else { 134 | sendMessage(CONSENT_MSG_TYPE, MSG_DELAY_MS); 135 | } 136 | } 137 | 138 | /** 139 | * If a user has not set consent, the consent dialog box is displayed. 140 | */ 141 | private void showConsentDialog(List adProviders) { 142 | Log.i(TAG, "Show consent dialog."); 143 | ConsentDialog dialog = new ConsentDialog(this, adProviders); 144 | dialog.setCanceledOnTouchOutside(false); 145 | dialog.show(); 146 | } 147 | 148 | private int getPreferences(String key, int defValue) { 149 | SharedPreferences preferences = getSharedPreferences(AdsConstant.SP_NAME, Context.MODE_PRIVATE); 150 | int value = preferences.getInt(key, defValue); 151 | Log.i(TAG, "Key:" + key + ", Preference value is: " + value); 152 | return value; 153 | } 154 | 155 | private void sendMessage(int what, int delayMillis) { 156 | Message msg = Message.obtain(); 157 | msg.what = what; 158 | mHandler.sendMessageDelayed(msg, delayMillis); 159 | } 160 | 161 | @Override 162 | public boolean onCreateOptionsMenu(Menu menu) { 163 | MenuInflater inflater = getMenuInflater(); 164 | inflater.inflate(R.menu.ad_sample_menu, menu); 165 | return true; 166 | } 167 | 168 | @Override 169 | public boolean onOptionsItemSelected(MenuItem item) { 170 | switch (item.getItemId()) { 171 | case R.id.privacy_settings: 172 | startActivity(new Intent(getApplicationContext(), ProtocolActivity.class)); 173 | break; 174 | case R.id.consent_settings: 175 | startActivity(new Intent(getApplicationContext(), ConsentActivity.class)); 176 | break; 177 | } 178 | return super.onOptionsItemSelected(item); 179 | } 180 | 181 | @Override 182 | public void onConfigurationChanged(Configuration newConfig) { 183 | super.onConfigurationChanged(newConfig); 184 | } 185 | 186 | private void checkConsentStatus() { 187 | final List adProviderList = new ArrayList<>(); 188 | Consent consentInfo = Consent.getInstance(this); 189 | consentInfo.addTestDeviceId("********"); 190 | consentInfo.setDebugNeedConsent(DebugNeedConsent.DEBUG_NEED_CONSENT); 191 | consentInfo.requestConsentUpdate(new ConsentUpdateListener() { 192 | @Override 193 | public void onSuccess(ConsentStatus consentStatus, boolean isNeedConsent, List adProviders) { 194 | Log.i(TAG, "ConsentStatus: " + consentStatus + ", isNeedConsent: " + isNeedConsent); 195 | if (isNeedConsent && consentStatus == ConsentStatus.UNKNOWN) { 196 | if (adProviders != null && adProviders.size() > 0) { 197 | adProviderList.addAll(adProviders); 198 | } 199 | showConsentDialog(adProviderList); 200 | } 201 | } 202 | 203 | @Override 204 | public void onFail(String errorDescription) { 205 | Log.e(TAG, "User's consent status failed to update: " + errorDescription); 206 | if (getPreferences(AdsConstant.SP_CONSENT_KEY, AdsConstant.DEFAULT_SP_CONSENT_VALUE) < 0) { 207 | // In this example, if the request fails, the consent dialog box is still displayed. In this case, the ad publisher list is empty. 208 | showConsentDialog(adProviderList); 209 | } 210 | } 211 | }); 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/NativeActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.view.ViewGroup; 23 | import android.widget.Button; 24 | import android.widget.ScrollView; 25 | import android.widget.RadioButton; 26 | import android.widget.Toast; 27 | 28 | import com.huawei.hms.ads.AdListener; 29 | import com.huawei.hms.ads.AdParam; 30 | import com.huawei.hms.ads.VideoConfiguration; 31 | import com.huawei.hms.ads.nativead.DislikeAdListener; 32 | import com.huawei.hms.ads.nativead.NativeAd; 33 | import com.huawei.hms.ads.nativead.NativeAdConfiguration; 34 | import com.huawei.hms.ads.nativead.NativeAdLoader; 35 | 36 | public class NativeActivity extends BaseActivity { 37 | private static final String TAG = NativeActivity.class.getSimpleName(); 38 | 39 | private RadioButton bigImage; 40 | private RadioButton threeSmall; 41 | private RadioButton smallImage; 42 | private RadioButton videoWithText; 43 | private RadioButton appDownloadBtn; 44 | 45 | private Button loadBtn; 46 | private ScrollView adScrollView; 47 | 48 | private NativeAd globalNativeAd; 49 | 50 | @Override 51 | protected void onCreate(Bundle savedInstanceState) { 52 | super.onCreate(savedInstanceState); 53 | setTitle(getString(R.string.native_ad)); 54 | setContentView(R.layout.activity_native); 55 | 56 | bigImage = findViewById(R.id.radio_button_large); 57 | threeSmall = findViewById(R.id.radio_button_three_small); 58 | smallImage = findViewById(R.id.radio_button_small); 59 | videoWithText = findViewById(R.id.radio_button_video); 60 | appDownloadBtn = findViewById(R.id.radio_button_app_download_button); 61 | 62 | loadBtn = findViewById(R.id.btn_load); 63 | adScrollView = findViewById(R.id.scroll_view_ad); 64 | 65 | loadBtn.setOnClickListener(new View.OnClickListener() { 66 | @Override 67 | public void onClick(View view) { 68 | loadAd(getAdId()); 69 | } 70 | }); 71 | 72 | loadAd(getAdId()); 73 | } 74 | 75 | /** 76 | * Initialize ad slot ID and layout template. 77 | * 78 | * @return ad slot ID 79 | */ 80 | private String getAdId() { 81 | String adId = getString(R.string.ad_id_native); 82 | ; 83 | if (bigImage.isChecked()) { 84 | adId = getString(R.string.ad_id_native); 85 | } else if (smallImage.isChecked()) { 86 | adId = getString(R.string.ad_id_native_small); 87 | } else if (threeSmall.isChecked()) { 88 | adId = getString(R.string.ad_id_native_three); 89 | } else if (videoWithText.isChecked()) { 90 | adId = getString(R.string.ad_id_native_video); 91 | } else if (appDownloadBtn.isChecked()) { 92 | adId = getString(R.string.ad_id_native_video); 93 | } 94 | return adId; 95 | } 96 | 97 | /** 98 | * Load a native ad. 99 | * 100 | * @param adId ad slot ID. 101 | */ 102 | private void loadAd(String adId) { 103 | updateStatus(null, false); 104 | 105 | NativeAdLoader.Builder builder = new NativeAdLoader.Builder(this, adId); 106 | builder.setNativeAdLoadedListener(new NativeAd.NativeAdLoadedListener() { 107 | @Override 108 | public void onNativeAdLoaded(NativeAd nativeAd) { 109 | // Call this method when an ad is successfully loaded. 110 | updateStatus(getString(R.string.status_load_ad_success), true); 111 | 112 | // Display native ad. 113 | showNativeAd(nativeAd); 114 | } 115 | }).setAdListener(new AdListener() { 116 | @Override 117 | public void onAdLoaded() { 118 | updateStatus(getString(R.string.status_load_ad_finish), true); 119 | } 120 | 121 | @Override 122 | public void onAdFailed(int errorCode) { 123 | // Call this method when an ad fails to be loaded. 124 | updateStatus(getString(R.string.status_load_ad_fail) + errorCode, true); 125 | } 126 | }); 127 | 128 | VideoConfiguration videoConfiguration = new VideoConfiguration.Builder() 129 | .setStartMuted(true) 130 | .build(); 131 | 132 | NativeAdConfiguration adConfiguration = new NativeAdConfiguration.Builder() 133 | .setChoicesPosition(NativeAdConfiguration.ChoicesPosition.BOTTOM_RIGHT) // Set custom attributes. 134 | .setVideoConfiguration(videoConfiguration) 135 | .setRequestMultiImages(true) 136 | .build(); 137 | 138 | NativeAdLoader nativeAdLoader = builder.setNativeAdOptions(adConfiguration).build(); 139 | nativeAdLoader.loadAd(new AdParam.Builder().build()); 140 | 141 | updateStatus(getString(R.string.status_ad_loading), false); 142 | } 143 | 144 | /** 145 | * Display native ad. 146 | * 147 | * @param nativeAd native ad object that contains ad materials. 148 | */ 149 | private void showNativeAd(NativeAd nativeAd) { 150 | // Destroy the original native ad. 151 | if (null != globalNativeAd) { 152 | globalNativeAd.destroy(); 153 | } 154 | globalNativeAd = nativeAd; 155 | 156 | final View nativeView = createNativeView(nativeAd, adScrollView); 157 | if (nativeView != null) { 158 | globalNativeAd.setDislikeAdListener(new DislikeAdListener() { 159 | @Override 160 | public void onAdDisliked() { 161 | // Call this method when an ad is closed. 162 | updateStatus(getString(R.string.ad_is_closed), true); 163 | adScrollView.removeView(nativeView); 164 | } 165 | }); 166 | 167 | // Add NativeView to the app UI. 168 | adScrollView.removeAllViews(); 169 | adScrollView.addView(nativeView); 170 | } 171 | } 172 | 173 | /** 174 | * Create a nativeView by creativeType and fill in ad material. 175 | * 176 | * @param nativeAd native ad object that contains ad materials. 177 | * @param parentView parent view of nativeView. 178 | */ 179 | private View createNativeView(NativeAd nativeAd, ViewGroup parentView) { 180 | int createType = nativeAd.getCreativeType(); 181 | Log.i(TAG, "Native ad createType is " + createType); 182 | if (createType == 2 || createType == 102) { 183 | // Large image 184 | return NativeViewFactory.createImageOnlyAdView(nativeAd, parentView); 185 | } else if (createType == 3 || createType == 6) { 186 | // Large image with text or video with text 187 | return NativeViewFactory.createMediumAdView(nativeAd, parentView); 188 | } else if (createType == 103 || createType == 106) { 189 | // Large image with text or Video with text, using AppDownloadButton template. 190 | return NativeViewFactory.createAppDownloadButtonAdView(nativeAd, parentView); 191 | } else if (createType == 7 || createType == 107) { 192 | // Small image with text- 193 | return NativeViewFactory.createSmallImageAdView(nativeAd, parentView); 194 | } else if (createType == 8 || createType == 108) { 195 | // Three small images with text 196 | return NativeViewFactory.createThreeImagesAdView(nativeAd, parentView); 197 | } else { 198 | // Undefined creative type 199 | return null; 200 | } 201 | } 202 | 203 | /** 204 | * Update tip and status of the load button. 205 | * 206 | * @param text tip. 207 | * @param loadBtnEnabled status of the load button. 208 | */ 209 | private void updateStatus(String text, boolean loadBtnEnabled) { 210 | if (null != text) { 211 | Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 212 | } 213 | loadBtn.setEnabled(loadBtnEnabled); 214 | } 215 | 216 | @Override 217 | protected void onDestroy() { 218 | if (null != globalNativeAd) { 219 | globalNativeAd.destroy(); 220 | } 221 | 222 | super.onDestroy(); 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/ProtocolActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.os.Bundle; 20 | 21 | import com.huawei.hms.ads.sdk.dialogs.ProtocolDialog; 22 | 23 | /** 24 | * Activity for displaying privacy information. 25 | */ 26 | public class ProtocolActivity extends BaseActivity implements ProtocolDialog.ProtocolDialogCallback { 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setTitle(getString(R.string.privacy_settings)); 31 | 32 | showProtocolDialog(); 33 | } 34 | 35 | /** 36 | * Display a protocol dialog. 37 | */ 38 | private void showProtocolDialog() { 39 | // Start to process the protocol dialog box. 40 | ProtocolDialog dialog = new ProtocolDialog(this); 41 | dialog.setCallback(this); 42 | dialog.setCanceledOnTouchOutside(false); 43 | dialog.show(); 44 | } 45 | 46 | @Override 47 | public void agree() { 48 | finish(); 49 | } 50 | 51 | @Override 52 | public void cancel() { 53 | finish(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import java.util.Random; 20 | 21 | import android.os.Bundle; 22 | import android.view.View; 23 | import android.widget.Button; 24 | import android.widget.TextView; 25 | import android.widget.Toast; 26 | 27 | import com.huawei.hms.ads.AdParam; 28 | import com.huawei.hms.ads.reward.Reward; 29 | import com.huawei.hms.ads.reward.RewardAd; 30 | import com.huawei.hms.ads.reward.RewardAdLoadListener; 31 | import com.huawei.hms.ads.reward.RewardAdStatusListener; 32 | 33 | /** 34 | * Activity for displaying a rewarded ad. 35 | */ 36 | public class RewardActivity extends BaseActivity { 37 | private static final int PLUS_SCORE = 1; 38 | 39 | private static final int MINUS_SCORE = 5; 40 | 41 | private static final int RANGE = 2; 42 | 43 | private TextView rewardedTitle; 44 | 45 | private TextView scoreView; 46 | 47 | private Button reStartButton; 48 | 49 | private Button watchAdButton; 50 | 51 | private RewardAd rewardedAd; 52 | 53 | private int score = 1; 54 | 55 | private final int defaultScore = 10; 56 | 57 | @Override 58 | protected void onCreate(Bundle savedInstanceState) { 59 | super.onCreate(savedInstanceState); 60 | setTitle(getString(R.string.reward_ad)); 61 | setContentView(R.layout.activity_reward); 62 | 63 | rewardedTitle = findViewById(R.id.text_reward); 64 | rewardedTitle.setText(R.string.reward_ad_title); 65 | 66 | // Load a rewarded ad. 67 | loadRewardAd(); 68 | 69 | // Load a score view. 70 | loadScoreView(); 71 | 72 | // Load the button for watching a rewarded ad. 73 | loadWatchButton(); 74 | 75 | // Load the button for starting a game. 76 | loadPlayButton(); 77 | } 78 | 79 | /** 80 | * Load a rewarded ad. 81 | */ 82 | private void loadRewardAd() { 83 | if (rewardedAd == null) { 84 | rewardedAd = new RewardAd(RewardActivity.this, getString(R.string.ad_id_reward)); 85 | } 86 | 87 | RewardAdLoadListener rewardAdLoadListener = new RewardAdLoadListener() { 88 | @Override 89 | public void onRewardAdFailedToLoad(int errorCode) { 90 | showToast("onRewardAdFailedToLoad " + "errorCode is :" + errorCode); 91 | } 92 | 93 | @Override 94 | public void onRewardedLoaded() { 95 | showToast("onRewardedLoaded"); 96 | } 97 | }; 98 | 99 | rewardedAd.loadAd(new AdParam.Builder().build(), rewardAdLoadListener); 100 | } 101 | 102 | /** 103 | * Display a rewarded ad. 104 | */ 105 | private void rewardAdShow() { 106 | if (rewardedAd.isLoaded()) { 107 | rewardedAd.show(RewardActivity.this, new RewardAdStatusListener() { 108 | @Override 109 | public void onRewardAdClosed() { 110 | showToast("onRewardAdClosed"); 111 | 112 | loadRewardAd(); 113 | } 114 | 115 | @Override 116 | public void onRewardAdFailedToShow(int errorCode) { 117 | showToast("onRewardAdFailedToShow " + "errorCode is :" + errorCode); 118 | } 119 | 120 | @Override 121 | public void onRewardAdOpened() { 122 | showToast("onRewardAdOpened"); 123 | } 124 | 125 | @Override 126 | public void onRewarded(Reward reward) { 127 | // You are advised to grant a reward immediately and at the same time, check whether the reward 128 | // takes effect on the server. If no reward information is configured, grant a reward based on the 129 | // actual scenario. 130 | int addScore = reward.getAmount() == 0 ? defaultScore : reward.getAmount(); 131 | showToast("Watch video show finished , add " + addScore + " scores"); 132 | score += addScore; 133 | setScore(score); 134 | loadRewardAd(); 135 | } 136 | }); 137 | } 138 | } 139 | 140 | private void showToast(final String text) { 141 | runOnUiThread(new Runnable() { 142 | @Override 143 | public void run() { 144 | Toast.makeText(RewardActivity.this, text, Toast.LENGTH_SHORT).show(); 145 | } 146 | }); 147 | } 148 | 149 | /** 150 | * Set a score. 151 | * 152 | * @param score 153 | */ 154 | private void setScore(int score) { 155 | scoreView.setText("Score:" + score); 156 | } 157 | 158 | /** 159 | * Load the button for watching a rewarded ad. 160 | */ 161 | private void loadWatchButton() { 162 | watchAdButton = findViewById(R.id.show_video_button); 163 | watchAdButton.setOnClickListener(new View.OnClickListener() { 164 | @Override 165 | public void onClick(View v) { 166 | rewardAdShow(); 167 | } 168 | }); 169 | } 170 | 171 | /** 172 | * Load the button for starting a game. 173 | */ 174 | private void loadPlayButton() { 175 | reStartButton = findViewById(R.id.play_button); 176 | reStartButton.setOnClickListener(new View.OnClickListener() { 177 | @Override 178 | public void onClick(View v) { 179 | play(); 180 | } 181 | }); 182 | } 183 | 184 | private void loadScoreView() { 185 | scoreView = findViewById(R.id.score_count_text); 186 | scoreView.setText("Score:" + score); 187 | } 188 | 189 | /** 190 | * Used to play a game. 191 | */ 192 | private void play() { 193 | // If the score is 0, a message is displayed, asking users to watch the ad in exchange for scores. 194 | if (score == 0) { 195 | Toast.makeText(RewardActivity.this, "Watch video ad to add score", Toast.LENGTH_SHORT).show(); 196 | return; 197 | } 198 | 199 | // The value 0 or 1 is returned randomly. If the value is 1, the score increases by 1. If the value is 0, the 200 | // score decreases by 5. If the score is a negative number, the score is set to 0. 201 | int random = new Random().nextInt(RANGE); 202 | if (random == 1) { 203 | score += PLUS_SCORE; 204 | Toast.makeText(RewardActivity.this, "You win!", Toast.LENGTH_SHORT).show(); 205 | } else { 206 | score -= MINUS_SCORE; 207 | score = score < 0 ? 0 : score; 208 | Toast.makeText(RewardActivity.this, "You lose!", Toast.LENGTH_SHORT).show(); 209 | } 210 | setScore(score); 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/SplashActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk; 18 | 19 | import android.app.Activity; 20 | import android.content.Intent; 21 | import android.content.pm.ActivityInfo; 22 | import android.content.res.Configuration; 23 | import android.os.Bundle; 24 | import android.os.Handler; 25 | import android.os.Message; 26 | import android.util.Log; 27 | import android.widget.Toast; 28 | 29 | import androidx.annotation.NonNull; 30 | 31 | import com.huawei.hms.ads.AdParam; 32 | import com.huawei.hms.ads.AudioFocusType; 33 | import com.huawei.hms.ads.splash.SplashAdDisplayListener; 34 | import com.huawei.hms.ads.splash.SplashView; 35 | 36 | 37 | public class SplashActivity extends Activity { 38 | private static final String TAG = SplashActivity.class.getSimpleName(); 39 | 40 | // Ad display timeout interval, in milliseconds. 41 | private static final int AD_TIMEOUT = 10000; 42 | 43 | // Ad display timeout message flag. 44 | private static final int MSG_AD_TIMEOUT = 1001; 45 | 46 | /** 47 | * Pause flag. 48 | * On the splash ad screen: 49 | * Set this parameter to true when exiting the app to ensure that the app home screen is not displayed. 50 | * Set this parameter to false when returning to the splash ad screen from another screen to ensure that the app home screen can be displayed properly. 51 | */ 52 | private boolean hasPaused = false; 53 | 54 | // Callback handler used when the ad display timeout message is received. 55 | private Handler timeoutHandler = new Handler(new Handler.Callback() { 56 | @Override 57 | public boolean handleMessage(@NonNull Message msg) { 58 | if (SplashActivity.this.hasWindowFocus()) { 59 | jump(); 60 | } 61 | return false; 62 | } 63 | }); 64 | 65 | private SplashView splashView; 66 | 67 | private SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() { 68 | @Override 69 | public void onAdLoaded() { 70 | // Call this method when an ad is successfully loaded. 71 | Log.i(TAG, "SplashAdLoadListener onAdLoaded."); 72 | Toast.makeText(SplashActivity.this, getString(R.string.status_load_ad_success), Toast.LENGTH_SHORT).show(); 73 | } 74 | 75 | @Override 76 | public void onAdFailedToLoad(int errorCode) { 77 | // Call this method when an ad fails to be loaded. 78 | Log.i(TAG, "SplashAdLoadListener onAdFailedToLoad, errorCode: " + errorCode); 79 | Toast.makeText(SplashActivity.this, getString(R.string.status_load_ad_fail) + errorCode, Toast.LENGTH_SHORT).show(); 80 | jump(); 81 | } 82 | 83 | @Override 84 | public void onAdDismissed() { 85 | // Call this method when the ad display is complete. 86 | Log.i(TAG, "SplashAdLoadListener onAdDismissed."); 87 | Toast.makeText(SplashActivity.this, getString(R.string.status_ad_dismissed), Toast.LENGTH_SHORT).show(); 88 | jump(); 89 | } 90 | }; 91 | 92 | private SplashAdDisplayListener adDisplayListener = new SplashAdDisplayListener() { 93 | @Override 94 | public void onAdShowed() { 95 | // Call this method when an ad is displayed. 96 | Log.i(TAG, "SplashAdDisplayListener onAdShowed."); 97 | } 98 | 99 | @Override 100 | public void onAdClick() { 101 | // Call this method when an ad is clicked. 102 | Log.i(TAG, "SplashAdDisplayListener onAdClick."); 103 | } 104 | }; 105 | 106 | @Override 107 | protected void onCreate(Bundle savedInstanceState) { 108 | super.onCreate(savedInstanceState); 109 | setContentView(R.layout.activity_splash); 110 | 111 | loadAd(); 112 | } 113 | 114 | private void loadAd() { 115 | Log.i(TAG, "Start to load ad"); 116 | // Lock the screen orientation on the device. Your app will automatically adapt to the screen orientation. 117 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED); 118 | int orientation = getScreenOrientation(); 119 | AdParam adParam = new AdParam.Builder().build(); 120 | splashView = findViewById(R.id.splash_ad_view); 121 | splashView.setAdDisplayListener(adDisplayListener); 122 | 123 | String slotId; 124 | // Set the default slogan and the splash ad unit ID based on the screen orientation on the device. 125 | if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { 126 | splashView.setSloganResId(R.drawable.default_slogan); 127 | slotId = getString(R.string.ad_id_splash); 128 | } else { 129 | splashView.setSloganResId(R.drawable.default_slogan_landscape); 130 | slotId = getString(R.string.ad_id_splash_landscape); 131 | } 132 | splashView.setLogo(findViewById(R.id.logo_area)); 133 | 134 | // Set app icon. 135 | splashView.setLogoResId(R.mipmap.ic_launcher); 136 | // Set app name. 137 | splashView.setMediaNameResId(R.string.media_name); 138 | // Set the audio focus type for a video splash ad. 139 | splashView.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE); 140 | 141 | splashView.load(getString(R.string.ad_id_splash), ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, adParam, splashAdLoadListener); 142 | Log.i(TAG, "End to load ad"); 143 | 144 | // Remove the timeout message from the message queue. 145 | timeoutHandler.removeMessages(MSG_AD_TIMEOUT); 146 | // Send a delay message to ensure that the app home screen can be displayed when the ad display times out. 147 | timeoutHandler.sendEmptyMessageDelayed(MSG_AD_TIMEOUT, AD_TIMEOUT); 148 | } 149 | 150 | private int getScreenOrientation() { 151 | Configuration config = getResources().getConfiguration(); 152 | if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 153 | return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 154 | } else { 155 | return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 156 | } 157 | } 158 | 159 | /** 160 | * Switch from the splash ad screen to the app home screen when the ad display is complete. 161 | */ 162 | private void jump() { 163 | Log.i(TAG, "jump hasPaused: " + hasPaused); 164 | if (!hasPaused) { 165 | hasPaused = true; 166 | Log.i(TAG, "jump into application"); 167 | 168 | startActivity(new Intent(SplashActivity.this, MainActivity.class)); 169 | 170 | Handler mainHandler = new Handler(); 171 | mainHandler.postDelayed(new Runnable() { 172 | @Override 173 | public void run() { 174 | finish(); 175 | } 176 | }, 1000); 177 | } 178 | } 179 | 180 | /** 181 | * Set this parameter to true when exiting the app to ensure that the app home screen is not displayed. 182 | */ 183 | @Override 184 | protected void onStop() { 185 | Log.i(TAG, "SplashActivity onStop."); 186 | // Remove the timeout message from the message queue. 187 | timeoutHandler.removeMessages(MSG_AD_TIMEOUT); 188 | hasPaused = true; 189 | super.onStop(); 190 | } 191 | 192 | /** 193 | * Call this method when returning to the splash ad screen from another screen to access the app home screen. 194 | */ 195 | @Override 196 | protected void onRestart() { 197 | Log.i(TAG, "SplashActivity onRestart."); 198 | super.onRestart(); 199 | hasPaused = false; 200 | jump(); 201 | } 202 | 203 | @Override 204 | protected void onDestroy() { 205 | Log.i(TAG, "SplashActivity onDestroy."); 206 | super.onDestroy(); 207 | if (splashView != null) { 208 | splashView.destroyView(); 209 | } 210 | } 211 | 212 | @Override 213 | protected void onPause() { 214 | Log.i(TAG, "SplashActivity onPause."); 215 | super.onPause(); 216 | if (splashView != null) { 217 | splashView.pauseView(); 218 | } 219 | } 220 | 221 | @Override 222 | protected void onResume() { 223 | Log.i(TAG, "SplashActivity onResume."); 224 | super.onResume(); 225 | if (splashView != null) { 226 | splashView.resumeView(); 227 | } 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /app/src/main/java/com/huawei/hms/ads/sdk/dialogs/ProtocolDialog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.hms.ads.sdk.dialogs; 18 | 19 | import android.app.AlertDialog; 20 | import android.app.Dialog; 21 | import android.content.Context; 22 | import android.content.Intent; 23 | import android.content.SharedPreferences; 24 | import android.content.pm.PackageManager; 25 | import android.content.pm.ResolveInfo; 26 | import android.graphics.Color; 27 | import android.graphics.Typeface; 28 | import android.os.Bundle; 29 | import android.text.Spannable; 30 | import android.text.SpannableStringBuilder; 31 | import android.text.method.LinkMovementMethod; 32 | import android.text.method.ScrollingMovementMethod; 33 | import android.text.style.ClickableSpan; 34 | import android.text.style.ForegroundColorSpan; 35 | import android.text.style.StyleSpan; 36 | import android.view.LayoutInflater; 37 | import android.view.View; 38 | import android.view.Window; 39 | import android.widget.Button; 40 | import android.widget.LinearLayout; 41 | import android.widget.TextView; 42 | 43 | import androidx.annotation.NonNull; 44 | 45 | import com.huawei.hms.ads.sdk.AdsConstant; 46 | import com.huawei.hms.ads.sdk.R; 47 | 48 | import java.util.List; 49 | 50 | /** 51 | * Control on privacy-related dialog boxes. 52 | */ 53 | public class ProtocolDialog extends Dialog { 54 | private static final String ACTION_SIMPLE_PRIVACY = "com.huawei.hms.ppskit.ACTION.SIMPLE_PRIVACY"; 55 | 56 | private static final String ACTION_OAID_SETTING = "com.huawei.hms.action.OAID_SETTING"; 57 | 58 | private Context mContext; 59 | 60 | private TextView titleTv; 61 | 62 | private TextView protocolTv; 63 | 64 | private Button confirmButton; 65 | 66 | private Button cancelButton; 67 | 68 | private LayoutInflater inflater; 69 | 70 | private ProtocolDialogCallback mCallback; 71 | 72 | /** 73 | * Privacy protocol dialog box callback interface. 74 | */ 75 | public interface ProtocolDialogCallback { 76 | void agree(); 77 | void cancel(); 78 | } 79 | 80 | /** 81 | * Constructor. 82 | * 83 | * @param context context 84 | */ 85 | public ProtocolDialog(@NonNull Context context) { 86 | // Customize a dialog box style. 87 | super(context, R.style.dialog); 88 | mContext = context; 89 | } 90 | 91 | /** 92 | * Set a dialog box callback. 93 | * 94 | * @param callback callback 95 | */ 96 | public void setCallback(ProtocolDialogCallback callback) { 97 | mCallback = callback; 98 | } 99 | 100 | @Override 101 | protected void onCreate(Bundle savedInstanceState) { 102 | super.onCreate(savedInstanceState); 103 | 104 | Window dialogWindow = getWindow(); 105 | dialogWindow.requestFeature(Window.FEATURE_NO_TITLE); 106 | 107 | inflater = LayoutInflater.from(mContext); 108 | LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.dialog_protocol, null); 109 | setContentView(rootView); 110 | 111 | titleTv = findViewById(R.id.uniform_dialog_title); 112 | titleTv.setText(mContext.getString(R.string.protocol_title)); 113 | protocolTv = findViewById(R.id.protocol_center_content); 114 | 115 | initClickableSpan(); 116 | initButtonBar(rootView); 117 | } 118 | 119 | /** 120 | * Initialize the page of the button bar. 121 | * 122 | * @param rootView rootView 123 | */ 124 | private void initButtonBar(LinearLayout rootView) { 125 | confirmButton = rootView.findViewById(R.id.base_okBtn); 126 | confirmButton.setOnClickListener(new View.OnClickListener() { 127 | @Override 128 | public void onClick(View v) { 129 | SharedPreferences preferences = 130 | mContext.getSharedPreferences(AdsConstant.SP_NAME, Context.MODE_PRIVATE); 131 | SharedPreferences.Editor editor = preferences.edit(); 132 | editor.putInt(AdsConstant.SP_PROTOCOL_KEY, 1).commit(); 133 | dismiss(); 134 | 135 | if (mCallback != null) { 136 | mCallback.agree(); 137 | } 138 | } 139 | }); 140 | 141 | cancelButton = rootView.findViewById(R.id.base_cancelBtn); 142 | cancelButton.setOnClickListener(new View.OnClickListener() { 143 | @Override 144 | public void onClick(View v) { 145 | dismiss(); 146 | if (mCallback != null) { 147 | mCallback.cancel(); 148 | } 149 | } 150 | }); 151 | } 152 | 153 | private void initClickableSpan() { 154 | // Add a text-based tapping event. 155 | protocolTv.setMovementMethod(ScrollingMovementMethod.getInstance()); 156 | String privacyInfoText = mContext.getString(R.string.protocol_content_text); 157 | final SpannableStringBuilder spanPrivacyInfoText = new SpannableStringBuilder(privacyInfoText); 158 | 159 | // Set the listener on the event for tapping some text. 160 | ClickableSpan personalizedAdsTouchHere = new ClickableSpan() { 161 | @Override 162 | public void onClick(View widget) { 163 | startActivity(ACTION_OAID_SETTING); 164 | } 165 | }; 166 | 167 | int privacyStart = mContext.getResources().getInteger(R.integer.privacy_start); 168 | int privacyEnd = mContext.getResources().getInteger(R.integer.privacy_end); 169 | StyleSpan privacySpan = new StyleSpan(Typeface.BOLD); 170 | spanPrivacyInfoText.setSpan(privacySpan, privacyStart, privacyEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 171 | 172 | ForegroundColorSpan colorPersonalize = new ForegroundColorSpan(Color.BLUE); 173 | int personalizedStart = mContext.getResources().getInteger(R.integer.personalized_start); 174 | int personalizedEnd = mContext.getResources().getInteger(R.integer.personalized_end); 175 | spanPrivacyInfoText.setSpan(personalizedAdsTouchHere, personalizedStart, personalizedEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 176 | spanPrivacyInfoText.setSpan(colorPersonalize, personalizedStart, personalizedEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 177 | 178 | protocolTv.setText(spanPrivacyInfoText); 179 | protocolTv.setMovementMethod(LinkMovementMethod.getInstance()); 180 | } 181 | 182 | private void startActivity(String action) { 183 | Intent enterNative = new Intent(action); 184 | PackageManager pkgMng = mContext.getPackageManager(); 185 | if (pkgMng != null) { 186 | List list = pkgMng.queryIntentActivities(enterNative, 0); 187 | if (!list.isEmpty()) { 188 | enterNative.setPackage("com.huawei.hwid"); 189 | mContext.startActivity(enterNative); 190 | } else { 191 | // A message is displayed, indicating that no function is available and asking users to install HMS Core of the latest version. 192 | addAlertView(); 193 | } 194 | } 195 | } 196 | 197 | /** 198 | * Prompt dialog box, indicating that no function is available and asking users to install Huawei Mobile Services (APK) of the latest version. 199 | */ 200 | private void addAlertView() { 201 | AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 202 | builder.setTitle(mContext.getString(R.string.alert_title)); 203 | builder.setMessage(mContext.getString(R.string.alert_content)); 204 | builder.setPositiveButton(mContext.getString(R.string.alert_confirm), null); 205 | builder.show(); 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/btn_check_off_emui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xhdpi/btn_check_off_emui.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/btn_check_on_emui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xhdpi/btn_check_on_emui.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/btn_check_on_pressed_emui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xhdpi/btn_check_on_pressed_emui.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_sb_cancel_gray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xhdpi/ic_sb_cancel_gray.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/popup_full_bright_emui.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xhdpi/popup_full_bright_emui.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/btn_default_disabled_emui.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xxhdpi/btn_default_disabled_emui.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/btn_default_normal_emui.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xxhdpi/btn_default_normal_emui.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/btn_default_pressed_emui.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable-xxhdpi/btn_default_pressed_emui.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/app_whythisad_info.xml: -------------------------------------------------------------------------------- 1 | 3 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_check_off_emui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable/btn_check_off_emui.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_check_on_emui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable/btn_check_on_emui.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/btn_check_on_pressed_emui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable/btn_check_on_pressed_emui.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/default_slogan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable/default_slogan.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/default_slogan_landscape.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/drawable/default_slogan_landscape.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/detail_checkbox_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/emui_button_select.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/native_button_rounded_corners_shape.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/native_flag_rounded_corners_shape.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_banner.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 14 | 15 | 24 | 25 | 33 | 34 | 42 | 43 | 44 | 45 | 53 | 54 | 59 | 60 | 66 | 67 | 72 | 73 | 78 | 79 | 84 | 85 | 90 | 91 | 96 | 97 | 98 | 103 | 104 | 110 | 111 | 116 | 117 | 122 | 123 | 128 | 129 | 130 | 131 |