Skip to content

Commit

Permalink
Moved Syncthing constants to seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Oct 4, 2017
1 parent 4c2b325 commit 1fd8621
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.nutomic.syncthingandroid.model.Config;
import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.model.Options;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Languages;
Expand Down Expand Up @@ -94,13 +95,13 @@ public void onActivityCreated(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.app_settings);
PreferenceScreen screen = getPreferenceScreen();
mAlwaysRunInBackground =
(CheckBoxPreference) findPreference(SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND);
(CheckBoxPreference) findPreference(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND);
mSyncOnlyCharging =
(CheckBoxPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_CHARGING);
(CheckBoxPreference) findPreference(Constants.PREF_SYNC_ONLY_CHARGING);
mSyncOnlyWifi =
(CheckBoxPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI);
(CheckBoxPreference) findPreference(Constants.PREF_SYNC_ONLY_WIFI);
mSyncOnlyOnSSIDs =
(WifiSsidPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI_SSIDS);
(WifiSsidPreference) findPreference(Constants.PREF_SYNC_ONLY_WIFI_SSIDS);

mSyncOnlyCharging.setEnabled(mAlwaysRunInBackground.isChecked());
mSyncOnlyWifi.setEnabled(mAlwaysRunInBackground.isChecked());
Expand Down Expand Up @@ -140,8 +141,8 @@ public void onActivityCreated(Bundle savedInstanceState) {
Preference environmentVariables = findPreference("environment_variables");
Preference stReset = findPreference("streset");

mUseRoot = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_ROOT);
Preference useWakelock = findPreference(SyncthingService.PREF_USE_WAKE_LOCK);
mUseRoot = (CheckBoxPreference) findPreference(Constants.PREF_USE_ROOT);
Preference useWakelock = findPreference(Constants.PREF_USE_WAKE_LOCK);
Preference foregroundService = findPreference("run_as_foreground_service");
Preference useTor = findPreference("use_tor");

Expand Down Expand Up @@ -284,7 +285,7 @@ public boolean onRequireRestart(Preference preference, Object o) {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
switch (preference.getKey()) {
case SyncthingService.PREF_ALWAYS_RUN_IN_BACKGROUND:
case Constants.PREF_ALWAYS_RUN_IN_BACKGROUND:
boolean value = (Boolean) o;
mAlwaysRunInBackground.setSummary((value)
? R.string.always_run_in_background_enabled
Expand All @@ -298,7 +299,7 @@ public boolean onPreferenceChange(Preference preference, Object o) {
mSyncOnlyWifi.setChecked(false);
}
break;
case SyncthingService.PREF_SYNC_ONLY_WIFI:
case Constants.PREF_SYNC_ONLY_WIFI:
mSyncOnlyOnSSIDs.setEnabled((Boolean) o);
break;
case KEY_STTRACE:
Expand Down Expand Up @@ -328,7 +329,7 @@ public boolean onPreferenceChange(Preference preference, Object o) {
@Override
public boolean onPreferenceClick(Preference preference) {
switch (preference.getKey()) {
case SyncthingService.PREF_USE_ROOT:
case Constants.PREF_USE_ROOT:
if (mUseRoot.isChecked()) {
// Only check preference after root was granted.
mUseRoot.setChecked(false);
Expand All @@ -345,7 +346,7 @@ public boolean onPreferenceClick(Preference preference) {
mSyncthingService.exportConfig();
Toast.makeText(getActivity(),
getString(R.string.config_export_successful,
SyncthingService.EXPORT_PATH), Toast.LENGTH_LONG).show();
Constants.EXPORT_PATH), Toast.LENGTH_LONG).show();
})
.setNegativeButton(android.R.string.no, null)
.show();
Expand All @@ -363,7 +364,7 @@ public boolean onPreferenceClick(Preference preference) {
} else {
Toast.makeText(getActivity(),
getString(R.string.config_import_failed,
SyncthingService.EXPORT_PATH), Toast.LENGTH_LONG).show();
Constants.EXPORT_PATH), Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(android.R.string.no, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import android.webkit.WebViewClient;

import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.ConfigXml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -157,7 +159,7 @@ public void onBackPressed() {
private void loadCaCert() {
InputStream inStream = null;
try {
String httpsCertPath = getFilesDir() + "/" + SyncthingService.HTTPS_CERT_FILE;
File httpsCertPath = Constants.getHttpsCertFile(this);
inStream = new FileInputStream(httpsCertPath);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
mCaCert = (X509Certificate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.nutomic.syncthingandroid.activities.DeviceActivity;
import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.views.DevicesAdapter;

Expand Down Expand Up @@ -58,7 +59,7 @@ public void run() {
getActivity().runOnUiThread(DeviceListFragment.this::updateList);
}

}, 0, SyncthingService.GUI_UPDATE_INTERVAL);
}, 0, Constants.GUI_UPDATE_INTERVAL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import com.nutomic.syncthingandroid.model.Connections;
import com.nutomic.syncthingandroid.model.SystemInfo;
import com.nutomic.syncthingandroid.model.SystemVersion;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Util;

import java.io.File;
import java.net.URL;
import java.text.NumberFormat;
import java.util.Locale;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void run() {
updateGui();
}

}, 0, SyncthingService.GUI_UPDATE_INTERVAL);
}, 0, Constants.GUI_UPDATE_INTERVAL);
}

@Override
Expand Down Expand Up @@ -197,12 +197,11 @@ private void onReceiveConnections(Connections connections) {

private void showQrCode() {
//The QRCode request takes one paramteer called "text", which is the text to be converted to a QRCode.
File httpsCertPath = new File(mActivity.getFilesDir(), SyncthingService.HTTPS_CERT_FILE);
String apiKey = mActivity.getApi().getGui().apiKey;
String deviceId = mActivity.getApi().getLocalDevice().deviceID;
URL url = mActivity.getApi().getUrl();
new ImageGetRequest(mActivity, url, ImageGetRequest.QR_CODE_GENERATOR, httpsCertPath,
apiKey, ImmutableMap.of("text", deviceId),qrCodeBitmap -> {
new ImageGetRequest(mActivity, url, ImageGetRequest.QR_CODE_GENERATOR, apiKey,
ImmutableMap.of("text", deviceId),qrCodeBitmap -> {
mActivity.showQrCodeDialog(deviceId, qrCodeBitmap);
mActivity.closeDrawer();
}, error -> Toast.makeText(mActivity, R.string.could_not_access_deviceid, Toast.LENGTH_SHORT).show());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.nutomic.syncthingandroid.activities.FolderActivity;
import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.views.FoldersAdapter;

Expand Down Expand Up @@ -53,7 +54,7 @@ public void run() {
getActivity().runOnUiThread(FolderListFragment.this::updateList);
}

}, 0, SyncthingService.GUI_UPDATE_INTERVAL);
}, 0, Constants.GUI_UPDATE_INTERVAL);
}

@Override
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,15 @@
import com.android.volley.toolbox.Volley;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.nutomic.syncthingandroid.service.Constants;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
Expand All @@ -43,7 +35,6 @@
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public abstract class ApiRequest {

Expand Down Expand Up @@ -79,14 +70,12 @@ private RequestQueue getVolleyQueue() {
private final Context mContext;
private final URL mUrl;
private final String mPath;
private final File mHttpsCertPath;
private final String mApiKey;

public ApiRequest(Context context, URL url, String path, File httpsCertPath, String apiKey) {
public ApiRequest(Context context, URL url, String path, String apiKey) {
mContext = context;
mUrl = url;
mPath = path;
mHttpsCertPath = httpsCertPath;
mApiKey = apiKey;
}

Expand Down Expand Up @@ -177,7 +166,8 @@ public boolean verify(String hostname, SSLSession session) {
private SSLSocketFactory getSslSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new SyncthingTrustManager(mHttpsCertPath)},
File httpsCertPath = Constants.getHttpsCertFile(mContext);
sslContext.init(null, new TrustManager[]{new SyncthingTrustManager(httpsCertPath)},
new SecureRandom());
return sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.android.volley.Request;
import com.google.common.base.Optional;

import java.io.File;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
Expand All @@ -26,9 +25,9 @@ public class GetRequest extends ApiRequest {
public static final String URI_REPORT = "/rest/svc/report";
public static final String URI_EVENTS = "/rest/events";

public GetRequest(Context context, URL url, String path, File httpsCertPath, String apiKey,
public GetRequest(Context context, URL url, String path, String apiKey,
@Nullable Map<String, String> params, OnSuccessListener listener) {
super(context, url, path, httpsCertPath, apiKey);
super(context, url, path, apiKey);
Map<String, String> safeParams = Optional.fromNullable(params).or(Collections.emptyMap());
Uri uri = buildUri(safeParams);
connect(Request.Method.GET, uri, null, listener, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import com.google.common.base.Optional;

import java.io.File;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
Expand All @@ -15,9 +14,10 @@ public class ImageGetRequest extends ApiRequest {

public static final String QR_CODE_GENERATOR = "/qr/";

public ImageGetRequest(Context context, URL url, String path, File httpsCertPath, String apiKey,
@Nullable Map<String, String> params, OnImageSuccessListener onSuccessListener, OnErrorListener onErrorListener) {
super(context, url, path, httpsCertPath, apiKey);
public ImageGetRequest(Context context, URL url, String path, String apiKey,
@Nullable Map<String, String> params,
OnImageSuccessListener onSuccessListener, OnErrorListener onErrorListener) {
super(context, url, path, apiKey);
Map<String, String> safeParams = Optional.fromNullable(params).or(Collections.emptyMap());
Uri uri = buildUri(safeParams);
makeImageRequest(uri, onSuccessListener, onErrorListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.android.volley.Request;
import com.android.volley.VolleyError;

import java.io.File;
import java.net.URL;
import java.util.Collections;

Expand All @@ -26,9 +25,9 @@ public class PollWebGuiAvailableTask extends ApiRequest {
private final OnSuccessListener mListener;
private final Handler mHandler = new Handler();

public PollWebGuiAvailableTask(Context context, URL url, File httpsCertPath, String apiKey,
public PollWebGuiAvailableTask(Context context, URL url, String apiKey,
OnSuccessListener listener) {
super(context, url, "", httpsCertPath, apiKey);
super(context, url, "", apiKey);
mListener = listener;
performRequest();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

import com.android.volley.Request;

import java.io.File;
import java.net.URL;
import java.util.Collections;

public class PostConfigRequest extends ApiRequest {

private static final String URI_CONFIG = "/rest/system/config";

public PostConfigRequest(Context context, URL url, File httpsCertPath, String apiKey, String config,
public PostConfigRequest(Context context, URL url, String apiKey, String config,
OnSuccessListener listener) {
super(context, url, URI_CONFIG, httpsCertPath, apiKey);
super(context, url, URI_CONFIG, apiKey);
Uri uri = buildUri(Collections.emptyMap());
connect(Request.Method.POST, uri, config, listener, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import com.android.volley.Request;
import com.google.common.collect.ImmutableMap;

import java.io.File;
import java.net.URL;

public class PostScanRequest extends ApiRequest {

private static final String URI_SCAN = "/rest/db/scan";

public PostScanRequest(Context context, URL url, File httpsCertPath, String apiKey,
public PostScanRequest(Context context, URL url, String apiKey,
String folder, String sub) {
super(context, url, URI_SCAN, httpsCertPath, apiKey);
super(context, url, URI_SCAN, apiKey);
Uri uri = buildUri(ImmutableMap.of("folder", folder, "sub", sub));
connect(Request.Method.POST, uri, null, null, null);
}
Expand Down
Loading

0 comments on commit 1fd8621

Please sign in to comment.