Add callback for initialization done

Add callback for initialization done in the framework, and listen to it
in the testapps. Make initialization asynchronous as well for both
download and streaming.

Change-Id: I898ac5a0fb4de85c187aef67e8e4d21423eac76c
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 490f3fa..f7abeb9 100644
--- a/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsSampleDownloadService.java
+++ b/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsSampleDownloadService.java
@@ -60,6 +60,7 @@
     }};
 
     private static final String LOG_TAG = "EmbmsSampleDownload";
+    private static final long INITIALIZATION_DELAY = 200;
     private static final long SEND_FILE_SERVICE_INFO_DELAY = 500;
     private static final long DOWNLOAD_DELAY_MS = 1000;
     private static final long FILE_SEPARATION_DELAY = 500;
@@ -67,7 +68,8 @@
     private final IMbmsDownloadService mBinder = new MbmsDownloadServiceBase() {
         @Override
         public void initialize(String appName, int subId, IMbmsDownloadManagerCallback listener) {
-            String[] packageNames = getPackageManager().getPackagesForUid(Binder.getCallingUid());
+            int packageUid = Binder.getCallingUid();
+            String[] packageNames = getPackageManager().getPackagesForUid(packageUid);
             if (packageNames == null) {
                 throw new SecurityException("No matching packages found for your UID");
             }
@@ -77,24 +79,29 @@
                         "service");
             }
 
-            FrontendAppIdentifier appKey =
-                    new FrontendAppIdentifier(Binder.getCallingUid(), appName, subId);
-            if (!mAppCallbacks.containsKey(appKey)) {
-                mAppCallbacks.put(appKey, listener);
-                ComponentName appReceiver = MbmsDownloadManager.getAppReceiverFromUid(
-                        EmbmsSampleDownloadService.this, Binder.getCallingUid());
-                mAppReceivers.put(appKey, appReceiver);
-            } else {
-                // Stick the error callback on a different thread so that we're not calling back
-                // to the app on the same thread.
-                mHandler.post(() -> {
+            // Do initialization with a bit of a delay to simulate work being done.
+            mHandler.postDelayed(() -> {
+                FrontendAppIdentifier appKey =
+                        new FrontendAppIdentifier(packageUid, appName, subId);
+                if (!mAppCallbacks.containsKey(appKey)) {
+                    mAppCallbacks.put(appKey, listener);
+                    ComponentName appReceiver = MbmsDownloadManager.getAppReceiverFromUid(
+                            EmbmsSampleDownloadService.this, packageUid);
+                    mAppReceivers.put(appKey, appReceiver);
+                } else {
                     try {
                         listener.error(MbmsException.ERROR_ALREADY_INITIALIZED, "");
                     } catch (RemoteException e) {
                         // ignore, it was an error anyway
                     }
-                });
-            }
+                    return;
+                }
+                try {
+                    listener.middlewareReady();
+                } catch (RemoteException e) {
+                    // TODO: call dispose
+                }
+            }, INITIALIZATION_DELAY);
         }
 
         @Override
diff --git a/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsTestStreamingService.java b/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsTestStreamingService.java
index 11a9bde..313512f 100644
--- a/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsTestStreamingService.java
+++ b/testapps/EmbmsServiceTestApp/src/com/android/phone/testapps/embmsmw/EmbmsTestStreamingService.java
@@ -49,6 +49,7 @@
 
     private static final String TAG = "EmbmsTestStreaming";
 
+    private static final long INITIALIZATION_DELAY = 200;
     private static final long SEND_SERVICE_LIST_DELAY = 300;
     private static final long START_STREAMING_DELAY = 500;
 
@@ -81,7 +82,8 @@
     private final IMbmsStreamingService.Stub mBinder = new MbmsStreamingServiceBase() {
         @Override
         public int initialize(IMbmsStreamingManagerCallback listener, String appName, int subId) {
-            String[] packageNames = getPackageManager().getPackagesForUid(Binder.getCallingUid());
+            int packageUid = Binder.getCallingUid();
+            String[] packageNames = getPackageManager().getPackagesForUid(packageUid);
             if (packageNames == null) {
                 throw new SecurityException("No matching packages found for your UID");
             }
@@ -91,13 +93,26 @@
                         "service");
             }
 
-            FrontendAppIdentifier appKey =
-                    new FrontendAppIdentifier(Binder.getCallingUid(), appName, subId);
-            if (!mAppCallbacks.containsKey(appKey)) {
-                mAppCallbacks.put(appKey, listener);
-            } else {
-                return MbmsException.ERROR_ALREADY_INITIALIZED;
-            }
+            mHandler.postDelayed(() -> {
+                FrontendAppIdentifier appKey =
+                        new FrontendAppIdentifier(packageUid, appName, subId);
+                if (!mAppCallbacks.containsKey(appKey)) {
+                    mAppCallbacks.put(appKey, listener);
+                } else {
+                    try {
+                        listener.error(MbmsException.ERROR_ALREADY_INITIALIZED, "");
+                    } catch (RemoteException e) {
+                        // ignore, it was an error anyway
+                    }
+                    return;
+                }
+                try {
+                    listener.middlewareReady();
+                } catch (RemoteException e) {
+                    StreamStateTracker.disposeAll(appKey);
+                    mAppCallbacks.remove(appKey);
+                }
+            }, INITIALIZATION_DELAY);
             return 0;
         }
 
diff --git a/testapps/EmbmsTestDownloadApp/res/layout/activity_main.xml b/testapps/EmbmsTestDownloadApp/res/layout/activity_main.xml
index 07c7d37..048e3dc 100644
--- a/testapps/EmbmsTestDownloadApp/res/layout/activity_main.xml
+++ b/testapps/EmbmsTestDownloadApp/res/layout/activity_main.xml
@@ -38,6 +38,11 @@
         android:layout_height="wrap_content"
         android:text="@string/bind_button" />
     <Button
+        android:id="@+id/set_temp_root_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/set_temp_root_button" />
+    <Button
         android:id="@+id/get_file_services_button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
diff --git a/testapps/EmbmsTestDownloadApp/res/values/donottranslate_strings.xml b/testapps/EmbmsTestDownloadApp/res/values/donottranslate_strings.xml
index a29aea3..2d99962 100644
--- a/testapps/EmbmsTestDownloadApp/res/values/donottranslate_strings.xml
+++ b/testapps/EmbmsTestDownloadApp/res/values/donottranslate_strings.xml
@@ -19,4 +19,5 @@
     <string name="bind_button">Bind</string>
     <string name="request_dl_button">Request DL</string>
     <string name="get_file_services_button">Fetch file services</string>
+    <string name="set_temp_root_button">Set temp file root</string>
 </resources>
\ No newline at end of file
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 b7d3e01..3937bbd 100644
--- a/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/EmbmsTestDownloadApp.java
+++ b/testapps/EmbmsTestDownloadApp/src/com/android/phone/testapps/embmsdownload/EmbmsTestDownloadApp.java
@@ -146,6 +146,12 @@
                             Toast.LENGTH_SHORT).show());
             updateFileServicesList(services);
         }
+
+        @Override
+        public void middlewareReady() {
+            runOnUiThread(() -> Toast.makeText(EmbmsTestDownloadApp.this,
+                    "Initialization done", Toast.LENGTH_SHORT).show());
+        }
     };
 
     private MbmsDownloadManager mDownloadManager;
@@ -172,20 +178,29 @@
         downloadedImages.setAdapter(mImageAdapter);
 
         Button bindButton = (Button) findViewById(R.id.bind_button);
-        bindButton.setOnClickListener((view) -> mHandler.post(() -> {
+        bindButton.setOnClickListener((view) -> {
             try {
-                mDownloadManager = MbmsDownloadManager.createManager(this, mCallback, APP_NAME);
-                File downloadDir = new File(EmbmsTestDownloadApp.this.getFilesDir(),
-                        CUSTOM_EMBMS_TEMP_FILE_LOCATION);
-                downloadDir.mkdirs();
-                mDownloadManager.setTempFileRootDirectory(downloadDir);
-                runOnUiThread(() -> Toast.makeText(EmbmsTestDownloadApp.this,
-                        "Initialization done", Toast.LENGTH_SHORT).show());
+                mDownloadManager = MbmsDownloadManager.create(this, mCallback, APP_NAME);
             } catch (MbmsException e) {
-                runOnUiThread(() -> Toast.makeText(EmbmsTestDownloadApp.this,
-                        "caught MbmsException: " + e.getErrorCode(), Toast.LENGTH_SHORT).show());
+                Toast.makeText(EmbmsTestDownloadApp.this,
+                        "caught MbmsException: " + e.getErrorCode(), Toast.LENGTH_SHORT).show();
             }
-        }));
+        });
+
+        Button setTempFileRootButton = (Button) findViewById(R.id.set_temp_root_button);
+        setTempFileRootButton.setOnClickListener((view) -> {
+            File downloadDir = new File(EmbmsTestDownloadApp.this.getFilesDir(),
+                    CUSTOM_EMBMS_TEMP_FILE_LOCATION);
+            downloadDir.mkdirs();
+            try {
+                mDownloadManager.setTempFileRootDirectory(downloadDir);
+                Toast.makeText(EmbmsTestDownloadApp.this,
+                        "temp file root set to " + downloadDir, Toast.LENGTH_SHORT).show();
+            } catch (MbmsException e) {
+                Toast.makeText(EmbmsTestDownloadApp.this,
+                        "caught MbmsException: " + e.getErrorCode(), Toast.LENGTH_SHORT).show();
+            }
+        });
 
         Button getFileServicesButton = (Button) findViewById(R.id.get_file_services_button);
         getFileServicesButton.setOnClickListener((view) -> mHandler.post(() -> {
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 00e76c1..c3be6ef 100644
--- a/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/EmbmsTestStreamingApp.java
+++ b/testapps/EmbmsTestStreamingApp/src/com/android/phone/testapps/embmsfrontend/EmbmsTestStreamingApp.java
@@ -54,6 +54,12 @@
                             Toast.LENGTH_SHORT).show());
             updateStreamingServicesList(services);
         }
+
+        @Override
+        public void middlewareReady() {
+            runOnUiThread(() -> Toast.makeText(EmbmsTestStreamingApp.this, "Successfully bound",
+                    Toast.LENGTH_SHORT).show());
+        }
     };
 
     private final class StreamingServiceInfoAdapter
@@ -151,22 +157,16 @@
         mTrackedStreamingServiceAdapter = new TrackedStreamAdapter(this);
 
         Button bindButton = (Button) findViewById(R.id.bind_button);
-        bindButton.setOnClickListener((view) ->
-            mHandler.post(() -> {
-                try {
-                    mStreamingManager = MbmsStreamingManager.create(
-                            EmbmsTestStreamingApp.this, mStreamingListener, APP_NAME);
-                } catch (MbmsException e) {
-                    EmbmsTestStreamingApp.this.runOnUiThread(() ->
-                            Toast.makeText(EmbmsTestStreamingApp.this,
-                                    "Init error: " + e.getErrorCode(), Toast.LENGTH_SHORT).show());
-                    return;
-                }
-                EmbmsTestStreamingApp.this.runOnUiThread(() ->
-                        Toast.makeText(EmbmsTestStreamingApp.this, "Successfully bound",
-                                Toast.LENGTH_SHORT).show());
-            })
-        );
+        bindButton.setOnClickListener((view) -> {
+            try {
+                mStreamingManager = MbmsStreamingManager.create(
+                        EmbmsTestStreamingApp.this, mStreamingListener, APP_NAME);
+            } catch (MbmsException e) {
+                Toast.makeText(EmbmsTestStreamingApp.this,
+                        "Init error: " + e.getErrorCode(), Toast.LENGTH_SHORT).show();
+                return;
+            }
+        });
 
         Button getStreamingServicesButton = (Button)
                 findViewById(R.id.get_streaming_services_button);