Skip to content

Commit

Permalink
Removed Device Sync request
Browse files Browse the repository at this point in the history
  • Loading branch information
DaVinci9196 committed Nov 12, 2024
1 parent 71a8f59 commit bad3c52
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 887 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ package com.google.android.play.core.assetpacks.protocol;

interface IAssetModuleServiceCallback {
oneway void onStartDownload(int status, in Bundle bundle) = 1;
oneway void onCancelDownload(int status) = 2;
oneway void onGetSession(int status) = 3;
oneway void onCancelDownload(int status, in Bundle bundle) = 2;
oneway void onGetSession(int status, in Bundle bundle) = 3;
oneway void onGetSessionStates(in List<Bundle> list) = 4;
oneway void onNotifyChunkTransferred(in Bundle bundle,in Bundle bundle2) = 5;
oneway void onError(in Bundle bundle) = 6;
oneway void onNotifyModuleCompleted(in Bundle bundle,in Bundle bundle2) = 7;
oneway void onNotifySessionFailed(in Bundle bundle) = 9;
oneway void onNotifySessionFailed(in Bundle bundle,in Bundle bundle2) = 9;
oneway void onKeepAlive(in Bundle bundle, in Bundle bundle2) = 10;
oneway void onGetChunkFileDescriptor(in Bundle bundle, in Bundle bundle2) = 11;
oneway void onRequestDownloadInfo(in Bundle bundle, in Bundle bundle2) = 12;
oneway void onRemoveModule() = 13;
oneway void onCancelDownloads() = 14;
oneway void onRemoveModule(in Bundle bundle,in Bundle bundle2) = 13;
oneway void onCancelDownloads(in Bundle bundle) = 14;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.android.vending.R
import com.google.android.finsky.assetmoduleservice.DownloadData
import com.google.android.play.core.assetpacks.protocol.IAssetModuleServiceCallback
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
Expand Down Expand Up @@ -150,7 +152,7 @@ class DownloadManager(private val context: Context) {
}

@Synchronized
fun prepareDownload(downloadData: DownloadData, moduleName: String) {
fun prepareDownload(downloadData: DownloadData, moduleName: String, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "prepareDownload: ${downloadData.packageName}")
initNotification(moduleName, downloadData.packageName)
val future = executor.submit {
Expand All @@ -167,7 +169,7 @@ class DownloadManager(private val context: Context) {
}
val filesDir = "${context.filesDir}/assetpacks/$index/$resourcePackageName/$chunkName/"
val destination = File(filesDir, resourceBlockName)
startDownload(moduleName, resourceLink, destination, downloadData)
startDownload(moduleName, resourceLink, destination, downloadData,callback)
sendBroadcastForExistingFile(context, downloadData, moduleName, dataBundle, destination)
}
updateProgress(moduleName, 100)
Expand All @@ -186,7 +188,7 @@ class DownloadManager(private val context: Context) {
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
}

private fun startDownload(moduleName: String, downloadLink: String, destinationFile: File, downloadData: DownloadData) {
private fun startDownload(moduleName: String, downloadLink: String, destinationFile: File, downloadData: DownloadData, callback: IAssetModuleServiceCallback?) {
val packData = downloadData.getModuleData(moduleName)
val uri = Uri.parse(downloadLink).toString()
val connection = URL(uri).openConnection() as HttpURLConnection
Expand Down Expand Up @@ -228,6 +230,8 @@ class DownloadManager(private val context: Context) {
} catch (e: Exception) {
Log.e(TAG, "prepareDownload: startDownload error ", e)
downloadData.updateDownloadStatus(moduleName, STATUS_FAILED)
cancelDownload(moduleName)
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, ACCESS_DENIED) })
} finally {
connection.disconnect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleService
import com.google.android.finsky.API_NOT_AVAILABLE
import com.google.android.finsky.DownloadManager
import com.google.android.finsky.ERROR_CODE_FAIL
import com.google.android.finsky.KEY_BYTE_LENGTH
import com.google.android.finsky.KEY_CHUNK_FILE_DESCRIPTOR
import com.google.android.finsky.KEY_CHUNK_NAME
Expand All @@ -28,10 +28,10 @@ import com.google.android.finsky.KEY_MODULE_NAME
import com.google.android.finsky.KEY_PACK_NAMES
import com.google.android.finsky.KEY_PLAY_CORE_VERSION_CODE
import com.google.android.finsky.KEY_RESOURCE_BLOCK_NAME
import com.google.android.finsky.KEY_RESOURCE_LINK
import com.google.android.finsky.KEY_RESOURCE_PACKAGE_NAME
import com.google.android.finsky.KEY_SESSION_ID
import com.google.android.finsky.KEY_SLICE_ID
import com.google.android.finsky.NO_ERROR
import com.google.android.finsky.STATUS_COMPLETED
import com.google.android.finsky.STATUS_DOWNLOADING
import com.google.android.finsky.STATUS_INITIAL_STATE
Expand Down Expand Up @@ -76,8 +76,7 @@ class AssetModuleServiceImpl(
override fun startDownload(packageName: String?, list: MutableList<Bundle>?, bundle: Bundle?, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "Method (startDownload) called by packageName -> $packageName")
if (packageName == null || list == null || bundle == null) {
Log.d(TAG, "startDownload: params invalid ")
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, -5) })
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, API_NOT_AVAILABLE) })
return
}
if (downloadData == null || downloadData?.packageName != packageName) {
Expand All @@ -100,49 +99,49 @@ class AssetModuleServiceImpl(
val packData = downloadData?.getModuleData(moduleName!!)
if (packData?.status == STATUS_INITIAL_STATE){
DownloadManager.get(context).shouldStop(false)
DownloadManager.get(context).prepareDownload(downloadData!!, moduleName!!)
DownloadManager.get(context).prepareDownload(downloadData!!, moduleName!!,callback)
}
}
}

override fun getSessionStates(packageName: String?, bundle: Bundle?, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "Method (getSessionStates) called by packageName -> $packageName")
val installedAssetModuleNames = mutableListOf<String>()
bundle?.getParcelableArrayList<Bundle>(KEY_INSTALLED_ASSET_MODULE)?.forEach { item ->
item.keySet().forEach { subKey ->
(item.get(subKey) as? String)?.let { installedAssetModuleNames.add(it) }
}
}
if (packageName == downloadData?.packageName) {
val installedAssetModuleNames = bundle?.getParcelableArrayList<Bundle>(KEY_INSTALLED_ASSET_MODULE)
?.flatMap { it.keySet().mapNotNull { subKey -> it.get(subKey) as? String } }
?.toMutableList() ?: mutableListOf()

val listBundleData: MutableList<Bundle> = mutableListOf()

packageName.takeIf { it == downloadData?.packageName }?.let {
downloadData?.moduleNames?.forEach { moduleName ->
if (installedAssetModuleNames.contains(moduleName)) { return@forEach }
val packData = downloadData?.getModuleData(moduleName)
for (dataBundle in packData!!.packBundleList) {
val resourcePackageName: String? = dataBundle.getString(KEY_RESOURCE_PACKAGE_NAME)
val chunkName: String? = dataBundle.getString(KEY_CHUNK_NAME)
val resourceLink: String? = dataBundle.getString(KEY_RESOURCE_LINK)
val index: Int = dataBundle.getInt(KEY_INDEX)
val resourceBlockName: String? = dataBundle.getString(KEY_RESOURCE_BLOCK_NAME)
if (resourcePackageName == null || chunkName == null || resourceLink == null || resourceBlockName == null) {
continue
if (moduleName in installedAssetModuleNames) return@forEach

listBundleData.add(sendBroadcastForExistingFile(context, downloadData!!, moduleName, null, null))

downloadData?.getModuleData(moduleName)?.packBundleList?.forEach { dataBundle ->
val destination = dataBundle.run {
val resourcePackageName = getString(KEY_RESOURCE_PACKAGE_NAME)
val chunkName = getString(KEY_CHUNK_NAME)
val resourceBlockName = getString(KEY_RESOURCE_BLOCK_NAME)
if (resourcePackageName == null || chunkName == null || resourceBlockName == null) return@forEach
File("${context.filesDir}/assetpacks/${getInt(KEY_INDEX)}/$resourcePackageName/$chunkName/", resourceBlockName)
}
val filesDir = "${context.filesDir}/assetpacks/$index/$resourcePackageName/$chunkName/"
val destination = File(filesDir, resourceBlockName)
val byteLength = dataBundle.getLong(KEY_BYTE_LENGTH)
if (destination.exists() && destination.length() == byteLength) {
sendBroadcastForExistingFile(context, downloadData!!, moduleName, dataBundle, destination)

if (destination.exists() && destination.length() == dataBundle.getLong(KEY_BYTE_LENGTH)) {
listBundleData.add(sendBroadcastForExistingFile(context, downloadData!!, moduleName, dataBundle, destination))
}
}
}
}
Log.d(TAG, "getSessionStates: $listBundleData")
callback?.onGetSessionStates(listBundleData)
}

override fun notifyChunkTransferred(packageName: String?, bundle: Bundle?, bundle2: Bundle?, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "Method (notifyChunkTransferred) called by packageName -> $packageName")
val moduleName = bundle?.getString(KEY_MODULE_NAME)
if (moduleName.isNullOrEmpty()) {
Log.d(TAG, "notifyChunkTransferred: params invalid ")
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, -5) })
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, API_NOT_AVAILABLE) })
return
}
val sessionId = bundle.getInt(KEY_SESSION_ID)
Expand All @@ -151,15 +150,14 @@ class AssetModuleServiceImpl(
val downLoadFile = "${context.filesDir.absolutePath}/assetpacks/$sessionId/$moduleName/$sliceId/$chunkNumber"
fileDescriptorMap[downLoadFile]?.close()
fileDescriptorMap.remove(downLoadFile)
callback?.onNotifyChunkTransferred(bundle, Bundle().apply { putInt(KEY_ERROR_CODE, 0) })
callback?.onNotifyChunkTransferred(bundle, Bundle().apply { putInt(KEY_ERROR_CODE, NO_ERROR) })
}

override fun notifyModuleCompleted(packageName: String?, bundle: Bundle?, bundle2: Bundle?, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "Method (notifyModuleCompleted) called but not implemented by packageName -> $packageName")
val moduleName = bundle?.getString(KEY_MODULE_NAME)
if (moduleName.isNullOrEmpty()) {
Log.d(TAG, "notifyModuleCompleted: params invalid ")
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, -5) })
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, API_NOT_AVAILABLE) })
return
}
Log.d(TAG, "notify: moduleName: $moduleName packNames: ${downloadData?.moduleNames}")
Expand All @@ -170,7 +168,7 @@ class AssetModuleServiceImpl(

override fun notifySessionFailed(packageName: String?, bundle: Bundle?, bundle2: Bundle?, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "Method (notifySessionFailed) called but not implemented by packageName -> $packageName")
callback?.onNotifySessionFailed(bundle)
callback?.onNotifySessionFailed(Bundle(),Bundle())
}

override fun keepAlive(packageName: String?, bundle: Bundle?, callback: IAssetModuleServiceCallback?) {
Expand All @@ -181,8 +179,7 @@ class AssetModuleServiceImpl(
Log.d(TAG, "Method (getChunkFileDescriptor) called by packageName -> $packageName")
val moduleName = bundle.getString(KEY_MODULE_NAME)
if (moduleName.isNullOrEmpty()) {
Log.d(TAG, "getChunkFileDescriptor: params invalid ")
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, -5) })
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, API_NOT_AVAILABLE) })
return
}
val parcelFileDescriptor = runCatching {
Expand All @@ -201,18 +198,17 @@ class AssetModuleServiceImpl(
override fun requestDownloadInfo(packageName: String?, list: MutableList<Bundle>?, bundle: Bundle?, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "Method (requestDownloadInfo) called by packageName -> $packageName")
if (packageName == null || list == null || bundle == null) {
Log.w(TAG, "requestDownloadInfo: params invalid ")
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, -5) })
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, API_NOT_AVAILABLE) })
return
}
if (downloadData == null || downloadData?.packageName != packageName) {
val requestedAssetModuleNames = list.map { it.getString(KEY_MODULE_NAME) }.filter { !it.isNullOrEmpty() }
val playCoreVersionCode = bundle.getInt(KEY_PLAY_CORE_VERSION_CODE)
downloadData = httpClient.initAssertModuleData(context, packageName, accountManager, requestedAssetModuleNames, playCoreVersionCode)
}
if (downloadData?.errorCode == ERROR_CODE_FAIL) {
if (downloadData?.errorCode == API_NOT_AVAILABLE) {
if (moduleErrorRequested.contains(packageName)) {
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, -5) })
callback?.onError(Bundle().apply { putInt(KEY_ERROR_CODE, API_NOT_AVAILABLE) })
return
}
moduleErrorRequested.add(packageName)
Expand All @@ -232,6 +228,7 @@ class AssetModuleServiceImpl(

override fun cancelDownloads(packageName: String?, list: MutableList<Bundle>?, bundle: Bundle?, callback: IAssetModuleServiceCallback?) {
Log.d(TAG, "Method (cancelDownloads) called but not implemented by packageName -> $packageName")
callback?.onCancelDownloads(Bundle())
}

companion object {
Expand Down
Loading

0 comments on commit bad3c52

Please sign in to comment.