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;
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<Map.Entry<String, String>> {
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<Map.Entry<String, String>> {
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<Map.Entry<String, String>> {
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<Map.Entry<String, String>> {
}
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;
}
@ -157,9 +162,11 @@ class SambaConfiguration implements Iterable<Map.Entry<String, String>> {
@Override
public void onSucceeded(Void result) {
if (mListener != null) {
mListener.onConfigurationChanged();
}
}
}
interface OnConfigurationChangedListener {
void onConfigurationChanged();

View file

@ -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() {
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 onConfigurationChanged() {
public void onAvailable(Network network) {
mSambaClient.reset();
}
});
}
public static ShareManager getServerManager(Context context) {

View file

@ -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);