├── image ├── main.png ├── result.png ├── suggest.png ├── news-search.gif ├── web-search.gif ├── custom-search.gif ├── image-search.gif └── video-search.gif ├── SearchKit_android_SampleCode ├── settings.gradle ├── app │ ├── src │ │ ├── main │ │ │ ├── res │ │ │ │ ├── drawable │ │ │ │ │ ├── net_error.png │ │ │ │ │ ├── selectedittextshape.xml │ │ │ │ │ ├── shape_linear.xml │ │ │ │ │ ├── shape_search_text.xml │ │ │ │ │ ├── ic_public_search.xml │ │ │ │ │ └── ic_launcher_background.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ │ ├── ic_launcher.png │ │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ │ ├── ic_launcher.png │ │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ │ ├── ic_launcher.png │ │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ │ ├── ic_launcher.png │ │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ │ ├── ic_launcher.png │ │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── values │ │ │ │ │ ├── colors.xml │ │ │ │ │ ├── strings.xml │ │ │ │ │ └── styles.xml │ │ │ │ ├── layout │ │ │ │ │ ├── fragment_video.xml │ │ │ │ │ ├── fragment_web.xml │ │ │ │ │ ├── recycler_suggest.xml │ │ │ │ │ ├── fragment_image.xml │ │ │ │ │ ├── fragment_news.xml │ │ │ │ │ ├── recycler_web.xml │ │ │ │ │ ├── recycler_content.xml │ │ │ │ │ ├── activity_search.xml │ │ │ │ │ ├── activity_web.xml │ │ │ │ │ ├── activity_news.xml │ │ │ │ │ ├── activity_video.xml │ │ │ │ │ └── activity_image.xml │ │ │ │ └── drawable-v24 │ │ │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── java │ │ │ │ └── com │ │ │ │ │ └── huawei │ │ │ │ │ └── searchindex │ │ │ │ │ ├── network │ │ │ │ │ ├── UrlHelper.java │ │ │ │ │ ├── QueryService.java │ │ │ │ │ └── NetworkManager.java │ │ │ │ │ ├── bean │ │ │ │ │ ├── ListBean.java │ │ │ │ │ └── TokenResponse.java │ │ │ │ │ ├── adapter │ │ │ │ │ ├── ViewPagerAdapter.java │ │ │ │ │ ├── SuggestAdapter.java │ │ │ │ │ ├── WebAdapter.java │ │ │ │ │ └── ContentAdapter.java │ │ │ │ │ ├── utils │ │ │ │ │ └── AnimationUtils.java │ │ │ │ │ ├── activity │ │ │ │ │ ├── WebActivity.java │ │ │ │ │ ├── NewsActivity.java │ │ │ │ │ ├── VideoActivity.java │ │ │ │ │ ├── ImageActivity.java │ │ │ │ │ └── SearchActivity.java │ │ │ │ │ └── fragment │ │ │ │ │ ├── WebFragment.java │ │ │ │ │ ├── NewsFragment.java │ │ │ │ │ ├── VideoFragment.java │ │ │ │ │ └── ImageFragment.java │ │ │ └── AndroidManifest.xml │ │ ├── test │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── huawei │ │ │ │ └── searchindex │ │ │ │ └── ExampleUnitTest.java │ │ └── androidTest │ │ │ └── java │ │ │ └── com │ │ │ └── huawei │ │ │ └── searchindex │ │ │ └── ExampleInstrumentedTest.java │ ├── proguard-rules.pro │ ├── agconnect-services.json │ └── build.gradle ├── build.gradle └── gradle.properties ├── README_ZH.md ├── README.md └── LICENSE /image/main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/main.png -------------------------------------------------------------------------------- /image/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/result.png -------------------------------------------------------------------------------- /image/suggest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/suggest.png -------------------------------------------------------------------------------- /image/news-search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/news-search.gif -------------------------------------------------------------------------------- /image/web-search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/web-search.gif -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name='SearchKit_CodeLab' 2 | include ':app' 3 | -------------------------------------------------------------------------------- /image/custom-search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/custom-search.gif -------------------------------------------------------------------------------- /image/image-search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/image-search.gif -------------------------------------------------------------------------------- /image/video-search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/image/video-search.gif -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/drawable/net_error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/drawable/net_error.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HMS-Core/hms-search-demo/HEAD/SearchKit_android_SampleCode/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/drawable/selectedittextshape.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/drawable/shape_linear.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/drawable/shape_search_text.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | #007CE5 7 | 8 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SearchKitCodeLab 3 | 4 | Web 5 | Image 6 | Video 7 | News 8 | aaa 9 | 10 | Default Title(Network error!) 11 | 12 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/test/java/com/huawei/searchindex/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.huawei.searchindex; 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 | } -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/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 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/drawable/ic_public_search.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/androidTest/java/com/huawei/searchindex/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.huawei.searchindex; 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.example.searchindex", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/fragment_video.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 24 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/fragment_web.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 24 | 25 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/recycler_suggest.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 20 | 21 | 26 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/fragment_image.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 24 | 25 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/fragment_news.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 24 | 25 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | // maven { url 'http://szg1.artifactory.inhuawei.com/artifactory/sz-maven-public/' } 9 | maven {url 'https://developer.huawei.com/repo/'} 10 | 11 | } 12 | dependencies { 13 | classpath 'com.android.tools.build:gradle:3.5.4' 14 | classpath 'com.huawei.agconnect:agcp:1.4.1.300' 15 | 16 | 17 | // NOTE: Do not place your application dependencies here; they belong 18 | // in the individual module build.gradle files 19 | } 20 | } 21 | 22 | allprojects { 23 | repositories { 24 | google() 25 | jcenter() 26 | // maven { url 'http://szg1.artifactory.inhuawei.com/artifactory/sz-maven-public/' } 27 | maven {url 'https://developer.huawei.com/repo/'} 28 | 29 | } 30 | } 31 | 32 | task clean(type: Delete) { 33 | delete rootProject.buildDir 34 | } 35 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/network/UrlHelper.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.searchindex.network; 17 | 18 | public class UrlHelper { 19 | /** 20 | * The Content REQUEST_TOKEN. 21 | */ 22 | public static final String REQUEST_TOKEN = "oauth2/v3/token"; 23 | // public static final String REQUEST_TOKEN = "oauth2/v2/token/https://logintestlf.hwcloudtest.cn/"; 24 | } 25 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/agconnect-services.json: -------------------------------------------------------------------------------- 1 | { 2 | "agcgw":{ 3 | "backurl":"connect-dre.dbankcloud.cn", 4 | "url":"connect-dre.hispace.hicloud.com" 5 | }, 6 | "client":{ 7 | "cp_id":"890375200000000009", 8 | "product_id":"736430079244664609", 9 | "client_id":"472143567276474368", 10 | "client_secret":"8421EA5FE4B1068E2D64B774BAAFDC9BC0F3618DD7A894ACCEF91F85CAF91635", 11 | "app_id":"103039265", 12 | "package_name":"com.huawei.searchindex", 13 | "api_key":"CgB6e3x9CgAoJqdNxEtRYjesrnkjhW23G5SLHXeuV+bmFuaYq26UlEG81soWHQyRzKhpN+C5BFc+o5lLBQWKzONa" 14 | }, 15 | "service":{ 16 | "analytics":{ 17 | "collector_url":"datacollector-dre.dt.hicloud.com,datacollector-dre.dt.dbankcloud.cn", 18 | "resource_id":"p1", 19 | "channel_id":"" 20 | }, 21 | "search":{ 22 | "url":"https://search-dre.cloud.huawei.com" 23 | }, 24 | "cloudstorage":{ 25 | "storage_url":"https://ops-dre.agcstorage.link" 26 | }, 27 | "ml":{ 28 | "mlservice_url":"ml-api-dre.ai.dbankcloud.com,ml-api-dre.ai.dbankcloud.cn" 29 | } 30 | }, 31 | "region":"DE", 32 | "configuration_version":"1.0" 33 | } -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/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 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/network/QueryService.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.searchindex.network; 17 | 18 | import com.huawei.searchindex.bean.TokenResponse; 19 | 20 | import io.reactivex.Observable; 21 | import retrofit2.http.Field; 22 | import retrofit2.http.FormUrlEncoded; 23 | import retrofit2.http.POST; 24 | 25 | public interface QueryService { 26 | @FormUrlEncoded 27 | @POST(UrlHelper.REQUEST_TOKEN) 28 | Observable getRequestToken( 29 | @Field("grant_type") String grantType, 30 | @Field("client_id") String ClientId, 31 | @Field("client_secret") String clientSecret); 32 | } 33 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/bean/ListBean.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.searchindex.bean; 17 | 18 | public class ListBean { 19 | String title; 20 | String url; 21 | String click_url; 22 | 23 | public String getClick_url() { 24 | return click_url; 25 | } 26 | 27 | public void setClick_url(String click_url) { 28 | this.click_url = click_url; 29 | } 30 | 31 | public String getTitle() { 32 | return title; 33 | } 34 | 35 | public void setTitle(String title) { 36 | this.title = title; 37 | } 38 | 39 | public String getUrl() { 40 | return url; 41 | } 42 | 43 | public void setUrl(String url) { 44 | this.url = url; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/bean/TokenResponse.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.searchindex.bean; 17 | 18 | public class TokenResponse { 19 | String access_token; 20 | Integer expires_in; 21 | String token_type; 22 | 23 | public String getAccess_token() { 24 | return access_token; 25 | } 26 | 27 | public void setAccess_token(String access_token) { 28 | this.access_token = access_token; 29 | } 30 | 31 | public Integer getExpires_in() { 32 | return expires_in; 33 | } 34 | 35 | public void setExpires_in(Integer expires_in) { 36 | this.expires_in = expires_in; 37 | } 38 | 39 | public String getToken_type() { 40 | return token_type; 41 | } 42 | 43 | public void setToken_type(String token_type) { 44 | this.token_type = token_type; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/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.searchindex" 8 | minSdkVersion 24 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | repositories { 22 | flatDir { 23 | dirs 'libs' 24 | } 25 | } 26 | 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | 32 | implementation 'androidx.appcompat:appcompat:1.1.0' 33 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 34 | testImplementation 'junit:junit:4.12' 35 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 36 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 37 | implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: []) 38 | 39 | implementation 'com.google.android.material:material:1.2.1' 40 | implementation 'androidx.recyclerview:recyclerview:1.1.0' 41 | implementation 'com.github.bumptech.glide:glide:4.10.0' 42 | 43 | //rxJava 44 | implementation 'io.reactivex.rxjava2:rxjava:2.2.19' 45 | implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' 46 | implementation 'com.squareup.retrofit2:retrofit:2.5.0' 47 | implementation 'com.squareup.retrofit2:converter-gson:2.5.0' 48 | implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0' 49 | 50 | implementation 'com.huawei.hms:searchkit:5.0.4.303' 51 | } 52 | apply plugin: 'com.huawei.agconnect' 53 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/recycler_web.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 | 26 | 27 | 38 | 39 | 49 | 50 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/recycler_content.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 31 | 32 | 40 | 41 | 50 | 51 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/adapter/ViewPagerAdapter.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.searchindex.adapter; 17 | 18 | import android.content.Context; 19 | import androidx.annotation.Nullable; 20 | import androidx.fragment.app.Fragment; 21 | import androidx.fragment.app.FragmentManager; 22 | import androidx.fragment.app.FragmentPagerAdapter; 23 | 24 | import com.huawei.searchindex.R; 25 | import com.huawei.searchindex.fragment.ImageFragment; 26 | import com.huawei.searchindex.fragment.NewsFragment; 27 | import com.huawei.searchindex.fragment.VideoFragment; 28 | import com.huawei.searchindex.fragment.WebFragment; 29 | 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | public class ViewPagerAdapter extends FragmentPagerAdapter { 34 | private List fragments = new ArrayList<>(); 35 | 36 | private Context context; 37 | 38 | private int[] titleids = { 39 | R.string.fragment_web, R.string.fragment_image, R.string.fragment_video, R.string.fragment_news 40 | }; 41 | 42 | public ViewPagerAdapter(FragmentManager fm, Context context) { 43 | super(fm); 44 | fragments.add(WebFragment.newInstance()); 45 | fragments.add(ImageFragment.newInstance()); 46 | fragments.add(VideoFragment.newInstance()); 47 | fragments.add(NewsFragment.newInstance()); 48 | this.context = context; 49 | } 50 | 51 | @Override 52 | public Fragment getItem(int position) { 53 | return fragments.get(position); 54 | } 55 | 56 | @Override 57 | public int getCount() { 58 | return fragments.size(); 59 | } 60 | 61 | @Nullable 62 | @Override 63 | public CharSequence getPageTitle(int position) { 64 | return context.getString(titleids[position]); 65 | } 66 | 67 | public List getFragments() { 68 | return fragments; 69 | } 70 | 71 | public int getPageTitleSize() { 72 | return titleids.length; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /README_ZH.md: -------------------------------------------------------------------------------- 1 | # 华为搜索服务示例代码 2 | 3 | 中文 | [English](README.md) 4 | 5 | ## 目录 6 | 7 | * [简介](#简介) 8 | * [安装](#安装) 9 | * [环境要求](#环境要求) 10 | * [配置](#配置) 11 | * [示例代码](#示例代码) 12 | * [授权许可](#授权许可) 13 | 14 | 15 | ## 简介 16 | 本实例代码封装华为搜索服务(HUAWEI Search Kit)安卓接口,提供样例代码供您参考。 17 | 18 | ## 安装 19 | 下载并解压缩示例代码包。 20 | 21 | 刷新项目,确保项目同步成功。 22 | 23 | ## 环境要求 24 | 建议使用Search Kit 5.0.4.305及以上版本。 25 | Android SDK运行要求Android API为24(Android 7.0)及以上版本。 26 | Android studio. 27 | 28 | ## 配置 29 | 1. 在华为开发者联盟注册成为开发者并登陆。 30 | 2. 创建一个项目,在项目下添加应用,并设置应用包名。 31 | 3. 在"项目设置 -> API管理"中,找到Search Kit,打开API开关。 32 | 4. 在"项目设置 -> 常规"中,点击"项目"中"数据存储位置"后的"设置",在弹出的对话框中选择一个数据存储位置。 33 | 5. 下载"agconnect-services.json"文件并将其拷贝到应用级根目录下。 34 | 6. 在项目级build.gradle文件中配置Maven仓地址: maven {url 'https://developer.huawei.com/repo/'} 和插件地址: 'com.huawei.agconnect:agcp:1.3.1.300'。 35 | 7. 在应用级build.gradle文件到最后一行添加: 'com.huawei.agconnect'。 36 | 8. 在应用级build.gradle文件中添加依赖 com.huawei.hms:searchkit:5.0.4.305。 37 | 9. 同步项目。 38 | 39 | ## 示例代码 40 | 41 | 1. 网页搜索示例代码: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 42 | 43 | 2. 图片搜索示例代码: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 44 | 45 | 3. 视频搜索示例代码: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 46 | 47 | 4. 新闻搜索示例代码: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 48 | 49 | 5. Custom search示例代码: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 50 | 51 | 6. 文本补齐示例代码: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 52 | 53 | 7. 拼写检查示例代码: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 54 | 55 | 56 | ## 运行结果 57 | **网页搜索** 58 | 59 | 60 | 61 | **图片搜索** 62 | 63 | 64 | 65 | **视频搜索** 66 | 67 | 68 | 69 | **新闻搜索** 70 | 71 | 72 | 73 | **个性化搜索** 74 | 75 | 76 | 77 | ## 授权许可 78 | 华为搜索服务示例代码经过[Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0)授权许可. 79 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/adapter/SuggestAdapter.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.searchindex.adapter; 17 | 18 | import android.view.LayoutInflater; 19 | import android.view.View; 20 | import android.view.ViewGroup; 21 | import android.widget.TextView; 22 | import androidx.annotation.NonNull; 23 | import androidx.recyclerview.widget.RecyclerView; 24 | 25 | import com.huawei.searchindex.R; 26 | 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | 30 | public class SuggestAdapter extends RecyclerView.Adapter { 31 | private List suggestList = new ArrayList<>(); 32 | 33 | public SuggestAdapter(List list) { 34 | this.suggestList = list; 35 | } 36 | 37 | @NonNull 38 | @Override 39 | public SuggestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 40 | View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_suggest, parent, false); 41 | return new SuggestViewHolder(v); 42 | } 43 | 44 | @Override 45 | public void onBindViewHolder(@NonNull SuggestViewHolder holder, final int position) { 46 | if (suggestList != null && !suggestList.isEmpty()) { 47 | holder.tv_suggest.setText(suggestList.get(position)); 48 | 49 | holder.itemView.setOnClickListener( 50 | new View.OnClickListener() { 51 | @Override 52 | public void onClick(View v) { 53 | onItemClickListener.click(suggestList.get(position)); 54 | } 55 | }); 56 | } 57 | } 58 | 59 | @Override 60 | public int getItemCount() { 61 | return suggestList.size(); 62 | } 63 | 64 | public static class SuggestViewHolder extends RecyclerView.ViewHolder { 65 | public TextView tv_suggest; 66 | 67 | public SuggestViewHolder(@NonNull View itemView) { 68 | super(itemView); 69 | tv_suggest = itemView.findViewById(R.id.tv_suggest); 70 | } 71 | } 72 | 73 | public void clear() { 74 | if (suggestList != null && !suggestList.isEmpty()) { 75 | suggestList.clear(); 76 | notifyDataSetChanged(); 77 | } 78 | } 79 | 80 | private OnItemClickListener onItemClickListener; 81 | 82 | public void setOnClickListener(OnItemClickListener onClickListener) { 83 | this.onItemClickListener = onClickListener; 84 | } 85 | 86 | public interface OnItemClickListener { 87 | void click(String text); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/utils/AnimationUtils.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.searchindex.utils; 17 | 18 | import android.view.View; 19 | import android.view.ViewGroup; 20 | import android.view.animation.Animation; 21 | import android.view.animation.Transformation; 22 | 23 | public class AnimationUtils { 24 | public static void expand(final View v) { 25 | int matchParentMeasureSpec = 26 | View.MeasureSpec.makeMeasureSpec(((View) v.getParent()).getWidth(), View.MeasureSpec.EXACTLY); 27 | int wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 28 | v.measure(matchParentMeasureSpec, wrapContentMeasureSpec); 29 | final int targetHeight = v.getMeasuredHeight(); 30 | 31 | v.getLayoutParams().height = 1; 32 | v.setVisibility(View.VISIBLE); 33 | 34 | Animation a = 35 | new Animation() { 36 | @Override 37 | protected void applyTransformation(float interpolatedTime, Transformation t) { 38 | v.getLayoutParams().height = 39 | interpolatedTime == 1 40 | ? ViewGroup.LayoutParams.WRAP_CONTENT 41 | : (int) (targetHeight * interpolatedTime); 42 | v.requestLayout(); 43 | } 44 | 45 | @Override 46 | public boolean willChangeBounds() { 47 | return true; 48 | } 49 | }; 50 | 51 | a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density)); 52 | v.startAnimation(a); 53 | } 54 | 55 | public static void collapse(final View v) { 56 | final int initialHeight = v.getMeasuredHeight(); 57 | 58 | Animation a = 59 | new Animation() { 60 | @Override 61 | protected void applyTransformation(float interpolatedTime, Transformation t) { 62 | if (interpolatedTime == 1) { 63 | v.setVisibility(View.GONE); 64 | } else { 65 | v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime); 66 | v.requestLayout(); 67 | } 68 | } 69 | 70 | @Override 71 | public boolean willChangeBounds() { 72 | return true; 73 | } 74 | }; 75 | 76 | a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density)); 77 | v.startAnimation(a); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HMS Search Kit Demo 2 | 3 | English | [中文](README_ZH.md) 4 | 5 | ## Table of Contents 6 | 7 | * [Introduction](#introduction) 8 | * [Installation](#installation) 9 | * [Supported Environments](#supported-environments) 10 | * [Configuration ](#configuration) 11 | * [Sample Code](#sample-code) 12 | * [License](#license) 13 | 14 | 15 | ## Introduction 16 | HMS Search Kit sample code encapsulates APIs of the HUAWEI Search Kit. It provides a sample programs for your reference or usage. 17 | 18 | ## Installation 19 | Before using HMS Search Kit sample code, check whether the BD_SearchKitCodeLab environment has been installed. 20 | Decompress the HMS Search Kit sample code package. 21 | 22 | Refresh the project and ensure that is successfully. 23 | 24 | ## Supported Environments 25 | searchkit 5.0.4.305 or a later version is recommended. 26 | The Android SDK is available only on a mobile device running Android API-Level 24 (Android 7.0 Marshmallow) or higher. 27 | Android studio. 28 | 29 | ## Configuration 30 | 1. Register and sign in to HUAWEI Developers. 31 | 2. Create a project and then create an app in the project, enter the project package name. 32 | 3. Go to Project Settings > Manage APIs, find the Search Kit API, and enable it. 33 | 4. Go to Project Settings > General information, click Set next to Data storage location under Project information, and select a data storage location in the displayed dialog box. 34 | 5. Download the agconnect-services.json file and place it to the app's root directory of the project. 35 | 6. Add the Maven repository address maven {url 'https://developer.huawei.com/repo/'} and plug-in class path 'com.huawei.agconnect:agcp:1.3.1.300' to the project-level build.gradle file. 36 | 7. Add apply plugin: 'com.huawei.agconnect' to the last line of the app-level build.gradle file. 37 | 8. Configure the dependency com.huawei.hms:searchkit:5.0.4.305 in the app-level buildle.gradle file. 38 | 9. Synchronize the project. 39 | 40 | ## Sample Code 41 | 42 | 1. Web search. 43 | Code location: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 44 | 45 | 2. Image search. 46 | Code location: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 47 | 48 | 3. Video search. 49 | Code location: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 50 | 51 | 4. News search. 52 | Code location: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 53 | 54 | 5. Custom search. 55 | Code location: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 56 | 57 | 6. Suggest. 58 | Code location: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 59 | 60 | 7. Spell check. 61 | Code location: [searchindex/SearchActivity.java](https://github.com/HMS-Core/hms-search-demo/blob/main/SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.java) 62 | 63 | 64 | ## Result 65 | **web search** 66 | 67 | 68 | 69 | **image search** 70 | 71 | 72 | 73 | **video search** 74 | 75 | 76 | 77 | **news search** 78 | 79 | 80 | 81 | **custom search** 82 | 83 | 84 | 85 | ## License 86 | HMS Search Kit sample is licensed under the [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0). 87 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/network/NetworkManager.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.searchindex.network; 17 | 18 | import android.content.Context; 19 | import android.util.Log; 20 | 21 | import com.huawei.secure.android.common.ssl.SecureSSLSocketFactory; 22 | import com.huawei.secure.android.common.ssl.SecureX509TrustManager; 23 | import com.huawei.secure.android.common.ssl.hostname.StrictHostnameVerifier; 24 | 25 | import java.io.IOException; 26 | import java.security.KeyManagementException; 27 | import java.security.KeyStoreException; 28 | import java.security.NoSuchAlgorithmException; 29 | import java.security.cert.CertificateException; 30 | import java.util.concurrent.TimeUnit; 31 | 32 | import javax.net.ssl.SSLSocketFactory; 33 | import javax.net.ssl.X509TrustManager; 34 | 35 | import okhttp3.OkHttpClient; 36 | import retrofit2.Retrofit; 37 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; 38 | import retrofit2.converter.gson.GsonConverterFactory; 39 | 40 | public class NetworkManager { 41 | private static final String TAG = NetworkManager.class.getSimpleName(); 42 | 43 | private static NetworkManager networkManager; 44 | 45 | /** 46 | * 获取NetworkManager的实例 47 | * 48 | * @return NetworkManager的实例 49 | */ 50 | public static NetworkManager getInstance() { 51 | if (networkManager == null) { 52 | syncInit(); 53 | } 54 | return networkManager; 55 | } 56 | 57 | /** 58 | * 单例 59 | */ 60 | private static synchronized void syncInit() { 61 | if (networkManager == null) { 62 | networkManager = new NetworkManager(); 63 | } 64 | } 65 | 66 | public QueryService createService(Context context, String baseUrl) { 67 | QueryService queryService = null; 68 | Retrofit retrofit = null; 69 | Retrofit.Builder builder = new Retrofit.Builder().baseUrl(baseUrl); 70 | OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); 71 | try { 72 | SSLSocketFactory ssf = SecureSSLSocketFactory.getInstance(context); 73 | X509TrustManager xtm = new SecureX509TrustManager(context); 74 | clientBuilder.sslSocketFactory(ssf, xtm); 75 | clientBuilder.hostnameVerifier(new StrictHostnameVerifier()); 76 | } catch (IOException e) { 77 | Log.e(TAG, "getRetrofit: " + e.getMessage()); 78 | } catch (IllegalAccessException e) { 79 | Log.e(TAG, "getRetrofit: " + e.getMessage()); 80 | } catch (NoSuchAlgorithmException e) { 81 | Log.e(TAG, "getRetrofit: " + e.getMessage()); 82 | } catch (KeyManagementException e) { 83 | Log.e(TAG, "getRetrofit: " + e.getMessage()); 84 | } catch (KeyStoreException e) { 85 | Log.e(TAG, "getRetrofit: " + e.getMessage()); 86 | } catch (CertificateException e) { 87 | Log.e(TAG, "getRetrofit: " + e.getMessage()); 88 | } catch (Exception e) { 89 | Log.e(TAG, "getRetrofit: " + e.getMessage()); 90 | } 91 | OkHttpClient client = 92 | clientBuilder 93 | .retryOnConnectionFailure(true) 94 | .readTimeout(5000, TimeUnit.MILLISECONDS) 95 | .connectTimeout(5000, TimeUnit.MILLISECONDS) 96 | .build(); 97 | try { 98 | retrofit = 99 | builder.client(client) 100 | .addConverterFactory(GsonConverterFactory.create()) 101 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 102 | .build(); 103 | queryService = retrofit.create(QueryService.class); 104 | } catch (Exception e) { 105 | Log.e(TAG, "createRestClient error: " + e.getMessage()); 106 | } 107 | return queryService; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/WebActivity.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.searchindex.activity; 17 | 18 | import android.os.Bundle; 19 | import android.text.TextUtils; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.widget.ImageView; 23 | import android.widget.RelativeLayout; 24 | import android.widget.TextView; 25 | import androidx.appcompat.app.AppCompatActivity; 26 | 27 | import com.huawei.searchindex.R; 28 | import com.huawei.searchindex.utils.AnimationUtils; 29 | 30 | import com.bumptech.glide.Glide; 31 | 32 | public class WebActivity extends AppCompatActivity { 33 | private static final String TAG = WebActivity.class.getSimpleName(); 34 | private TextView tvTitle, tvClickUrl, tvSnippet, tvSiteName, tvThumbnail; 35 | private ImageView imgThumbnail; 36 | private String title, clickUrl, snippet, siteName, thumbnail; 37 | private String empty = "null"; 38 | private TextView tvThumbnailPlus; 39 | private RelativeLayout rlThumbnail; 40 | 41 | @Override 42 | protected void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | setContentView(R.layout.activity_web); 45 | 46 | initView(); 47 | initData(); 48 | } 49 | 50 | private void initView() { 51 | tvTitle = findViewById(R.id.tv_web_title); 52 | tvClickUrl = findViewById(R.id.tv_web_click_url); 53 | tvSnippet = findViewById(R.id.tv_web_snippet); 54 | tvSiteName = findViewById(R.id.tv_web_site_name); 55 | tvThumbnail = findViewById(R.id.tv_web_thumbnail_url); 56 | imgThumbnail = findViewById(R.id.img_web_thumbnail); 57 | tvThumbnailPlus = findViewById(R.id.tv_thumbnail_plus); 58 | rlThumbnail = findViewById(R.id.linear_web_thumbnail_url); 59 | 60 | tvThumbnailPlus.setOnClickListener( 61 | new View.OnClickListener() { 62 | @Override 63 | public void onClick(View v) { 64 | if (tvThumbnailPlus.getText().equals("+")) { 65 | tvThumbnailPlus.setText("-"); 66 | AnimationUtils.expand(rlThumbnail); 67 | } else if (tvThumbnailPlus.getText().equals("-")) { 68 | tvThumbnailPlus.setText("+"); 69 | AnimationUtils.collapse(rlThumbnail); 70 | } 71 | } 72 | }); 73 | } 74 | 75 | private void initData() { 76 | try { 77 | if (getIntent().hasExtra("web_title")) { 78 | title = getIntent().getStringExtra("web_title"); 79 | } 80 | if (getIntent().hasExtra("web_click_url")) { 81 | clickUrl = getIntent().getStringExtra("web_click_url"); 82 | } 83 | if (getIntent().hasExtra("web_site_name")) { 84 | snippet = getIntent().getStringExtra("web_site_name"); 85 | } 86 | if (getIntent().hasExtra("web_snippet")) { 87 | siteName = getIntent().getStringExtra("web_snippet"); 88 | } 89 | if (getIntent().hasExtra("web_thumbnail")) { 90 | thumbnail = getIntent().getStringExtra("web_thumbnail"); 91 | } 92 | 93 | if (!TextUtils.isEmpty(title)) { 94 | tvTitle.setText(title); 95 | } else { 96 | tvTitle.setText(empty); 97 | } 98 | if (!TextUtils.isEmpty(clickUrl)) { 99 | tvClickUrl.setText(clickUrl); 100 | } else { 101 | tvClickUrl.setText(empty); 102 | } 103 | if (!TextUtils.isEmpty(snippet)) { 104 | tvSnippet.setText(snippet); 105 | } else { 106 | tvSnippet.setText(empty); 107 | } 108 | if (!TextUtils.isEmpty(siteName)) { 109 | tvSiteName.setText(siteName); 110 | } else { 111 | tvSiteName.setText(empty); 112 | } 113 | if (!TextUtils.isEmpty(thumbnail)) { 114 | tvThumbnail.setText(thumbnail); 115 | imgThumbnail.setVisibility(View.VISIBLE); 116 | Glide.with(this).load(thumbnail).dontAnimate().into(imgThumbnail); 117 | } else { 118 | tvThumbnail.setText(empty); 119 | imgThumbnail.setVisibility(View.GONE); 120 | } 121 | } catch (Exception e) { 122 | Log.e(TAG, "initData error: " + e.getMessage()); 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/activity_search.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 39 | 40 | 49 | 50 | 51 | 57 | 58 | 59 | 60 | 67 | 68 | 77 | 78 | 88 | 89 | 90 | 97 | 98 | 104 | 105 | 116 | 117 | 118 | 122 | 123 | 124 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/adapter/WebAdapter.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.searchindex.adapter; 17 | 18 | import android.content.Context; 19 | import android.graphics.Color; 20 | import android.text.SpannableString; 21 | import android.text.Spanned; 22 | import android.text.method.LinkMovementMethod; 23 | import android.text.style.ForegroundColorSpan; 24 | import android.text.style.URLSpan; 25 | import android.view.LayoutInflater; 26 | import android.view.View; 27 | import android.view.ViewGroup; 28 | import android.widget.ImageView; 29 | import android.widget.TextView; 30 | import androidx.annotation.NonNull; 31 | import androidx.recyclerview.widget.RecyclerView; 32 | 33 | import com.huawei.searchindex.R; 34 | import com.huawei.searchindex.bean.ListBean; 35 | 36 | import com.bumptech.glide.Glide; 37 | 38 | import java.util.ArrayList; 39 | import java.util.List; 40 | 41 | public class WebAdapter extends RecyclerView.Adapter { 42 | private List list = new ArrayList<>(); 43 | private Context context; 44 | 45 | public WebAdapter(Context context, List list) { 46 | this.context = context; 47 | this.list = list; 48 | } 49 | 50 | @NonNull 51 | @Override 52 | public WebViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 53 | View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_web, parent, false); 54 | return new WebViewHolder(v); 55 | } 56 | 57 | @Override 58 | public void onBindViewHolder(@NonNull WebViewHolder holder, final int position) { 59 | if (list != null && !list.isEmpty()) { 60 | if (list.get(position).getTitle() != null) { 61 | if (list.get(position).getClick_url() != null) { 62 | SpannableString spannableString = new SpannableString(list.get(position).getTitle()); 63 | spannableString.setSpan( 64 | new URLSpan(list.get(position).getClick_url()), 65 | 0, 66 | list.get(position).getTitle().length(), 67 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 68 | spannableString.setSpan( 69 | new ForegroundColorSpan(Color.BLACK), 70 | 0, 71 | list.get(position).getTitle().length(), 72 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 73 | holder.tvTitle.setText(spannableString); 74 | holder.tvTitle.setMovementMethod(LinkMovementMethod.getInstance()); 75 | } else { 76 | holder.tvTitle.setText(list.get(position).getTitle()); 77 | } 78 | } else { 79 | holder.tvTitle.setText(R.string.get_title_error); 80 | } 81 | if (context != null) { 82 | Glide.with(context) 83 | .load(list.get(position).getUrl()) 84 | .error(R.drawable.net_error) 85 | .dontAnimate() 86 | .into(holder.img); 87 | } 88 | holder.tvDetail.setOnClickListener( 89 | new View.OnClickListener() { 90 | @Override 91 | public void onClick(View v) { 92 | onItemClickListener.click(position); 93 | } 94 | }); 95 | } 96 | } 97 | 98 | @Override 99 | public int getItemCount() { 100 | return list.size(); 101 | } 102 | 103 | public static class WebViewHolder extends RecyclerView.ViewHolder { 104 | private TextView tvTitle, tvDetail; 105 | private ImageView img; 106 | 107 | public WebViewHolder(@NonNull View itemView) { 108 | super(itemView); 109 | tvTitle = itemView.findViewById(R.id.text_title); 110 | img = itemView.findViewById(R.id.img); 111 | tvDetail = itemView.findViewById(R.id.tv_detail); 112 | } 113 | } 114 | 115 | public void refresh(List newList) { 116 | if (list != null && !list.isEmpty()) { 117 | list.clear(); 118 | if (newList != null && !newList.isEmpty()) { 119 | list.addAll(newList); 120 | notifyDataSetChanged(); 121 | } 122 | } 123 | } 124 | 125 | private OnItemClickListener onItemClickListener; 126 | 127 | public void setOnClickListener(OnItemClickListener onClickListener) { 128 | this.onItemClickListener = onClickListener; 129 | } 130 | 131 | public interface OnItemClickListener { 132 | void click(int position); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/adapter/ContentAdapter.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.searchindex.adapter; 17 | 18 | import android.content.Context; 19 | import android.graphics.Color; 20 | import android.text.SpannableString; 21 | import android.text.Spanned; 22 | import android.text.TextUtils; 23 | import android.text.method.LinkMovementMethod; 24 | import android.text.style.ForegroundColorSpan; 25 | import android.text.style.URLSpan; 26 | import android.view.LayoutInflater; 27 | import android.view.View; 28 | import android.view.ViewGroup; 29 | import android.widget.ImageView; 30 | import android.widget.TextView; 31 | import androidx.annotation.NonNull; 32 | import androidx.recyclerview.widget.RecyclerView; 33 | 34 | import com.huawei.searchindex.R; 35 | import com.huawei.searchindex.bean.ListBean; 36 | 37 | import com.bumptech.glide.Glide; 38 | 39 | import java.util.ArrayList; 40 | import java.util.List; 41 | 42 | public class ContentAdapter extends RecyclerView.Adapter { 43 | private List list = new ArrayList<>(); 44 | private Context context; 45 | 46 | public ContentAdapter(Context context, List list) { 47 | this.context = context; 48 | this.list = list; 49 | } 50 | 51 | @NonNull 52 | @Override 53 | public ContentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 54 | View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_content, parent, false); 55 | return new ContentHolder(v); 56 | } 57 | 58 | @Override 59 | public void onBindViewHolder(@NonNull ContentHolder holder, final int position) { 60 | if (list != null && !list.isEmpty()) { 61 | if (list.get(position).getTitle() != null) { 62 | if (list.get(position).getClick_url() != null) { 63 | SpannableString spannableString = new SpannableString(list.get(position).getTitle()); 64 | spannableString.setSpan( 65 | new URLSpan(list.get(position).getClick_url()), 66 | 0, 67 | list.get(position).getTitle().length(), 68 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 69 | spannableString.setSpan( 70 | new ForegroundColorSpan(Color.BLACK), 71 | 0, 72 | list.get(position).getTitle().length(), 73 | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 74 | holder.tvTitle.setText(spannableString); 75 | holder.tvTitle.setMovementMethod(LinkMovementMethod.getInstance()); 76 | } else { 77 | holder.tvTitle.setText(list.get(position).getTitle()); 78 | } 79 | } else { 80 | holder.tvTitle.setText(R.string.get_title_error); 81 | } 82 | if (context != null && !TextUtils.isEmpty(list.get(position).getUrl())) { 83 | holder.img.setVisibility(View.VISIBLE); 84 | Glide.with(context) 85 | .load(list.get(position).getUrl()) 86 | .error(R.drawable.net_error) 87 | .dontAnimate() 88 | .into(holder.img); 89 | } else { 90 | holder.img.setVisibility(View.GONE); 91 | } 92 | holder.tvDetail.setOnClickListener( 93 | new View.OnClickListener() { 94 | @Override 95 | public void onClick(View v) { 96 | onItemClickListener.click(position); 97 | } 98 | }); 99 | } 100 | } 101 | 102 | @Override 103 | public int getItemCount() { 104 | return list.size(); 105 | } 106 | 107 | public static class ContentHolder extends RecyclerView.ViewHolder { 108 | private TextView tvTitle, tvDetail; 109 | private ImageView img; 110 | 111 | public ContentHolder(@NonNull View itemView) { 112 | super(itemView); 113 | tvTitle = itemView.findViewById(R.id.text_title); 114 | img = itemView.findViewById(R.id.img); 115 | tvDetail = itemView.findViewById(R.id.tv_detail); 116 | } 117 | } 118 | 119 | public void refresh(List newList) { 120 | if (list != null && !list.isEmpty()) { 121 | list.clear(); 122 | if (newList != null && !newList.isEmpty()) { 123 | list.addAll(newList); 124 | notifyDataSetChanged(); 125 | } 126 | } 127 | } 128 | 129 | private OnItemClickListener onItemClickListener; 130 | 131 | public void setOnClickListener(OnItemClickListener onClickListener) { 132 | this.onItemClickListener = onClickListener; 133 | } 134 | 135 | public interface OnItemClickListener { 136 | void click(int position); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/NewsActivity.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.searchindex.activity; 17 | 18 | import android.os.Bundle; 19 | import android.text.TextUtils; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.widget.ImageView; 23 | import android.widget.LinearLayout; 24 | import android.widget.RelativeLayout; 25 | import android.widget.TextView; 26 | import androidx.appcompat.app.AppCompatActivity; 27 | 28 | import com.huawei.searchindex.R; 29 | import com.huawei.searchindex.utils.AnimationUtils; 30 | 31 | import com.bumptech.glide.Glide; 32 | 33 | public class NewsActivity extends AppCompatActivity { 34 | private static final String TAG = NewsActivity.class.getSimpleName(); 35 | private TextView tvTitle, tvClickUrl, tvThumbnail, tvSiteName, tvLogo, tvPublishTime; 36 | private ImageView imgThumbnail; 37 | private String title, clickUrl, thumbnail, siteName, logo, publishTime; 38 | private String empty = "null"; 39 | private TextView tvProViderPlus, tvThumbnailPlus; 40 | private LinearLayout llProviderImage; 41 | private RelativeLayout rlThumbnail; 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_news); 47 | 48 | initView(); 49 | initData(); 50 | } 51 | 52 | private void initView() { 53 | tvTitle = findViewById(R.id.tv_news_title); 54 | tvClickUrl = findViewById(R.id.tv_news_click_url); 55 | tvThumbnail = findViewById(R.id.tv_news_thumbnail_url); 56 | tvSiteName = findViewById(R.id.tv_news_site_name); 57 | tvLogo = findViewById(R.id.tv_news_logo); 58 | tvPublishTime = findViewById(R.id.tv_news_publish_time); 59 | imgThumbnail = findViewById(R.id.img_news_thumbnail); 60 | tvProViderPlus = findViewById(R.id.tv_provider_plus); 61 | tvThumbnailPlus = findViewById(R.id.tv_thumbnail_plus); 62 | llProviderImage = findViewById(R.id.linear_provider); 63 | rlThumbnail = findViewById(R.id.linear_news_thumbnail_url); 64 | 65 | tvProViderPlus.setOnClickListener( 66 | new View.OnClickListener() { 67 | @Override 68 | public void onClick(View v) { 69 | if (tvProViderPlus.getText().equals("+")) { 70 | AnimationUtils.expand(llProviderImage); 71 | tvProViderPlus.setText("-"); 72 | } else if (tvProViderPlus.getText().equals("-")) { 73 | tvProViderPlus.setText("+"); 74 | AnimationUtils.collapse(llProviderImage); 75 | } 76 | } 77 | }); 78 | 79 | tvThumbnailPlus.setOnClickListener( 80 | new View.OnClickListener() { 81 | @Override 82 | public void onClick(View v) { 83 | if (tvThumbnailPlus.getText().equals("+")) { 84 | tvThumbnailPlus.setText("-"); 85 | AnimationUtils.expand(rlThumbnail); 86 | } else if (tvThumbnailPlus.getText().equals("-")) { 87 | tvThumbnailPlus.setText("+"); 88 | AnimationUtils.collapse(rlThumbnail); 89 | } 90 | } 91 | }); 92 | } 93 | 94 | private void initData() { 95 | try { 96 | if (getIntent().hasExtra("news_title")) { 97 | title = getIntent().getStringExtra("news_title"); 98 | } 99 | if (getIntent().hasExtra("news_click_url")) { 100 | clickUrl = getIntent().getStringExtra("news_click_url"); 101 | } 102 | if (getIntent().hasExtra("news_logo")) { 103 | logo = getIntent().getStringExtra("news_logo"); 104 | } 105 | if (getIntent().hasExtra("news_site_name")) { 106 | siteName = getIntent().getStringExtra("news_site_name"); 107 | } 108 | if (getIntent().hasExtra("news_publish")) { 109 | publishTime = getIntent().getStringExtra("news_publish"); 110 | } 111 | if (getIntent().hasExtra("news_thumbnail")) { 112 | thumbnail = getIntent().getStringExtra("news_thumbnail"); 113 | } 114 | 115 | if (!TextUtils.isEmpty(title)) { 116 | tvTitle.setText(title); 117 | } else { 118 | tvTitle.setText(empty); 119 | } 120 | if (!TextUtils.isEmpty(clickUrl)) { 121 | tvClickUrl.setText(clickUrl); 122 | } else { 123 | tvClickUrl.setText(empty); 124 | } 125 | if (!TextUtils.isEmpty(logo)) { 126 | tvLogo.setText(logo); 127 | } else { 128 | tvLogo.setText(empty); 129 | } 130 | if (!TextUtils.isEmpty(siteName)) { 131 | tvSiteName.setText(siteName); 132 | } else { 133 | tvSiteName.setText(empty); 134 | } 135 | if (!TextUtils.isEmpty(publishTime)) { 136 | tvPublishTime.setText(publishTime); 137 | } else { 138 | tvPublishTime.setText(empty); 139 | } 140 | if (!TextUtils.isEmpty(thumbnail)) { 141 | tvThumbnail.setText(thumbnail); 142 | imgThumbnail.setVisibility(View.VISIBLE); 143 | Glide.with(this).load(thumbnail).dontAnimate().into(imgThumbnail); 144 | } else { 145 | tvThumbnail.setText(empty); 146 | imgThumbnail.setVisibility(View.GONE); 147 | } 148 | } catch (Exception e) { 149 | Log.e(TAG, "initData error: " + e.getMessage()); 150 | } 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/fragment/WebFragment.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.searchindex.fragment; 17 | 18 | import android.content.Intent; 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.TextView; 25 | import androidx.fragment.app.Fragment; 26 | import androidx.recyclerview.widget.LinearLayoutManager; 27 | import androidx.recyclerview.widget.RecyclerView; 28 | 29 | import com.huawei.hms.searchkit.bean.WebItem; 30 | import com.huawei.searchindex.R; 31 | import com.huawei.searchindex.activity.WebActivity; 32 | import com.huawei.searchindex.adapter.ContentAdapter; 33 | import com.huawei.searchindex.bean.ListBean; 34 | 35 | import java.util.ArrayList; 36 | import java.util.List; 37 | 38 | public class WebFragment extends Fragment { 39 | private static final String TAG = WebFragment.class.getSimpleName(); 40 | private View view; 41 | private RecyclerView recyclerView; 42 | private ContentAdapter adapter; 43 | private TextView tvEmpty; 44 | 45 | /** 46 | * New instance main fragment. 47 | * 48 | * @return the main fragment 49 | */ 50 | public static WebFragment newInstance() { 51 | return new WebFragment(); 52 | } 53 | 54 | @Override 55 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 56 | // Inflate the layout for this fragment 57 | view = inflater.inflate(R.layout.fragment_web, container, false); 58 | 59 | initView(); 60 | 61 | return view; 62 | } 63 | 64 | private void initView() { 65 | if (view != null) { 66 | recyclerView = view.findViewById(R.id.recycler_view); 67 | tvEmpty = view.findViewById(R.id.tv_empty); 68 | 69 | LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 70 | recyclerView.setLayoutManager(layoutManager); 71 | } else { 72 | Log.e(TAG, "view is null"); 73 | } 74 | } 75 | 76 | public void setValue(List list) { 77 | List listBeans = new ArrayList<>(); 78 | if (list != null && !list.isEmpty()) { 79 | for (int i = 0; i < list.size(); i++) { 80 | ListBean bean = new ListBean(); 81 | if (list.get(i).getTitle() != null) { 82 | bean.setTitle(list.get(i).getTitle()); 83 | } 84 | if (list.get(i).getThumbnail() != null && list.get(i).getThumbnail().getUrl() != null) { 85 | bean.setUrl(list.get(i).getThumbnail().getUrl()); 86 | } else { 87 | bean.setUrl(""); 88 | } 89 | if (list.get(i).getClickUrl() != null) { 90 | bean.setClick_url(list.get(i).getClickUrl()); 91 | } 92 | listBeans.add(bean); 93 | } 94 | if (adapter != null) { 95 | adapter.refresh(listBeans); 96 | if (recyclerView != null && tvEmpty != null) { 97 | tvEmpty.setVisibility(View.GONE); 98 | recyclerView.setVisibility(View.VISIBLE); 99 | } 100 | } else { 101 | adapter = new ContentAdapter(getActivity(), listBeans); 102 | if (recyclerView != null && tvEmpty != null) { 103 | recyclerView.setAdapter(adapter); 104 | tvEmpty.setVisibility(View.GONE); 105 | recyclerView.setVisibility(View.VISIBLE); 106 | } 107 | } 108 | onClick(list); 109 | } else { 110 | if (recyclerView != null && tvEmpty != null) { 111 | tvEmpty.setVisibility(View.VISIBLE); 112 | recyclerView.setVisibility(View.GONE); 113 | } 114 | } 115 | } 116 | 117 | private void onClick(final List list) { 118 | if (adapter != null) { 119 | adapter.setOnClickListener( 120 | new ContentAdapter.OnItemClickListener() { 121 | @Override 122 | public void click(int position) { 123 | Intent intent = new Intent(getActivity(), WebActivity.class); 124 | if (list.get(position).getTitle() != null) { 125 | intent.putExtra("web_title", list.get(position).getTitle()); 126 | } else { 127 | intent.putExtra("web_title", ""); 128 | } 129 | if (list.get(position).getClickUrl() != null) { 130 | intent.putExtra("web_click_url", list.get(position).getClickUrl()); 131 | } else { 132 | intent.putExtra("web_click_url", ""); 133 | } 134 | if (list.get(position).getSiteName() != null) { 135 | intent.putExtra("web_site_name", list.get(position).getSiteName()); 136 | } else { 137 | intent.putExtra("web_site_name", ""); 138 | } 139 | if (list.get(position).getSnippet() != null) { 140 | intent.putExtra("web_snippet", list.get(position).getSnippet()); 141 | } else { 142 | intent.putExtra("web_snippet", ""); 143 | } 144 | if (list.get(position).getThumbnail() != null 145 | && list.get(position).getThumbnail().getUrl() != null) { 146 | intent.putExtra("web_thumbnail", list.get(position).getThumbnail().getUrl()); 147 | } else { 148 | intent.putExtra("web_thumbnail", ""); 149 | } 150 | startActivity(intent); 151 | } 152 | }); 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/VideoActivity.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.searchindex.activity; 17 | 18 | import android.os.Bundle; 19 | import android.text.TextUtils; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.widget.ImageView; 23 | import android.widget.LinearLayout; 24 | import android.widget.RelativeLayout; 25 | import android.widget.TextView; 26 | import androidx.appcompat.app.AppCompatActivity; 27 | 28 | import com.huawei.searchindex.R; 29 | import com.huawei.searchindex.utils.AnimationUtils; 30 | 31 | import com.bumptech.glide.Glide; 32 | 33 | public class VideoActivity extends AppCompatActivity { 34 | private static final String TAG = VideoActivity.class.getSimpleName(); 35 | private TextView tvTitle, tvClickUrl, tvThumbnail, tvSiteName, tvLogo, tvPublishTime, tvDuration; 36 | private ImageView imgThumbnail; 37 | private String title, clickUrl, thumbnail, siteName, logo, publishTime, duration; 38 | private String empty = "null"; 39 | private TextView tvProViderPlus, tvThumbnailPlus; 40 | private LinearLayout llProviderImage; 41 | private RelativeLayout rlThumbnail; 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_video); 47 | 48 | initView(); 49 | initData(); 50 | } 51 | 52 | private void initView() { 53 | tvTitle = findViewById(R.id.tv_video_title); 54 | tvClickUrl = findViewById(R.id.tv_video_click_url); 55 | tvThumbnail = findViewById(R.id.tv_video_thumbnail_url); 56 | tvSiteName = findViewById(R.id.tv_video_site_name); 57 | tvLogo = findViewById(R.id.tv_video_logo); 58 | tvPublishTime = findViewById(R.id.tv_video_publish_time); 59 | tvDuration = findViewById(R.id.tv_video_duration); 60 | imgThumbnail = findViewById(R.id.img_video_thumbnail); 61 | tvProViderPlus = findViewById(R.id.tv_provider_plus); 62 | tvThumbnailPlus = findViewById(R.id.tv_thumbnail_plus); 63 | llProviderImage = findViewById(R.id.linear_provider); 64 | rlThumbnail = findViewById(R.id.linear_video_thumbnail_url); 65 | 66 | tvProViderPlus.setOnClickListener( 67 | new View.OnClickListener() { 68 | @Override 69 | public void onClick(View v) { 70 | if (tvProViderPlus.getText().equals("+")) { 71 | tvProViderPlus.setText("-"); 72 | AnimationUtils.expand(llProviderImage); 73 | } else if (tvProViderPlus.getText().equals("-")) { 74 | tvProViderPlus.setText("+"); 75 | AnimationUtils.collapse(llProviderImage); 76 | } 77 | } 78 | }); 79 | 80 | tvThumbnailPlus.setOnClickListener( 81 | new View.OnClickListener() { 82 | @Override 83 | public void onClick(View v) { 84 | if (tvThumbnailPlus.getText().equals("+")) { 85 | tvThumbnailPlus.setText("-"); 86 | AnimationUtils.expand(rlThumbnail); 87 | } else if (tvThumbnailPlus.getText().equals("-")) { 88 | tvThumbnailPlus.setText("+"); 89 | AnimationUtils.collapse(rlThumbnail); 90 | } 91 | } 92 | }); 93 | } 94 | 95 | private void initData() { 96 | try { 97 | if (getIntent().hasExtra("video_title")) { 98 | title = getIntent().getStringExtra("video_title"); 99 | } 100 | if (getIntent().hasExtra("video_click_url")) { 101 | clickUrl = getIntent().getStringExtra("video_click_url"); 102 | } 103 | if (getIntent().hasExtra("video_logo")) { 104 | logo = getIntent().getStringExtra("video_logo"); 105 | } 106 | if (getIntent().hasExtra("video_site_name")) { 107 | siteName = getIntent().getStringExtra("video_site_name"); 108 | } 109 | if (getIntent().hasExtra("video_publish")) { 110 | publishTime = getIntent().getStringExtra("video_publish"); 111 | } 112 | if (getIntent().hasExtra("video_duration")) { 113 | duration = getIntent().getStringExtra("video_duration"); 114 | } 115 | if (getIntent().hasExtra("video_thumbnail")) { 116 | thumbnail = getIntent().getStringExtra("video_thumbnail"); 117 | } 118 | 119 | if (!TextUtils.isEmpty(title)) { 120 | tvTitle.setText(title); 121 | } else { 122 | tvTitle.setText(empty); 123 | } 124 | if (!TextUtils.isEmpty(clickUrl)) { 125 | tvClickUrl.setText(clickUrl); 126 | } else { 127 | tvClickUrl.setText(empty); 128 | } 129 | if (!TextUtils.isEmpty(logo)) { 130 | tvLogo.setText(logo); 131 | } else { 132 | tvLogo.setText(empty); 133 | } 134 | if (!TextUtils.isEmpty(siteName)) { 135 | tvSiteName.setText(siteName); 136 | } else { 137 | tvSiteName.setText(empty); 138 | } 139 | if (!TextUtils.isEmpty(publishTime)) { 140 | tvPublishTime.setText(publishTime); 141 | } else { 142 | tvPublishTime.setText(empty); 143 | } 144 | if (!TextUtils.isEmpty(duration)) { 145 | tvDuration.setText(duration); 146 | } else { 147 | tvDuration.setText(empty); 148 | } 149 | if (!TextUtils.isEmpty(thumbnail)) { 150 | tvThumbnail.setText(thumbnail); 151 | imgThumbnail.setVisibility(View.VISIBLE); 152 | Glide.with(this).load(thumbnail).dontAnimate().into(imgThumbnail); 153 | } else { 154 | tvThumbnail.setText(empty); 155 | imgThumbnail.setVisibility(View.GONE); 156 | } 157 | } catch (Exception e) { 158 | Log.e(TAG, "initData error: " + e.getMessage()); 159 | } 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/ImageActivity.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.searchindex.activity; 17 | 18 | import android.os.Bundle; 19 | import android.text.TextUtils; 20 | import android.util.Log; 21 | import android.view.View; 22 | import android.widget.ImageView; 23 | import android.widget.LinearLayout; 24 | import android.widget.RelativeLayout; 25 | import android.widget.TextView; 26 | import androidx.appcompat.app.AppCompatActivity; 27 | 28 | import com.huawei.searchindex.R; 29 | import com.huawei.searchindex.utils.AnimationUtils; 30 | 31 | import com.bumptech.glide.Glide; 32 | 33 | public class ImageActivity extends AppCompatActivity { 34 | private static final String TAG = ImageActivity.class.getSimpleName(); 35 | private TextView tvTitle, tvClickUrl, tvHostUrl, tvContentUrl, tvThumbnail, tvHeigth, tvWidth, tvPublishTime; 36 | private ImageView imgThumbnail; 37 | private String title, clickUrl, hostUrl, contentUrl, thumbnail, heigth, width, publishTime; 38 | private String empty = "null"; 39 | private TextView tvImagePlus, tvThumbnailPlus; 40 | private LinearLayout llSourceImage; 41 | private RelativeLayout rlThumbnail; 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_image); 47 | initView(); 48 | initData(); 49 | } 50 | 51 | private void initView() { 52 | tvTitle = findViewById(R.id.tv_image_title); 53 | tvClickUrl = findViewById(R.id.tv_image_click_url); 54 | tvHostUrl = findViewById(R.id.tv_image_host_url); 55 | tvContentUrl = findViewById(R.id.tv_image_content_url); 56 | tvThumbnail = findViewById(R.id.tv_image_thumbnail_url); 57 | tvHeigth = findViewById(R.id.tv_image_height); 58 | tvWidth = findViewById(R.id.tv_image_width); 59 | tvPublishTime = findViewById(R.id.tv_image_public_time); 60 | imgThumbnail = findViewById(R.id.img_image_thumbnail); 61 | tvImagePlus = findViewById(R.id.tv_image_plus); 62 | tvThumbnailPlus = findViewById(R.id.tv_thumbnail_plus); 63 | rlThumbnail = findViewById(R.id.linear_image_thumbnail_url); 64 | llSourceImage = findViewById(R.id.linear_source_image); 65 | 66 | tvImagePlus.setOnClickListener( 67 | new View.OnClickListener() { 68 | @Override 69 | public void onClick(View v) { 70 | if (tvImagePlus.getText().equals("+")) { 71 | tvImagePlus.setText("-"); 72 | AnimationUtils.expand(llSourceImage); 73 | } else if (tvImagePlus.getText().equals("-")) { 74 | tvImagePlus.setText("+"); 75 | AnimationUtils.collapse(llSourceImage); 76 | } 77 | } 78 | }); 79 | 80 | tvThumbnailPlus.setOnClickListener( 81 | new View.OnClickListener() { 82 | @Override 83 | public void onClick(View v) { 84 | if (tvThumbnailPlus.getText().equals("+")) { 85 | tvThumbnailPlus.setText("-"); 86 | AnimationUtils.expand(rlThumbnail); 87 | } else if (tvThumbnailPlus.getText().equals("-")) { 88 | tvThumbnailPlus.setText("+"); 89 | AnimationUtils.collapse(rlThumbnail); 90 | } 91 | } 92 | }); 93 | } 94 | 95 | private void initData() { 96 | try { 97 | if (getIntent().hasExtra("img_title")) { 98 | title = getIntent().getStringExtra("img_title"); 99 | } 100 | if (getIntent().hasExtra("img_click_url")) { 101 | clickUrl = getIntent().getStringExtra("img_click_url"); 102 | } 103 | if (getIntent().hasExtra("img_host_url")) { 104 | hostUrl = getIntent().getStringExtra("img_host_url"); 105 | } 106 | if (getIntent().hasExtra("img_content_url")) { 107 | contentUrl = getIntent().getStringExtra("img_content_url"); 108 | } 109 | if (getIntent().hasExtra("img_thumbnail")) { 110 | thumbnail = getIntent().getStringExtra("img_thumbnail"); 111 | } 112 | if (getIntent().hasExtra("img_height")) { 113 | heigth = getIntent().getStringExtra("img_height"); 114 | } 115 | if (getIntent().hasExtra("img_width")) { 116 | width = getIntent().getStringExtra("img_width"); 117 | } 118 | if (getIntent().hasExtra("img_publish")) { 119 | publishTime = getIntent().getStringExtra("img_publish"); 120 | } 121 | 122 | if (!TextUtils.isEmpty(title)) { 123 | tvTitle.setText(title); 124 | } else { 125 | tvTitle.setText(empty); 126 | } 127 | if (!TextUtils.isEmpty(clickUrl)) { 128 | tvClickUrl.setText(clickUrl); 129 | } else { 130 | tvClickUrl.setText(empty); 131 | } 132 | if (!TextUtils.isEmpty(hostUrl)) { 133 | tvHostUrl.setText(hostUrl); 134 | } else { 135 | tvHostUrl.setText(empty); 136 | } 137 | if (!TextUtils.isEmpty(contentUrl)) { 138 | tvContentUrl.setText(contentUrl); 139 | } else { 140 | tvContentUrl.setText(empty); 141 | } 142 | if (!TextUtils.isEmpty(heigth)) { 143 | tvHeigth.setText(heigth); 144 | } else { 145 | tvHeigth.setText(empty); 146 | } 147 | if (!TextUtils.isEmpty(width)) { 148 | tvWidth.setText(width); 149 | } else { 150 | tvWidth.setText(empty); 151 | } 152 | if (!TextUtils.isEmpty(publishTime)) { 153 | tvPublishTime.setText(publishTime); 154 | } else { 155 | tvPublishTime.setText(empty); 156 | } 157 | if (!TextUtils.isEmpty(thumbnail)) { 158 | tvThumbnail.setText(thumbnail); 159 | imgThumbnail.setVisibility(View.VISIBLE); 160 | Glide.with(this).load(thumbnail).dontAnimate().into(imgThumbnail); 161 | } else { 162 | tvThumbnail.setText(empty); 163 | imgThumbnail.setVisibility(View.GONE); 164 | } 165 | } catch (Exception e) { 166 | Log.e(TAG, "initData error: " + e.getMessage()); 167 | } 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/fragment/NewsFragment.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.searchindex.fragment; 17 | 18 | import android.content.Intent; 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.TextView; 25 | import androidx.fragment.app.Fragment; 26 | import androidx.recyclerview.widget.LinearLayoutManager; 27 | import androidx.recyclerview.widget.RecyclerView; 28 | 29 | import com.huawei.hms.searchkit.bean.NewsItem; 30 | import com.huawei.searchindex.R; 31 | import com.huawei.searchindex.activity.NewsActivity; 32 | import com.huawei.searchindex.adapter.ContentAdapter; 33 | import com.huawei.searchindex.bean.ListBean; 34 | 35 | import java.util.ArrayList; 36 | import java.util.List; 37 | 38 | public class NewsFragment extends Fragment { 39 | private static final String TAG = WebFragment.class.getSimpleName(); 40 | private View view; 41 | private RecyclerView recyclerView; 42 | private ContentAdapter adapter; 43 | private TextView tvEmpty; 44 | 45 | /** 46 | * New instance main fragment. 47 | * 48 | * @return the main fragment 49 | */ 50 | public static NewsFragment newInstance() { 51 | return new NewsFragment(); 52 | } 53 | 54 | @Override 55 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 56 | // Inflate the layout for this fragment 57 | view = inflater.inflate(R.layout.fragment_news, container, false); 58 | 59 | initView(); 60 | 61 | return view; 62 | } 63 | 64 | private void initView() { 65 | if (view != null) { 66 | recyclerView = view.findViewById(R.id.recycler_view); 67 | tvEmpty = view.findViewById(R.id.tv_empty); 68 | 69 | LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 70 | recyclerView.setLayoutManager(layoutManager); 71 | } else { 72 | Log.e(TAG, "view is null"); 73 | } 74 | } 75 | 76 | public void setValue(List list) { 77 | List listBeans = new ArrayList<>(); 78 | if (list != null && !list.isEmpty()) { 79 | for (int i = 0; i < list.size(); i++) { 80 | ListBean bean = new ListBean(); 81 | if (list.get(i).getTitle() != null) { 82 | bean.setTitle(list.get(i).getTitle()); 83 | } 84 | if (list.get(i).getThumbnail() != null && list.get(i).getThumbnail().getUrl() != null) { 85 | bean.setUrl(list.get(i).getThumbnail().getUrl()); 86 | } else { 87 | bean.setUrl(""); 88 | } 89 | if (list.get(i).getClickUrl() != null) { 90 | bean.setClick_url(list.get(i).getClickUrl()); 91 | } 92 | listBeans.add(bean); 93 | } 94 | if (adapter != null) { 95 | adapter.refresh(listBeans); 96 | if (recyclerView != null && tvEmpty != null) { 97 | tvEmpty.setVisibility(View.GONE); 98 | recyclerView.setVisibility(View.VISIBLE); 99 | } 100 | } else { 101 | adapter = new ContentAdapter(getActivity(), listBeans); 102 | if (recyclerView != null && tvEmpty != null) { 103 | recyclerView.setAdapter(adapter); 104 | tvEmpty.setVisibility(View.GONE); 105 | recyclerView.setVisibility(View.VISIBLE); 106 | } 107 | } 108 | onClick(list); 109 | } else { 110 | if (recyclerView != null && tvEmpty != null) { 111 | tvEmpty.setVisibility(View.VISIBLE); 112 | recyclerView.setVisibility(View.GONE); 113 | } 114 | } 115 | } 116 | 117 | private void onClick(final List list) { 118 | if (adapter != null) { 119 | adapter.setOnClickListener( 120 | new ContentAdapter.OnItemClickListener() { 121 | @Override 122 | public void click(int position) { 123 | Intent intent = new Intent(getActivity(), NewsActivity.class); 124 | if (list.get(position).getTitle() != null) { 125 | intent.putExtra("news_title", list.get(position).getTitle()); 126 | } else { 127 | intent.putExtra("news_title", ""); 128 | } 129 | if (list.get(position).getClickUrl() != null) { 130 | intent.putExtra("news_click_url", list.get(position).getClickUrl()); 131 | } else { 132 | intent.putExtra("news_click_url", ""); 133 | } 134 | if (list.get(position).getPublishTime() != null) { 135 | intent.putExtra("news_publish", list.get(position).getPublishTime()); 136 | } else { 137 | intent.putExtra("news_publish", ""); 138 | } 139 | if (list.get(position).getThumbnail() != null 140 | && list.get(position).getThumbnail().getUrl() != null) { 141 | intent.putExtra("news_thumbnail", list.get(position).getThumbnail().getUrl()); 142 | } else { 143 | intent.putExtra("news_thumbnail", ""); 144 | } 145 | if (list.get(position).getProvider() != null 146 | && list.get(position).getProvider().getSiteName() != null) { 147 | intent.putExtra("news_site_name", list.get(position).getProvider().getSiteName()); 148 | } else { 149 | intent.putExtra("news_site_name", ""); 150 | } 151 | if (list.get(position).getProvider() != null 152 | && list.get(position).getProvider().getLogo() != null) { 153 | intent.putExtra("news_logo", list.get(position).getProvider().getLogo()); 154 | } else { 155 | intent.putExtra("news_logo", ""); 156 | } 157 | startActivity(intent); 158 | } 159 | }); 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/fragment/VideoFragment.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.searchindex.fragment; 17 | 18 | import android.content.Intent; 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.TextView; 25 | import androidx.fragment.app.Fragment; 26 | import androidx.recyclerview.widget.LinearLayoutManager; 27 | import androidx.recyclerview.widget.RecyclerView; 28 | 29 | import com.huawei.hms.searchkit.bean.VideoItem; 30 | import com.huawei.searchindex.R; 31 | import com.huawei.searchindex.activity.VideoActivity; 32 | import com.huawei.searchindex.adapter.ContentAdapter; 33 | import com.huawei.searchindex.bean.ListBean; 34 | 35 | import java.util.ArrayList; 36 | import java.util.List; 37 | 38 | public class VideoFragment extends Fragment { 39 | private static final String TAG = WebFragment.class.getSimpleName(); 40 | private View view; 41 | private RecyclerView recyclerView; 42 | private ContentAdapter adapter; 43 | private TextView tvEmpty; 44 | 45 | /** 46 | * New instance main fragment. 47 | * 48 | * @return the main fragment 49 | */ 50 | public static VideoFragment newInstance() { 51 | return new VideoFragment(); 52 | } 53 | 54 | @Override 55 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 56 | // Inflate the layout for this fragment 57 | view = inflater.inflate(R.layout.fragment_video, container, false); 58 | 59 | initView(); 60 | 61 | return view; 62 | } 63 | 64 | private void initView() { 65 | if (view != null) { 66 | recyclerView = view.findViewById(R.id.recycler_view); 67 | tvEmpty = view.findViewById(R.id.tv_empty); 68 | 69 | LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 70 | recyclerView.setLayoutManager(layoutManager); 71 | } else { 72 | Log.e(TAG, "view is null"); 73 | } 74 | } 75 | 76 | public void setValue(List list) { 77 | List listBeans = new ArrayList<>(); 78 | if (list != null && !list.isEmpty()) { 79 | for (int i = 0; i < list.size(); i++) { 80 | ListBean bean = new ListBean(); 81 | if (list.get(i).getTitle() != null) { 82 | bean.setTitle(list.get(i).getTitle()); 83 | } 84 | if (list.get(i).getThumbnail() != null && list.get(i).getThumbnail().getUrl() != null) { 85 | bean.setUrl(list.get(i).getThumbnail().getUrl()); 86 | } else { 87 | bean.setUrl(""); 88 | } 89 | if (list.get(i).getClickUrl() != null) { 90 | bean.setClick_url(list.get(i).getClickUrl()); 91 | } 92 | listBeans.add(bean); 93 | } 94 | if (adapter != null) { 95 | adapter.refresh(listBeans); 96 | if (recyclerView != null && tvEmpty != null) { 97 | tvEmpty.setVisibility(View.GONE); 98 | recyclerView.setVisibility(View.VISIBLE); 99 | } 100 | } else { 101 | adapter = new ContentAdapter(getActivity(), listBeans); 102 | if (recyclerView != null && tvEmpty != null) { 103 | recyclerView.setAdapter(adapter); 104 | tvEmpty.setVisibility(View.GONE); 105 | recyclerView.setVisibility(View.VISIBLE); 106 | } 107 | } 108 | onClick(list); 109 | } else { 110 | if (recyclerView != null && tvEmpty != null) { 111 | tvEmpty.setVisibility(View.VISIBLE); 112 | recyclerView.setVisibility(View.GONE); 113 | } 114 | } 115 | } 116 | 117 | private void onClick(final List list) { 118 | if (adapter != null) { 119 | adapter.setOnClickListener( 120 | new ContentAdapter.OnItemClickListener() { 121 | @Override 122 | public void click(int position) { 123 | Intent intent = new Intent(getActivity(), VideoActivity.class); 124 | if (list.get(position).getTitle() != null) { 125 | intent.putExtra("video_title", list.get(position).getTitle()); 126 | } else { 127 | intent.putExtra("video_title", ""); 128 | } 129 | if (list.get(position).getClickUrl() != null) { 130 | intent.putExtra("video_click_url", list.get(position).getClickUrl()); 131 | } else { 132 | intent.putExtra("video_click_url", ""); 133 | } 134 | if (list.get(position).getPublishTime() != null) { 135 | intent.putExtra("video_publish", list.get(position).getPublishTime()); 136 | } else { 137 | intent.putExtra("video_publish", ""); 138 | } 139 | if (list.get(position).getDuration() != null) { 140 | intent.putExtra("video_duration", String.valueOf(list.get(position).getDuration())); 141 | } else { 142 | intent.putExtra("video_duration", ""); 143 | } 144 | if (list.get(position).getThumbnail() != null 145 | && list.get(position).getThumbnail().getUrl() != null) { 146 | intent.putExtra("video_thumbnail", list.get(position).getThumbnail().getUrl()); 147 | } else { 148 | intent.putExtra("video_thumbnail", ""); 149 | } 150 | if (list.get(position).getProvider() != null 151 | && list.get(position).getProvider().getSiteName() != null) { 152 | intent.putExtra("video_site_name", list.get(position).getProvider().getSiteName()); 153 | } else { 154 | intent.putExtra("video_site_name", ""); 155 | } 156 | if (list.get(position).getProvider() != null 157 | && list.get(position).getProvider().getLogo() != null) { 158 | intent.putExtra("video_logo", list.get(position).getProvider().getLogo()); 159 | } else { 160 | intent.putExtra("video_logo", ""); 161 | } 162 | startActivity(intent); 163 | } 164 | }); 165 | } 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/fragment/ImageFragment.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.searchindex.fragment; 17 | 18 | import android.content.Intent; 19 | import android.os.Bundle; 20 | import android.util.Log; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.TextView; 25 | import androidx.fragment.app.Fragment; 26 | import androidx.recyclerview.widget.LinearLayoutManager; 27 | import androidx.recyclerview.widget.RecyclerView; 28 | 29 | import com.huawei.hms.searchkit.bean.ImageItem; 30 | import com.huawei.searchindex.R; 31 | import com.huawei.searchindex.activity.ImageActivity; 32 | import com.huawei.searchindex.adapter.WebAdapter; 33 | import com.huawei.searchindex.bean.ListBean; 34 | 35 | import java.util.ArrayList; 36 | import java.util.List; 37 | 38 | public class ImageFragment extends Fragment { 39 | private static final String TAG = WebFragment.class.getSimpleName(); 40 | private View view; 41 | private RecyclerView recyclerView; 42 | private WebAdapter adapter; 43 | private TextView tvEmpty; 44 | 45 | /** 46 | * New instance main fragment. 47 | * 48 | * @return the main fragment 49 | */ 50 | public static ImageFragment newInstance() { 51 | return new ImageFragment(); 52 | } 53 | 54 | @Override 55 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 56 | // Inflate the layout for this fragment 57 | view = inflater.inflate(R.layout.fragment_image, container, false); 58 | 59 | initView(); 60 | 61 | return view; 62 | } 63 | 64 | private void initView() { 65 | if (view != null) { 66 | recyclerView = view.findViewById(R.id.recycler_view); 67 | tvEmpty = view.findViewById(R.id.tv_empty); 68 | 69 | LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 70 | recyclerView.setLayoutManager(layoutManager); 71 | } else { 72 | Log.e(TAG, "view is null"); 73 | } 74 | } 75 | 76 | public void setValue(List list) { 77 | List listBeans = new ArrayList<>(); 78 | if (list != null && !list.isEmpty()) { 79 | for (int i = 0; i < list.size(); i++) { 80 | ListBean bean = new ListBean(); 81 | if (list.get(i).getTitle() != null) { 82 | bean.setTitle(list.get(i).getTitle()); 83 | } 84 | if (list.get(i).getThumbnail() != null && list.get(i).getThumbnail().getUrl() != null) { 85 | bean.setUrl(list.get(i).getThumbnail().getUrl()); 86 | } else { 87 | bean.setUrl(""); 88 | } 89 | if (list.get(i).getClickUrl() != null) { 90 | bean.setClick_url(list.get(i).getClickUrl()); 91 | } 92 | listBeans.add(bean); 93 | } 94 | if (adapter != null) { 95 | adapter.refresh(listBeans); 96 | if (recyclerView != null && tvEmpty != null) { 97 | tvEmpty.setVisibility(View.GONE); 98 | recyclerView.setVisibility(View.VISIBLE); 99 | } 100 | } else { 101 | adapter = new WebAdapter(getActivity(), listBeans); 102 | if (recyclerView != null && tvEmpty != null) { 103 | recyclerView.setAdapter(adapter); 104 | tvEmpty.setVisibility(View.GONE); 105 | recyclerView.setVisibility(View.VISIBLE); 106 | } 107 | } 108 | onClick(list); 109 | } else { 110 | if (recyclerView != null && tvEmpty != null) { 111 | tvEmpty.setVisibility(View.VISIBLE); 112 | recyclerView.setVisibility(View.GONE); 113 | } 114 | } 115 | } 116 | 117 | private void onClick(final List list) { 118 | if (adapter != null) { 119 | adapter.setOnClickListener( 120 | new WebAdapter.OnItemClickListener() { 121 | @Override 122 | public void click(int position) { 123 | Intent intent = new Intent(getActivity(), ImageActivity.class); 124 | if (list.get(position).getTitle() != null) { 125 | intent.putExtra("img_title", list.get(position).getTitle()); 126 | } else { 127 | intent.putExtra("img_title", ""); 128 | } 129 | if (list.get(position).getClickUrl() != null) { 130 | intent.putExtra("img_click_url", list.get(position).getClickUrl()); 131 | } else { 132 | intent.putExtra("img_click_url", ""); 133 | } 134 | if (list.get(position).getThumbnail() != null 135 | && list.get(position).getThumbnail().getUrl() != null) { 136 | intent.putExtra("img_thumbnail", list.get(position).getThumbnail().getUrl()); 137 | } else { 138 | intent.putExtra("img_thumbnail", ""); 139 | } 140 | if (list.get(position).getSourceImage() != null 141 | && list.get(position).getSourceImage().getImageHostpageUrl() != null) { 142 | intent.putExtra( 143 | "img_host_url", list.get(position).getSourceImage().getImageHostpageUrl()); 144 | } else { 145 | intent.putExtra("img_host_url", ""); 146 | } 147 | if (list.get(position).getSourceImage() != null 148 | && list.get(position).getSourceImage().getImageContentUrl() != null) { 149 | intent.putExtra( 150 | "img_content_url", list.get(position).getSourceImage().getImageContentUrl()); 151 | } else { 152 | intent.putExtra("img_content_url", ""); 153 | } 154 | if (list.get(position).getSourceImage() != null 155 | && list.get(position).getSourceImage().getHeight() != null) { 156 | intent.putExtra("img_height", list.get(position).getSourceImage().getHeight()); 157 | } else { 158 | intent.putExtra("img_height", ""); 159 | } 160 | if (list.get(position).getSourceImage() != null 161 | && list.get(position).getSourceImage().getWidth() != null) { 162 | intent.putExtra("img_width", list.get(position).getSourceImage().getWidth()); 163 | } else { 164 | intent.putExtra("img_width", ""); 165 | } 166 | if (list.get(position).getSourceImage() != null 167 | && list.get(position).getSourceImage().getPublishTime() != null) { 168 | intent.putExtra("img_publish", list.get(position).getSourceImage().getPublishTime()); 169 | } else { 170 | intent.putExtra("img_publish", ""); 171 | } 172 | startActivity(intent); 173 | } 174 | }); 175 | } 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 16 | 17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 18 | 19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 20 | 21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 22 | 23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 24 | 25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 26 | 27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 28 | 29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 30 | 31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 36 | 37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 38 | You must cause any modified files to carry prominent notices stating that You changed the files; and 39 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 41 | 42 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 43 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 44 | 45 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 46 | 47 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 48 | 49 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 50 | 51 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 52 | 53 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/activity_web.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 24 | 25 | 29 | 36 | 40 | 49 | 61 | 62 | 68 | 69 | 73 | 82 | 94 | 95 | 101 | 102 | 106 | 115 | 127 | 128 | 134 | 135 | 139 | 148 | 160 | 161 | 167 | 168 | 171 | 172 | 183 | 184 | 195 | 196 | 202 | 203 | 208 | 218 | 231 | 232 | 241 | 242 | 243 | 244 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/activity_news.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 24 | 25 | 29 | 37 | 41 | 50 | 59 | 60 | 66 | 67 | 71 | 80 | 89 | 90 | 96 | 97 | 101 | 110 | 119 | 120 | 126 | 127 | 130 | 131 | 142 | 143 | 154 | 155 | 156 | 162 | 169 | 173 | 182 | 191 | 192 | 198 | 199 | 203 | 212 | 221 | 222 | 228 | 229 | 230 | 233 | 234 | 245 | 246 | 257 | 258 | 264 | 265 | 271 | 281 | 294 | 295 | 304 | 305 | 306 | 307 | 308 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/activity_video.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 24 | 25 | 29 | 37 | 41 | 50 | 59 | 60 | 66 | 67 | 71 | 80 | 89 | 90 | 96 | 97 | 100 | 101 | 112 | 113 | 124 | 125 | 126 | 132 | 133 | 140 | 141 | 145 | 154 | 163 | 164 | 170 | 171 | 175 | 184 | 193 | 194 | 200 | 201 | 202 | 203 | 207 | 216 | 225 | 226 | 232 | 233 | 237 | 246 | 255 | 256 | 262 | 263 | 264 | 267 | 268 | 279 | 280 | 291 | 292 | 298 | 299 | 305 | 315 | 328 | 329 | 338 | 339 | 340 | 341 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/res/layout/activity_image.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 24 | 28 | 36 | 40 | 49 | 58 | 59 | 65 | 66 | 70 | 79 | 88 | 89 | 95 | 96 | 99 | 110 | 111 | 122 | 123 | 124 | 130 | 131 | 138 | 142 | 151 | 160 | 161 | 167 | 168 | 172 | 181 | 190 | 191 | 197 | 198 | 202 | 210 | 219 | 220 | 226 | 227 | 231 | 239 | 248 | 249 | 255 | 256 | 260 | 268 | 277 | 278 | 284 | 285 | 286 | 289 | 300 | 301 | 312 | 313 | 314 | 320 | 321 | 327 | 336 | 346 | 347 | 356 | 357 | 358 | 359 | -------------------------------------------------------------------------------- /SearchKit_android_SampleCode/app/src/main/java/com/huawei/searchindex/activity/SearchActivity.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.searchindex.activity; 17 | 18 | import android.content.Context; 19 | import android.content.pm.ApplicationInfo; 20 | import android.content.pm.PackageManager; 21 | import android.os.Bundle; 22 | import android.text.Editable; 23 | import android.text.TextUtils; 24 | import android.text.TextWatcher; 25 | import android.util.Log; 26 | import android.view.KeyEvent; 27 | import android.view.View; 28 | import android.view.inputmethod.EditorInfo; 29 | import android.view.inputmethod.InputMethodManager; 30 | import android.widget.EditText; 31 | import android.widget.LinearLayout; 32 | import android.widget.TextView; 33 | import androidx.appcompat.app.AppCompatActivity; 34 | import androidx.fragment.app.FragmentManager; 35 | import androidx.recyclerview.widget.LinearLayoutManager; 36 | import androidx.recyclerview.widget.RecyclerView; 37 | import androidx.viewpager.widget.ViewPager; 38 | 39 | import com.huawei.hms.searchkit.SearchKitInstance; 40 | import com.huawei.hms.searchkit.bean.AutoSuggestResponse; 41 | import com.huawei.hms.searchkit.bean.BaseSearchResponse; 42 | import com.huawei.hms.searchkit.bean.CommonSearchRequest; 43 | import com.huawei.hms.searchkit.bean.ImageItem; 44 | import com.huawei.hms.searchkit.bean.NewsItem; 45 | import com.huawei.hms.searchkit.bean.SpellCheckResponse; 46 | import com.huawei.hms.searchkit.bean.VideoItem; 47 | import com.huawei.hms.searchkit.bean.WebItem; 48 | import com.huawei.hms.searchkit.bean.WebSearchRequest; 49 | import com.huawei.hms.searchkit.utils.Language; 50 | import com.huawei.hms.searchkit.utils.Region; 51 | import com.huawei.searchindex.R; 52 | import com.huawei.searchindex.adapter.SuggestAdapter; 53 | import com.huawei.searchindex.adapter.ViewPagerAdapter; 54 | import com.huawei.searchindex.bean.TokenResponse; 55 | import com.huawei.searchindex.fragment.ImageFragment; 56 | import com.huawei.searchindex.fragment.NewsFragment; 57 | import com.huawei.searchindex.fragment.VideoFragment; 58 | import com.huawei.searchindex.fragment.WebFragment; 59 | import com.huawei.searchindex.network.NetworkManager; 60 | import com.huawei.searchindex.network.QueryService; 61 | 62 | import com.google.android.material.tabs.TabLayout; 63 | 64 | import java.util.ArrayList; 65 | import java.util.List; 66 | 67 | import io.reactivex.Observable; 68 | import io.reactivex.ObservableEmitter; 69 | import io.reactivex.ObservableOnSubscribe; 70 | import io.reactivex.Observer; 71 | import io.reactivex.android.schedulers.AndroidSchedulers; 72 | import io.reactivex.disposables.Disposable; 73 | import io.reactivex.functions.Consumer; 74 | import io.reactivex.schedulers.Schedulers; 75 | 76 | public class SearchActivity extends AppCompatActivity { 77 | private static final String TAG = SearchActivity.class.getSimpleName(); 78 | private EditText editSearch; 79 | private LinearLayout linearSpellCheck, linearViewPager; 80 | private RecyclerView recyclerSuggest, recyclerContent; 81 | private ViewPager viewPager; 82 | private TabLayout tabLayout; 83 | private ViewPagerAdapter viewPagerAdapter; 84 | private SuggestAdapter suggestAdapter; 85 | private TextView tvSpellCheck; 86 | public static int mCurrentPage = 0; 87 | public static final WebSearchRequest webRequest = new WebSearchRequest(); 88 | public static final CommonSearchRequest commonRequest = new CommonSearchRequest(); 89 | 90 | @Override 91 | protected void onCreate(Bundle savedInstanceState) { 92 | super.onCreate(savedInstanceState); 93 | setContentView(R.layout.activity_search); 94 | 95 | SearchKitInstance.enableLog(); 96 | SearchKitInstance.init(this, "103039265"); 97 | // SearchKitInstance.init(this, "101513199"); 98 | 99 | initRetrofit(); 100 | 101 | initView(); 102 | } 103 | 104 | private void initView() { 105 | editSearch = findViewById(R.id.search_view); 106 | linearSpellCheck = findViewById(R.id.linear_spell_check); 107 | recyclerSuggest = findViewById(R.id.suggest_list); 108 | recyclerContent = findViewById(R.id.content_list); 109 | tvSpellCheck = findViewById(R.id.tv_spell_check); 110 | viewPager = findViewById(R.id.view_pager); 111 | tabLayout = findViewById(R.id.tab_layout); 112 | linearViewPager = findViewById(R.id.linear_view_pager); 113 | 114 | LinearLayoutManager layoutManager = new LinearLayoutManager(SearchActivity.this); 115 | recyclerSuggest.setLayoutManager(layoutManager); 116 | 117 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SearchActivity.this); 118 | recyclerContent.setLayoutManager(linearLayoutManager); 119 | 120 | FragmentManager fm = getSupportFragmentManager(); 121 | if (null == viewPagerAdapter) { 122 | viewPagerAdapter = new ViewPagerAdapter(fm, this); 123 | } 124 | viewPager.setAdapter(viewPagerAdapter); 125 | tabLayout.setupWithViewPager(viewPager); 126 | viewPager.setOffscreenPageLimit(3); 127 | 128 | initViewPagerListener(); 129 | onEditTextListener(); 130 | } 131 | 132 | private void initViewPagerListener() { 133 | viewPager.addOnPageChangeListener( 134 | new ViewPager.OnPageChangeListener() { 135 | @Override 136 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} 137 | 138 | @Override 139 | public void onPageSelected(int position) { 140 | mCurrentPage = position; 141 | } 142 | 143 | @Override 144 | public void onPageScrollStateChanged(int state) {} 145 | }); 146 | } 147 | 148 | private void onEditTextListener() { 149 | editSearch.addTextChangedListener( 150 | new TextWatcher() { 151 | @Override 152 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 153 | 154 | @Override 155 | public void onTextChanged(CharSequence s, int start, int before, int count) { 156 | recyclerSuggest.setVisibility(View.VISIBLE); 157 | linearSpellCheck.setVisibility(View.GONE); 158 | 159 | linearViewPager.setVisibility(View.GONE); 160 | 161 | if (!TextUtils.isEmpty(s.toString())) { 162 | getSuggest(s.toString()); 163 | } else { 164 | recyclerSuggest.setVisibility(View.GONE); 165 | linearViewPager.setVisibility(View.VISIBLE); 166 | if (TextUtils.isEmpty(tvSpellCheck.getText().toString())) { 167 | linearSpellCheck.setVisibility(View.GONE); 168 | } else { 169 | linearSpellCheck.setVisibility(View.VISIBLE); 170 | } 171 | if (suggestAdapter != null) { 172 | suggestAdapter.clear(); 173 | } 174 | } 175 | } 176 | 177 | @Override 178 | public void afterTextChanged(Editable s) {} 179 | }); 180 | 181 | editSearch.setOnEditorActionListener( 182 | new TextView.OnEditorActionListener() { 183 | @Override 184 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 185 | if (actionId == EditorInfo.IME_ACTION_SEARCH) { 186 | recyclerSuggest.setVisibility(View.GONE); 187 | linearViewPager.setVisibility(View.VISIBLE); 188 | 189 | hintSoftKeyboard(); 190 | getSpellCheck(v.getText().toString()); 191 | return true; 192 | } 193 | return false; 194 | } 195 | }); 196 | } 197 | 198 | private void getSpellCheck(final String query) { 199 | Observable.create( 200 | new ObservableOnSubscribe() { 201 | @Override 202 | public void subscribe(ObservableEmitter emitter) throws Exception { 203 | SpellCheckResponse response = 204 | SearchKitInstance.getInstance() 205 | .getSearchHelper() 206 | .spellCheck(query, Language.ENGLISH); 207 | if (response != null && response.getCorrectedQuery() != null) { 208 | emitter.onNext(response.getCorrectedQuery()); 209 | } else { 210 | Log.e(TAG, "spell error"); 211 | emitter.onNext(""); 212 | } 213 | } 214 | }) 215 | .subscribeOn(Schedulers.newThread()) 216 | .observeOn(AndroidSchedulers.mainThread()) 217 | .subscribe( 218 | new Consumer() { 219 | @Override 220 | public void accept(String s) throws Exception { 221 | if (TextUtils.isEmpty(s)) { 222 | linearSpellCheck.setVisibility(View.GONE); 223 | } else { 224 | linearSpellCheck.setVisibility(View.VISIBLE); 225 | tvSpellCheck.setText(s); 226 | } 227 | doSearch(query); 228 | } 229 | }, 230 | StaticUtils.consumer); 231 | } 232 | 233 | private void getSuggest(final String query) { 234 | Observable.create( 235 | new ObservableOnSubscribe>() { 236 | @Override 237 | public void subscribe(ObservableEmitter> emitter) throws Exception { 238 | AutoSuggestResponse response = 239 | SearchKitInstance.getInstance() 240 | .getSearchHelper() 241 | .suggest(query, Language.ENGLISH); 242 | List list = new ArrayList(); 243 | if (response != null) { 244 | if (response.getSuggestions() != null && !response.getSuggestions().isEmpty()) { 245 | for (int i = 0; i < response.getSuggestions().size(); i++) { 246 | list.add(response.getSuggestions().get(i).getName()); 247 | } 248 | emitter.onNext(list); 249 | } 250 | } 251 | emitter.onComplete(); 252 | } 253 | }) 254 | .subscribeOn(Schedulers.newThread()) 255 | .observeOn(AndroidSchedulers.mainThread()) 256 | .subscribe( 257 | new Consumer>() { 258 | @Override 259 | public void accept(List list) throws Exception { 260 | if (suggestAdapter != null) { 261 | suggestAdapter.clear(); 262 | } 263 | suggestAdapter = new SuggestAdapter(list); 264 | recyclerSuggest.setAdapter(suggestAdapter); 265 | 266 | suggestAdapter.setOnClickListener( 267 | new SuggestAdapter.OnItemClickListener() { 268 | @Override 269 | public void click(String text) { 270 | doSearch(text); 271 | editSearch.setText(text); 272 | hintSoftKeyboard(); 273 | recyclerSuggest.setVisibility(View.GONE); 274 | 275 | linearViewPager.setVisibility(View.VISIBLE); 276 | } 277 | }); 278 | } 279 | }, 280 | StaticUtils.consumer); 281 | } 282 | 283 | private void doSearch(String query) { 284 | webRequest.setQ(query); 285 | webRequest.setLang(Language.ENGLISH); 286 | webRequest.setSregion(Region.UNITEDKINGDOM); 287 | webRequest.setPn(1); 288 | webRequest.setPs(10); 289 | webRequest.setWithin("www.amazon.com"); 290 | 291 | commonRequest.setQ(query); 292 | commonRequest.setLang(Language.ENGLISH); 293 | commonRequest.setSregion(Region.UNITEDKINGDOM); 294 | commonRequest.setPn(1); 295 | commonRequest.setPs(10); 296 | 297 | Observable.create(StaticUtils.observable) 298 | .subscribeOn(Schedulers.newThread()) 299 | .observeOn(AndroidSchedulers.mainThread()) 300 | .subscribe( 301 | new Consumer() { 302 | @Override 303 | public void accept(BaseSearchResponse baseSearchResponse) throws Exception { 304 | if (baseSearchResponse != null && baseSearchResponse.getData() != null) { 305 | if (mCurrentPage == 0) { 306 | WebFragment webFragment = 307 | (WebFragment) viewPagerAdapter.getFragments().get(mCurrentPage); 308 | webFragment.setValue((List) baseSearchResponse.getData()); 309 | } else if (mCurrentPage == 1) { 310 | ImageFragment imageFragment = 311 | (ImageFragment) viewPagerAdapter.getFragments().get(mCurrentPage); 312 | imageFragment.setValue((List) baseSearchResponse.getData()); 313 | } else if (mCurrentPage == 2) { 314 | VideoFragment videoFragment = 315 | (VideoFragment) viewPagerAdapter.getFragments().get(mCurrentPage); 316 | videoFragment.setValue((List) baseSearchResponse.getData()); 317 | } else if (mCurrentPage == 3) { 318 | NewsFragment newsFragment = 319 | (NewsFragment) viewPagerAdapter.getFragments().get(mCurrentPage); 320 | newsFragment.setValue((List) baseSearchResponse.getData()); 321 | } 322 | } 323 | } 324 | }, 325 | StaticUtils.consumer); 326 | } 327 | 328 | private void hintSoftKeyboard() { 329 | InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 330 | if (imm != null && imm.isActive() && this.getCurrentFocus() != null) { 331 | if (this.getCurrentFocus().getWindowToken() != null) { 332 | imm.hideSoftInputFromWindow( 333 | this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 334 | } 335 | } 336 | } 337 | 338 | public void initRetrofit() { 339 | ApplicationInfo appInfo = null; 340 | String baseUrl = ""; 341 | try { 342 | appInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); 343 | baseUrl = appInfo.metaData.getString("baseUrl"); 344 | } catch (PackageManager.NameNotFoundException e) { 345 | Log.e(TAG, "get meta data error: " + e.getMessage()); 346 | } 347 | QueryService service = NetworkManager.getInstance().createService(this, baseUrl); 348 | service.getRequestToken( 349 | "client_credentials", 350 | "103039265", 351 | "d64ba957b4c063403aecc700e138feec29ab4900ddd2c92a78bb72bd9325ee2c") 352 | // service.getRequestToken("client_credentials", "101513199", 353 | // "6333c6fa883a91f8b0bd783d43fda2b5695cb9d4612a481e2d55e6f80c2d870b") 354 | .subscribeOn(Schedulers.io()) 355 | .observeOn(AndroidSchedulers.mainThread()) 356 | .subscribe(new Observer() { 357 | @Override 358 | public void onSubscribe(Disposable d) { 359 | 360 | } 361 | 362 | @Override 363 | public void onNext(TokenResponse tokenResponse) { 364 | if (tokenResponse != null) { 365 | if (tokenResponse.getAccess_token() != null) { 366 | // Log.e(TAG, response.getBody().getAccess_token()); 367 | SearchKitInstance.getInstance().setInstanceCredential(tokenResponse.getAccess_token()); 368 | } else { 369 | Log.e(TAG, "get responseBody token is null"); 370 | } 371 | } else { 372 | Log.e(TAG, "get responseBody is null"); 373 | } 374 | } 375 | 376 | @Override 377 | public void onError(Throwable e) { 378 | Log.e(TAG, "get token error: " + e.getMessage()); 379 | } 380 | 381 | @Override 382 | public void onComplete() { 383 | 384 | } 385 | }); 386 | } 387 | 388 | private static class StaticUtils { 389 | private static class MyConsumer implements Consumer { 390 | @Override 391 | public void accept(Throwable throwable) throws Exception { 392 | Log.e(TAG, "do search error: " + throwable.getMessage()); 393 | } 394 | } 395 | 396 | private static Consumer consumer = new MyConsumer(); 397 | 398 | private static class MyObservable implements ObservableOnSubscribe { 399 | @Override 400 | public void subscribe(ObservableEmitter emitter) throws Exception { 401 | if (mCurrentPage == 0) { 402 | BaseSearchResponse> webResponse = 403 | SearchKitInstance.getInstance().getWebSearcher().search(webRequest); 404 | emitter.onNext(webResponse); 405 | } else if (mCurrentPage == 1) { 406 | BaseSearchResponse> imageResponse = 407 | SearchKitInstance.getInstance().getImageSearcher().search(commonRequest); 408 | emitter.onNext(imageResponse); 409 | } else if (mCurrentPage == 2) { 410 | BaseSearchResponse> videoResponse = 411 | SearchKitInstance.getInstance().getVideoSearcher().search(commonRequest); 412 | emitter.onNext(videoResponse); 413 | } else if (mCurrentPage == 3) { 414 | BaseSearchResponse> newsResponse = 415 | SearchKitInstance.getInstance().getNewsSearcher().search(commonRequest); 416 | emitter.onNext(newsResponse); 417 | } 418 | } 419 | } 420 | 421 | private static ObservableOnSubscribe observable = new MyObservable(); 422 | } 423 | } 424 | --------------------------------------------------------------------------------