From 94e47a194c58c34cd71bcdc68e68cd93cf18de9a Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Thu, 11 May 2023 23:40:19 -0700 Subject: [PATCH] Convert some files to kotlin --- app/build.gradle | 5 +- .../service/DownloadBroadcastReceiver.java | 18 --- .../service/DownloadBroadcastReceiver.kt | 12 ++ .../memories/service/DownloadService.java | 135 ------------------ .../memories/service/DownloadService.kt | 106 ++++++++++++++ .../java/gallery/memories/service/Fields.java | 27 ---- .../java/gallery/memories/service/Fields.kt | 23 +++ build.gradle | 8 +- 8 files changed, 152 insertions(+), 182 deletions(-) delete mode 100644 app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.java create mode 100644 app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.kt delete mode 100644 app/src/main/java/gallery/memories/service/DownloadService.java create mode 100644 app/src/main/java/gallery/memories/service/DownloadService.kt delete mode 100644 app/src/main/java/gallery/memories/service/Fields.java create mode 100644 app/src/main/java/gallery/memories/service/Fields.kt diff --git a/app/build.gradle b/app/build.gradle index 6fe672e1..f053c9d9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,6 @@ plugins { id 'com.android.application' + id 'kotlin-android' } android { @@ -32,8 +33,10 @@ android { } dependencies { + implementation 'androidx.core:core-ktx:1.10.1' + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.6.1' - implementation 'com.google.android.material:material:1.8.0' + implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.navigation:navigation-fragment:2.5.3' implementation 'androidx.navigation:navigation-ui:2.5.3' diff --git a/app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.java b/app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.java deleted file mode 100644 index f0955862..00000000 --- a/app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.java +++ /dev/null @@ -1,18 +0,0 @@ -package gallery.memories.service; - -import android.app.DownloadManager; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.widget.Toast; - -import gallery.memories.NativeX; - -public class DownloadBroadcastReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - if (NativeX.mDlService != null) { - NativeX.mDlService.runDownloadCallback(intent); - } - } -} diff --git a/app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.kt b/app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.kt new file mode 100644 index 00000000..1b0183ae --- /dev/null +++ b/app/src/main/java/gallery/memories/service/DownloadBroadcastReceiver.kt @@ -0,0 +1,12 @@ +package gallery.memories.service + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import gallery.memories.NativeX + +class DownloadBroadcastReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + NativeX.mDlService?.runDownloadCallback(intent) + } +} \ No newline at end of file diff --git a/app/src/main/java/gallery/memories/service/DownloadService.java b/app/src/main/java/gallery/memories/service/DownloadService.java deleted file mode 100644 index 6149fc98..00000000 --- a/app/src/main/java/gallery/memories/service/DownloadService.java +++ /dev/null @@ -1,135 +0,0 @@ -package gallery.memories.service; - -import android.app.DownloadManager; -import android.content.ContentUris; -import android.content.Context; -import android.content.Intent; -import android.database.Cursor; -import android.net.Uri; -import android.provider.MediaStore; -import android.webkit.CookieManager; -import android.widget.Toast; - -import androidx.appcompat.app.AppCompatActivity; -import androidx.collection.ArrayMap; - -import java.util.Map; - -public class DownloadService { - class DownloadCallback { - public void onComplete(Intent intent) { - } - } - - final AppCompatActivity mActivity; - final Map mDownloads = new ArrayMap<>(); - - public DownloadService(AppCompatActivity activity) { - mActivity = activity; - } - - public void runDownloadCallback(Intent intent) { - if (mActivity.isDestroyed()) return; - - String action = intent.getAction(); - - if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { - long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); - synchronized (mDownloads) { - DownloadCallback callback = mDownloads.get(id); - if (callback != null) { - callback.onComplete(intent); - mDownloads.remove(id); - return; - } - } - - Toast.makeText(mActivity, "Download Complete", Toast.LENGTH_SHORT).show(); - } - } - - public long queue(final String url, final String filename) { - Uri uri = Uri.parse(url); - DownloadManager manager = (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE); - DownloadManager.Request request = new DownloadManager.Request(uri); - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); - - // Copy all cookies from the webview to the download request - String cookies = CookieManager.getInstance().getCookie(url); - request.addRequestHeader("cookie", cookies); - - if (!filename.equals("")) { - // Save the file to external storage - request.setDestinationInExternalPublicDir(android.os.Environment.DIRECTORY_DOWNLOADS, "memories/" + filename); - } - - // Start the download - return manager.enqueue(request); - } - - public Boolean shareUrl(final String url) { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - intent.putExtra(Intent.EXTRA_TEXT, url); - mActivity.startActivity(Intent.createChooser(intent, null)); - return true; - } - - public Boolean shareBlobFromUrl(final String url) throws Exception { - final long id = queue(url, ""); - final Object sync = new Object(); - - synchronized (mDownloads) { - mDownloads.put(id, new DownloadCallback() { - @Override - public void onComplete(Intent intent) { - synchronized (sync) { - sync.notify(); - } - } - }); - } - - synchronized (sync) { - sync.wait(); - } - - // Get the URI of the downloaded file - String sUri = getDownloadedFileURI(id); - if (sUri == null) { - throw new Exception("Failed to download file"); - } - Uri uri = Uri.parse(sUri); - - // Create sharing intent - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType(mActivity.getContentResolver().getType(uri)); - intent.putExtra(Intent.EXTRA_STREAM, uri); - mActivity.startActivity(Intent.createChooser(intent, null)); - - return true; - } - - public Boolean shareLocal(final long id) throws Exception { - Uri uri = ContentUris.withAppendedId(MediaStore.Files.getContentUri("external"), id); - Intent intent = new Intent(Intent.ACTION_SEND); - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - intent.setType(mActivity.getContentResolver().getType(uri)); - intent.putExtra(Intent.EXTRA_STREAM, uri); - mActivity.startActivity(Intent.createChooser(intent, null)); - return true; - } - - protected String getDownloadedFileURI(long downloadId) { - DownloadManager downloadManager = (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE); - DownloadManager.Query query = new DownloadManager.Query(); - query.setFilterById(downloadId); - Cursor cursor = downloadManager.query(query); - if (cursor.moveToFirst()) { - int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI); - return cursor.getString(columnIndex); - } - cursor.close(); - return null; - } -} diff --git a/app/src/main/java/gallery/memories/service/DownloadService.kt b/app/src/main/java/gallery/memories/service/DownloadService.kt new file mode 100644 index 00000000..4ab083db --- /dev/null +++ b/app/src/main/java/gallery/memories/service/DownloadService.kt @@ -0,0 +1,106 @@ +package gallery.memories.service + +import android.app.DownloadManager +import android.content.ContentUris +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Environment +import android.provider.MediaStore +import android.webkit.CookieManager +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.collection.ArrayMap +import java.util.concurrent.CountDownLatch + +class DownloadService(val mActivity: AppCompatActivity) { + private val mDownloads: MutableMap Unit> = ArrayMap() + + fun runDownloadCallback(intent: Intent) { + if (mActivity.isDestroyed) return + + if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == intent.action) { + val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0) + synchronized(mDownloads) { + mDownloads[id]?.let { + it() + mDownloads.remove(id) + return + } + } + + Toast.makeText(mActivity, "Download Complete", Toast.LENGTH_SHORT).show() + } + } + + fun queue(url: String, filename: String): Long { + val uri = Uri.parse(url) + val manager = mActivity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager + val request = DownloadManager.Request(uri) + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) + + // Copy all cookies from the webview to the download request + val cookies = CookieManager.getInstance().getCookie(url) + request.addRequestHeader("cookie", cookies) + if (filename != "") { + // Save the file to external storage + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "memories/$filename") + } + + // Start the download + return manager.enqueue(request) + } + + fun shareUrl(url: String): Boolean { + val intent = Intent(Intent.ACTION_SEND) + intent.type = "text/plain" + intent.putExtra(Intent.EXTRA_TEXT, url) + mActivity.startActivity(Intent.createChooser(intent, null)) + return true + } + + @Throws(Exception::class) + fun shareBlobFromUrl(url: String): Boolean { + val id = queue(url, "") + val latch = CountDownLatch(1) + synchronized(mDownloads) { + mDownloads.put(id, fun() { latch.countDown() }) + } + latch.await() + + // Get the URI of the downloaded file + val sUri = getDownloadedFileURI(id) ?: throw Exception("Failed to download file") + val uri = Uri.parse(sUri) + + // Create sharing intent + val intent = Intent(Intent.ACTION_SEND) + intent.type = mActivity.contentResolver.getType(uri) + intent.putExtra(Intent.EXTRA_STREAM, uri) + mActivity.startActivity(Intent.createChooser(intent, null)) + return true + } + + @Throws(Exception::class) + fun shareLocal(id: Long): Boolean { + val uri = ContentUris.withAppendedId(MediaStore.Files.getContentUri("external"), id) + val intent = Intent(Intent.ACTION_SEND) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + intent.type = mActivity.contentResolver.getType(uri) + intent.putExtra(Intent.EXTRA_STREAM, uri) + mActivity.startActivity(Intent.createChooser(intent, null)) + return true + } + + private fun getDownloadedFileURI(downloadId: Long): String? { + val downloadManager = mActivity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager + val query = DownloadManager.Query() + query.setFilterById(downloadId) + val cursor = downloadManager.query(query) + if (cursor.moveToFirst()) { + val columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI) + return cursor.getString(columnIndex) + } + cursor.close() + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/gallery/memories/service/Fields.java b/app/src/main/java/gallery/memories/service/Fields.java deleted file mode 100644 index 416c48ef..00000000 --- a/app/src/main/java/gallery/memories/service/Fields.java +++ /dev/null @@ -1,27 +0,0 @@ -package gallery.memories.service; - -public class Fields { - public final static class Photo { - public static final String FILEID = "fileid"; - public static final String BASENAME = "basename"; - public static final String MIMETYPE = "mimetype"; - public static final String HEIGHT = "h"; - public static final String WIDTH = "w"; - public static final String SIZE = "size"; - public static final String ETAG = "etag"; - public static final String DATETAKEN = "datetaken"; - public static final String DAYID = "dayid"; - public static final String ISVIDEO = "isvideo"; - public static final String VIDEO_DURATION = "video_duration"; - public static final String EXIF = "exif"; - public static final String PERMISSIONS = "permissions"; - } - - public final static class Perm { - public static final String DELETE = "D"; - } - - public final static class Exif { - - } -} diff --git a/app/src/main/java/gallery/memories/service/Fields.kt b/app/src/main/java/gallery/memories/service/Fields.kt new file mode 100644 index 00000000..5f5b677f --- /dev/null +++ b/app/src/main/java/gallery/memories/service/Fields.kt @@ -0,0 +1,23 @@ +package gallery.memories.service + +class Fields { + object Photo { + const val FILEID = "fileid" + const val BASENAME = "basename" + const val MIMETYPE = "mimetype" + const val HEIGHT = "h" + const val WIDTH = "w" + const val SIZE = "size" + const val ETAG = "etag" + const val DATETAKEN = "datetaken" + const val DAYID = "dayid" + const val ISVIDEO = "isvideo" + const val VIDEO_DURATION = "video_duration" + const val EXIF = "exif" + const val PERMISSIONS = "permissions" + } + + object Perm { + const val DELETE = "D" + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 71b99f4c..54bf905d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,10 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. +buildscript { + ext.kotlin_version = '1.8.10' + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + plugins { id 'com.android.application' version '7.3.1' apply false id 'com.android.library' version '7.3.1' apply false