1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.phone.testapps.embmsdownload;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 import android.telephony.MbmsDownloadSession;
24 import android.telephony.mbms.FileInfo;
25 
26 import java.io.IOException;
27 import java.nio.file.FileSystems;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 
31 public class DownloadCompletionReceiver extends BroadcastReceiver {
32     @Override
onReceive(Context context, Intent intent)33     public void onReceive(Context context, Intent intent) {
34         if (EmbmsTestDownloadApp.DOWNLOAD_DONE_ACTION.equals(intent.getAction())) {
35             int result = intent.getIntExtra(MbmsDownloadSession.EXTRA_MBMS_DOWNLOAD_RESULT,
36                     MbmsDownloadSession.RESULT_CANCELLED);
37             if (result != MbmsDownloadSession.RESULT_SUCCESSFUL) {
38                 EmbmsTestDownloadApp.getInstance().onDownloadFailed(result);
39             }
40             Uri completedFile = intent.getParcelableExtra(
41                     MbmsDownloadSession.EXTRA_MBMS_COMPLETED_FILE_URI);
42 
43             EmbmsTestDownloadApp.getInstance().onDownloadDone(completedFile);
44         }
45     }
46 
getDestinationFile(Context context, String serviceId, FileInfo info)47     private Path getDestinationFile(Context context, String serviceId, FileInfo info) {
48         try {
49             if (serviceId.contains("2")) {
50                 String fileName = info.getUri().getLastPathSegment();
51                 Path destination = FileSystems.getDefault()
52                         .getPath(context.getFilesDir().getPath(), "images/animals/", fileName)
53                         .normalize();
54                 if (!Files.isDirectory(destination.getParent())) {
55                     Files.createDirectory(destination.getParent());
56                 }
57                 return destination;
58             } else {
59                 Path destination = FileSystems.getDefault()
60                         .getPath(context.getFilesDir().getPath(), "images/image.png")
61                         .normalize();
62                 if (!Files.isDirectory(destination.getParent())) {
63                     Files.createDirectory(destination.getParent());
64                 }
65                 return destination;
66             }
67         } catch (IOException e) {
68             return null;
69         }
70     }
71 }
72