diff options
-rw-r--r-- | android/src/com/mapswithme/maps/background/SystemDownloadCompletedService.java | 5 | ||||
-rw-r--r-- | android/src/com/mapswithme/maps/downloader/MapDownloadCompletedProcessor.java | 117 |
2 files changed, 121 insertions, 1 deletions
diff --git a/android/src/com/mapswithme/maps/background/SystemDownloadCompletedService.java b/android/src/com/mapswithme/maps/background/SystemDownloadCompletedService.java index a0218520d7..de61c5d2b5 100644 --- a/android/src/com/mapswithme/maps/background/SystemDownloadCompletedService.java +++ b/android/src/com/mapswithme/maps/background/SystemDownloadCompletedService.java @@ -44,7 +44,10 @@ public class SystemDownloadCompletedService extends JobIntentService if (!cursor.moveToFirst()) return; - BookmarksDownloadCompletedProcessor.process(getApplicationContext(), cursor); + if (MapDownloadCompletedProcessor.isSupported(cursor)) + MapDownloadCompletedProcessor.process(getApplicationContext(), id, cursor); + else + BookmarksDownloadCompletedProcessor.process(getApplicationContext(), cursor); } finally { diff --git a/android/src/com/mapswithme/maps/downloader/MapDownloadCompletedProcessor.java b/android/src/com/mapswithme/maps/downloader/MapDownloadCompletedProcessor.java new file mode 100644 index 0000000000..edeae6abad --- /dev/null +++ b/android/src/com/mapswithme/maps/downloader/MapDownloadCompletedProcessor.java @@ -0,0 +1,117 @@ +package com.mapswithme.maps.downloader; + +import android.app.DownloadManager; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.os.FileUtils; +import android.text.TextUtils; + +import androidx.annotation.MainThread; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.mapswithme.maps.MwmApplication; +import com.mapswithme.util.concurrency.UiThread; +import com.mapswithme.util.log.Logger; +import com.mapswithme.util.log.LoggerFactory; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class MapDownloadCompletedProcessor +{ + private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.DOWNLOADER); + private static final String TAG = MapDownloadCompletedProcessor.class.getSimpleName(); + + public static boolean isSupported(@NonNull Cursor cursor) + { + String targetUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI)); + String targetUriPath = Uri.parse(targetUri).getPath(); + + return !TextUtils.isEmpty(targetUriPath) && MapManager.nativeIsUrlSupported(targetUriPath); + } + + public static void process(@NonNull Context context, long id, @NonNull Cursor cursor) + { + try + { + String targetUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI)); + int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); + String downloadedFileUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); + + boolean result = processColumns(context, status, targetUri, downloadedFileUri); + UiThread.run(new MapDownloadCompletedTask(context, result, id)); + LOGGER.d(TAG, "Processing for map downloading for request id " + id + " is finished."); + } + catch (Exception e) + { + LOGGER.e(TAG, "Failed to process map download for request id " + id + ". Exception ", e); + } + } + + private static boolean processColumns(@NonNull Context context, int status, + @Nullable String targetUri, @Nullable String localFileUri) + { + String targetUriPath = Uri.parse(targetUri).getPath(); + Uri downloadedFileUri = Uri.parse(localFileUri); + + if (status != DownloadManager.STATUS_SUCCESSFUL || TextUtils.isEmpty(targetUriPath) + || downloadedFileUri == null) + { + return false; + } + + String dstPath = MapManager.nativeGetFilePathByUrl(targetUriPath); + return copyFile(context, downloadedFileUri, dstPath); + } + + private static boolean copyFile(@NonNull Context context, @NonNull Uri from, @NonNull String to) + { + try (InputStream in = context.getContentResolver().openInputStream(from)) + { + if (in == null) + return false; + + try (OutputStream out = new FileOutputStream(to)) + { + FileUtils.copy(in, out); + context.getContentResolver().delete(from, null, null); + return true; + } + } + catch (IOException e) + { + LOGGER.e(TAG, "Failed to copy or delete downloaded map file from " + from.toString() + + " to " + to + ". Exception ", e); + return false; + } + } + + private static class MapDownloadCompletedTask implements Runnable + { + @NonNull + private final Context mAppContext; + private final boolean mStatus; + private final long mId; + + private MapDownloadCompletedTask(@NonNull Context applicationContext, boolean status, long id) + { + mAppContext = applicationContext; + mStatus = status; + mId = id; + } + + @Override + @MainThread + public void run() + { + MwmApplication application = MwmApplication.from(mAppContext); + if (!application.arePlatformAndCoreInitialized()) + return; + + MapManager.nativeOnDownloadFinished(mStatus, mId); + } + } +} |