├── .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 |
141 |
142 |
149 |
150 |
158 |
159 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_bidding_splash.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_consent.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
23 |
24 |
31 |
32 |
33 |
40 |
41 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_instream.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
12 |
17 |
25 |
26 |
27 |
32 |
33 |
49 |
50 |
65 |
66 |
82 |
83 |
94 |
95 |
111 |
112 |
113 |
120 |
126 |
131 |
136 |
137 |
144 |
149 |
154 |
155 |
156 |
157 |
158 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_interstitial.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
15 |
16 |
24 |
25 |
33 |
34 |
40 |
41 |
47 |
48 |
53 |
54 |
55 |
56 |
65 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_native.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
11 |
12 |
22 |
23 |
27 |
28 |
34 |
35 |
41 |
42 |
47 |
48 |
53 |
54 |
59 |
60 |
65 |
66 |
67 |
74 |
75 |
76 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_reward.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
15 |
16 |
24 |
25 |
33 |
34 |
42 |
43 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_splash.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
15 |
16 |
23 |
24 |
31 |
32 |
36 |
37 |
44 |
45 |
52 |
53 |
54 |
62 |
63 |
64 |
65 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_consent.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
25 |
26 |
27 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_consent_content.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
20 |
21 |
29 |
30 |
40 |
41 |
50 |
51 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_consent_moreinfo.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
20 |
21 |
29 |
30 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_consent_partner_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
19 |
20 |
30 |
31 |
39 |
40 |
48 |
49 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_protocol.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
25 |
26 |
27 |
36 |
37 |
50 |
51 |
52 |
53 |
60 |
61 |
71 |
72 |
81 |
82 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/native_ad_with_app_download_btn_template.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
19 |
20 |
25 |
33 |
34 |
41 |
48 |
49 |
57 |
64 |
65 |
66 |
67 |
73 |
81 |
82 |
88 |
89 |
96 |
97 |
103 |
110 |
111 |
117 |
118 |
125 |
126 |
127 |
128 |
129 |
135 |
136 |
151 |
152 |
164 |
165 |
166 |
167 |
168 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/native_common_medium_template.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
19 |
20 |
25 |
26 |
35 |
36 |
46 |
47 |
61 |
62 |
63 |
69 |
70 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/native_image_only_template.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
19 |
20 |
33 |
34 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/native_small_image_template.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
24 |
25 |
33 |
39 |
40 |
46 |
51 |
52 |
60 |
61 |
74 |
82 |
83 |
90 |
98 |
99 |
106 |
107 |
115 |
116 |
123 |
131 |
132 |
139 |
140 |
148 |
149 |
150 |
151 |
156 |
157 |
169 |
170 |
171 |
172 |
173 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/native_small_template.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
26 |
27 |
36 |
37 |
45 |
46 |
55 |
56 |
69 |
70 |
79 |
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/native_three_images_template.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
17 |
25 |
26 |
27 |
31 |
32 |
38 |
39 |
45 |
46 |
52 |
53 |
54 |
59 |
60 |
65 |
71 |
72 |
79 |
80 |
93 |
100 |
101 |
108 |
116 |
117 |
123 |
124 |
132 |
133 |
139 |
147 |
148 |
154 |
155 |
163 |
164 |
165 |
166 |
171 |
172 |
184 |
185 |
186 |
187 |
188 |
189 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/native_video_template.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
19 |
20 |
25 |
26 |
35 |
36 |
48 |
49 |
62 |
63 |
64 |
70 |
71 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/ad_sample_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values-zh-rCN/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | HUAWEI Ads Demo
3 | Banner Ad
4 | Native Ad
5 | Reward Ad
6 | Interstitial Ad
7 | Instream Ad
8 | Privacy Settings
9 | Bidding Splash Ad
10 | Consent Settings
11 | Load Ad
12 |
13 |
14 | testw6vs28auh3
15 | SIZE_320*50
16 | SIZE_320*100
17 | SIZE_SMART
18 | SIZE_300*250
19 | SIZE_360*57
20 | SIZE_360*144
21 | Black
22 | White
23 | Red
24 | Transparent
25 | Size:
26 | Color:
27 |
28 |
29 | testu7m3hc4gvm
30 | testb65czjivt9
31 | testr6w14o0hqz
32 | testy63txaom86
33 | AD
34 | Display form:
35 | Large image with text.
36 | Three images with text.
37 | Small image with text.
38 | Video with text.
39 | Ad with app download button.
40 | Ad loading state: being loaded.
41 | Ad loading state: loaded successfully.
42 | Ad loading state: loaded finish.
43 | Ad loading state: failed to be loaded. Error code:
44 | Video playback state: starting to be played.
45 | Video playback state: being played.
46 | Video playback state: playback completed.
47 | Ad is closed.
48 | 版本
49 | 介绍
50 | 隐私
51 | 权限
52 |
53 |
54 | testx9dtjwj8hp
55 | This is rewarded ads sample.
56 | Play
57 | Watch Video
58 |
59 |
60 | testd7c5cewoj6
61 | testq6zq98hecj
62 | SDK Demo
63 | SDK Demo
64 | Copyright 2020. XXX Technologies Co., Ltd
65 | Splash Ad is dismissed.
66 |
67 |
68 | This is interstitial ad sample.
69 | teste9ih9j0rc3
70 | testb4znbuh3n2
71 | Load Ad
72 | Image
73 | Video
74 |
75 |
76 | Your video content
77 | Skip
78 | Learn more
79 | testy3cglm3pj0
80 | Normal video is playing
81 | Instream ads is playing
82 | Load Ad
83 | Register
84 | Mute
85 | UnMute
86 | Play
87 | Pause
88 | Loading
89 | Ad Loaded
90 |
91 |
92 | Privacy Example of HUAWEI X
93 |
94 | 1.Privacy description\n
95 | The HuaweiAdsSdk_Demo is software providing a code demo for the HUAWEI Ads SDK. Connecting to the network, the software collects and processes information to identify devices, providing customized services or ads. If you do not agree to collect the preceding information or do not agree to call related permissions or functions of your mobile phones, the software cannot run properly. You can stop data collection and uploading by uninstalling or exiting this software.\n\n
96 | 2.Demo description\n
97 | This demo is for reference only. Modify the content based on the user protocol specifications. \n\n
98 | 3.Advertising and marketing\n
99 | We will create a user group based on your personal information, collect your device information, usage information, and ad interaction information in this app, and display more relevant personalized ads and other promotion content. In this process, we will strictly protect your privacy. You can learn more about how we collect and use your personal information in personalized ads based on Ads and Privacy.
100 | If you want to restrict personalized ads, you can tap here to open the ad setting page and enable the function of restricting personalized ads. After the function is enabled, you will still receive equivalent number of ads. However, the ad relevance will be reduced.
101 |
102 | 1033
103 | 1048
104 | 1104
105 | 1108
106 | Don\'t remind me again
107 | Cancel
108 | Setting
109 | Agree
110 | Cancel
111 | Reminder
112 | HMS Core of this version does not have the related function. Please install HMS Core (APK) of the latest version in AppGallery.
113 | OK
114 |
115 |
116 | Consent Example of HUAWEI X
117 | Privacy Policy
118 | Close
119 | Do Not Load Ads
120 | The Ads in HUAWEI X is provided in collaboration with our advertising partners. To provide this service, we need to share certain information about you with these partners, including your city-level location as well as your usage records for the news service.\n\n
121 | For more information about our partners and how your data is processed, please touch here.\n\n
122 | By touching AGREE, you indicate that you agree to share the above personal information with our partners so that they can provide you with personalized advertisements on behalf of their customers, based on interests and preferences identified or predicted through analysis of your personal information.
123 | \n\nYou can withdraw your consent at any time by going to [app settings].
124 | \n\nIf you touch SKIP, your data will not be shared with our partners and you will not receive personalized ads.
125 | 347
126 | 351
127 | The Ads in HUAWEI X is provided in collaboration with our partners. You can find a full list of our partners for each country/region here.\n\n
128 | In order to provide you with personalized advertisements, we need to share the following information with our partners:\n\n
129 | • User information, including advertising ID, city of residence, country, and language.\n\n
130 | • Device information, including device name and model, operating system version, screen size, and network type.\n\n
131 | • Service usage information, including news ID and records of views, clicks, dislikes, shares, and comments for news content and advertisements.\n\n
132 | With your consent, the above information will be shared with our partners so that they can provide you with personalized advertisements on behalf of their customers, based on interests and preferences identified or predicted through analysis of your personal information.\n\n
133 | You can withdraw your consent at any time by going to app settings.\n\n
134 | Without your consent, no data will be shared with our partners and you will not receive personalized ads.
135 | 133
136 | 137
137 | You can find a full list of our partners for each country/region here:\n\n
138 | Back
139 | Agree
140 | Skip
141 | You can change this setting anytime in the app.
142 | Thank you!
143 | This is a Consent Example
144 |
145 | Load Personalized Ads.
146 | Load Non-Personalized Ads.
147 | Do Not Load Ads.
148 |
149 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 | #e6000000
8 | #000000
9 | #ffffff
10 | #214EF3
11 | #338D8D8D
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 200dp
6 | 3dp
7 | 5dp
8 | 8dp
9 | 16dp
10 | 100dp
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #26A69A
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/test/java/com/huawei/hms/ads/sdk/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.huawei.hms.ads.sdk;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | google()
6 | jcenter()
7 | maven { url 'https://developer.huawei.com/repo/' }
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:4.1.1'
11 |
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | }
15 | }
16 |
17 | allprojects {
18 | repositories {
19 | google()
20 | jcenter()
21 | maven { url 'https://developer.huawei.com/repo/' }
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Nov 23 12:07:20 IST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/result/Banner.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/result/Banner.gif
--------------------------------------------------------------------------------
/result/Interstitial.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/result/Interstitial.gif
--------------------------------------------------------------------------------
/result/Native.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/result/Native.gif
--------------------------------------------------------------------------------
/result/Reward.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/result/Reward.gif
--------------------------------------------------------------------------------
/result/Roll.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/result/Roll.gif
--------------------------------------------------------------------------------
/result/Splash.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HMS-Core/hms-ads-demo-java/2c34f38d459f091b0f948861971a19d36985c878/result/Splash.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='HuaweiAdsSdk_Demo'
3 |
--------------------------------------------------------------------------------