[Huawei Developers] Freemind R:
Introduction

Flight booking app allows user to search and book flight. In this article, we will integrate Huawei Cloud Storage into demo flight booking application.

Huawei storage is versatile and maintenance-free.This enables us to safely and economically store large quantities of data such as photos, audios and videos generated by users.

The service is stable, secure, efficient, and easy-to-use, and can free you from development, deployment, O&M, and capacity expansion of storage servers. Developers do not need to pay attention to indicators such as availability, reliability, and durability and can focus on service capability building and operations, improving user experience.

For prerequisite and set up, refer to part 1.

Usecase

1. Once user books the flight from the app, we will store flight ticket in Huawei cloud storage.

2. User can navigate to ItineraryActivity from navigation drawer to browse all the flight ticket booked by him in past.

Accessing Huawei Cloud Storage

Since Cloud Storage is still in Beta state, we need to send an application email to agconnect@huawei.com to apply for the service.Set your email title in the following format: [Cloud Storage]-[Company name]-[Developer account ID]-[Project ID]. For details about how to query the developer account ID and project ID, refer to Querying the Developer Account ID and Project ID.

Usually it takes 1 to 3 days for activation, once activated, developer can integrate cloud DB service in their app.

Getting started with Cloud Storage

1. Log in to AppGallery Connect and select My Projects.

2. Select Application for which you want integrate Cloud storage.

3. Enable Cloud storage settings in Manage APIs Section.

4. Choose Build > Cloud Storage. The Cloud Storage page is displayed. click Enable now.

5. On the page displayed, set Storage instance and Data storage location.Click Next.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030054328.74175922643331211667878235817289:50511111010448:2800:1C43644CA98F4D63E745EB73AAE28DC71F6C5D62910EA4750B9F2DFF3DD8946E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030054328.74175922643331211667878235817289:50511111010448:2800:1C43644CA98F4D63E745EB73AAE28DC71F6C5D62910EA4750B9F2DFF3DD8946E.png[/IMG]

6 . The Define security rules page is displayed by default.Default security rules allow all reads and writes of authenticated users. In this article we will use default security rules.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030054854.11043811841667518967129358285955:50511111010448:2800:B459CE53BAB8FCA8F8C84DB4B969569A8FA29A6393F517806C3A857BD80CA316.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030054854.11043811841667518967129358285955:50511111010448:2800:B459CE53BAB8FCA8F8C84DB4B969569A8FA29A6393F517806C3A857BD80CA316.png[/IMG]

7. Cloud Storage is successfully enabled for the project.

8. Make sure Auth Service in Enabled in AGC. Choose Build > Auth Service., click Enable now in the upper right corner. Enable Huawei Account in Authentication mode.

9. Open agconnect-services.json file and add storage-related content to the service tag.

"cloudstorage":{
"storage_url":"https://ops-dra.agcstorage.link",
"default_storage": "your default storage defined in AGC"
}

Since we have set Data storage location as Singapore, storage URL is https://ops-dra.agcstorage.linkn

If location is set as China, storage location will be https://agc-storage-drcn.platform.dbankcloud.cn

If Location is set as Germany, storage location will be https://ops-dre.agcstorage.link

The value of default_storage is the information entered in the storage instance box on the Project Setting > Build > Cloud Storage page.

10. Add below Permission in AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

11. Add Cloud storage dependencies in app-level build.gradle

implementation "com.huawei.agconnect:agconnect-storage:1.3.0.300"

12. If app is integrated with Android SDK 29 or later, we also need to add the following attributes under application in AndroidMainfest.xml

<application
android:name=".EaseMyFlightApplication"
android:allowBackup="false"
android:requestLegacyExternalStorage="true">

13. To initialize cloud storage

AGCStorageManagement storageManagement = AGCStorageManagement.getInstance();

14. In Flight booking app, we will upload the flight ticket (in pdf format) in cloud storage. Create a reference for the file to be uploaded and enter the file address planned on the cloud.

StorageReference reference = storageManagement.getStorageReference("Tickets/" + TicketName + “.pdf”);

15. To upload flight ticket

UploadTask task = reference.putFile(new File("ticket path")); task.addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception exception) {
Log.e(TAG, "Cloud storage file upload failed :" + exception.getMessage());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.UploadResult>(){
@Override
public void onSuccess(UploadTask.UploadResult uploadResult) {
Log.d(TAG, "Cloud storage file upload success");
}
});

16. To download flight booking ticket from cloud storage

StorageReference reference = storageManagement.getStorageReference("Tickets/"+ TicketName);

File file = new File(FileUtils.getAppPath(mContext) + TicketName+ ".pdf" );
if(file.exists()) {
file.delete();
}
DownloadTask task = reference.getFile(file);
task.addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception exception) {
exception.printStackTrace();
}
}).addOnSuccessListener(new OnSuccessListener<DownloadTask.DownloadResult>(){
@Override
public void onSuccess(DownloadTask.DownloadResult DownloadResult) {
Intent intent = new Intent(mContext, FlightTicketViewerActivity.class);
intent.putExtra("path", FileUtils.getAppPath(mContext) + fileName+ ".pdf");
mContext.startActivity(intent);
}
}).addOnProgressListener(new OnProgressListener<DownloadTask.DownloadResult>(){
@Override
public void onProgress(DownloadTask.DownloadResult DownloadResult) {
Log.d(TAG, String.format("progress: %f", (DownloadResult.getBytesTransferred()*1.0)/DownloadResult.getTotalByteCount()));
}
}).addOnPausedListener(new OnPausedListener<DownloadTask.DownloadResult>(){
@Override
public void onPaused(DownloadTask.DownloadResult DownloadResult) {
}
});

Screenshots

1. Uploaded flight ticket in AGC console

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030062141.42129480120552594944857267741328:50511111010448:2800:CD3C71C3A0A34E3B2E518A7C78A2BFB384DEF5E049E1FCA006B45FDA7A9EC699.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030062141.42129480120552594944857267741328:50511111010448:2800:CD3C71C3A0A34E3B2E518A7C78A2BFB384DEF5E049E1FCA006B45FDA7A9EC699.png[/IMG]

2. Downloaded ticket in flight booking app

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030062421.74305295420800920659747753226724:50511111010448:2800:0321B3E937B37E6B0FB666E99FC29F93145DA3EE48E7E8FF4A29D480F7BD0F8F.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201030062421.74305295420800920659747753226724:50511111010448:2800:0321B3E937B37E6B0FB666E99FC29F93145DA3EE48E7E8FF4A29D480F7BD0F8F.gif[/IMG]

Conclusion

In this article, we have learnt how to upload and download flight ticket using cloud storage. Users flight booking information is stored in App Gallery connect. We will show downloaded flight ticket in Itinerary Activity once user click the flight details from history list.

Reference

Huawei Cloud Storage