package com.st.yd.wf.hms_integrationdemo0513;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.location.FusedLocationProviderClient;
import com.huawei.hms.location.LocationCallback;
import com.huawei.hms.location.LocationRequest;
import com.huawei.hms.location.LocationResult;
import com.huawei.hms.location.LocationServices;
import com.huawei.hms.mlsdk.MLAnalyzerFactory;
import com.huawei.hms.mlsdk.common.MLFrame;
import com.huawei.hms.mlsdk.text.MLLocalTextSetting;
import com.huawei.hms.mlsdk.text.MLText;
import com.huawei.hms.mlsdk.text.MLTextAnalyzer;
import com.huawei.hms.support.account.AccountAuthManager;
import com.huawei.hms.support.account.request.AccountAuthParams;
import com.huawei.hms.support.account.request.AccountAuthParamsHelper;
import com.huawei.hms.support.account.result.AuthAccount;
import com.huawei.hms.support.account.service.AccountAuthService;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnAuthCode;
private Button btnIdToken;
private Button btnSilent;
private Button btnSignOut;
private Button btnCancelAuth;
private Button btnRequestLocation;
private Button btnRemoveLocation;
private Button btnLocalText;
private ImageView imageView;
private AccountAuthService authService;
private LocationCallback locationCallback;
private FusedLocationProviderClient fusedLocationProviderClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Context context = MainActivity.this;
MyHmsMessageService.getToken(context);
}
private void initView(){
btnAuthCode = findViewById(R.id.btn_authcode);
btnIdToken = findViewById(R.id.btn_idtoken);
btnSilent = findViewById(R.id.btn_silent);
btnSignOut = findViewById(R.id.btn_signout);
btnCancelAuth = findViewById(R.id.btn_cancelauth);
btnRequestLocation = findViewById(R.id.btn_requestLocation);
btnRemoveLocation = findViewById(R.id.btn_removeLocation);
btnLocalText = findViewById(R.id.btn_localText);
imageView = findViewById(R.id.imageView);
btnAuthCode.setOnClickListener(this);
btnIdToken.setOnClickListener(this);
btnSilent.setOnClickListener(this);
btnSignOut.setOnClickListener(this);
btnCancelAuth.setOnClickListener(this);
btnRequestLocation.setOnClickListener(this);
btnRemoveLocation.setOnClickListener(this);
btnLocalText.setOnClickListener(this);
authService = getAuthCodeSignInService();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
System.out.println("====== location:"+locationResult.getLocations().toString());
}
};
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_authcode:
authcodeSignIn(authService);
break;
case R.id.btn_idtoken:
idtokenSignIn();
break;
case R.id.btn_silent:
silentSignIn();
break;
case R.id.btn_signout:
authSignOut(authService);
break;
case R.id.btn_cancelauth:
cancelAuth(authService);
break;
case R.id.btn_requestLocation:
requestLocation();
break;
case R.id.btn_removeLocation:
removeLocation();
break;
case R.id.btn_localText:
analyseText();
break;
}
}
private void analyseText() {
MLLocalTextSetting mlLocalTextSetting = new MLLocalTextSetting.Factory()
.setOCRMode(MLLocalTextSetting.OCR_DETECT_MODE)
.setLanguage("zh")
.create();
MLTextAnalyzer localTextAnalyzer = MLAnalyzerFactory
.getInstance()
.getLocalTextAnalyzer();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.text);
imageView.setImageBitmap(bitmap);
MLFrame mlFrame = MLFrame.fromBitmap(bitmap);
// System.out.println(mlFrame.acquireGrayByteBuffer().toString());
Task<MLText> mlTextTask = localTextAnalyzer.asyncAnalyseFrame(mlFrame);
mlTextTask.addOnSuccessListener(new OnSuccessListener<MLText>() {
@Override
public void onSuccess(MLText mlText) {
System.out.println("============successful"+mlTextTask.getResult().getStringValue());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
System.out.println("============failure"+mlTextTask.getException().toString());
}
});
try {
localTextAnalyzer.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
private void removeLocation() {
Task<Void> task = fusedLocationProviderClient.removeLocationUpdates(locationCallback);
task.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
System.out.println("=======remove location successful");
}else {
System.out.println("=======remove location failure");
}
}
});
}
private void requestLocation() {
LocationRequest locationRequest = new LocationRequest()
.setInterval(3000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Task<Void> task = fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
task.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
System.out.println("============= request location successful");
}else {
System.out.println("============= request location failure");
}
}
});
}
private void cancelAuth(AccountAuthService authService) {
Task<Void> task = authService.cancelAuthorization();
task.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
System.out.println("============Cancel authorization successful");
}
});
}
private void authSignOut(AccountAuthService service) {
Task<Void> task = service.signOut();
task.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
System.out.println("======Sign out successful");
}
}
});
}
private AccountAuthService getAuthCodeSignInService() {
AccountAuthParams params = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode().createParams();
AccountAuthService service = AccountAuthManager.getService(this,params);
return service;
}
private void silentSignIn() {
AccountAuthParams params = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).createParams();
AccountAuthService service = AccountAuthManager.getService(this, params);
Task<AuthAccount> task = service.silentSignIn();
task.addOnCompleteListener(new OnCompleteListener<AuthAccount>() {
@Override
public void onComplete(Task<AuthAccount> task) {
if (task.isSuccessful()) {
System.out.println("=========="+task.getResult().toString());
}else {
System.out.println("=========="+task.getException().toString());
}
}
});
}
private void idtokenSignIn() {
AccountAuthParams params = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setIdToken().createParams();
AccountAuthService service = AccountAuthManager.getService(this, params);
startActivityForResult(service.getSignInIntent(),8888);
}
private void authcodeSignIn(AccountAuthService service ) {
startActivityForResult(service.getSignInIntent(),7777);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==7777){
Task<AuthAccount> task = AccountAuthManager.parseAuthResultFromIntent(data);
task.addOnCompleteListener(new OnCompleteListener<AuthAccount>() {
@Override
public void onComplete(Task<AuthAccount> task) {
if (task.isSuccessful()) {
System.out.println("=========="+task.getResult().toString());
}else {
System.out.println("=========="+task.getException().toString());
}
}
});
}
if (requestCode==8888){
Task<AuthAccount> task = AccountAuthManager.parseAuthResultFromIntent(data);
task.addOnCompleteListener(new OnCompleteListener<AuthAccount>() {
@Override
public void onComplete(Task<AuthAccount> task) {
if (task.isSuccessful()) {
System.out.println("======"+task.getResult().toString());
}else {
System.out.println("======"+task.getException().toString());
}
}
});
}
}
}
package com.st.yd.wf.hms_integrationdemo0513;
import android.content.Context;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.push.HmsMessageService;
public class MyHmsMessageService extends HmsMessageService {
public static void getToken(Context context){
new Thread(){
@Override
public void run() {
try {
String app_id = "108279483";
String tokenScope = "HCM";
String token = HmsInstanceId.getInstance(context).getToken(app_id, tokenScope);
System.out.println("======token:"+token);
} catch (ApiException e) {
e.printStackTrace();
}
}
}.start();
}
}