Skip to content

Commit 9f6d599

Browse files
[path_provider] Clean up Java code (#8240)
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.
1 parent ebe5367 commit 9f6d599

File tree

6 files changed

+52
-137
lines changed

6 files changed

+52
-137
lines changed

packages/path_provider/path_provider_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.2.15
2+
3+
* Removes unnecessary native code.
4+
15
## 2.2.14
26

37
* Updates annotations lib to 1.9.1.

packages/path_provider/path_provider_android/android/src/main/java/io/flutter/plugins/pathprovider/PathProviderPlugin.java

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.util.Log;
99
import androidx.annotation.NonNull;
1010
import androidx.annotation.Nullable;
11+
import androidx.annotation.VisibleForTesting;
1112
import io.flutter.embedding.engine.plugins.FlutterPlugin;
1213
import io.flutter.plugin.common.BinaryMessenger;
1314
import io.flutter.plugins.pathprovider.Messages.PathProviderApi;
@@ -44,17 +45,17 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
4445

4546
@Override
4647
public @Nullable String getTemporaryPath() {
47-
return getPathProviderTemporaryDirectory();
48+
return context.getCacheDir().getPath();
4849
}
4950

5051
@Override
5152
public @Nullable String getApplicationSupportPath() {
52-
return getApplicationSupportDirectory();
53+
return PathUtils.getFilesDir(context);
5354
}
5455

5556
@Override
5657
public @Nullable String getApplicationDocumentsPath() {
57-
return getPathProviderApplicationDocumentsDirectory();
58+
return PathUtils.getDataDirectory(context);
5859
}
5960

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

6566
@Override
6667
public @Nullable String getExternalStoragePath() {
67-
return getPathProviderStorageDirectory();
68-
}
69-
70-
@Override
71-
public @NonNull List<String> getExternalCachePaths() {
72-
return getPathProviderExternalCacheDirectories();
73-
}
74-
75-
@Override
76-
public @NonNull List<String> getExternalStoragePaths(
77-
@NonNull Messages.StorageDirectory directory) {
78-
return getPathProviderExternalStorageDirectories(directory);
79-
}
80-
81-
private String getPathProviderTemporaryDirectory() {
82-
return context.getCacheDir().getPath();
83-
}
84-
85-
private String getApplicationSupportDirectory() {
86-
return PathUtils.getFilesDir(context);
87-
}
88-
89-
private String getPathProviderApplicationDocumentsDirectory() {
90-
return PathUtils.getDataDirectory(context);
91-
}
92-
93-
private String getPathProviderStorageDirectory() {
9468
final File dir = context.getExternalFilesDir(null);
9569
if (dir == null) {
9670
return null;
9771
}
9872
return dir.getAbsolutePath();
9973
}
10074

101-
private List<String> getPathProviderExternalCacheDirectories() {
75+
@Override
76+
public @NonNull List<String> getExternalCachePaths() {
10277
final List<String> paths = new ArrayList<>();
103-
10478
for (File dir : context.getExternalCacheDirs()) {
10579
if (dir != null) {
10680
paths.add(dir.getAbsolutePath());
10781
}
10882
}
83+
return paths;
84+
}
10985

86+
@Override
87+
public @NonNull List<String> getExternalStoragePaths(
88+
@NonNull Messages.StorageDirectory directory) {
89+
final List<String> paths = new ArrayList<>();
90+
for (File dir : context.getExternalFilesDirs(getStorageDirectoryString(directory))) {
91+
if (dir != null) {
92+
paths.add(dir.getAbsolutePath());
93+
}
94+
}
11095
return paths;
11196
}
11297

113-
private String getStorageDirectoryString(@NonNull Messages.StorageDirectory directory) {
98+
@VisibleForTesting
99+
String getStorageDirectoryString(@NonNull Messages.StorageDirectory directory) {
114100
switch (directory) {
115101
case ROOT:
116102
return null;
@@ -138,17 +124,4 @@ private String getStorageDirectoryString(@NonNull Messages.StorageDirectory dire
138124
throw new RuntimeException("Unrecognized directory: " + directory);
139125
}
140126
}
141-
142-
private List<String> getPathProviderExternalStorageDirectories(
143-
@NonNull Messages.StorageDirectory directory) {
144-
final List<String> paths = new ArrayList<>();
145-
146-
for (File dir : context.getExternalFilesDirs(getStorageDirectoryString(directory))) {
147-
if (dir != null) {
148-
paths.add(dir.getAbsolutePath());
149-
}
150-
}
151-
152-
return paths;
153-
}
154127
}

packages/path_provider/path_provider_android/android/src/main/java/io/flutter/plugins/pathprovider/StorageDirectoryMapper.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.pathprovider;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNull;
9+
10+
public class PathProviderPluginTest {
11+
@org.junit.Test
12+
public void testStorageDirectoryTypeTranslation() {
13+
final PathProviderPlugin plugin = new PathProviderPlugin();
14+
assertNull(plugin.getStorageDirectoryString(Messages.StorageDirectory.ROOT));
15+
assertEquals("music", plugin.getStorageDirectoryString(Messages.StorageDirectory.MUSIC));
16+
assertEquals("podcasts", plugin.getStorageDirectoryString(Messages.StorageDirectory.PODCASTS));
17+
assertEquals(
18+
"ringtones", plugin.getStorageDirectoryString(Messages.StorageDirectory.RINGTONES));
19+
assertEquals("alarms", plugin.getStorageDirectoryString(Messages.StorageDirectory.ALARMS));
20+
assertEquals(
21+
"notifications", plugin.getStorageDirectoryString(Messages.StorageDirectory.NOTIFICATIONS));
22+
assertEquals("pictures", plugin.getStorageDirectoryString(Messages.StorageDirectory.PICTURES));
23+
assertEquals("movies", plugin.getStorageDirectoryString(Messages.StorageDirectory.MOVIES));
24+
assertEquals(
25+
"downloads", plugin.getStorageDirectoryString(Messages.StorageDirectory.DOWNLOADS));
26+
assertEquals("dcim", plugin.getStorageDirectoryString(Messages.StorageDirectory.DCIM));
27+
}
28+
}

packages/path_provider/path_provider_android/android/src/test/java/io/flutter/plugins/pathprovider/StorageDirectoryMapperTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/path_provider/path_provider_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: path_provider_android
22
description: Android implementation of the path_provider plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
5-
version: 2.2.14
5+
version: 2.2.15
66

77
environment:
88
sdk: ^3.5.0

0 commit comments

Comments
 (0)