Skip to content

Commit

Permalink
[path_provider] Clean up Java code (flutter#8240)
Browse files Browse the repository at this point in the history
Minor cleanup in the native implementation code:
- Removes a utility to map from index integers to directories, which hasn't been used since the Pigeon conversion but was accidentally left.
- Inlines all the implementations of path getters; many methods were pointlessly delegating their implementation to another private method, which is a relic of the pre-Pigeon structure.

All of the method implementations were moved without any changes.
  • Loading branch information
stuartmorgan authored Dec 9, 2024
1 parent ebe5367 commit 9f6d599
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 137 deletions.
4 changes: 4 additions & 0 deletions packages/path_provider/path_provider_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.15

* Removes unnecessary native code.

## 2.2.14

* Updates annotations lib to 1.9.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugins.pathprovider.Messages.PathProviderApi;
Expand Down Expand Up @@ -44,17 +45,17 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

@Override
public @Nullable String getTemporaryPath() {
return getPathProviderTemporaryDirectory();
return context.getCacheDir().getPath();
}

@Override
public @Nullable String getApplicationSupportPath() {
return getApplicationSupportDirectory();
return PathUtils.getFilesDir(context);
}

@Override
public @Nullable String getApplicationDocumentsPath() {
return getPathProviderApplicationDocumentsDirectory();
return PathUtils.getDataDirectory(context);
}

@Override
Expand All @@ -64,53 +65,38 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

@Override
public @Nullable String getExternalStoragePath() {
return getPathProviderStorageDirectory();
}

@Override
public @NonNull List<String> getExternalCachePaths() {
return getPathProviderExternalCacheDirectories();
}

@Override
public @NonNull List<String> getExternalStoragePaths(
@NonNull Messages.StorageDirectory directory) {
return getPathProviderExternalStorageDirectories(directory);
}

private String getPathProviderTemporaryDirectory() {
return context.getCacheDir().getPath();
}

private String getApplicationSupportDirectory() {
return PathUtils.getFilesDir(context);
}

private String getPathProviderApplicationDocumentsDirectory() {
return PathUtils.getDataDirectory(context);
}

private String getPathProviderStorageDirectory() {
final File dir = context.getExternalFilesDir(null);
if (dir == null) {
return null;
}
return dir.getAbsolutePath();
}

private List<String> getPathProviderExternalCacheDirectories() {
@Override
public @NonNull List<String> getExternalCachePaths() {
final List<String> paths = new ArrayList<>();

for (File dir : context.getExternalCacheDirs()) {
if (dir != null) {
paths.add(dir.getAbsolutePath());
}
}
return paths;
}

@Override
public @NonNull List<String> getExternalStoragePaths(
@NonNull Messages.StorageDirectory directory) {
final List<String> paths = new ArrayList<>();
for (File dir : context.getExternalFilesDirs(getStorageDirectoryString(directory))) {
if (dir != null) {
paths.add(dir.getAbsolutePath());
}
}
return paths;
}

private String getStorageDirectoryString(@NonNull Messages.StorageDirectory directory) {
@VisibleForTesting
String getStorageDirectoryString(@NonNull Messages.StorageDirectory directory) {
switch (directory) {
case ROOT:
return null;
Expand Down Expand Up @@ -138,17 +124,4 @@ private String getStorageDirectoryString(@NonNull Messages.StorageDirectory dire
throw new RuntimeException("Unrecognized directory: " + directory);
}
}

private List<String> getPathProviderExternalStorageDirectories(
@NonNull Messages.StorageDirectory directory) {
final List<String> paths = new ArrayList<>();

for (File dir : context.getExternalFilesDirs(getStorageDirectoryString(directory))) {
if (dir != null) {
paths.add(dir.getAbsolutePath());
}
}

return paths;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.pathprovider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class PathProviderPluginTest {
@org.junit.Test
public void testStorageDirectoryTypeTranslation() {
final PathProviderPlugin plugin = new PathProviderPlugin();
assertNull(plugin.getStorageDirectoryString(Messages.StorageDirectory.ROOT));
assertEquals("music", plugin.getStorageDirectoryString(Messages.StorageDirectory.MUSIC));
assertEquals("podcasts", plugin.getStorageDirectoryString(Messages.StorageDirectory.PODCASTS));
assertEquals(
"ringtones", plugin.getStorageDirectoryString(Messages.StorageDirectory.RINGTONES));
assertEquals("alarms", plugin.getStorageDirectoryString(Messages.StorageDirectory.ALARMS));
assertEquals(
"notifications", plugin.getStorageDirectoryString(Messages.StorageDirectory.NOTIFICATIONS));
assertEquals("pictures", plugin.getStorageDirectoryString(Messages.StorageDirectory.PICTURES));
assertEquals("movies", plugin.getStorageDirectoryString(Messages.StorageDirectory.MOVIES));
assertEquals(
"downloads", plugin.getStorageDirectoryString(Messages.StorageDirectory.DOWNLOADS));
assertEquals("dcim", plugin.getStorageDirectoryString(Messages.StorageDirectory.DCIM));
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion packages/path_provider/path_provider_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: path_provider_android
description: Android implementation of the path_provider plugin.
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
version: 2.2.14
version: 2.2.15

environment:
sdk: ^3.5.0
Expand Down

0 comments on commit 9f6d599

Please sign in to comment.