Reconstruct the init process to make sure smb.conf is respected.

This fixes #18.
This commit is contained in:
Garfield Tan 2017-07-12 11:57:37 -07:00
parent 495631e724
commit 54096c3249
3 changed files with 58 additions and 37 deletions

View file

@ -17,6 +17,7 @@
package com.google.android.sambadocumentsprovider; package com.google.android.sambadocumentsprovider;
import android.support.annotation.Nullable;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.util.Log; import android.util.Log;
import com.google.android.sambadocumentsprovider.base.BiResultTask; import com.google.android.sambadocumentsprovider.base.BiResultTask;
@ -32,6 +33,10 @@ import java.util.Map.Entry;
class SambaConfiguration implements Iterable<Map.Entry<String, String>> { class SambaConfiguration implements Iterable<Map.Entry<String, String>> {
static {
System.loadLibrary("samba_client");
}
private static final String TAG = "SambaConfiguration"; private static final String TAG = "SambaConfiguration";
private static final String HOME_VAR = "HOME"; private static final String HOME_VAR = "HOME";
@ -48,10 +53,10 @@ class SambaConfiguration implements Iterable<Map.Entry<String, String>> {
setHomeEnv(homeFolder.getAbsolutePath()); setHomeEnv(homeFolder.getAbsolutePath());
} }
public void flushAsDefault(OnConfigurationChangedListener listener) { public void flushAsDefault() {
File smbFile = getSmbFile(mHomeFolder); File smbFile = getSmbFile(mHomeFolder);
if (!smbFile.exists()) { if (!smbFile.exists()) {
flush(listener); flush(null);
} }
} }
@ -69,7 +74,7 @@ class SambaConfiguration implements Iterable<Map.Entry<String, String>> {
new LoadTask(listener).execute(); new LoadTask(listener).execute();
} }
public void flush(OnConfigurationChangedListener listener) { public void flush(@Nullable OnConfigurationChangedListener listener) {
new FlushTask(listener).execute(); new FlushTask(listener).execute();
} }
@ -143,9 +148,9 @@ class SambaConfiguration implements Iterable<Map.Entry<String, String>> {
} }
private class FlushTask extends BiResultTask<Void, Void, Void> { private class FlushTask extends BiResultTask<Void, Void, Void> {
private final OnConfigurationChangedListener mListener; private final @Nullable OnConfigurationChangedListener mListener;
private FlushTask(OnConfigurationChangedListener listener) { private FlushTask(@Nullable OnConfigurationChangedListener listener) {
mListener = listener; mListener = listener;
} }
@ -157,7 +162,9 @@ class SambaConfiguration implements Iterable<Map.Entry<String, String>> {
@Override @Override
public void onSucceeded(Void result) { public void onSucceeded(Void result) {
mListener.onConfigurationChanged(); if (mListener != null) {
mListener.onConfigurationChanged();
}
} }
} }

View file

@ -35,41 +35,41 @@ public class SambaProviderApplication extends Application {
private final DocumentCache mCache = new DocumentCache(); private final DocumentCache mCache = new DocumentCache();
private final TaskManager mTaskManager = new TaskManager(); private final TaskManager mTaskManager = new TaskManager();
private final SmbFacade mSambaClient;
private final CredentialCache mCredentialCache; private SmbFacade mSambaClient;
private CredentialCache mCredentialCache;
private SambaConfiguration mSambaConf; private SambaConfiguration mSambaConf;
private ShareManager mShareManager; private ShareManager mShareManager;
public SambaProviderApplication() {
final SambaMessageLooper looper = new SambaMessageLooper();
mCredentialCache = looper.getCredentialCache();
mSambaClient = looper.getClient();
}
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
final ConnectivityManager manager = init(this);
(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();
} }
private void initializeSambaConf() { public static void init(Context context) {
mSambaConf = new SambaConfiguration(getDir("home", MODE_PRIVATE)); ((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 // 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. // 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. // Urge from users to disable SMB1 by default.
mSambaConf.addConfiguration("client min protocol", "SMB2"); mSambaConf.addConfiguration("client min protocol", "SMB2");
mSambaConf.addConfiguration("client max protocol", "SMB3"); mSambaConf.addConfiguration("client max protocol", "SMB3");
mSambaConf.flushAsDefault(new OnConfigurationChangedListener() { mSambaConf.flushAsDefault();
@Override }
public void onConfigurationChanged() {
mSambaClient.reset(); 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) { public static ShareManager getServerManager(Context context) {

View file

@ -145,6 +145,8 @@ public class SambaDocumentsProvider extends DocumentsProvider {
@Override @Override
public boolean onCreate() { public boolean onCreate() {
final Context context = getContext(); final Context context = getContext();
SambaProviderApplication.init(getContext());
mClient = SambaProviderApplication.getSambaClient(context); mClient = SambaProviderApplication.getSambaClient(context);
mCache = SambaProviderApplication.getDocumentCache(context); mCache = SambaProviderApplication.getDocumentCache(context);
mTaskManager = SambaProviderApplication.getTaskManager(context); mTaskManager = SambaProviderApplication.getTaskManager(context);