Merge "Changes to testapps for EMBMS api tweaks"
diff --git a/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsSampleDownloadService.java b/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsSampleDownloadService.java
index 128793f..7bd0f70 100644
--- a/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsSampleDownloadService.java
+++ b/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsSampleDownloadService.java
@@ -371,11 +371,8 @@
             c.onProgressUpdated(request, fileToDownload, 10, 10, 10, 10);
         }
         // Take a round-trip through the download request serialization to exercise it
-        DownloadRequest request1 = new DownloadRequest.Builder(request.getSourceUri())
-                .setSubscriptionId(request.getSubscriptionId())
-                .setServiceId(request.getFileServiceId())
-                .setOpaqueData(request.getOpaqueData())
-                .build();
+        DownloadRequest request1 = DownloadRequest.Builder.fromSerializedRequest(
+                request.toByteArray()).build();
 
         Intent downloadResultIntent =
                 new Intent(VendorUtils.ACTION_DOWNLOAD_RESULT_INTERNAL);
diff --git a/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/DownloadCompletionReceiver.java b/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/DownloadCompletionReceiver.java
index 3c94b76..736b912 100644
--- a/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/DownloadCompletionReceiver.java
+++ b/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/DownloadCompletionReceiver.java
@@ -21,14 +21,12 @@
 import android.content.Intent;
 import android.net.Uri;
 import android.telephony.MbmsDownloadSession;
-import android.telephony.mbms.DownloadRequest;
 import android.telephony.mbms.FileInfo;
 
 import java.io.IOException;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.StandardCopyOption;
 
 public class DownloadCompletionReceiver extends BroadcastReceiver {
     @Override
@@ -41,22 +39,8 @@
             }
             Uri completedFile = intent.getParcelableExtra(
                     MbmsDownloadSession.EXTRA_MBMS_COMPLETED_FILE_URI);
-            FileInfo completedFileInfo = intent.getParcelableExtra(
-                    MbmsDownloadSession.EXTRA_MBMS_FILE_INFO);
-            DownloadRequest request = intent.getParcelableExtra(
-                    MbmsDownloadSession.EXTRA_MBMS_DOWNLOAD_REQUEST);
 
-            Path destinationFile = getDestinationFile(context,
-                    request.getFileServiceId(), completedFileInfo);
-            Path sourceFile = FileSystems.getDefault().getPath(completedFile.getPath());
-            try {
-                Files.move(sourceFile, destinationFile, StandardCopyOption.REPLACE_EXISTING);
-            } catch (IOException e) {
-                return;
-            }
-
-            EmbmsTestDownloadApp.getInstance().onDownloadDone(
-                    Uri.fromFile(destinationFile.toFile()));
+            EmbmsTestDownloadApp.getInstance().onDownloadDone(completedFile);
         }
     }
 
diff --git a/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/EmbmsTestDownloadApp.java b/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/EmbmsTestDownloadApp.java
index 4083f67..76baf05 100644
--- a/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/EmbmsTestDownloadApp.java
+++ b/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/EmbmsTestDownloadApp.java
@@ -44,6 +44,7 @@
 import android.widget.Toast;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -203,7 +204,7 @@
 
         Button bindButton = (Button) findViewById(R.id.bind_button);
         bindButton.setOnClickListener((view) -> {
-            mDownloadManager = MbmsDownloadSession.create(this, mCallback, mHandler);
+            mDownloadManager = MbmsDownloadSession.create(this, mHandler::post, mCallback);
         });
 
         Button setTempFileRootButton = (Button) findViewById(R.id.set_temp_root_button);
@@ -291,26 +292,28 @@
                         "No DownloadRequest Pending for progress...", Toast.LENGTH_SHORT).show();
                 return;
             }
-            mDownloadManager.registerStateCallback(req, new DownloadStateCallback(
-                    DownloadStateCallback.PROGRESS_UPDATES) {
-                @Override
-                public void onProgressUpdated(DownloadRequest request, FileInfo fileInfo,
-                        int currentDownloadSize, int fullDownloadSize, int currentDecodedSize,
-                        int fullDecodedSize) {
-                    Toast.makeText(EmbmsTestDownloadApp.this,
-                            "Progress Updated (" + fileInfo + ") cd: " + currentDecodedSize
-                                    + " fd: " + fullDownloadSize, Toast.LENGTH_SHORT).show();
-                }
+            mDownloadManager.registerStateCallback(req, sInstance.getMainThreadHandler()::post,
+                    new DownloadStateCallback(DownloadStateCallback.PROGRESS_UPDATES) {
+                        @Override
+                        public void onProgressUpdated(DownloadRequest request, FileInfo fileInfo,
+                                int currentDownloadSize, int fullDownloadSize,
+                                int currentDecodedSize, int fullDecodedSize) {
+                            Toast.makeText(EmbmsTestDownloadApp.this,
+                                    "Progress Updated (" + fileInfo + ") cd: " + currentDecodedSize
+                                            + " fd: " + fullDownloadSize, Toast.LENGTH_SHORT)
+                                    .show();
+                        }
 
-                @Override
-                public void onStateUpdated(DownloadRequest request, FileInfo fileInfo,
-                        @MbmsDownloadSession.DownloadStatus int state) {
-                    // only registered for state callback, this shouldn't happen!
-                    Toast.makeText(EmbmsTestDownloadApp.this,
-                            "State ERROR: received state update for callback that didn't filter it",
-                            Toast.LENGTH_SHORT).show();
-                }
-            }, sInstance.getMainThreadHandler());
+                        @Override
+                        public void onStateUpdated(DownloadRequest request, FileInfo fileInfo,
+                                @MbmsDownloadSession.DownloadStatus int state) {
+                            // only registered for state callback, this shouldn't happen!
+                            Toast.makeText(EmbmsTestDownloadApp.this,
+                                    "State ERROR: received state update for callback that didn't"
+                                            + " filter it",
+                                    Toast.LENGTH_SHORT).show();
+                        }
+                    });
         });
 
         Button registerStateCallback =
@@ -327,26 +330,27 @@
                         "No DownloadRequest Pending for state...", Toast.LENGTH_SHORT).show();
                 return;
             }
-            mDownloadManager.registerStateCallback(req, new DownloadStateCallback(
-                    DownloadStateCallback.STATE_UPDATES) {
-                @Override
-                public void onProgressUpdated(DownloadRequest request, FileInfo fileInfo,
-                        int currentDownloadSize, int fullDownloadSize, int currentDecodedSize,
-                        int fullDecodedSize) {
-                    // only registered for state callback, this shouldn't happen!
-                    Toast.makeText(EmbmsTestDownloadApp.this,
-                            "Progress ERROR: received progress update for callback that didn't "
-                                    + "filter it", Toast.LENGTH_SHORT).show();
-                }
+            mDownloadManager.registerStateCallback(req, sInstance.getMainThreadHandler()::post,
+                    new DownloadStateCallback(DownloadStateCallback.STATE_UPDATES) {
+                        @Override
+                        public void onProgressUpdated(DownloadRequest request, FileInfo fileInfo,
+                                int currentDownloadSize, int fullDownloadSize,
+                                int currentDecodedSize, int fullDecodedSize) {
+                            // only registered for state callback, this shouldn't happen!
+                            Toast.makeText(EmbmsTestDownloadApp.this,
+                                    "Progress ERROR: received progress update for"
+                                            + " callback that didn't "
+                                            + "filter it", Toast.LENGTH_SHORT).show();
+                        }
 
-                @Override
-                public void onStateUpdated(DownloadRequest request, FileInfo fileInfo,
-                        @MbmsDownloadSession.DownloadStatus int state) {
-                    Toast.makeText(EmbmsTestDownloadApp.this,
-                            "State Updated (" + fileInfo + ") state: " + state,
-                            Toast.LENGTH_SHORT).show();
-                }
-            }, sInstance.getMainThreadHandler());
+                        @Override
+                        public void onStateUpdated(DownloadRequest request, FileInfo fileInfo,
+                                @MbmsDownloadSession.DownloadStatus int state) {
+                            Toast.makeText(EmbmsTestDownloadApp.this,
+                                    "State Updated (" + fileInfo + ") state: " + state,
+                                    Toast.LENGTH_SHORT).show();
+                        }
+                    });
         });
 
         Button registerAllCallbacks =
@@ -363,24 +367,26 @@
                         "No DownloadRequest Pending for state...", Toast.LENGTH_SHORT).show();
                 return;
             }
-            mDownloadManager.registerStateCallback(req, new DownloadStateCallback() {
-                @Override
-                public void onProgressUpdated(DownloadRequest request, FileInfo fileInfo,
-                        int currentDownloadSize, int fullDownloadSize, int currentDecodedSize,
-                        int fullDecodedSize) {
-                    Toast.makeText(EmbmsTestDownloadApp.this,
-                            "Progress Updated (" + fileInfo + ") cd: " + currentDecodedSize
-                                    + " fd: " + fullDownloadSize, Toast.LENGTH_SHORT).show();
-                }
+            mDownloadManager.registerStateCallback(req, sInstance.getMainThreadHandler()::post,
+                    new DownloadStateCallback() {
+                        @Override
+                        public void onProgressUpdated(DownloadRequest request, FileInfo fileInfo,
+                                int currentDownloadSize, int fullDownloadSize,
+                                int currentDecodedSize, int fullDecodedSize) {
+                            Toast.makeText(EmbmsTestDownloadApp.this,
+                                    "Progress Updated (" + fileInfo + ") cd: " + currentDecodedSize
+                                            + " fd: " + fullDownloadSize, Toast.LENGTH_SHORT)
+                                    .show();
+                        }
 
-                @Override
-                public void onStateUpdated(DownloadRequest request, FileInfo fileInfo,
-                        @MbmsDownloadSession.DownloadStatus int state) {
-                    Toast.makeText(EmbmsTestDownloadApp.this,
-                            "State Updated (" + fileInfo + ") state: " + state,
-                            Toast.LENGTH_SHORT).show();
-                }
-            }, sInstance.getMainThreadHandler());
+                        @Override
+                        public void onStateUpdated(DownloadRequest request, FileInfo fileInfo,
+                                @MbmsDownloadSession.DownloadStatus int state) {
+                            Toast.makeText(EmbmsTestDownloadApp.this,
+                                    "State Updated (" + fileInfo + ") state: " + state,
+                                    Toast.LENGTH_SHORT).show();
+                        }
+                    });
         });
     }
 
@@ -429,7 +435,8 @@
         Intent completionIntent = new Intent(DOWNLOAD_DONE_ACTION);
         completionIntent.setClass(this, DownloadCompletionReceiver.class);
 
-        DownloadRequest request = new DownloadRequest.Builder(sourceUriBuilder.build())
+        DownloadRequest request = new DownloadRequest.Builder(sourceUriBuilder.build(),
+                getDestination(info.getServiceId()))
                 .setServiceInfo(info)
                 .setAppIntent(completionIntent)
                 .setSubscriptionId(SubscriptionManager.getDefaultSubscriptionId())
@@ -438,4 +445,24 @@
         mDownloadManager.download(request);
         mDownloadRequestAdapter.add(request);
     }
+
+    private Uri getDestination(String serviceId) {
+        File dest;
+        try {
+            if (serviceId.contains("2")) {
+                dest = new File(getFilesDir().getCanonicalFile(), "images/animals/");
+                if (!dest.exists()) {
+                    dest.mkdirs();
+                }
+            } else {
+                dest = new File(getFilesDir().getCanonicalFile(), "images/");
+                if (!dest.exists()) {
+                    dest.mkdirs();
+                }
+            }
+            return Uri.fromFile(dest);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
 }
diff --git a/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/EmbmsTestStreamingApp.java b/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/EmbmsTestStreamingApp.java
index 0546c9d..75febda 100644
--- a/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/EmbmsTestStreamingApp.java
+++ b/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/EmbmsTestStreamingApp.java
@@ -154,7 +154,7 @@
         Button bindButton = (Button) findViewById(R.id.bind_button);
         bindButton.setOnClickListener((view) -> {
             mStreamingManager = MbmsStreamingSession.create(
-                    EmbmsTestStreamingApp.this, mStreamingListener, mHandler);
+                    EmbmsTestStreamingApp.this, mHandler::post, mStreamingListener);
         });
 
         Button getStreamingServicesButton = (Button)
diff --git a/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/StreamingServiceTracker.java b/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/StreamingServiceTracker.java
index 5244d30..d45b5fd 100644
--- a/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/StreamingServiceTracker.java
+++ b/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/StreamingServiceTracker.java
@@ -60,13 +60,13 @@
      * Start streaming using the provided streaming session
      */
     public boolean startStreaming(MbmsStreamingSession streamingManager) {
-        mStreamingService =
-                streamingManager.startStreaming(mStreamingServiceInfo, new Callback(), null);
+        mStreamingService = streamingManager.startStreaming(mStreamingServiceInfo,
+                mActivity.getMainThreadHandler()::post, new Callback());
         return true;
     }
 
     public void stopStreaming() {
-        mStreamingService.stopStreaming();
+        mStreamingService.close();
     }
 
     public String getServiceId() {