diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java index 111ee38b..b00fe8f9 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java @@ -24,6 +24,8 @@ import com.google.zxing.integration.android.IntentResult; import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SyncthingActivity; +import com.nutomic.syncthingandroid.model.Connection; +import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.util.Compression; @@ -59,7 +61,7 @@ public class DeviceFragment extends Fragment implements private SyncthingService mSyncthingService; - private RestApi.Device mDevice; + private Device mDevice; private View mIdContainer; @@ -157,7 +159,7 @@ public class DeviceFragment extends Fragment implements if (mIsCreateMode) { if (savedInstanceState != null) { - mDevice = (RestApi.Device) savedInstanceState.getSerializable("device"); + mDevice = (Device) savedInstanceState.getSerializable("device"); } if (mDevice == null) { initDevice(); @@ -244,7 +246,7 @@ public class DeviceFragment extends Fragment implements * version/address changes. */ @Override - public void onReceiveConnections(Map connections) { + public void onReceiveConnections(Map connections) { boolean viewsExist = mSyncthingVersionView != null && mCurrentAddressView != null; if (viewsExist && connections.containsKey(mDevice.deviceID)) { mCurrentAddressView.setVisibility(VISIBLE); @@ -262,9 +264,9 @@ public class DeviceFragment extends Fragment implements } if (!mIsCreateMode) { - List devices = mSyncthingService.getApi().getDevices(false); + List devices = mSyncthingService.getApi().getDevices(false); mDevice = null; - for (RestApi.Device device : devices) { + for (Device device : devices) { if (device.deviceID.equals( getActivity().getIntent().getStringExtra(EXTRA_DEVICE_ID))) { mDevice = device; @@ -365,7 +367,7 @@ public class DeviceFragment extends Fragment implements } private void initDevice() { - mDevice = new RestApi.Device(); + mDevice = new Device(); mDevice.name = ""; mDevice.deviceID = getActivity().getIntent().getStringExtra(EXTRA_DEVICE_ID); mDevice.addresses = DYNAMIC_ADDRESS; diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java index b560d3fd..aba9ab27 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java @@ -13,6 +13,7 @@ import android.widget.ListView; import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SyncthingActivity; +import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.util.DevicesAdapter; @@ -29,7 +30,7 @@ import java.util.TimerTask; public class DeviceListFragment extends ListFragment implements SyncthingService.OnApiChangeListener, ListView.OnItemClickListener { - private final static Comparator DEVICES_COMPARATOR = (lhs, rhs) -> lhs.name.compareTo(rhs.name); + private final static Comparator DEVICES_COMPARATOR = (lhs, rhs) -> lhs.name.compareTo(rhs.name); private DevicesAdapter mAdapter; @@ -97,7 +98,7 @@ public class DeviceListFragment extends ListFragment implements SyncthingService // Prevent scroll position reset due to list update from clear(). mAdapter.setNotifyOnChange(false); mAdapter.clear(); - List devices = activity.getApi().getDevices(false); + List devices = activity.getApi().getDevices(false); Collections.sort(devices, DEVICES_COMPARATOR); mAdapter.addAll(devices); mAdapter.updateConnections(activity.getApi()); diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java index f506ba39..e2c53406 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DrawerFragment.java @@ -13,6 +13,9 @@ import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.activities.MainActivity; import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.WebGuiActivity; +import com.nutomic.syncthingandroid.model.Connection; +import com.nutomic.syncthingandroid.model.SystemInfo; +import com.nutomic.syncthingandroid.model.SystemVersion; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.util.Util; @@ -152,7 +155,7 @@ public class DrawerFragment extends Fragment implements RestApi.OnReceiveSystemI * Populates views with status received via {@link RestApi#getSystemInfo}. */ @Override - public void onReceiveSystemInfo(RestApi.SystemInfo info) { + public void onReceiveSystemInfo(SystemInfo info) { if (getActivity() == null) return; @@ -176,7 +179,7 @@ public class DrawerFragment extends Fragment implements RestApi.OnReceiveSystemI * Populates views with status received via {@link RestApi#getSystemInfo}. */ @Override - public void onReceiveSystemVersion(RestApi.SystemVersion info) { + public void onReceiveSystemVersion(SystemVersion info) { if (getActivity() == null) return; @@ -187,8 +190,8 @@ public class DrawerFragment extends Fragment implements RestApi.OnReceiveSystemI * Populates views with status received via {@link RestApi#getConnections}. */ @Override - public void onReceiveConnections(Map connections) { - RestApi.Connection c = connections.get(RestApi.TOTAL_STATS); + public void onReceiveConnections(Map connections) { + Connection c = connections.get(RestApi.TOTAL_STATS); mDownload.setText(Util.readableTransferRate(mActivity, c.inBits)); mUpload.setText(Util.readableTransferRate(mActivity, c.outBits)); } diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java index 3da5cda9..26a96325 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java @@ -23,11 +23,13 @@ import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import com.google.common.collect.ImmutableList; 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.fragments.dialog.KeepVersionsDialogFragment; +import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.SyncthingService; @@ -102,7 +104,7 @@ public class FolderFragment extends Fragment break; case R.id.device_toggle: List devicesList = mFolder.getDevices(); - RestApi.Device device = (RestApi.Device) view.getTag(); + Device device = (Device) view.getTag(); if (isChecked) { devicesList.add(device.deviceID); } else { @@ -269,13 +271,13 @@ public class FolderFragment extends Fragment mIdView.setText(mFolder.id); mPathView.setText(mFolder.path); mFolderMasterView.setChecked(mFolder.type.equals("readonly")); - List devicesList = mSyncthingService.getApi().getDevices(false); + List devicesList = mSyncthingService.getApi().getDevices(false); mDevicesContainer.removeAllViews(); if (devicesList.isEmpty()) { addEmptyDeviceListView(); } else { - for (RestApi.Device n : devicesList) { + for (Device n : devicesList) { addDeviceViewAndSetListener(n, LayoutInflater.from(getActivity())); } } @@ -360,8 +362,13 @@ public class FolderFragment extends Fragment mFolder.setDevices(new ArrayList<>()); mFolder.versioning = new Folder.Versioning(); String deviceId = getActivity().getIntent().getStringExtra(EXTRA_DEVICE_ID); - //if (deviceId != null) - // TODO mFolder.devices.add(deviceId); + if (deviceId != null) { + List devices = ImmutableList.builder() + .addAll(mFolder.getDevices()) + .add(deviceId) + .build(); + mFolder.setDevices(devices); + } } private void prepareEditMode() { @@ -383,7 +390,7 @@ public class FolderFragment extends Fragment mDevicesContainer.addView(emptyView, params); } - private void addDeviceViewAndSetListener(RestApi.Device device, LayoutInflater inflater) { + private void addDeviceViewAndSetListener(Device device, LayoutInflater inflater) { inflater.inflate(R.layout.item_device_form, mDevicesContainer); SwitchCompat deviceView = (SwitchCompat) mDevicesContainer.getChildAt(mDevicesContainer.getChildCount()-1); deviceView.setChecked(mFolder.getDevices().contains(device.deviceID)); diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java index 3674eba2..df3d8fd8 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java @@ -17,6 +17,7 @@ import android.widget.Toast; import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.activities.SyncthingActivity; +import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.preferences.WifiSsidPreference; import com.nutomic.syncthingandroid.syncthing.RestApi; import com.nutomic.syncthingandroid.syncthing.SyncthingService; @@ -233,8 +234,8 @@ public class SettingsFragment extends PreferenceFragment } else if (preference.equals(mSyncOnlyWifi)) { mSyncOnlyOnSSIDs.setEnabled((Boolean) o); } else if (preference.getKey().equals(DEVICE_NAME_KEY)) { - RestApi.Device old = mSyncthingService.getApi().getLocalDevice(); - RestApi.Device updated = new RestApi.Device(); + Device old = mSyncthingService.getApi().getLocalDevice(); + Device updated = new Device(); updated.addresses = old.addresses; updated.compression = old.compression; updated.deviceID = old.deviceID; diff --git a/src/main/java/com/nutomic/syncthingandroid/model/Connection.java b/src/main/java/com/nutomic/syncthingandroid/model/Connection.java new file mode 100644 index 00000000..7af69c40 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/model/Connection.java @@ -0,0 +1,13 @@ +package com.nutomic.syncthingandroid.model; + +public class Connection { + public String at; + public long inBytesTotal; + public long outBytesTotal; + public long inBits; + public long outBits; + public String address; + public String clientVersion; + public int completion; + public boolean connected; +} diff --git a/src/main/java/com/nutomic/syncthingandroid/model/Device.java b/src/main/java/com/nutomic/syncthingandroid/model/Device.java new file mode 100644 index 00000000..91904f97 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/model/Device.java @@ -0,0 +1,12 @@ +package com.nutomic.syncthingandroid.model; + +import java.io.Serializable; +import java.util.List; + +public class Device implements Serializable { + public List addresses; + public String name; + public String deviceID; + public String compression; + public boolean introducer; +} diff --git a/src/main/java/com/nutomic/syncthingandroid/model/Model.java b/src/main/java/com/nutomic/syncthingandroid/model/Model.java new file mode 100644 index 00000000..093c3df9 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/model/Model.java @@ -0,0 +1,17 @@ +package com.nutomic.syncthingandroid.model; + +public class Model { + public long globalBytes; + public long globalDeleted; + public long globalFiles; + public long localBytes; + public long localDeleted; + public long localFiles; + public long inSyncBytes; + public long inSyncFiles; + public long needBytes; + public long needFiles; + public long needDeletes; + public String state; + public String invalid; +} diff --git a/src/main/java/com/nutomic/syncthingandroid/model/SystemInfo.java b/src/main/java/com/nutomic/syncthingandroid/model/SystemInfo.java new file mode 100644 index 00000000..909b7e29 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/model/SystemInfo.java @@ -0,0 +1,14 @@ +package com.nutomic.syncthingandroid.model; + +import java.util.Map; + +public class SystemInfo { + public long alloc; + public double cpuPercent; + public int goroutines; + public String myID; + public long sys; + public boolean discoveryEnabled; + public int discoveryMethods; + public Map discoveryErrors; +} diff --git a/src/main/java/com/nutomic/syncthingandroid/model/SystemVersion.java b/src/main/java/com/nutomic/syncthingandroid/model/SystemVersion.java new file mode 100644 index 00000000..5fd1b766 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/model/SystemVersion.java @@ -0,0 +1,9 @@ +package com.nutomic.syncthingandroid.model; + +public class SystemVersion { + public String arch; + public String codename; + public String longVersion; + public String os; + public String version; +} diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java index c4c0552f..bc829a8e 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java @@ -18,6 +18,7 @@ import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.fragments.DeviceFragment; import com.nutomic.syncthingandroid.fragments.FolderFragment; +import com.nutomic.syncthingandroid.model.Device; import java.io.File; import java.util.concurrent.TimeUnit; @@ -125,7 +126,7 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene pi = PendingIntent.getActivity(mContext, requestCode, intent, 0); String deviceName = null; - for (RestApi.Device d : mApi.getDevices(false)) { + for (Device d : mApi.getDevices(false)) { if (d.deviceID.equals(deviceId)) deviceName = RestApi.getDeviceDisplayName(d); } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index 1544cc26..019cf0b5 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -19,7 +19,12 @@ import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.activities.RestartActivity; import com.nutomic.syncthingandroid.http.GetTask; import com.nutomic.syncthingandroid.http.PostTask; +import com.nutomic.syncthingandroid.model.Connection; +import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.model.Folder; +import com.nutomic.syncthingandroid.model.Model; +import com.nutomic.syncthingandroid.model.SystemInfo; +import com.nutomic.syncthingandroid.model.SystemVersion; import com.nutomic.syncthingandroid.util.FolderObserver; import org.json.JSONArray; @@ -70,61 +75,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, private static final List USAGE_REPORTING_DECIDED = Arrays.asList(USAGE_REPORTING_ACCEPTED, USAGE_REPORTING_DENIED); - public static class Device implements Serializable { - public List addresses; - public String name; - public String deviceID; - public String compression; - public boolean introducer; - } - - public static class SystemInfo { - public long alloc; - public double cpuPercent; - public int goroutines; - public String myID; - public long sys; - public boolean discoveryEnabled; - public int discoveryMethods; - public Map discoveryErrors; - } - - public static class SystemVersion { - public String arch; - public String codename; - public String longVersion; - public String os; - public String version; - } - - public static class Connection { - public String at; - public long inBytesTotal; - public long outBytesTotal; - public long inBits; - public long outBits; - public String address; - public String clientVersion; - public int completion; - public boolean connected; - } - - public static class Model { - public long globalBytes; - public long globalDeleted; - public long globalFiles; - public long localBytes; - public long localDeleted; - public long localFiles; - public long inSyncBytes; - public long inSyncFiles; - public long needBytes; - public long needFiles; - public long needDeletes; - public String state; - public String invalid; - } - public interface OnConfigChangedListener { void onConfigChanged(); } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java b/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java index e3432049..bd5c96d9 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java @@ -11,6 +11,8 @@ import android.widget.ArrayAdapter; import android.widget.TextView; import com.nutomic.syncthingandroid.R; +import com.nutomic.syncthingandroid.model.Connection; +import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.syncthing.RestApi; import java.util.HashMap; @@ -19,10 +21,10 @@ import java.util.Map; /** * Generates item views for device items. */ -public class DevicesAdapter extends ArrayAdapter +public class DevicesAdapter extends ArrayAdapter implements RestApi.OnReceiveConnectionsListener { - private Map mConnections = + private Map mConnections = new HashMap<>(); public DevicesAdapter(Context context) { @@ -44,7 +46,7 @@ public class DevicesAdapter extends ArrayAdapter TextView upload = (TextView) convertView.findViewById(R.id.upload); String deviceId = getItem(position).deviceID; - RestApi.Connection conn = mConnections.get(deviceId); + Connection conn = mConnections.get(deviceId); name.setText(RestApi.getDeviceDisplayName(getItem(position))); Resources r = getContext().getResources(); @@ -80,7 +82,7 @@ public class DevicesAdapter extends ArrayAdapter } @Override - public void onReceiveConnections(Map connections) { + public void onReceiveConnections(Map connections) { mConnections = connections; notifyDataSetChanged(); } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java b/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java index 762e134b..56864c5d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java @@ -13,6 +13,7 @@ import android.widget.TextView; import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.model.Folder; +import com.nutomic.syncthingandroid.model.Model; import com.nutomic.syncthingandroid.syncthing.RestApi; import java.util.HashMap; @@ -26,7 +27,7 @@ import static android.view.View.VISIBLE; public class FoldersAdapter extends ArrayAdapter implements RestApi.OnReceiveModelListener { - private final HashMap mModels = new HashMap<>(); + private final HashMap mModels = new HashMap<>(); private final LayoutInflater mInflater; public FoldersAdapter(Context context) { @@ -48,7 +49,7 @@ public class FoldersAdapter extends ArrayAdapter TextView invalid = (TextView) convertView.findViewById(R.id.invalid); Folder folder = getItem(position); - RestApi.Model model = mModels.get(folder.id); + Model model = mModels.get(folder.id); label.setText(TextUtils.isEmpty(folder.label) ? folder.id : folder.label); state.setTextColor(ContextCompat.getColor(getContext(), R.color.text_green)); directory.setText(folder.path); @@ -103,7 +104,7 @@ public class FoldersAdapter extends ArrayAdapter } @Override - public void onReceiveModel(String folderId, RestApi.Model model) { + public void onReceiveModel(String folderId, Model model) { mModels.put(folderId, model); notifyDataSetChanged(); }