更新時間:2019-11-20
/** * The type Login activity. */ public class LoginActivity extends Activity implements View.OnClickListener { private static final String REGISTER_SERVER = ""; private static final String PORT = ""; private static final String ACCOUNT = ""; private static final String PASSWORD = ""; private String regServer = ""; // register server address private String serverPort = ""; // server port private String sipNumber = ""; // sip number private String password = ""; // password private EditText regServerEditText; private EditText serverPortEditText; private EditText usernameEditText; private EditText passwordEditText; private Button loginButton; private IntentFilter filter; private TsdkManager tsdkManager; private String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}; private static final int REQUEST_PERMISSION_CODE = 1; // Request status code private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (TupLoginService.TUP_REGISTER_EVENT.equals(action)) { int result = intent.getIntExtra(TupLoginService.REGISTER_RESULT, -1); switch (result) { case 0: Toast.makeText(LoginActivity.this, getString(R.string.login_success), Toast.LENGTH_LONG).show(); break; default: break; } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); initFilter(); } private void initView() { regServerEditText = (EditText) findViewById(R.id.et_register_server_address); serverPortEditText = (EditText) findViewById(R.id.et_server_port); usernameEditText = (EditText) findViewById(R.id.et_username); passwordEditText = (EditText) findViewById(R.id.et_password); regServerEditText.setText(REGISTER_SERVER); usernameEditText.setText(ACCOUNT); passwordEditText.setText(PASSWORD); serverPortEditText.setText(PORT); loginButton = (Button) findViewById(R.id.btn_login); loginButton.setOnClickListener(this); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { initPermission(); } else { startService(); } } private void startService() { String appPath = getApplicationInfo().dataDir + "/lib"; startService(this, appPath); } private void initPermission() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSION_CODE); } else { startService(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_PERMISSION_CODE: if (0 == grantResults[0]) { startService(); } else { Toast.makeText(LoginActivity.this, getString(R.string.set_permission_write), Toast.LENGTH_SHORT).show(); } break; default: break; } } private void initFilter() { filter = new IntentFilter(); filter.addAction(TupLoginService.TUP_REGISTER_EVENT); registerReceiver(receiver, filter); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_login: login(); break; default: break; } } private void login() { regServer = regServerEditText.getText().toString().trim(); sipNumber = usernameEditText.getText().toString().trim(); password = passwordEditText.getText().toString().trim(); serverPort = serverPortEditText.getText().toString().trim(); if (TextUtils.isEmpty(sipNumber) || TextUtils.isEmpty(password) || TextUtils.isEmpty(regServer) || TextUtils.isEmpty(serverPort)) { Toast.makeText(LoginActivity.this, getString(R.string.account_information_not_empty), Toast.LENGTH_SHORT).show(); return; } TupLoginService.getInstance().authorizeLogin(regServer, serverPort, sipNumber, password); } /** * Init int. * @param context the context * @param appPath the app path * @return the result */ public boolean startService(Context context, String appPath) { int ret; tsdkManager = TsdkManager.getInstance(context, appPath, ServiceNotify.getInstance()); /* Step 1, set log param */ TsdkLogParam logParam = new TsdkLogParam(); logParam.setFileCount(1); logParam.setLevel(TsdkLogLevel.TSDK_E_LOG_DEBUG); logParam.setMaxSizeKb(1024 * 4); logParam.setPath(Environment.getExternalStorageDirectory() + File.separator + "HelloWorld" + "/"); ret = tsdkManager.setConfigParam(logParam); if (ret != 0) { return false; } /* Step 2, init sdk */ TsdkAppInfoParam appInfoParam = new TsdkAppInfoParam(); appInfoParam.setClientType(TSDK_E_CLIENT_MOBILE); appInfoParam.setProductName("eSDK-Mobile"); appInfoParam.setDeviceSn("123"); appInfoParam.setSupportAudioAndVideoCall(1); appInfoParam.setSupportAudioAndVideoConf(0); appInfoParam.setSupportDataConf(0); appInfoParam.setSupportCtd(0); appInfoParam.setSupportEnterpriseAddressBook(0); appInfoParam.setSupportIm(0); appInfoParam.setSupportRichMediaMessage(0); ret = tsdkManager.init(appInfoParam); if (ret != 0) { return false; } return true; } @Override protected void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); } } |
/** * The type EC application. */ public class ECApplication extends Application { private static Application app; /** * Gets app. * * @return the app */ public static Application getApp() { return app; } @Override public void onCreate() { super.onCreate(); app = this; } @Override public void onTerminate() { super.onTerminate(); TsdkManager.getInstance().uninit(); } } |
/** * The type login mgr. */ public class TupLoginService { /** * The constant TUP_REGISTER_EVENT. */ public static final String TUP_REGISTER_EVENT = "tup_register_event"; /** * The constant REGISTER_RESULT. */ public static final String REGISTER_RESULT = "register_result"; private static TupLoginService tupLoginService; private TupLoginService() { } /** * Get instance. * * @return the instance. */ public static synchronized TupLoginService getInstance() { if (tupLoginService == null) { tupLoginService = new TupLoginService(); } return tupLoginService; } /** * Authorize login boolean. * * @param regServer the reg server * @param serverPort the server port * @param sipNumber the sip number * @param password the password * @return the boolean */ public boolean authorizeLogin(String regServer, String serverPort, String sipNumber, String password) { int ret; TsdkLoginParam tsdkLoginParam = new TsdkLoginParam(); tsdkLoginParam.setUserId(1); tsdkLoginParam.setAuthType(TsdkAuthType.TSDK_E_AUTH_NORMAL); tsdkLoginParam.setUserName(sipNumber); tsdkLoginParam.setPassword(password); tsdkLoginParam.setServerAddr(regServer); tsdkLoginParam.setServerPort(Integer.parseInt(serverPort)); tsdkLoginParam.setServerVersion(""); tsdkLoginParam.setServerType(TsdkServerType.TSDK_E_SERVER_TYPE_PORTAL); tsdkLoginParam.setUserTicket(""); ret = TsdkManager.getInstance().getLoginManager().login(tsdkLoginParam); if (ret != 0) { return false; } return true; } /** * Login un init int. * * @return the int */ public int logout() { int ret = TsdkManager.getInstance().getLoginManager().logout(); if (ret != 0) { Log.e("logout", "login is failed, return " + ret); } return ret; } public void handleAuthSuccess() { Log.d("TupLoginService", "onEvtAuthSuccess."); Intent intent = new Intent(); intent.setAction(TUP_REGISTER_EVENT); intent.putExtra(REGISTER_RESULT, 0); ECApplication.getApp().sendBroadcast(intent); } } |
/** * The type service notify. */ public class ServiceNotify implements TsdkNotify { public static ServiceNotify instance; public static ServiceNotify getInstance() { if (null == instance) { instance = new ServiceNotify(); } return instance; } @Override public void onEvtAuthSuccess(int i, TsdkImLoginParam tsdkImLoginParam) { TupLoginService.getInstance().handleAuthSuccess(); } @Override public void onEvtAuthFailed(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtAuthRefreshFailed(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtLoginSuccess(int i, TsdkServiceAccountType tsdkServiceAccountType, TsdkLoginSuccessInfo tsdkLoginSuccessInfo) { } @Override public void onEvtLoginFailed(int i, TsdkServiceAccountType tsdkServiceAccountType, TsdkLoginFailedInfo tsdkLoginFailedInfo) { } @Override public void onEvtLogoutSuccess(int i, TsdkServiceAccountType tsdkServiceAccountType) { } @Override public void onEvtLogoutFailed(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtForceLogout(int i, TsdkServiceAccountType tsdkServiceAccountType, TsdkForceLogoutInfo tsdkForceLogoutInfo) { } @Override public void onEvtVoipAccountStatus(int i, TsdkVoipAccountInfo tsdkVoipAccountInfo) { } @Override public void onEvtImAccountStatus(int i) { } @Override public void onEvtFirewallDetectFailed(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtBuildStgTunnelFailed(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtSecurityTunnelInfoInd(int i, int i1, TsdkSecurityTunnelInfo tsdkSecurityTunnelInfo) { } @Override public void onEvtGetTempUserResult(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtModifyPasswordResult(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtCallStartResult(TsdkCall tsdkCall, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtCallIncoming(TsdkCall tsdkCall, Boolean aBoolean) { } @Override public void onEvtCallOutgoing(TsdkCall tsdkCall) { } @Override public void onEvtCallRingback(TsdkCall tsdkCall) { } @Override public void onEvtCallRtpCreated(TsdkCall tsdkCall) { } @Override public void onEvtCallConnected(TsdkCall tsdkCall) { } @Override public void onEvtCallEnded(TsdkCall tsdkCall) { } @Override public void onEvtCallDestroy(TsdkCall tsdkCall) { } @Override public void onEvtOpenVideoReq(TsdkCall tsdkCall, TsdkVideoOrientation tsdkVideoOrientation) { } @Override public void onEvtRefuseOpenVideoInd(TsdkCall tsdkCall) { } @Override public void onEvtCloseVideoInd(TsdkCall tsdkCall) { } @Override public void onEvtOpenVideoInd(TsdkCall tsdkCall) { } @Override public void onEvtRefreshViewInd(TsdkCall tsdkCall, TsdkVideoViewRefresh tsdkVideoViewRefresh) { } @Override public void onEvtCallRouteChange(TsdkCall tsdkCall, int i) { } @Override public void onEvtPlayMediaEnd(int i) { } @Override public void onEvtSessionModified(TsdkCall tsdkCall, TsdkSessionModified tsdkSessionModified) { } @Override public void onEvtSessionCodec(TsdkCall tsdkCall, TsdkSessionCodec tsdkSessionCodec) { } @Override public void onEvtHoldSuccess(TsdkCall tsdkCall) { } @Override public void onEvtHoldFailed(TsdkCall tsdkCall) { } @Override public void onEvtUnholdSuccess(TsdkCall tsdkCall) { } @Override public void onEvtUnholdFailed(TsdkCall tsdkCall) { } @Override public void onEvtEndcallFailed(TsdkCall tsdkCall, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtDivertFailed(TsdkCall tsdkCall) { } @Override public void onEvtBldTransferSuccess(TsdkCall tsdkCall) { } @Override public void onEvtBldTransferFailed(TsdkCall tsdkCall) { } @Override public void onEvtSetIptServiceResult(int i, TsdkSetIptServiceResult tsdkSetIptServiceResult) { } @Override public void onEvtIptServiceInfo(TsdkIptServiceInfoSet tsdkIptServiceInfoSet) { } @Override public void onEvtBookConfResult(TsdkCommonResult tsdkCommonResult, TsdkConfBaseInfo tsdkConfBaseInfo) { } @Override public void onEvtQueryConfListResult(TsdkCommonResult tsdkCommonResult, TsdkConfListInfo tsdkConfListInfo) { } @Override public void onEvtQueryConfDetailResult(TsdkCommonResult tsdkCommonResult, TsdkConfDetailInfo tsdkConfDetailInfo) { } @Override public void onEvtJoinConfResult(TsdkConference tsdkConference, TsdkCommonResult tsdkCommonResult, TsdkJoinConfIndInfo tsdkJoinConfIndInfo) { } @Override public void onEvtGetDataconfParamResult(TsdkConference tsdkConference, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtConfctrlOperationResult(TsdkConference tsdkConference, TsdkConfOperationResult tsdkConfOperationResult) { } @Override public void onEvtInfoAndStatusUpdate(TsdkConference tsdkConference) { } @Override public void onEvtSpeakerInd(TsdkConference tsdkConference, TsdkConfSpeakerInfo tsdkConfSpeakerInfo) { } @Override public void onEvtRequestConfRightFailed(TsdkConference tsdkConference, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtConfIncomingInd(TsdkConference tsdkConference) { } @Override public void onEvtConfEndInd(TsdkConference tsdkConference) { } @Override public void onEvtJoinDataConfResult(TsdkConference tsdkConference, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtAsStateChange(TsdkConference tsdkConference, TsdkConfAppShareType tsdkConfAppShareType, TsdkConfAsStateInfo tsdkConfAsStateInfo) { } @Override public void onEvtAsOwnerChange(TsdkConference tsdkConference, TsdkConfAsActionType tsdkConfAsActionType, TsdkAttendee tsdkAttendee) { } @Override public void onEvtDsDocNew(TsdkConference tsdkConference, TsdkDocBaseInfo tsdkDocBaseInfo) { } @Override public void onEvtDsDocDel(TsdkConference tsdkConference, TsdkDocShareDelDocInfo tsdkDocShareDelDocInfo) { } @Override public void onEvtWbDocNew(TsdkConference tsdkConference, TsdkDocBaseInfo tsdkDocBaseInfo) { } @Override public void onEvtWbDocDel(TsdkConference tsdkConference, TsdkWbDelDocInfo tsdkWbDelDocInfo) { } @Override public void onEvtRecvChatMsg(TsdkConference tsdkConference, TsdkConfChatMsgInfo tsdkConfChatMsgInfo) { } @Override public void onEvtTransToConfResult(TsdkCall tsdkCall, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtCtdStartCallResult(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtCtdEndCallResult(int i, TsdkCommonResult tsdkCommonResult) { } @Override public void onEvtCtdCallStatusNotify(int i, TsdkCtdCallStatus tsdkCtdCallStatus) { } @Override public void onEvtSearchContactsResult(int i, TsdkCommonResult tsdkCommonResult, TsdkSearchContactsResult tsdkSearchContactsResult) { } @Override public void onEvtSearchDeptResult(int i, TsdkCommonResult tsdkCommonResult, TsdkSearchDepartmentResult tsdkSearchDepartmentResult) { } @Override public void onEvtGetIconResult(int i, TsdkCommonResult tsdkCommonResult, TsdkGetIconResult tsdkGetIconResult) { } @Override public void onEvtAddFriendInd(TsdkBeAddedFriendInfo tsdkBeAddedFriendInfo) { } @Override public void onEvtUserStatusUpdate(List<TsdkImUserStatusUpdateInfo> list) { } @Override public void onEvtUserInfoUpdate(List<TsdkImUserInfo> list) { } @Override public void onEvtJoinChatGroupReq(TsdkChatGroup tsdkChatGroup, TsdkReqJoinChatGroupMsg tsdkReqJoinChatGroupMsg) { } @Override public void onEvtJoinChatGroupRsp(TsdkChatGroup tsdkChatGroup, TsdkRspJoinChatGroupMsg tsdkRspJoinChatGroupMsg) { } @Override public void onEvtJoinChatGroupInd(TsdkChatGroup tsdkChatGroup, TsdkBeAddedToChatGroupInfo tsdkBeAddedToChatGroupInfo) { } @Override public void onEvtDelChatGroupMemberResult(TsdkChatGroup tsdkChatGroup, TsdkDelChatGroupMemberResult tsdkDelChatGroupMemberResult) { } @Override public void onEvtLeaveChatGroupResult(TsdkChatGroup tsdkChatGroup, TsdkLeaveChatGroupResult tsdkLeaveChatGroupResult) { } @Override public void onEvtChatGroupInfoUpdate(TsdkChatGroup tsdkChatGroup, TsdkChatGroupUpdateInfo tsdkChatGroupUpdateInfo, TsdkChatGroupInfoUpdateType tsdkChatGroupInfoUpdateType) { } @Override public void onEvtInputtingStatusInd(TsdkInputtingStatusInfo tsdkInputtingStatusInfo) { } @Override public void onEvtChatMsg(TsdkChatMsgInfo tsdkChatMsgInfo) { } @Override public void onEvtBatchChatMsg(TsdkBatchChatMsgInfo tsdkBatchChatMsgInfo) { } @Override public void onEvtSystemBulletin(TsdkChatMsgInfo tsdkChatMsgInfo) { } @Override public void onEvtSms(TsdkSmsInfo tsdkSmsInfo) { } @Override public void onEvtUndeliverInd(TsdkChatMsgUndeliverInfo tsdkChatMsgUndeliverInfo) { } @Override public void onEvtMsgReadInd(TsdkMsgReadIndInfo tsdkMsgReadIndInfo) { } @Override public void onEvtMsgSendResult(TsdkSendChatMsgResult tsdkSendChatMsgResult) { } @Override public void onEvtMsgWithdrawResult(TsdkChatMsgWithdrawResult tsdkChatMsgWithdrawResult) { } @Override public void onEvtMsgWithdrawInd(TsdkChatMsgWithdrawInfo tsdkChatMsgWithdrawInfo) { } } |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <RelativeLayout android:layout_width="300dp" android:layout_height="wrap_content"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/llTextContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/et_register_server_address" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:hint="@string/register_server" android:inputType="textVisiblePassword" android:singleLine="true" android:textSize="13sp" /> <EditText android:id="@+id/et_server_port" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:hint="@string/server_port" android:inputType="textVisiblePassword" android:singleLine="true" android:textSize="13sp" /> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:hint="@string/account" android:inputType="textVisiblePassword" android:singleLine="true" android:textSize="13sp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:hint="@string/password" android:inputType="textPassword" android:singleLine="true" android:textSize="13sp" > </EditText> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="43dp" android:layout_marginTop="10dp" android:text="@string/login" android:textSize="15sp" /> </LinearLayout> </ScrollView> </RelativeLayout> </LinearLayout> |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld"> <!-- uses permission --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <application android:name="com.hauwei.esdk.tup.helloworld.application.ECApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.hauwei.esdk.tup.helloworld.ui.LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
<resources> <string name="app_name">HelloWrold</string> <string name="register_server">RegServer</string> <string name="server_port">ServerPort</string> <string name="account">Account</string> <string name="password">Password</string> <string name="login">Login</string> <string name="login_success">Login success</string> <string name="account_information_not_empty">Please enter your login parameters</string> <string name="set_permission_write">Please enable storage permission in the settings, otherwise the function will not be available.</string> </resources> |
apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.1" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } defaultConfig { applicationId "com.huawei.esdk.tup.helloworld" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { assets.srcDirs = ['assets', 'src/main/assets', 'src/main/assets/'] jniLibs.srcDirs = ['libs'] } instrumentTest.setRoot('tests') debug.setRoot('build-types/debug') release.setRoot('build-types/release') } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.google.code.gson:gson:2.3.1' implementation 'androidx.core:core:1.0.2' }