Overview

CaaS Engine provides open APIs that are based on the HUAWEI MeeTime service, helping you to incorporate voice and video calling into your apps' social framework (such as contacts and friends) with ease.

What You Will Create

In this codelab, you will create a demo project, integrate the CaaS Engine SDK into the demo project, and set up the complete CaaS Engine service process.

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

CaaS Engine integration requires the following preparations:

For details, see App Development

1. Integrate the CaaS Engine SDK.

Configure the repository in the build.gradle file of the project.

buildscript { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } } allprojects { repositories { maven {url 'https://developer.huawei.com/repo/'} google() jcenter() } }

Add the following dependencies to the build.gradle file in the app directory of the project.

repositories { flatDir { dirs 'libs' } } dependencies { implementation group: 'com.huawei.caaskit', name: 'caaskitlite', version: '1.0.1.400', ext: 'aar' }

Click the sync button, as shown in the following figure.

After the synchronization, the following screen is displayed.

2. Add the app ID.

Add the app ID generated when creating the app on HUAWEI Developers to the AndroidManifest.xml file of the app. The name is com.huawei.hms.client.appid, and the value is appid=****, where ****** indicates the ID of your app.** For details about how to apply for the permission, see Configuring App Information in AppGallery Connect.

3. Initialize the service.

CaasKitHelper.java

public class CaasKitHelper { private static final String TAG = "CaasKitHelper"; private static com.huawei.caaskitdemo.caaskit.CaasKitHelper mCaasKitHelper; private HwCaasServiceManager mHwCaasServiceManager; private HwCaasHandler mHwCaasHandler; private Context mContext; private boolean mIsCaasKitInit; private CaasKitHelper() { mContext = CaasKitApplication.getContext(); } public static com.huawei.caaskitdemo.caaskit.CaasKitHelper getInstance() { if (mCaasKitHelper == null) { synchronized (com.huawei.caaskitdemo.caaskit.CaasKitHelper.class) { if (mCaasKitHelper == null) { mCaasKitHelper = new CaasKitHelper(); } } } return mCaasKitHelper; } public void caasKitInit(HwCaasServiceCallBack callback) { Log.d(TAG, "caasKitInit."); if (!mIsCaasKitInit) { /** Initialize mHwCaasServiceManager instance. */ mHwCaasServiceManager = HwCaasServiceManager.init(); /** Initialize HwCaasHandler instance through handlerType. */ mHwCaasServiceManager.initHandler(mContext, HwCaasUtils.NORMAL_CALL_TYPE, callback); mIsCaasKitInit = true; } } public void setHwCaasHandler(HwCaasHandler handler) { Log.d(TAG, "setHwCaasHandler."); mHwCaasHandler = handler; } public void queryHiCallAbility(String phoneNumber) { Log.d(TAG, "queryHiCallAbility."); /** The format for querying phoneNumber after Sha256 encryption is required must be a number with a country code Example: +861320750xxxx */ String phoneNumberSha256 = Sha256Util.encryptNumber(phoneNumber); if (mHwCaasHandler != null) { mHwCaasHandler.queryCallAbility(phoneNumberSha256, HwCaasUtils.CallAbilityType.NORMAL_CALL); } } public void setCallAbilityCallBack(HwCallAbilityCallBack callback) { Log.d(TAG, "setCallAbilityCallBack."); if (mHwCaasHandler != null) { mHwCaasHandler.setCallAbilityCallBack(callback); } } public void setMakeCallCallBack(HwMakeCallCallBack callback) { Log.d(TAG, "setMakeCallCallBack."); if (mHwCaasHandler != null) { mHwCaasHandler.setMakeCallCallBack(callback); } } }

Add the following code to where appropriate:
CaasKitActivity.java

public class CaasKitActivity extends AppCompatActivity { private static final String TAG = "CaasKitActivity"; private static final int PHONE_NOT_SUPPORT_HICALL = 3002; ... private HwCaasServiceCallBack mCallBack = new HwCaasServiceCallBack() { @Override public void initSuccess(HwCaasHandler handler) { Log.i(TAG, "HwCaasServiceCallBack:initSuccess."); /** Callback after successful initialization of HwCaasHandler. */ CaasKitHelper.getInstance().setHwCaasHandler(handler); CaasKitHelper.getInstance().setCallAbilityCallBack(mAbilityCallBack); CaasKitHelper.getInstance().setMakeCallCallBack(new HwMakeCallCallBack() { @Override public void makeCallResult(String number, int retCode) { Log.d(TAG, "number: " + number + " retCode: " + retCode); } }); } @Override public void initFail(int retCode) { /** Callback if init Handler fail. */ Log.i(TAG, "HwCaasServiceCallBack:initFail:retCode: " + retCode); ...//close the call entry toast("HwCaasServiceCallBack initFail"); } @Override public void releaseSuccess() { /** Callback after successful release of mHwCaasServiceManager. */ Log.i(TAG, "HwCaasServiceCallBack:releaseSuccess"); CaasKitHelper.getInstance().setHwCaasHandler(null); } }; private HwCallAbilityCallBack mAbilityCallBack = new HwCallAbilityCallBack() { @Override public void callAbilityResult(String phoneNumberSha256, int retCode) { Log.i(TAG, "HwCallAbilityCallBack:phoneNumberSha256:" + phoneNumberSha256 + " retCode:" + retCode); if (retCode == 0) { ...//Both audio and video calls are supported. The UI display is changed to display the audio and video call entry. } else if (retCode == 1) { ...//Only audio calls are supported. The UI display is changed, and only the audio call entry is displayed. } else if (retCode == PHONE_NOT_SUPPORT_HICALL) { ...//Audio and video calls are not supported. Close the call entrys. } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_caaskitquickcall); ... CaasKitHelper.getInstance().caasKitInit(mCallBack); } }

4. Query the calling capabilities.

CaasKitHelper.java

public class CaasKitHelper { ... public void queryHiCallAbility(String phoneNumber) { Log.d(TAG, "queryHiCallAbility."); /** The format for querying phoneNumber after Sha256 encryption is required must be a number with a country code Example: +861320750xxxx */ String phoneNumberSha256 = Sha256Util.encryptNumber(phoneNumber); if (mHwCaasHandler != null) { mHwCaasHandler.queryCallAbility(phoneNumberSha256, HwCaasUtils.CallAbilityType.NORMAL_CALL); } } ... }

Add the following code to where appropriate:
CaasKitActivity.java

public class CaasKitActivity extends AppCompatActivity { ... private Button mStartQuery; ... private void initData() { ... mStartQuery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CaasKitHelper.getInstance().queryHiCallAbility(mPhoneNumber.getText().toString()); } }); ... } }

5. Implement voice and video calls.

CaasKitHelper.java

public class CaasKitHelper { ... public int makeCall(String phoneNumber, HwCaasUtils.CallType callType) { Log.d(TAG, "makeCall."); if (mHwCaasHandler != null) { int retCode = mHwCaasHandler.makeCall(phoneNumber, callType); Log.d(TAG, "makeCall retCode: " + retCode); return retCode; } return -1; } ... }

Add the following code to where appropriate:
CaasKitActivity.java

public class CaasKitActivity extends AppCompatActivity { ... private Button mMakeVideoCall; private Button mMakeAudioCall; ... private void initData() { ... mMakeVideoCall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { makeCall(HwCaasUtils.CallType.VIDEO_CALL); } }); mMakeAudioCall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { makeCall(HwCaasUtils.CallType.VOICE_CALL); } }); ... } private void makeCall(HwCaasUtils.CallType callType) { int retCode = CaasKitHelper.getInstance().makeCall(mPhoneNumber.getText().toString(), callType); Log.d(TAG, "makeCall:retCode: " + retCode); } }

6. Release resources.

CaasKitHelper.java

public class CaasKitHelper { ... public void caasKitRelease() { if (mIsCaasKitInit) { if (mHwCaasServiceManager != null) { /** Source release */ mHwCaasServiceManager.release(); mHwCaasServiceManager = null; } mIsCaasKitInit = false; } } }

Add the following code to where appropriate:
CaasKitActivity.java

public class CaasKitActivity extends AppCompatActivity { ... @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy."); CaasKitHelper.getInstance().caasKitRelease(); } }

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following link:

Related documents

Download

Code copied