1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-10 20:15:54 +00:00

Minor refactoring.

This commit is contained in:
Felix Ableitner 2014-06-22 22:52:32 +02:00
parent dff9156d77
commit d0e7f57812
7 changed files with 24 additions and 32 deletions

View file

@ -28,7 +28,6 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
/** /**

View file

@ -6,9 +6,8 @@ import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.util.NodeAdapter; import com.nutomic.syncthingandroid.util.NodesAdapter;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -19,7 +18,7 @@ import java.util.TimerTask;
public class NodesFragment extends LoadingListFragment implements public class NodesFragment extends LoadingListFragment implements
SyncthingService.OnApiAvailableListener, ListView.OnItemClickListener { SyncthingService.OnApiAvailableListener, ListView.OnItemClickListener {
private NodeAdapter mAdapter; private NodesAdapter mAdapter;
private Timer mTimer; private Timer mTimer;
@ -27,7 +26,7 @@ public class NodesFragment extends LoadingListFragment implements
@Override @Override
public void onInitAdapter(MainActivity activity) { public void onInitAdapter(MainActivity activity) {
mAdapter = new NodeAdapter(activity); mAdapter = new NodesAdapter(activity);
mAdapter.add(activity.getApi().getNodes()); mAdapter.add(activity.getApi().getNodes());
setListAdapter(mAdapter, R.string.nodes_list_empty); setListAdapter(mAdapter, R.string.nodes_list_empty);
mInitialized = true; mInitialized = true;

View file

@ -110,7 +110,7 @@ public class RepoSettingsActivity extends PreferenceActivity
else if (getIntent().getAction().equals(ACTION_EDIT)) { else if (getIntent().getAction().equals(ACTION_EDIT)) {
setTitle(R.string.edit_repo); setTitle(R.string.edit_repo);
mRepositoryId.setEnabled(false); mRepositoryId.setEnabled(false);
List<RestApi.Repository> repos = mSyncthingService.getApi().getRepositories(); List<RestApi.Repository> repos = mSyncthingService.getApi().getRepos();
for (int i = 0; i < repos.size(); i++) { for (int i = 0; i < repos.size(); i++) {
if (repos.get(i).ID.equals(getIntent().getStringExtra(KEY_REPO_ID))) { if (repos.get(i).ID.equals(getIntent().getStringExtra(KEY_REPO_ID))) {
mRepo = repos.get(i); mRepo = repos.get(i);
@ -174,7 +174,7 @@ public class RepoSettingsActivity extends PreferenceActivity
Toast.makeText(this, R.string.repo_path_required, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.repo_path_required, Toast.LENGTH_LONG).show();
return true; return true;
} }
mSyncthingService.getApi().editRepository(mRepo, true); mSyncthingService.getApi().editRepo(mRepo, true);
finish(); finish();
return true; return true;
} }
@ -258,7 +258,7 @@ public class RepoSettingsActivity extends PreferenceActivity
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialogInterface, int i) { public void onClick(DialogInterface dialogInterface, int i) {
mSyncthingService.getApi().deleteRepository(mRepo); mSyncthingService.getApi().deleteRepo(mRepo);
finish(); finish();
} }
}) })
@ -271,7 +271,7 @@ public class RepoSettingsActivity extends PreferenceActivity
private void repositoryUpdated() { private void repositoryUpdated() {
if (getIntent().getAction().equals(ACTION_EDIT)) { if (getIntent().getAction().equals(ACTION_EDIT)) {
mSyncthingService.getApi().editRepository(mRepo, false); mSyncthingService.getApi().editRepo(mRepo, false);
} }
} }

View file

@ -5,7 +5,6 @@ import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.RestApi;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.util.ReposAdapter; import com.nutomic.syncthingandroid.util.ReposAdapter;
@ -27,7 +26,7 @@ public class ReposFragment extends LoadingListFragment implements
@Override @Override
public void onInitAdapter(MainActivity activity) { public void onInitAdapter(MainActivity activity) {
mAdapter = new ReposAdapter(activity); mAdapter = new ReposAdapter(activity);
mAdapter.add(activity.getApi().getRepositories()); mAdapter.add(activity.getApi().getRepos());
setListAdapter(mAdapter, R.string.repositories_list_empty); setListAdapter(mAdapter, R.string.repositories_list_empty);
mInitialized = true; mInitialized = true;
} }
@ -69,4 +68,5 @@ public class ReposFragment extends LoadingListFragment implements
intent.putExtra(RepoSettingsActivity.KEY_REPO_ID, mAdapter.getItem(i).ID); intent.putExtra(RepoSettingsActivity.KEY_REPO_ID, mAdapter.getItem(i).ID);
startActivity(intent); startActivity(intent);
} }
} }

View file

@ -14,11 +14,9 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.lang.ref.WeakReference;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -386,7 +384,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/** /**
* Returns a list of all existing repositores. * Returns a list of all existing repositores.
*/ */
public List<Repository> getRepositories() { public List<Repository> getRepos() {
if (mConfig == null) if (mConfig == null)
return new ArrayList<Repository>(); return new ArrayList<Repository>();
@ -610,7 +608,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
/** /**
* Updates or creates the given node. * Updates or creates the given node.
*/ */
public void editRepository(Repository repository, boolean create) { public void editRepo(Repository repo, boolean create) {
try { try {
JSONArray repos = mConfig.getJSONArray("Repositories"); JSONArray repos = mConfig.getJSONArray("Repositories");
JSONObject r = null; JSONObject r = null;
@ -621,18 +619,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
else { else {
for (int i = 0; i < repos.length(); i++) { for (int i = 0; i < repos.length(); i++) {
JSONObject json = repos.getJSONObject(i); JSONObject json = repos.getJSONObject(i);
if (repository.ID.equals(json.getString("ID"))) { if (repo.ID.equals(json.getString("ID"))) {
r = repos.getJSONObject(i); r = repos.getJSONObject(i);
break; break;
} }
} }
} }
r.put("Directory", repository.Directory); r.put("Directory", repo.Directory);
r.put("ID", repository.ID); r.put("ID", repo.ID);
r.put("IgnorePerms", true); r.put("IgnorePerms", true);
r.put("ReadOnly", repository.ReadOnly); r.put("ReadOnly", repo.ReadOnly);
JSONArray nodes = new JSONArray(); JSONArray nodes = new JSONArray();
for (Node n : repository.Nodes) { for (Node n : repo.Nodes) {
JSONObject element = new JSONObject(); JSONObject element = new JSONObject();
element.put("Addresses", n.Addresses); element.put("Addresses", n.Addresses);
element.put("Name", n.Name); element.put("Name", n.Name);
@ -641,30 +639,30 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener {
} }
r.put("Nodes", nodes); r.put("Nodes", nodes);
JSONObject versioning = new JSONObject(); JSONObject versioning = new JSONObject();
versioning.put("Type", repository.Versioning.getType()); versioning.put("Type", repo.Versioning.getType());
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
versioning.put("Params", params); versioning.put("Params", params);
for (String key : repository.Versioning.getParams().keySet()) { for (String key : repo.Versioning.getParams().keySet()) {
params.put(key, repository.Versioning.getParams().get(key)); params.put(key, repo.Versioning.getParams().get(key));
} }
r.put("Versioning", versioning); r.put("Versioning", versioning);
configUpdated(); configUpdated();
} }
catch (JSONException e) { catch (JSONException e) {
Log.w(TAG, "Failed to edit repo " + repository.ID + " at " + repository.Directory, e); Log.w(TAG, "Failed to edit repo " + repo.ID + " at " + repo.Directory, e);
} }
} }
/** /**
* Deletes the given repository from syncthing. * Deletes the given repository from syncthing.
*/ */
public void deleteRepository(Repository repository) { public void deleteRepo(Repository repo) {
try { try {
JSONArray repos = mConfig.getJSONArray("Repositories"); JSONArray repos = mConfig.getJSONArray("Repositories");
for (int i = 0; i < repos.length(); i++) { for (int i = 0; i < repos.length(); i++) {
JSONObject json = repos.getJSONObject(i); JSONObject json = repos.getJSONObject(i);
if (repository.ID.equals(json.getString("ID"))) { if (repo.ID.equals(json.getString("ID"))) {
mConfig.remove("Repositories"); mConfig.remove("Repositories");
mConfig.put("Repositories", delete(repos, repos.getJSONObject(i))); mConfig.put("Repositories", delete(repos, repos.getJSONObject(i)));
break; break;

View file

@ -1,9 +1,6 @@
package com.nutomic.syncthingandroid.util; package com.nutomic.syncthingandroid.util;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;

View file

@ -6,7 +6,6 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
@ -20,13 +19,13 @@ import java.util.Map;
/** /**
* Generates item views for node items. * Generates item views for node items.
*/ */
public class NodeAdapter extends ArrayAdapter<RestApi.Node> public class NodesAdapter extends ArrayAdapter<RestApi.Node>
implements RestApi.OnReceiveConnectionsListener { implements RestApi.OnReceiveConnectionsListener {
private Map<String, RestApi.Connection> mConnections = private Map<String, RestApi.Connection> mConnections =
new HashMap<String, RestApi.Connection>(); new HashMap<String, RestApi.Connection>();
public NodeAdapter(Context context) { public NodesAdapter(Context context) {
super(context, R.layout.node_list_item); super(context, R.layout.node_list_item);
} }