1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-30 00:01:19 +00:00

Added SyncthingActivity to avoid code duplication.

This commit is contained in:
Felix Ableitner 2014-08-17 02:25:50 +02:00
parent 76e87932f9
commit 49ec0734f5
10 changed files with 148 additions and 184 deletions

View file

@ -6,11 +6,9 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -25,7 +23,6 @@ import android.widget.TextView;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
@ -34,7 +31,7 @@ import java.util.Arrays;
/** /**
* Activity that allows selecting a directory in the local file system. * Activity that allows selecting a directory in the local file system.
*/ */
public class FolderPickerActivity extends ActionBarActivity public class FolderPickerActivity extends SyncthingActivity
implements AdapterView.OnItemClickListener, SyncthingService.OnApiChangeListener { implements AdapterView.OnItemClickListener, SyncthingService.OnApiChangeListener {
private static final String TAG = "FolderPickerActivity"; private static final String TAG = "FolderPickerActivity";
@ -49,21 +46,6 @@ public class FolderPickerActivity extends ActionBarActivity
private File mLocation; private File mLocation;
private SyncthingService mSyncthingService;
private final ServiceConnection mSyncthingServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
mSyncthingService = binder.getService();
mSyncthingService.registerOnApiChangeListener(FolderPickerActivity.this);
}
public void onServiceDisconnected(ComponentName className) {
mSyncthingService = null;
}
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -79,15 +61,12 @@ public class FolderPickerActivity extends ActionBarActivity
mLocation = new File(getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY)); mLocation = new File(getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY));
refresh(); refresh();
bindService(new Intent(this, SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
} }
@Override @Override
public void onDestroy() { public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
super.onDestroy(); super.onServiceConnected(componentName, iBinder);
unbindService(mSyncthingServiceConnection); getService().registerOnApiChangeListener(this);
} }
@Override @Override

View file

@ -3,10 +3,8 @@ package com.nutomic.syncthingandroid.activities;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
@ -22,7 +20,6 @@ import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab; import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener; import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -33,35 +30,15 @@ import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.fragments.LocalNodeInfoFragment; import com.nutomic.syncthingandroid.fragments.LocalNodeInfoFragment;
import com.nutomic.syncthingandroid.fragments.NodesFragment; import com.nutomic.syncthingandroid.fragments.NodesFragment;
import com.nutomic.syncthingandroid.fragments.ReposFragment; import com.nutomic.syncthingandroid.fragments.ReposFragment;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
/** /**
* Shows {@link com.nutomic.syncthingandroid.fragments.ReposFragment} and {@link com.nutomic.syncthingandroid.fragments.NodesFragment} in different tabs, and * Shows {@link com.nutomic.syncthingandroid.fragments.ReposFragment} and {@link com.nutomic.syncthingandroid.fragments.NodesFragment} in different tabs, and
* {@link com.nutomic.syncthingandroid.fragments.LocalNodeInfoFragment} in the navigation drawer. * {@link com.nutomic.syncthingandroid.fragments.LocalNodeInfoFragment} in the navigation drawer.
*/ */
public class MainActivity extends ActionBarActivity public class MainActivity extends SyncthingActivity
implements SyncthingService.OnApiChangeListener { implements SyncthingService.OnApiChangeListener {
private SyncthingService mSyncthingService;
private final ServiceConnection mSyncthingServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
mSyncthingService = binder.getService();
mSyncthingService.registerOnApiChangeListener(MainActivity.this);
mSyncthingService.registerOnApiChangeListener(mRepositoriesFragment);
mSyncthingService.registerOnApiChangeListener(mNodesFragment);
mSyncthingService.registerOnApiChangeListener(mLocalNodeInfoFragment);
}
public void onServiceDisconnected(ComponentName className) {
mSyncthingService = null;
}
};
private AlertDialog mLoadingDialog; private AlertDialog mLoadingDialog;
/** /**
@ -84,7 +61,7 @@ public class MainActivity extends ActionBarActivity
LayoutInflater inflater = getLayoutInflater(); LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.loading_dialog, null); View dialogLayout = inflater.inflate(R.layout.loading_dialog, null);
TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text); TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text);
loadingText.setText((mSyncthingService.isFirstStart()) loadingText.setText((getService().isFirstStart())
? R.string.web_gui_creating_key ? R.string.web_gui_creating_key
: R.string.api_loading); : R.string.api_loading);
@ -207,28 +184,30 @@ public class MainActivity extends ActionBarActivity
mLocalNodeInfoFragment = new LocalNodeInfoFragment(); mLocalNodeInfoFragment = new LocalNodeInfoFragment();
} }
getApplicationContext().startService(
new Intent(this, SyncthingService.class));
bindService(new Intent(this, SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
getSupportFragmentManager() getSupportFragmentManager()
.beginTransaction() .beginTransaction()
.replace(R.id.drawer, mLocalNodeInfoFragment) .replace(R.id.drawer, mLocalNodeInfoFragment)
.commit(); .commit();
mDrawerToggle = mLocalNodeInfoFragment.new Toggle(this, mDrawerLayout, mDrawerToggle = mLocalNodeInfoFragment.new Toggle(this, mDrawerLayout,
R.drawable.ic_drawer); R.drawable.ic_drawer);
mDrawerLayout.setDrawerListener(mDrawerToggle);
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
unbindService(mSyncthingServiceConnection);
if (mLoadingDialog != null) { if (mLoadingDialog != null) {
mLoadingDialog.dismiss(); mLoadingDialog.dismiss();
} }
} }
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
super.onServiceConnected(componentName, iBinder);
getService().registerOnApiChangeListener(mRepositoriesFragment);
getService().registerOnApiChangeListener(mNodesFragment);
}
/** /**
* Saves fragment states. * Saves fragment states.
*/ */
@ -307,13 +286,4 @@ public class MainActivity extends ActionBarActivity
mDrawerToggle.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig);
} }
/**
* Returns RestApi instance, or null if SyncthingService is not yet connected.
*/
public RestApi getApi() {
return (mSyncthingService != null)
? mSyncthingService.getApi()
: null;
}
} }

View file

@ -3,7 +3,6 @@ package com.nutomic.syncthingandroid.activities;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.fragments.NodeSettingsFragment; import com.nutomic.syncthingandroid.fragments.NodeSettingsFragment;
@ -13,7 +12,7 @@ import com.nutomic.syncthingandroid.fragments.SettingsFragment;
/** /**
* General Activity used by all PreferenceFragments. * General Activity used by all PreferenceFragments.
*/ */
public class SettingsActivity extends ActionBarActivity { public class SettingsActivity extends SyncthingActivity {
public static final String ACTION_APP_SETTINGS_FRAGMENT = "app_settings_fragment"; public static final String ACTION_APP_SETTINGS_FRAGMENT = "app_settings_fragment";

View file

@ -0,0 +1,89 @@
package com.nutomic.syncthingandroid.activities;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
import java.util.LinkedList;
/**
* Connects to {@link SyncthingService} and provides access to it.
*/
public class SyncthingActivity extends ActionBarActivity implements ServiceConnection {
private SyncthingService mSyncthingService;
private LinkedList<OnServiceConnectedListener> mServiceConnectedListeners = new LinkedList<>();
/**
* To be used for Fragments.
*/
public interface OnServiceConnectedListener {
public void onServiceConnected();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindService(new Intent(this, SyncthingService.class),
this, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(this);
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) iBinder;
mSyncthingService = binder.getService();
for (OnServiceConnectedListener listener : mServiceConnectedListeners) {
listener.onServiceConnected();
}
mServiceConnectedListeners.clear();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mSyncthingService = null;
}
/**
* Used for Fragments to use the Activity's service connection.
*/
public void registerOnServiceConnectedListener(OnServiceConnectedListener listener) {
if (mSyncthingService != null) {
listener.onServiceConnected();
}
else {
mServiceConnectedListeners.addLast(listener);
}
}
/**
* Returns service object (or null if not bound).
*/
public SyncthingService getService() {
return mSyncthingService;
}
/**
* Returns RestApi instance, or null if SyncthingService is not yet connected.
*/
public RestApi getApi() {
return (getService() != null)
? getService().getApi()
: null;
}
}

View file

@ -2,12 +2,8 @@ package com.nutomic.syncthingandroid.activities;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.view.View; import android.view.View;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
@ -15,32 +11,16 @@ import android.widget.ProgressBar;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
/** /**
* Holds a WebView that shows the web ui of the local syncthing instance. * Holds a WebView that shows the web ui of the local syncthing instance.
*/ */
public class WebGuiActivity extends ActionBarActivity implements SyncthingService.OnWebGuiAvailableListener { public class WebGuiActivity extends SyncthingActivity implements SyncthingService.OnWebGuiAvailableListener {
private WebView mWebView; private WebView mWebView;
private View mLoadingView; private View mLoadingView;
private SyncthingService mSyncthingService;
private final ServiceConnection mSyncthingServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
mSyncthingService = binder.getService();
mSyncthingService.registerOnWebGuiAvailableListener(WebGuiActivity.this);
}
public void onServiceDisconnected(ComponentName className) {
mSyncthingService = null;
}
};
/** /**
* Hides the loading screen and shows the WebView once it is fully loaded. * Hides the loading screen and shows the WebView once it is fully loaded.
*/ */
@ -73,9 +53,12 @@ public class WebGuiActivity extends ActionBarActivity implements SyncthingServic
mWebView = (WebView) findViewById(R.id.webview); mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(mWebViewClient); mWebView.setWebViewClient(mWebViewClient);
}
bindService(new Intent(this, SyncthingService.class), @Override
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE); public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
super.onServiceConnected(componentName, iBinder);
getService().registerOnWebGuiAvailableListener(WebGuiActivity.this);
} }
/** /**
@ -83,13 +66,7 @@ public class WebGuiActivity extends ActionBarActivity implements SyncthingServic
*/ */
@Override @Override
public void onWebGuiAvailable() { public void onWebGuiAvailable() {
mWebView.loadUrl(mSyncthingService.getApi().getUrl()); mWebView.loadUrl(getApi().getUrl());
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(mSyncthingServiceConnection);
} }
} }

View file

@ -26,8 +26,7 @@ import java.util.TimerTask;
* Displays information about the local node. * Displays information about the local node.
*/ */
public class LocalNodeInfoFragment extends Fragment public class LocalNodeInfoFragment extends Fragment
implements RestApi.OnReceiveSystemInfoListener, RestApi.OnReceiveConnectionsListener, implements RestApi.OnReceiveSystemInfoListener, RestApi.OnReceiveConnectionsListener {
SyncthingService.OnApiChangeListener {
private TextView mNodeId; private TextView mNodeId;
@ -99,6 +98,7 @@ public class LocalNodeInfoFragment extends Fragment
} }
private void onDrawerOpened() { private void onDrawerOpened() {
// FIXME: never called
mTimer = new Timer(); mTimer = new Timer();
mTimer.schedule(new TimerTask() { mTimer.schedule(new TimerTask() {
@Override @Override
@ -111,14 +111,6 @@ public class LocalNodeInfoFragment extends Fragment
mActivity.supportInvalidateOptionsMenu(); mActivity.supportInvalidateOptionsMenu();
} }
@Override
public void onApiChange(SyncthingService.State currentState) {
if (currentState != SyncthingService.State.ACTIVE)
return;
updateGui();
}
/** /**
* Invokes status callbacks. * Invokes status callbacks.
*/ */

View file

@ -4,13 +4,9 @@ import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
import android.preference.Preference; import android.preference.Preference;
import android.support.v4.preference.PreferenceFragment; import android.support.v4.preference.PreferenceFragment;
@ -22,9 +18,9 @@ import android.widget.Toast;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SettingsActivity;
import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,9 +29,9 @@ import java.util.Map;
* Shows node details and allows changing them. * Shows node details and allows changing them.
*/ */
public class NodeSettingsFragment extends PreferenceFragment implements public class NodeSettingsFragment extends PreferenceFragment implements
Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener, SyncthingActivity.OnServiceConnectedListener, Preference.OnPreferenceChangeListener,
RestApi.OnReceiveConnectionsListener, SyncthingService.OnApiChangeListener, Preference.OnPreferenceClickListener, RestApi.OnReceiveConnectionsListener,
RestApi.OnNodeIdNormalizedListener { SyncthingService.OnApiChangeListener, RestApi.OnNodeIdNormalizedListener {
public static final String EXTRA_NODE_ID = "node_id"; public static final String EXTRA_NODE_ID = "node_id";
@ -43,19 +39,7 @@ public class NodeSettingsFragment extends PreferenceFragment implements
private SyncthingService mSyncthingService; private SyncthingService mSyncthingService;
private final ServiceConnection mSyncthingServiceConnection = new ServiceConnection() { // FIXME: is null
public void onServiceConnected(ComponentName className, IBinder service) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
mSyncthingService = binder.getService();
mSyncthingService.registerOnApiChangeListener(NodeSettingsFragment.this);
}
public void onServiceDisconnected(ComponentName className) {
mSyncthingService = null;
}
};
private RestApi.Node mNode; private RestApi.Node mNode;
private Preference mNodeId; private Preference mNodeId;
@ -74,6 +58,8 @@ public class NodeSettingsFragment extends PreferenceFragment implements
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
((SyncthingActivity) getActivity()).registerOnServiceConnectedListener(this);
mIsCreate = ((SettingsActivity) getActivity()).getIsCreate(); mIsCreate = ((SettingsActivity) getActivity()).getIsCreate();
setHasOptionsMenu(true); setHasOptionsMenu(true);
@ -96,9 +82,12 @@ public class NodeSettingsFragment extends PreferenceFragment implements
mCurrentAddress = findPreference("current_address"); mCurrentAddress = findPreference("current_address");
mCurrentAddress.setSummary("?"); mCurrentAddress.setSummary("?");
} }
}
getActivity().bindService(new Intent(getActivity(), SyncthingService.class), @Override
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE); public void onServiceConnected() {
mSyncthingService = ((SyncthingActivity) getActivity()).getService();
mSyncthingService.registerOnApiChangeListener(this);
} }
@Override @Override
@ -191,12 +180,6 @@ public class NodeSettingsFragment extends PreferenceFragment implements
} }
} }
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unbindService(mSyncthingServiceConnection);
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object o) { public boolean onPreferenceChange(Preference preference, Object o) {
if (preference instanceof EditTextPreference) { if (preference instanceof EditTextPreference) {

View file

@ -10,6 +10,7 @@ import android.widget.ListView;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.MainActivity; import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SettingsActivity;
import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.util.NodesAdapter; import com.nutomic.syncthingandroid.util.NodesAdapter;
@ -48,7 +49,7 @@ public class NodesFragment extends ListFragment implements SyncthingService.OnAp
} }
private void initAdapter() { private void initAdapter() {
MainActivity activity = (MainActivity) getActivity(); SyncthingActivity activity = (SyncthingActivity) getActivity();
if (activity == null || activity.getApi() == null) if (activity == null || activity.getApi() == null)
return; return;

View file

@ -23,6 +23,7 @@ import android.widget.Toast;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.FolderPickerActivity; import com.nutomic.syncthingandroid.activities.FolderPickerActivity;
import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SettingsActivity;
import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder; import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
@ -35,7 +36,8 @@ import java.util.List;
* Shows repo details and allows changing them. * Shows repo details and allows changing them.
*/ */
public class RepoSettingsFragment extends PreferenceFragment public class RepoSettingsFragment extends PreferenceFragment
implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener, implements SyncthingActivity.OnServiceConnectedListener,
Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener,
SyncthingService.OnApiChangeListener { SyncthingService.OnApiChangeListener {
private static final int DIRECTORY_REQUEST_CODE = 234; private static final int DIRECTORY_REQUEST_CODE = 234;
@ -50,19 +52,7 @@ public class RepoSettingsFragment extends PreferenceFragment
private SyncthingService mSyncthingService; private SyncthingService mSyncthingService;
private final ServiceConnection mSyncthingServiceConnection = new ServiceConnection() { // FIXME: is null
public void onServiceConnected(ComponentName className, IBinder service) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
mSyncthingService = binder.getService();
mSyncthingService.registerOnApiChangeListener(RepoSettingsFragment.this);
}
public void onServiceDisconnected(ComponentName className) {
mSyncthingService = null;
}
};
private RestApi.Repo mRepo; private RestApi.Repo mRepo;
private EditTextPreference mRepoId; private EditTextPreference mRepoId;
@ -83,7 +73,9 @@ public class RepoSettingsFragment extends PreferenceFragment
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mIsCreate = ((SettingsActivity) getActivity()).getIsCreate(); SettingsActivity activity = (SettingsActivity) getActivity();
activity.registerOnServiceConnectedListener(this);
mIsCreate = activity.getIsCreate();
setHasOptionsMenu(true); setHasOptionsMenu(true);
if (mIsCreate) { if (mIsCreate) {
@ -105,9 +97,6 @@ public class RepoSettingsFragment extends PreferenceFragment
mVersioning.setOnPreferenceChangeListener(this); mVersioning.setOnPreferenceChangeListener(this);
mVersioningKeep = (EditTextPreference) findPreference("versioning_keep"); mVersioningKeep = (EditTextPreference) findPreference("versioning_keep");
mVersioningKeep.setOnPreferenceChangeListener(this); mVersioningKeep.setOnPreferenceChangeListener(this);
getActivity().bindService(new Intent(getActivity(), SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
} }
@Override @Override
@ -167,6 +156,12 @@ public class RepoSettingsFragment extends PreferenceFragment
} }
} }
@Override
public void onServiceConnected() {
mSyncthingService = ((SyncthingActivity) getActivity()).getService();
mSyncthingService.registerOnApiChangeListener(this);
}
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
@ -216,12 +211,6 @@ public class RepoSettingsFragment extends PreferenceFragment
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unbindService(mSyncthingServiceConnection);
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object o) { public boolean onPreferenceChange(Preference preference, Object o) {
if (preference instanceof EditTextPreference) { if (preference instanceof EditTextPreference) {

View file

@ -17,12 +17,14 @@ import android.text.InputType;
import android.view.MenuItem; import android.view.MenuItem;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder; import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
public class SettingsFragment extends PreferenceFragment public class SettingsFragment extends PreferenceFragment
implements SyncthingService.OnApiChangeListener, Preference.OnPreferenceChangeListener { implements SyncthingActivity.OnServiceConnectedListener,
SyncthingService.OnApiChangeListener, Preference.OnPreferenceChangeListener {
private static final String SYNCTHING_OPTIONS_KEY = "syncthing_options"; private static final String SYNCTHING_OPTIONS_KEY = "syncthing_options";
@ -42,22 +44,6 @@ public class SettingsFragment extends PreferenceFragment
private SyncthingService mSyncthingService; private SyncthingService mSyncthingService;
/**
* Binds to service and sets syncthing preferences from Rest API.
*/
private final ServiceConnection mSyncthingServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
SyncthingServiceBinder binder = (SyncthingServiceBinder) service;
mSyncthingService = binder.getService();
mSyncthingService.registerOnApiChangeListener(SettingsFragment.this);
}
public void onServiceDisconnected(ComponentName className) {
mSyncthingService = null;
}
};
@Override @Override
public void onApiChange(SyncthingService.State currentState) { public void onApiChange(SyncthingService.State currentState) {
mOptionsScreen.setEnabled(currentState == SyncthingService.State.ACTIVE); mOptionsScreen.setEnabled(currentState == SyncthingService.State.ACTIVE);
@ -110,8 +96,7 @@ public class SettingsFragment extends PreferenceFragment
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
getActivity().bindService(new Intent(getActivity(), SyncthingService.class), ((SyncthingActivity) getActivity()).registerOnServiceConnectedListener(this);
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
addPreferencesFromResource(R.xml.app_settings); addPreferencesFromResource(R.xml.app_settings);
PreferenceScreen screen = getPreferenceScreen(); PreferenceScreen screen = getPreferenceScreen();
@ -125,9 +110,9 @@ public class SettingsFragment extends PreferenceFragment
} }
@Override @Override
public void onDestroy() { public void onServiceConnected() {
super.onDestroy(); mSyncthingService = ((SyncthingActivity) getActivity()).getService();
getActivity().unbindService(mSyncthingServiceConnection); mSyncthingService.registerOnApiChangeListener(this);
} }
/** /**