|
@@ -1,320 +0,0 @@
|
|
|
-package com.quansu.heifengwuliu.utils.search
|
|
|
-
|
|
|
-import android.app.Activity
|
|
|
-import android.app.PendingIntent
|
|
|
-import android.content.ComponentName
|
|
|
-import android.content.Context
|
|
|
-import android.content.Intent
|
|
|
-import android.graphics.BitmapFactory
|
|
|
-import android.net.Uri
|
|
|
-import android.text.TextUtils
|
|
|
-import androidx.browser.customtabs.CustomTabsIntent
|
|
|
-import com.billy.cc.core.component.CC
|
|
|
-import com.quansu.heifengwuliu.App
|
|
|
-import com.quansu.heifengwuliu.R
|
|
|
-import com.quansu.heifengwuliu.model.SearchEngine
|
|
|
-import com.quansu.heifengwuliu.receiver.ActionBroadcastReceiver
|
|
|
-import com.quansu.heifengwuliu.repository.SearchRepository
|
|
|
-import com.quansu.heifengwuliu.utils.CustomTabActivityHelper
|
|
|
-import com.quansu.heifengwuliu.utils.IntentUtils
|
|
|
-import com.quansu.heifengwuliu.utils.ShareUtils
|
|
|
-import com.ysnows.base.utils.PackageUtils
|
|
|
-import com.ysnows.base.utils.Toasts
|
|
|
-import java.io.UnsupportedEncodingException
|
|
|
-import java.net.URLEncoder
|
|
|
-
|
|
|
-class SearchAwoke(private val context: Context) {
|
|
|
-
|
|
|
- fun search(kw: String, searchCallBack: SearchCallBack, repository: SearchRepository? = null) {
|
|
|
- search(kw, App.default_search, repository = repository, searchCallBack)
|
|
|
- }
|
|
|
-
|
|
|
- fun search(kw: String, searchEngine: SearchEngine? = App.default_search, repository: SearchRepository? = null, searchCallBack: SearchCallBack? = null) {
|
|
|
- if (searchEngine == null) {
|
|
|
- Toasts.toast(context, "请先设定一个默认搜索引擎")
|
|
|
- return
|
|
|
- }
|
|
|
- if (TextUtils.isEmpty(kw.trim { it <= ' ' })) {
|
|
|
- Toasts.toast(context, "搜个空白有啥子用?")
|
|
|
- return
|
|
|
- }
|
|
|
- try {
|
|
|
- if (searchEngine.type == SearchEngine.TYPE_SEARCH) {
|
|
|
- val url: String
|
|
|
- url = if (kw.startsWith("http://") || kw.startsWith("https://")) {
|
|
|
- kw
|
|
|
- } else {
|
|
|
- val encodeStr = URLEncoder.encode(kw, "UTF-8")
|
|
|
- String.format(searchEngine.url, encodeStr)
|
|
|
- }
|
|
|
- openInBrowser(url)
|
|
|
- } else if (searchEngine.type == SearchEngine.TYPE_APP) {
|
|
|
- val encodeStr = URLEncoder.encode(kw, "UTF-8")
|
|
|
- val url = String.format(searchEngine.url, encodeStr)
|
|
|
- if (PackageUtils.isAppInstalled(searchEngine.packageName)) {
|
|
|
- openInApp(url)
|
|
|
- } else {
|
|
|
- Toasts.toast(context, R.string.install_app_first_please)
|
|
|
- return
|
|
|
- }
|
|
|
- } else if (searchEngine.type == SearchEngine.TYPE_VIEW) {
|
|
|
- val encodeStr = URLEncoder.encode(kw, "UTF-8")
|
|
|
- val url = String.format(searchEngine.url, encodeStr)
|
|
|
- if (PackageUtils.isAppInstalled(searchEngine.packageName)) {
|
|
|
- openInAppWithComponent(url, searchEngine.packageName, searchEngine.className)
|
|
|
- } else {
|
|
|
- Toasts.toast(context, R.string.install_app_first_please)
|
|
|
- return
|
|
|
- }
|
|
|
- } else if (searchEngine.type == SearchEngine.TYPE_VIEW_WITH_URI) {
|
|
|
- val encodeStr = URLEncoder.encode(kw, "UTF-8")
|
|
|
- val url = String.format(searchEngine.url, encodeStr)
|
|
|
- if (PackageUtils.isAppInstalled(searchEngine.packageName)) {
|
|
|
- openInAppWithUri(url)
|
|
|
- } else {
|
|
|
- Toasts.toast(context, R.string.install_app_first_please)
|
|
|
- return
|
|
|
- }
|
|
|
- } else if (searchEngine.type == SearchEngine.TYPE_SHARE) {
|
|
|
- if (PackageUtils.isAppInstalled(searchEngine.packageName)) {
|
|
|
- openInAppWithShareComponent(kw, searchEngine.packageName, searchEngine.className)
|
|
|
- } else {
|
|
|
- Toasts.toast(context, R.string.install_app_first_please)
|
|
|
- return
|
|
|
- }
|
|
|
- } else if (searchEngine.type == SearchEngine.TYPE_SHARE_ACTION) {
|
|
|
- ShareUtils.share(context, kw)
|
|
|
- } else if (searchEngine.type == SearchEngine.TYPE_IN_APP) {
|
|
|
- //调用APP内的功能
|
|
|
- val urlStr = searchEngine.url
|
|
|
- val uri = Uri.parse(urlStr)
|
|
|
- uri.scheme
|
|
|
- var path = uri.path
|
|
|
- path = path.substring(1, path.length)
|
|
|
- val host = uri.host
|
|
|
- val queryParameterNames = uri.queryParameterNames
|
|
|
- val builder = CC.obtainBuilder(host)
|
|
|
- .setContext(context)
|
|
|
- .setActionName(path)
|
|
|
- builder.addParam("kw", kw)
|
|
|
- for (queryParameterName in queryParameterNames) {
|
|
|
- builder.addParam(queryParameterName, uri.getQueryParameter(queryParameterName))
|
|
|
- }
|
|
|
- builder.build()
|
|
|
- .callAsync()
|
|
|
- } else if (SearchEngine.TYPE_IN_CHARGE_OUT == searchEngine.type) {
|
|
|
- CC.obtainBuilder("charge")
|
|
|
- .setActionName("out")
|
|
|
- .addParam("kw", kw)
|
|
|
- .addParam("payway", 2)
|
|
|
- .addParam("chargeCat", 1)
|
|
|
- .setContext(context)
|
|
|
- .build()
|
|
|
- .callAsync()
|
|
|
- } else if (SearchEngine.TYPE_IN_CHARGE_IN == searchEngine.type) {
|
|
|
- CC.obtainBuilder("charge")
|
|
|
- .setActionName("in")
|
|
|
- .addParam("kw", kw)
|
|
|
- .setContext(context)
|
|
|
- .build()
|
|
|
- .callAsync()
|
|
|
- } else if (SearchEngine.TYPE_ADD_TODO == searchEngine.type) {
|
|
|
-
|
|
|
- if (repository == null) {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- repository.addTodo(kw)
|
|
|
- .doOnNext { }
|
|
|
- .subscribe()
|
|
|
-
|
|
|
- } else if (SearchEngine.TYPE_ROUTE == searchEngine.type) {
|
|
|
- val uri = Uri.parse(searchEngine.url)
|
|
|
- val host = uri.host
|
|
|
- val lastPathSegment = uri.lastPathSegment
|
|
|
- CC.obtainBuilder(host)
|
|
|
- .setActionName(lastPathSegment)
|
|
|
- .setContext(context)
|
|
|
- .addParam("query", kw)
|
|
|
- .build()
|
|
|
- .callAsync()
|
|
|
- }
|
|
|
- searchCallBack?.onSearched()
|
|
|
- } catch (e: UnsupportedEncodingException) {
|
|
|
- e.printStackTrace()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private fun openInAppWithComponent(url: String, packageName: String, className: String) {
|
|
|
- val intent: Intent
|
|
|
- val uri = Uri.parse(url)
|
|
|
- intent = Intent(Intent.ACTION_VIEW, uri)
|
|
|
- intent.component = ComponentName(packageName, className)
|
|
|
- context.startActivity(intent)
|
|
|
- }
|
|
|
-
|
|
|
- private fun openInAppWithUri(url: String) {
|
|
|
- context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
|
|
|
- }
|
|
|
-
|
|
|
- private fun openInAppWithShareComponent(content: String, packageName: String, className: String) {
|
|
|
- val intent = IntentUtils.shareIntent(packageName, className, content)
|
|
|
- context.startActivity(intent)
|
|
|
- }
|
|
|
-
|
|
|
- private fun openInApp(url: String) {
|
|
|
- val uri = Uri.parse(url)
|
|
|
- val intent = Intent(Intent.ACTION_VIEW, uri)
|
|
|
- intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
|
|
- context.startActivity(intent)
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 用系统浏览器打开搜索
|
|
|
- *
|
|
|
- * @param url
|
|
|
- */
|
|
|
- private fun openInBrowser(url: String) {
|
|
|
-
|
|
|
- openInCustomTab(url)
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 用系统浏览器打开搜索
|
|
|
- *
|
|
|
- * @param url
|
|
|
- */
|
|
|
- private fun openInSystemBrowser(url: String) {
|
|
|
- val uri = Uri.parse(url)
|
|
|
- val intent = Intent(Intent.ACTION_VIEW, uri)
|
|
|
- context.startActivity(intent)
|
|
|
-
|
|
|
-// String chromePackageName = getChromePackageName();
|
|
|
-// if (!TextUtils.isEmpty(chromePackageName)) {
|
|
|
-// Bundle extras = new Bundle();
|
|
|
-// extras.putBinder(EXTRA_CUSTOM_TABS_SESSION,
|
|
|
-// null /* Set to null for no session */);
|
|
|
-// intentObj.putExtra(EXTRA_CUSTOM_TABS_TOOLBAR_COLOR, Color.parseColor("#3C2DCB"));
|
|
|
-// intentObj.putExtra(EXTRA_CUSTOM_TABS_SHARE_MENU_ITEM, true);
|
|
|
-// intentObj.setPackage(chromePackageName);
|
|
|
-// intentObj.setData(uri);
|
|
|
-// intentObj.putExtras(extras);
|
|
|
-// }
|
|
|
- }
|
|
|
-
|
|
|
- fun openInCustomTab(url: String?) {
|
|
|
- val color = context.resources.getColor(R.color.colorPrimary)
|
|
|
- val secondaryColor = context.resources.getColor(R.color.colorPrimary)
|
|
|
- val intentBuilder = CustomTabsIntent.Builder()
|
|
|
- intentBuilder.setToolbarColor(color)
|
|
|
- intentBuilder.setSecondaryToolbarColor(secondaryColor)
|
|
|
-
|
|
|
- //Generally you do not want to decode bitmaps in the UI thread. Decoding it in the
|
|
|
- //UI thread to keep the example short.
|
|
|
- val actionLabel = context.getString(R.string.pocket)
|
|
|
- val icon = BitmapFactory.decodeResource(context.resources,
|
|
|
- R.drawable.pocket)
|
|
|
- val pendingIntent = createPendingIntent(ActionBroadcastReceiver.ACTION_ACTION_BUTTON)
|
|
|
- intentBuilder.setActionButton(icon, actionLabel, pendingIntent)
|
|
|
-
|
|
|
-// if (mAddMenusCheckbox.isChecked()) {
|
|
|
-// String menuItemTitle = getString(R.string.menu_item_title);
|
|
|
-// PendingIntent menuItemPendingIntent =
|
|
|
-// createPendingIntent(ActionBroadcastReceiver.ACTION_MENU_ITEM);
|
|
|
-// intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent);
|
|
|
-// }
|
|
|
-//
|
|
|
- intentBuilder.addDefaultShareMenuItem()
|
|
|
- //
|
|
|
-// if (mToolbarItemCheckbox.isChecked()) {
|
|
|
-// //Generally you do not want to decode bitmaps in the UI thread. Decoding it in the
|
|
|
-// //UI thread to keep the example short.
|
|
|
-// String actionLabel = getString(R.string.label_action);
|
|
|
-// Bitmap icon = BitmapFactory.decodeResource(getResources(),
|
|
|
-// android.R.drawable.ic_menu_share);
|
|
|
-// PendingIntent pendingIntent =
|
|
|
-// createPendingIntent(ActionBroadcastReceiver.ACTION_TOOLBAR);
|
|
|
-// intentBuilder.addToolbarItem(TOOLBAR_ITEM_ID, icon, actionLabel, pendingIntent);
|
|
|
-// }
|
|
|
-//
|
|
|
- intentBuilder.setShowTitle(true)
|
|
|
- intentBuilder.enableUrlBarHiding()
|
|
|
- intentBuilder.setCloseButtonIcon(
|
|
|
- BitmapFactory.decodeResource(context.resources, R.drawable.ic_back))
|
|
|
- intentBuilder.setStartAnimations(context, R.anim.slide_in_right, R.anim.slide_out_left)
|
|
|
- intentBuilder.setExitAnimations(context, android.R.anim.slide_in_left,
|
|
|
- android.R.anim.slide_out_right)
|
|
|
- CustomTabActivityHelper.openCustomTab(
|
|
|
- context as Activity, intentBuilder.build(), Uri.parse(url)) { activity, uri ->
|
|
|
- val urlStr = uri.toString()
|
|
|
- openInSystemBrowser(urlStr)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- fun openInCustomTabIntent(url: String?): Intent {
|
|
|
- val color = context.resources.getColor(R.color.colorPrimary)
|
|
|
- val secondaryColor = context.resources.getColor(R.color.colorPrimary)
|
|
|
- val intentBuilder = CustomTabsIntent.Builder()
|
|
|
- intentBuilder.setToolbarColor(color)
|
|
|
- intentBuilder.setSecondaryToolbarColor(secondaryColor)
|
|
|
-
|
|
|
- //Generally you do not want to decode bitmaps in the UI thread. Decoding it in the
|
|
|
- //UI thread to keep the example short.
|
|
|
- val actionLabel = context.getString(R.string.pocket)
|
|
|
- val icon = BitmapFactory.decodeResource(context.resources,
|
|
|
- R.drawable.pocket)
|
|
|
- val pendingIntent = createPendingIntent(ActionBroadcastReceiver.ACTION_ACTION_BUTTON)
|
|
|
- intentBuilder.setActionButton(icon, actionLabel, pendingIntent)
|
|
|
-
|
|
|
-// if (mAddMenusCheckbox.isChecked()) {
|
|
|
-// String menuItemTitle = getString(R.string.menu_item_title);
|
|
|
-// PendingIntent menuItemPendingIntent =
|
|
|
-// createPendingIntent(ActionBroadcastReceiver.ACTION_MENU_ITEM);
|
|
|
-// intentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent);
|
|
|
-// }
|
|
|
-//
|
|
|
- intentBuilder.addDefaultShareMenuItem()
|
|
|
- //
|
|
|
-// if (mToolbarItemCheckbox.isChecked()) {
|
|
|
-// //Generally you do not want to decode bitmaps in the UI thread. Decoding it in the
|
|
|
-// //UI thread to keep the example short.
|
|
|
-// String actionLabel = getString(R.string.label_action);
|
|
|
-// Bitmap icon = BitmapFactory.decodeResource(getResources(),
|
|
|
-// android.R.drawable.ic_menu_share);
|
|
|
-// PendingIntent pendingIntent =
|
|
|
-// createPendingIntent(ActionBroadcastReceiver.ACTION_TOOLBAR);
|
|
|
-// intentBuilder.addToolbarItem(TOOLBAR_ITEM_ID, icon, actionLabel, pendingIntent);
|
|
|
-// }
|
|
|
-//
|
|
|
- intentBuilder.setShowTitle(true)
|
|
|
- intentBuilder.enableUrlBarHiding()
|
|
|
- intentBuilder.setCloseButtonIcon(
|
|
|
- BitmapFactory.decodeResource(context.resources, R.drawable.ic_back_black))
|
|
|
- intentBuilder.setStartAnimations(context, R.anim.slide_in_right, R.anim.slide_out_left)
|
|
|
- intentBuilder.setExitAnimations(context, android.R.anim.slide_in_left,
|
|
|
- android.R.anim.slide_out_right)
|
|
|
- return CustomTabActivityHelper.openCustomTabIntent(context, intentBuilder.build(), url)
|
|
|
- }
|
|
|
-
|
|
|
- private fun createPendingIntent(actionSourceId: Int): PendingIntent {
|
|
|
- val actionIntent = Intent(
|
|
|
- context.applicationContext, ActionBroadcastReceiver::class.java)
|
|
|
- actionIntent.putExtra(ActionBroadcastReceiver.KEY_ACTION_SOURCE, actionSourceId)
|
|
|
- return PendingIntent.getBroadcast(
|
|
|
- context.applicationContext, actionSourceId, actionIntent, 0)
|
|
|
- }
|
|
|
-
|
|
|
- private fun getPocketIntent(sharedUrl: String): PendingIntent {
|
|
|
- val intentApp = Intent()
|
|
|
- intentApp.setClassName("com.ideashower.readitlater.pro", "com.ideashower.readitlater.activity.AddActivity")
|
|
|
- intentApp.putExtra(Intent.EXTRA_TEXT, sharedUrl)
|
|
|
- intentApp.addCategory(Intent.CATEGORY_HOME) // run in background
|
|
|
- intentApp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) //run in another task
|
|
|
- return PendingIntent.getActivity(
|
|
|
- context.applicationContext, 0, intentApp, 0)
|
|
|
- }
|
|
|
-
|
|
|
- interface SearchCallBack {
|
|
|
- fun onSearched()
|
|
|
- }
|
|
|
-}
|