Convert some files to kotlin

pull/653/merge
Varun Patil 2023-05-11 23:40:19 -07:00
parent 5386d72456
commit 94e47a194c
8 changed files with 152 additions and 182 deletions

View File

@ -1,5 +1,6 @@
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'kotlin-android'
} }
android { android {
@ -32,8 +33,10 @@ android {
} }
dependencies { 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 '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.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment:2.5.3' implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3' implementation 'androidx.navigation:navigation-ui:2.5.3'

View File

@ -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);
}
}
}

View File

@ -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)
}
}

View File

@ -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<Long, DownloadCallback> 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;
}
}

View File

@ -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<Long, () -> 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
}
}

View File

@ -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 {
}
}

View File

@ -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"
}
}

View File

@ -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 { plugins {
id 'com.android.application' version '7.3.1' apply false id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false id 'com.android.library' version '7.3.1' apply false