diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 074b4165..52c82f94 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -10,7 +10,6 @@ - resultLauncher = mActivity.registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == Activity.RESULT_OK) { + Intent intent = result.getData(); + if (intent != null) { + Uri uri = intent.getData(); + exportUpdate(uri); + } + } + }); + + Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType("application/zip"); + intent.putExtra(Intent.EXTRA_TITLE, update.getName()); + + resultLauncher.launch(intent); + } + + private void exportUpdate(Uri uri) { Intent intent = new Intent(mActivity, ExportUpdateService.class); intent.setAction(ExportUpdateService.ACTION_START_EXPORTING); - intent.putExtra(ExportUpdateService.EXTRA_SOURCE_FILE, update.getFile()); - intent.putExtra(ExportUpdateService.EXTRA_DEST_FILE, dest); + intent.putExtra(ExportUpdateService.EXTRA_SOURCE_FILE, mToBeExported.getFile()); + intent.putExtra(ExportUpdateService.EXTRA_DEST_URI, uri); mActivity.startService(intent); } diff --git a/src/org/lineageos/updater/misc/FileUtils.java b/src/org/lineageos/updater/misc/FileUtils.java index 3226c861..18ef5773 100644 --- a/src/org/lineageos/updater/misc/FileUtils.java +++ b/src/org/lineageos/updater/misc/FileUtils.java @@ -15,8 +15,15 @@ */ package org.lineageos.updater.misc; +import android.content.ContentResolver; +import android.database.Cursor; +import android.net.Uri; +import android.os.ParcelFileDescriptor; +import android.provider.OpenableColumns; import android.util.Log; +import androidx.annotation.NonNull; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -92,4 +99,33 @@ public class FileUtils { throw e; } } + + public static void copyFile(ContentResolver cr, File sourceFile, Uri destUri, + ProgressCallBack progressCallBack) throws IOException { + try (FileChannel sourceChannel = new FileInputStream(sourceFile).getChannel(); + ParcelFileDescriptor pfd = cr.openFileDescriptor(destUri, "w"); + FileChannel destChannel = new FileOutputStream(pfd.getFileDescriptor()).getChannel()) { + if (progressCallBack != null) { + ReadableByteChannel readableByteChannel = new CallbackByteChannel(sourceChannel, + sourceFile.length(), progressCallBack); + destChannel.transferFrom(readableByteChannel, 0, sourceChannel.size()); + } else { + destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); + } + } catch (IOException e) { + Log.e(TAG, "Could not copy file", e); + throw e; + } + } + + public static String queryName(@NonNull ContentResolver resolver, Uri uri) { + try (Cursor returnCursor = resolver.query(uri, null, null, null, null)) { + returnCursor.moveToFirst(); + int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); + return returnCursor.getString(nameIndex); + } catch (Exception e) { + // ignore + return null; + } + } } diff --git a/src/org/lineageos/updater/misc/PermissionsUtils.java b/src/org/lineageos/updater/misc/PermissionsUtils.java deleted file mode 100644 index aeaf0dc4..00000000 --- a/src/org/lineageos/updater/misc/PermissionsUtils.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2017 The LineageOS Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.lineageos.updater.misc; - -import android.Manifest; -import android.app.Activity; -import android.content.Context; -import android.content.pm.PackageManager; - -import java.util.ArrayList; -import java.util.List; - -public class PermissionsUtils { - - public static boolean hasPermission(Context context, String permission) { - int permissionState = context.checkSelfPermission(permission); - return permissionState == PackageManager.PERMISSION_GRANTED; - } - - /** - * Check the given permissions and requests them if needed. - * - * @param activity The target activity - * @param permissions The permissions to check - * @param requestCode @see Activity#requestPermissions(String[] , int) - * @return true if the permission is granted, false otherwise - */ - public static boolean checkAndRequestPermissions(final Activity activity, - final String[] permissions, final int requestCode) { - List permissionsList = new ArrayList<>(); - for (String permission : permissions) { - if (!hasPermission(activity, permission)) { - permissionsList.add(permission); - } - } - if (permissionsList.size() == 0) { - return true; - } else { - String[] permissionsArray = new String[permissionsList.size()]; - permissionsArray = permissionsList.toArray(permissionsArray); - activity.requestPermissions(permissionsArray, requestCode); - return false; - } - } - - /** - * Check and request the write external storage permission - * - * @see #checkAndRequestPermissions(Activity, String[], int) - */ - public static boolean checkAndRequestStoragePermission(Activity activity, int requestCode) { - return checkAndRequestPermissions(activity, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - requestCode); - } -}