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.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@ -25,7 +23,6 @@ import android.widget.TextView;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
import java.io.File;
import java.io.FileFilter;
@ -34,7 +31,7 @@ import java.util.Arrays;
/**
* 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 {
private static final String TAG = "FolderPickerActivity";
@ -49,21 +46,6 @@ public class FolderPickerActivity extends ActionBarActivity
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -79,15 +61,12 @@ public class FolderPickerActivity extends ActionBarActivity
mLocation = new File(getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY));
refresh();
bindService(new Intent(this, SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(mSyncthingServiceConnection);
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
super.onServiceConnected(componentName, iBinder);
getService().registerOnApiChangeListener(this);
}
@Override

View file

@ -3,10 +3,8 @@ package com.nutomic.syncthingandroid.activities;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.res.Configuration;
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.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@ -33,35 +30,15 @@ import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.fragments.LocalNodeInfoFragment;
import com.nutomic.syncthingandroid.fragments.NodesFragment;
import com.nutomic.syncthingandroid.fragments.ReposFragment;
import com.nutomic.syncthingandroid.syncthing.RestApi;
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
* {@link com.nutomic.syncthingandroid.fragments.LocalNodeInfoFragment} in the navigation drawer.
*/
public class MainActivity extends ActionBarActivity
public class MainActivity extends SyncthingActivity
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;
/**
@ -84,7 +61,7 @@ public class MainActivity extends ActionBarActivity
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.loading_dialog, null);
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.api_loading);
@ -207,28 +184,30 @@ public class MainActivity extends ActionBarActivity
mLocalNodeInfoFragment = new LocalNodeInfoFragment();
}
getApplicationContext().startService(
new Intent(this, SyncthingService.class));
bindService(new Intent(this, SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.drawer, mLocalNodeInfoFragment)
.commit();
mDrawerToggle = mLocalNodeInfoFragment.new Toggle(this, mDrawerLayout,
R.drawable.ic_drawer);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(mSyncthingServiceConnection);
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
}
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
super.onServiceConnected(componentName, iBinder);
getService().registerOnApiChangeListener(mRepositoriesFragment);
getService().registerOnApiChangeListener(mNodesFragment);
}
/**
* Saves fragment states.
*/
@ -307,13 +286,4 @@ public class MainActivity extends ActionBarActivity
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.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.fragments.NodeSettingsFragment;
@ -13,7 +12,7 @@ import com.nutomic.syncthingandroid.fragments.SettingsFragment;
/**
* 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";

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.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 android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@ -15,32 +11,16 @@ import android.widget.ProgressBar;
import com.nutomic.syncthingandroid.R;
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.
*/
public class WebGuiActivity extends ActionBarActivity implements SyncthingService.OnWebGuiAvailableListener {
public class WebGuiActivity extends SyncthingActivity implements SyncthingService.OnWebGuiAvailableListener {
private WebView mWebView;
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.
*/
@ -73,9 +53,12 @@ public class WebGuiActivity extends ActionBarActivity implements SyncthingServic
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(mWebViewClient);
}
bindService(new Intent(this, SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
@Override
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
public void onWebGuiAvailable() {
mWebView.loadUrl(mSyncthingService.getApi().getUrl());
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(mSyncthingServiceConnection);
mWebView.loadUrl(getApi().getUrl());
}
}

View file

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

View file

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

View file

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

View file

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

View file

@ -17,12 +17,14 @@ import android.text.InputType;
import android.view.MenuItem;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.syncthing.SyncthingServiceBinder;
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";
@ -42,22 +44,6 @@ public class SettingsFragment extends PreferenceFragment
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
public void onApiChange(SyncthingService.State currentState) {
mOptionsScreen.setEnabled(currentState == SyncthingService.State.ACTIVE);
@ -110,8 +96,7 @@ public class SettingsFragment extends PreferenceFragment
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().bindService(new Intent(getActivity(), SyncthingService.class),
mSyncthingServiceConnection, Context.BIND_AUTO_CREATE);
((SyncthingActivity) getActivity()).registerOnServiceConnectedListener(this);
addPreferencesFromResource(R.xml.app_settings);
PreferenceScreen screen = getPreferenceScreen();
@ -125,9 +110,9 @@ public class SettingsFragment extends PreferenceFragment
}
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unbindService(mSyncthingServiceConnection);
public void onServiceConnected() {
mSyncthingService = ((SyncthingActivity) getActivity()).getService();
mSyncthingService.registerOnApiChangeListener(this);
}
/**