diff --git a/app/src/main/java/com/google/android/sambadocumentsprovider/SambaConfiguration.java b/app/src/main/java/com/google/android/sambadocumentsprovider/SambaConfiguration.java index 6f525d4..1c45989 100644 --- a/app/src/main/java/com/google/android/sambadocumentsprovider/SambaConfiguration.java +++ b/app/src/main/java/com/google/android/sambadocumentsprovider/SambaConfiguration.java @@ -17,6 +17,7 @@ package com.google.android.sambadocumentsprovider; +import android.support.annotation.Nullable; import android.system.ErrnoException; import android.util.Log; import com.google.android.sambadocumentsprovider.base.BiResultTask; @@ -32,6 +33,10 @@ import java.util.Map.Entry; class SambaConfiguration implements Iterable> { + static { + System.loadLibrary("samba_client"); + } + private static final String TAG = "SambaConfiguration"; private static final String HOME_VAR = "HOME"; @@ -48,10 +53,10 @@ class SambaConfiguration implements Iterable> { setHomeEnv(homeFolder.getAbsolutePath()); } - public void flushAsDefault(OnConfigurationChangedListener listener) { + public void flushAsDefault() { File smbFile = getSmbFile(mHomeFolder); if (!smbFile.exists()) { - flush(listener); + flush(null); } } @@ -69,7 +74,7 @@ class SambaConfiguration implements Iterable> { new LoadTask(listener).execute(); } - public void flush(OnConfigurationChangedListener listener) { + public void flush(@Nullable OnConfigurationChangedListener listener) { new FlushTask(listener).execute(); } @@ -143,9 +148,9 @@ class SambaConfiguration implements Iterable> { } private class FlushTask extends BiResultTask { - private final OnConfigurationChangedListener mListener; + private final @Nullable OnConfigurationChangedListener mListener; - private FlushTask(OnConfigurationChangedListener listener) { + private FlushTask(@Nullable OnConfigurationChangedListener listener) { mListener = listener; } @@ -157,7 +162,9 @@ class SambaConfiguration implements Iterable> { @Override public void onSucceeded(Void result) { - mListener.onConfigurationChanged(); + if (mListener != null) { + mListener.onConfigurationChanged(); + } } } diff --git a/app/src/main/java/com/google/android/sambadocumentsprovider/SambaProviderApplication.java b/app/src/main/java/com/google/android/sambadocumentsprovider/SambaProviderApplication.java index a610557..8a97d43 100644 --- a/app/src/main/java/com/google/android/sambadocumentsprovider/SambaProviderApplication.java +++ b/app/src/main/java/com/google/android/sambadocumentsprovider/SambaProviderApplication.java @@ -35,41 +35,41 @@ public class SambaProviderApplication extends Application { private final DocumentCache mCache = new DocumentCache(); private final TaskManager mTaskManager = new TaskManager(); - private final SmbFacade mSambaClient; - private final CredentialCache mCredentialCache; + + private SmbFacade mSambaClient; + private CredentialCache mCredentialCache; private SambaConfiguration mSambaConf; private ShareManager mShareManager; - public SambaProviderApplication() { - final SambaMessageLooper looper = new SambaMessageLooper(); - mCredentialCache = looper.getCredentialCache(); - mSambaClient = looper.getClient(); - } - @Override public void onCreate() { super.onCreate(); - final ConnectivityManager manager = - (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); - manager.registerNetworkCallback( - new NetworkRequest.Builder() - .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) - .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET) - .build(), - new NetworkCallback() { - @Override - public void onAvailable(Network network) { - mSambaClient.reset(); - } - }); - - initializeSambaConf(); + init(this); } - private void initializeSambaConf() { - mSambaConf = new SambaConfiguration(getDir("home", MODE_PRIVATE)); + public static void init(Context context) { + ((SambaProviderApplication) context.getApplicationContext()).initialize(context); + } + + private void initialize(Context context) { + if (mSambaClient != null) { + // Already initialized. + return; + } + + initializeSambaConf(context); + + final SambaMessageLooper looper = new SambaMessageLooper(); + mCredentialCache = looper.getCredentialCache(); + mSambaClient = looper.getClient(); + + registerNetworkCallback(context); + } + + private void initializeSambaConf(Context context) { + mSambaConf = new SambaConfiguration(context.getDir("home", MODE_PRIVATE)); // lmhosts are not used in SambaDocumentsProvider and prioritize bcast because sometimes in home // settings DNS will resolve unknown domain name to a specific IP for advertisement. @@ -83,12 +83,24 @@ public class SambaProviderApplication extends Application { // Urge from users to disable SMB1 by default. mSambaConf.addConfiguration("client min protocol", "SMB2"); mSambaConf.addConfiguration("client max protocol", "SMB3"); - mSambaConf.flushAsDefault(new OnConfigurationChangedListener() { - @Override - public void onConfigurationChanged() { - mSambaClient.reset(); - } - }); + mSambaConf.flushAsDefault(); + } + + private void registerNetworkCallback(Context context) { + final ConnectivityManager manager = + (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); + manager.registerNetworkCallback( + new NetworkRequest.Builder() + .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) + .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET) + .build(), + new NetworkCallback() { + @Override + public void onAvailable(Network network) { + mSambaClient.reset(); + } + }); + } public static ShareManager getServerManager(Context context) { diff --git a/app/src/main/java/com/google/android/sambadocumentsprovider/provider/SambaDocumentsProvider.java b/app/src/main/java/com/google/android/sambadocumentsprovider/provider/SambaDocumentsProvider.java index 5e26bf7..bacb216 100644 --- a/app/src/main/java/com/google/android/sambadocumentsprovider/provider/SambaDocumentsProvider.java +++ b/app/src/main/java/com/google/android/sambadocumentsprovider/provider/SambaDocumentsProvider.java @@ -145,6 +145,8 @@ public class SambaDocumentsProvider extends DocumentsProvider { @Override public boolean onCreate() { final Context context = getContext(); + SambaProviderApplication.init(getContext()); + mClient = SambaProviderApplication.getSambaClient(context); mCache = SambaProviderApplication.getDocumentCache(context); mTaskManager = SambaProviderApplication.getTaskManager(context);