44
55package io .flutter .plugins .pathprovider ;
66
7+ import android .content .Context ;
78import android .os .Build .VERSION ;
89import android .os .Build .VERSION_CODES ;
910import androidx .annotation .NonNull ;
11+ import io .flutter .embedding .engine .plugins .FlutterPlugin ;
1012import io .flutter .plugin .common .MethodCall ;
1113import io .flutter .plugin .common .MethodChannel ;
1214import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
1719import java .util .ArrayList ;
1820import java .util .List ;
1921
20- public class PathProviderPlugin implements MethodCallHandler {
22+ public class PathProviderPlugin implements FlutterPlugin , MethodCallHandler {
2123
22- private final Registrar mRegistrar ;
24+ private Context context ;
25+ private MethodChannel channel ;
26+
27+ public PathProviderPlugin () {}
2328
2429 public static void registerWith (Registrar registrar ) {
25- MethodChannel channel =
26- new MethodChannel (registrar .messenger (), "plugins.flutter.io/path_provider" );
27- PathProviderPlugin instance = new PathProviderPlugin ( registrar );
28- channel .setMethodCallHandler (instance );
30+ PathProviderPlugin instance = new PathProviderPlugin ();
31+ instance . channel = new MethodChannel (registrar .messenger (), "plugins.flutter.io/path_provider" );
32+ instance . context = registrar . context ( );
33+ instance . channel .setMethodCallHandler (instance );
2934 }
3035
31- private PathProviderPlugin (Registrar registrar ) {
32- this .mRegistrar = registrar ;
36+ @ Override
37+ public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
38+ channel =
39+ new MethodChannel (
40+ binding .getFlutterEngine ().getDartExecutor (), "plugins.flutter.io/path_provider" );
41+ context = binding .getApplicationContext ();
42+ channel .setMethodCallHandler (this );
43+ }
44+
45+ @ Override
46+ public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
47+ channel .setMethodCallHandler (null );
48+ channel = null ;
3349 }
3450
3551 @ Override
@@ -61,19 +77,19 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
6177 }
6278
6379 private String getPathProviderTemporaryDirectory () {
64- return mRegistrar . context () .getCacheDir ().getPath ();
80+ return context .getCacheDir ().getPath ();
6581 }
6682
6783 private String getApplicationSupportDirectory () {
68- return PathUtils .getFilesDir (mRegistrar . context () );
84+ return PathUtils .getFilesDir (context );
6985 }
7086
7187 private String getPathProviderApplicationDocumentsDirectory () {
72- return PathUtils .getDataDirectory (mRegistrar . context () );
88+ return PathUtils .getDataDirectory (context );
7389 }
7490
7591 private String getPathProviderStorageDirectory () {
76- final File dir = mRegistrar . context () .getExternalFilesDir (null );
92+ final File dir = context .getExternalFilesDir (null );
7793 if (dir == null ) {
7894 return null ;
7995 }
@@ -84,13 +100,13 @@ private List<String> getPathProviderExternalCacheDirectories() {
84100 final List <String > paths = new ArrayList <>();
85101
86102 if (VERSION .SDK_INT >= VERSION_CODES .KITKAT ) {
87- for (File dir : mRegistrar . context () .getExternalCacheDirs ()) {
103+ for (File dir : context .getExternalCacheDirs ()) {
88104 if (dir != null ) {
89105 paths .add (dir .getAbsolutePath ());
90106 }
91107 }
92108 } else {
93- File dir = mRegistrar . context () .getExternalCacheDir ();
109+ File dir = context .getExternalCacheDir ();
94110 if (dir != null ) {
95111 paths .add (dir .getAbsolutePath ());
96112 }
@@ -103,13 +119,13 @@ private List<String> getPathProviderExternalStorageDirectories(String type) {
103119 final List <String > paths = new ArrayList <>();
104120
105121 if (VERSION .SDK_INT >= VERSION_CODES .KITKAT ) {
106- for (File dir : mRegistrar . context () .getExternalFilesDirs (type )) {
122+ for (File dir : context .getExternalFilesDirs (type )) {
107123 if (dir != null ) {
108124 paths .add (dir .getAbsolutePath ());
109125 }
110126 }
111127 } else {
112- File dir = mRegistrar . context () .getExternalFilesDir (type );
128+ File dir = context .getExternalFilesDir (type );
113129 if (dir != null ) {
114130 paths .add (dir .getAbsolutePath ());
115131 }
0 commit comments