[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723083000.56003083649349801145601178864497:50510806024746:2800:3479789A3AF7FAA85C7E8C6C65B199CF35C5EBF7BD5FDA087415EBD1C4625D98.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723083000.56003083649349801145601178864497:50510806024746:2800:3479789A3AF7FAA85C7E8C6C65B199CF35C5EBF7BD5FDA087415EBD1C4625D98.jpg[/IMG]​

In this tutorial, I will show you how to develop an Earthquake application with Android Studio using retrofit and Huawei mobile services (HMS Core). We will use the Huawei account kit and map kit.

With this application, users will be able to see earthquakes that happened in Turkey inside huawei map. Also they will be authenticated with their huawei ID.

1. Integrate Your Application

To start developing an app with Huawei mobile services, you need to integrate your application to HMS core. Check out this link to integrate your application. After finished your integration don’t forget to enable the account kit and map kit from App Gallery Connect.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723083620.48825477919347902468279291643873:50510806024746:2800:1D40CFE220C88BCA8F12D7C39CE7E8D5001E9E5EA94B572968369B326914FE19.jpeg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723083620.48825477919347902468279291643873:50510806024746:2800:1D40CFE220C88BCA8F12D7C39CE7E8D5001E9E5EA94B572968369B326914FE19.jpeg[/IMG]​

Note: Before we start if you don't have a Huawei device for testing. Please check this link to install the HMS Core toolkit. With this plug-in, you can use Huawei emulators for testing. (After download, select HMS and Cloud Debugging from top of the android studio)

2. Dependencies

Let’s start by adding our dependencies to the project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723083802.47587481973183449092724512607645:50510806024746:2800:8D7F20E7EA819BF29E184E1CD0E5188EB209E32345149011BAB7820228626AC9.jpeg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723083802.47587481973183449092724512607645:50510806024746:2800:8D7F20E7EA819BF29E184E1CD0E5188EB209E32345149011BAB7820228626AC9.jpeg[/IMG]​

3.Preparations for API

Finally, we are ready to start coding. First, start with our API structure.

Note: This endpoint used for the earthquakes that happened in Turkey. If you want to use a global API for your project this link might help you.

For model classes, check out my GitHub link. You will see 2 classes named TR_ApiModel and TR_APiModel_Result. (if you are using the global API, classes named ApiModel is your model classes.)

4. API Call with Retrofit

After we created our model classes successfully, let’s create our interface and make our first API call.

public interface ApiInterface {

String URL_GET="https://api.orhanaydogdu.com.tr/deprem/";

//latest 100 earthquakes
@GET("live.php?limit=100")
Call<TR_ApiModel> getDynamicTr();

//give url as parameter
@GET
Call<ApiModel> getDynamic(@Url String url);

}

Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(ApiInterface.URL_GET)
.client( new OkHttpClient())
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
ApiInterface userClient = retrofit.create(ApiInterface.class);

Call<TR_ApiModel> call = userClient.getDynamicTr();

call.enqueue(new Callback<TR_ApiModel>() {
@Override
public void onResponse(Call<TR_ApiModel> call, Response<TR_ApiModel> response) {

TR_ApiModel myTrData = response.body();

if (myTrData != null) {
// Send this data to your mapActivity
}

else {
Toast.makeText(YourActivity.this, "data null", Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Call<TR_ApiModel> call, Throwable t) {
Toast.makeText(YourActivity.this, t.toString(), Toast.LENGTH_LONG).show();
}
});

5. Huawei Account Kit

Now we are getting our earthquake data without any problem. Let’s continue with integrating kits into our application. We will start with the Account kit.

There are 2 methods to authenticate with Huawei ID. We will be using the ID token method because this method doesn't require an App server and has simpler integration. If you want to learn more about these methods check this page.

Let’s see the code for authentication with Huawei ID.

public class AccountActivity extends AppCompatActivity {

private HuaweiIdAuthParams authParams;
private HuaweiIdAuthService service;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wellcome);

authParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken().createParams();
service= HuaweiIdAuthManager.getService(AccountActivity.this, authParams);
Task<AuthHuaweiId> task = service.silentSignIn();
task.addOnSuccessListener(new OnSuccessListener<AuthHuaweiId>() {

@Override
public void onSuccess(AuthHuaweiId authHuaweiId) {

String uri = authHuaweiId.getAvatarUriString();
String name = authHuaweiId.getGivenName();
String familyName = authHuaweiId.getFamilyName(

Intent intent = new Intent (YourActivity.this, NextActivity.class);
intent.putExtra("uri",uri);
intent.putExtra("name",name);
intent.putExtra("familyname",familyName

startActivity(intent);
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
startActivityForResult(service.getSignInIntent(), 1111);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1111) {

Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);

if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
String uri = huaweiAccount.getAvatarUriString();
String name = huaweiAccount.getGivenName();
String familyName = huaweiAccount.getFamilyName();

Intent intent = new Intent (YourClass.this, YourActivity.class);

intent.putExtra("uri",uri);
intent.putExtra("name",name);
intent.putExtra("familyname",familyName);
startActivity(intent);
}
}
}

The code above has 2 parts. First, it tries to silent sign in. (if the user signed in before and not signed out, you can directly get user’s data and change your activity) If this task fails the second part of the code (onActivityResult) gets sign-in intent for you and the user enters account information to intent. If the task is successful the same as the first part you get users' information and change your activity.

Note: If you want to get the user's information as I did, be careful about getting this information because the user might not have a name, family name, or profile photo value associated with their account. In this situation, these values will be null.

Task<Void> signOutTask = service.signOut();
signOutTask.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
//Processing after the sign-out.
Log.i(TAG, "signOut complete");
}
});

To sign-out, you need to create a sign-out task like this. Service is the instance of HuaweiIdAuthService.

6. Huawei Map Kit

The final part is the map kit integration. In this part, I will use the data we get from API calls and add custom markers inside the Huawei map.

<com.huawei.hms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:mapType="normal"
map:uiCompass="true"
map:uiZoomControls="true"
map:cameraTargetLat="39.3799497"
map:cameraTargetLng="34.0919603"
map:cameraZoom="4.8" />

This is our XML file for map view just add this code to your layout file. You can also use Map fragment check this link for more information.

TargetLat , TargetLng, and camera zoom values set the starting location and starting zoom of your map.

public class MapFragment extends Fragment implements OnMapReadyCallback {

private HomeViewModel homeViewModel;
HuaweiMap hMap;
MapView mMapView;
TR_ApiModel myTrData;
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);

View root = inflater.inflate(R.layout.fragment_home, container, false);

mMapView = root.findViewById(R.id.mapp);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}

mMapView.onCreate(mapViewBundle);
mMapView.getMapAsync(this);
return root;
}

@Override
public void onStart() {
super.onStart();
mMapView.onStart();
}
@Override
public void onStop() {
super.onStop();
mMapView.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
public void onMapReady(HuaweiMap huaweiMap) {

hMap = huaweiMap;
Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(),
R.drawable.icons8y);

List<TR_ApiModel_Result> markerData = myTrData.getResult();

for (TR_ApiModel_Result result : markerData) {
hMap.addMarker(new MarkerOptions().position(new LatLng(result.getLat(), result.getLng())).title(result.getTitle() + " Magnitude : " + result.getMag().toString()).draggable(false)
.icon(BitmapDescriptorFactory.fromBitmap(icon)).clusterable(true));
}
}
}

This is our MapActivity. I used fragment to show the map but if you don't use fragment just add inside onCreate method instead of onCreateView.

The important part is onMapReady method. This method starts after map creation finished. First I created an icon for my markers. Then I added markers for the data I get from CallExample.java. Also, I used clusterable value true to avoid showing too many markers on my map.

If you want to update your map you can call “hmap.clear()” and add markers again with the updated data.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723090625.04770538173569475517327668624042:50510806024746:2800:A9D3264C425B8A86F5469141EDCA8BE642BA1114A0202BD6F74F0BDDE15E3779.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/189/578/5190090000026189578.20200723090625.04770538173569475517327668624042:50510806024746:2800:A9D3264C425B8A86F5469141EDCA8BE642BA1114A0202BD6F74F0BDDE15E3779.jpg[/IMG]​