文档管理中心
示例代码畅连能力屏幕共享

屏幕共享

代码结构

展开

模块

文件名

文件用途

CaaSEngineDemo

CaasKitHelper.java

对CaaS Engine接口的封装类。

CaasKitApplication.java

全局的Application。

CaasKitDemoActivity.java

全局Activity布局文件。

Screen sharing Demo

CaasKitHelper.java

收起
自动换行
深色代码主题
复制
  1. /*
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
  3. */
  4. package com.huawei.caaskitdemo.caaskit;
  5. import android.content.Context;
  6. import android.util.Log;
  7. import com.huawei.caas.caasservice.HwCaasHandler;
  8. import com.huawei.caas.caasservice.HwCaasServiceCallBack;
  9. import com.huawei.caas.caasservice.HwCaasServiceManager;
  10. import com.huawei.caas.caasservice.HwCaasUtils;
  11. import com.huawei.caas.caasservice.HwCallStateCallBack;
  12. import com.huawei.caaskitdemo.CaasKitApplication;
  13. /**
  14. * CaasKit Helper.
  15. *
  16. * @since 2019-10-21
  17. */
  18. public class CaasKitHelper {
  19. private static final String TAG = "CaasKitHelper";
  20. private static final int VIEWHEIGHT = 248;
  21. private static final int VIEWWIDTH = 256;
  22. private static final int LOCATION_X = 102;
  23. private static final int LOCATION_Y = 140;
  24. private static CaasKitHelper sCaasKitHelper;
  25. private HwCaasServiceManager mHwCaasServiceManager;
  26. private HwCaasHandler mHwCaasHandler;
  27. private Context mContext;
  28. private boolean mIsSendShowFail;
  29. private boolean mIsCaasKitInit;
  30. private boolean mIsHasCaaSContacts = false;
  31. private HwCallStateCallBack mCallStateCallBack = new HwCallStateCallBack() {
  32. @Override
  33. public void notifyCallState(HwCaasUtils.CallState state) {
  34. Log.d(TAG, "callState: " + state);
  35. }
  36. };
  37. private HwCaasServiceCallBack mCallBack = new HwCaasServiceCallBack() {
  38. @Override
  39. public void initSuccess(HwCaasHandler handler) {
  40. // callback after successful initialization of HwCaasHandler.
  41. mHwCaasHandler = handler;
  42. if (mHwCaasHandler != null) {
  43. // 显示在对端呼入界面的来电应用名称,必须设置,不然畅连入口无法显示
  44. mHwCaasHandler.setCallerAppName("Application");
  45. // 联系人选择悬浮窗也可以选择全屏列表(类似系统联系人界面)
  46. mHwCaasHandler.setContactViewStyle(HwCaasUtils.ContactsViewStyle.FULL_SCREEN);
  47. // 当选择悬浮窗时,可设置悬浮窗的大小和初始位置,选择全屏联系人界面了这些设置就不需要了
  48. // mHwCaasHandler.setContactViewStyle(HwCaasUtils.ContactsViewStyle.FLOAT_VIEW);
  49. // mHwCaasHandler.setContactViewSize(VIEWWIDTH, VIEWHEIGHT);
  50. // mHwCaasHandler.setFloatViewLocation(HwCaasUtils.STARTVIEW,
  51. // HwCaasUtils.POINT_RIGHTANDDOWN, LOCATION_X, LOCATION_Y);
  52. // mHwCaasHandler.setFloatViewLocation(HwCaasUtils.CONTACTVIEW, HwCaasUtils.POINT_RIGHTANDDOWN, LOCATION_X, LOCATION_Y);
  53. //监听通话状态,呼出中,通话中,无通话,不关心则不用设置
  54. mHwCaasHandler.setCallStateCallBack(mCallStateCallBack);
  55. //查询是否有符合要求的联系人,ContactsType.NORMAL_CONTACTS - 普通畅连联系人;ContactsType.SCREEN_SHARING_CONTACTS - 支持屏幕共享的联系人
  56. mIsHasCaaSContacts = mHwCaasHandler.hasCaaSContacts(HwCaasUtils.ContactsType.SCREEN_SHARING_CONTACTS);
  57. if (mIsSendShowFail) {
  58. sendShow();
  59. mIsSendShowFail = false;
  60. }
  61. }
  62. }
  63. @Override
  64. public void initFail(int retCode) {
  65. // callback if init Handler fail.
  66. Log.i(TAG, "retCode: " + retCode);
  67. }
  68. @Override
  69. public void releaseSuccess() {
  70. // callback after successful release of mHwCaasServiceManager.
  71. mHwCaasHandler = null;
  72. mIsSendShowFail = false;
  73. }
  74. };
  75. private CaasKitHelper() {
  76. mContext = CaasKitApplication.getContext();
  77. }
  78. public synchronized static CaasKitHelper getInstance() {
  79. if (sCaasKitHelper == null) {
  80. sCaasKitHelper = new CaasKitHelper();
  81. }
  82. return sCaasKitHelper;
  83. }
  84. public void caasKitInit() {
  85. Log.d(TAG, "caasKitInit.");
  86. if (!mIsCaasKitInit) {
  87. // initialize mHwCaasServiceManager instance.
  88. mHwCaasServiceManager = HwCaasServiceManager.init();
  89. // initialize HwCaasHandler instance through handlerType.
  90. mHwCaasServiceManager.initHandler(mContext, HwCaasUtils.SCREEN_SHARING_TYPE, mCallBack);
  91. mIsCaasKitInit = true;
  92. }
  93. }
  94. public void sendShow() {
  95. Log.d(TAG, "sendShow.");
  96. if (mHwCaasHandler != null) {
  97. Log.d(TAG, "isHasCaaSContacts: " + mIsHasCaaSContacts);
  98. if (mIsHasCaaSContacts) {
  99. // 点击入口在应用侧,直接显示联系人列表
  100. boolean ret = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SHOW_CONTACTS);
  101. // 应用侧控制显示悬浮球,用户点击后显示联系人列表
  102. // boolean ret = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SHOW);
  103. Log.d(TAG, "ret: " + ret);
  104. }
  105. } else {
  106. mIsSendShowFail = true;
  107. Log.e(TAG, "sendShow fail.");
  108. }
  109. }
  110. public boolean sendHide() {
  111. Log.d(TAG, "sendHide.");
  112. if (mHwCaasHandler != null) {
  113. // 隐藏联系人列表与 HwCaasUtils.SHOW_CONTACTS 配对使用
  114. boolean ret = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.HIDE_CONTACTS);
  115. // 隐藏悬浮球与 HwCaasUtils.SHOW 配对使用
  116. // boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.HIDE);
  117. Log.d(TAG, "ret: " + ret);
  118. return ret;
  119. }
  120. Log.e(TAG, "sendHide fail.");
  121. return false;
  122. }
  123. public void pauseShare() {
  124. Log.d(TAG, "pauseShare.");
  125. if (mHwCaasHandler != null) {
  126. boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SCREEN_SHARING_PAUSE);
  127. Log.d(TAG, "isSendoK: " + isSendoK);
  128. }
  129. }
  130. public void resumeShare() {
  131. Log.d(TAG, "resumeShare.");
  132. if (mHwCaasHandler != null) {
  133. boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.SCREEN_SHARING_RESUME);
  134. Log.d(TAG, "isSendoK: " + isSendoK);
  135. }
  136. }
  137. public void caasKitRelease() {
  138. Log.d(TAG, "caasKitRelease.");
  139. if (mIsCaasKitInit) {
  140. if (mHwCaasServiceManager != null) {
  141. // source release.
  142. mHwCaasServiceManager.release();
  143. mHwCaasServiceManager = null;
  144. }
  145. mIsCaasKitInit = false;
  146. }
  147. }
  148. }

CaasKitApplication.java

收起
自动换行
深色代码主题
复制
  1. /*
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
  3. */
  4. package com.huawei.caaskitdemo;
  5. import android.app.Application;
  6. import android.content.Context;
  7. import android.util.Log;
  8. /**
  9. * Main application.
  10. *
  11. * @since 2019-10-21
  12. */
  13. public class CaasKitApplication extends Application {
  14. private static final String TAG = "CaasKitApplication";
  15. private static CaasKitApplication sApplication;
  16. @Override
  17. public void onCreate() {
  18. Log.i(TAG, "onCreate.");
  19. super.onCreate();
  20. setApp(this);
  21. }
  22. public static CaasKitApplication getInstance() {
  23. return sApplication;
  24. }
  25. public static Context getContext() {
  26. return sApplication.getApplicationContext();
  27. }
  28. private static synchronized void setApp(CaasKitApplication app) {
  29. sApplication = app;
  30. }
  31. }

CaasKitDemoActivity.java

收起
自动换行
深色代码主题
复制
  1. /*
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
  3. */
  4. package com.huawei.caaskitdemo;
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.util.Log;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.view.WindowManager;
  12. import android.view.inputmethod.InputMethodManager;
  13. import android.widget.Button;
  14. import android.widget.ImageView;
  15. import android.widget.TextView;
  16. import com.huawei.caaskitdemo.caaskit.CaasKitHelper;
  17. public class CaasKitDemoActivity extends AppCompatActivity {
  18. private static final String TAG = "CaaSKitDemoActivity";
  19. private Button mCallShow;
  20. private Button mCallHide;
  21. private Button mSharePause;
  22. private Button mShareResume;
  23. private TextView mPasswordView;
  24. private ImageView mBackgroundView;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  29. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  30. setContentView(R.layout.activity_caaskitdemo);
  31. CaasKitHelper.getInstance().caasKitInit();
  32. mCallShow = findViewById(R.id.call_show);
  33. mCallHide = findViewById(R.id.call_hide);
  34. mCallShow.setOnClickListener(v ->{
  35. CaasKitHelper.getInstance().sendShow();
  36. });
  37. mCallHide.setOnClickListener(v ->{
  38. CaasKitHelper.getInstance().sendHide();
  39. });
  40. mSharePause = findViewById(R.id.share_pause);
  41. mShareResume = findViewById(R.id.share_resume);
  42. mSharePause.setOnClickListener(v ->{
  43. CaasKitHelper.getInstance().pauseShare();
  44. });
  45. mShareResume.setOnClickListener(v ->{
  46. CaasKitHelper.getInstance().resumeShare();
  47. });
  48. mPasswordView = findViewById(R.id.paasword_view);
  49. mPasswordView.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
  50. @Override
  51. public void onFocusChange(View v, boolean hasFocus) {
  52. Log.d(TAG, "onFocusChange:" + hasFocus);
  53. if (hasFocus) {
  54. CaasKitHelper.getInstance().pauseShare();
  55. } else {
  56. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  57. imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
  58. CaasKitHelper.getInstance().resumeShare();
  59. }
  60. }
  61. });
  62. mBackgroundView = findViewById(R.id.back_ground);
  63. mBackgroundView.setOnTouchListener(new View.OnTouchListener() {
  64. @Override
  65. public boolean onTouch(View var1, MotionEvent var2) {
  66. if (mPasswordView != null) {
  67. mPasswordView.clearFocus();
  68. }
  69. return true;
  70. }
  71. });
  72. }
  73. @Override
  74. protected void onPause() {
  75. Log.d(TAG, "onPause.");
  76. CaasKitHelper.getInstance().pauseShare();
  77. super.onPause();
  78. }
  79. @Override
  80. protected void onStop() {
  81. super.onStop();
  82. Log.d(TAG, "onStop.");
  83. }
  84. @Override
  85. protected void onResume() {
  86. super.onResume();
  87. Log.d(TAG, "onResume.");
  88. CaasKitHelper.getInstance().resumeShare();
  89. }
  90. @Override
  91. protected void onDestroy() {
  92. super.onDestroy();
  93. Log.d(TAG, "onDestroy.");
  94. CaasKitHelper.getInstance().caasKitRelease();
  95. }
  96. }
在 示例代码 中进行搜索
请输入您想要搜索的关键词