1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-29 15:51:17 +00:00

Make status LED un-clickable, Reduce verbose logging (#254)

Change summary:
* Make status light un-clickable
* Add configurable verbose logging
* Reduce verbose logging
* Initialize model#Folder.label
* EventProcessor: Ignore event "FolderResumed"

Detailed changelog:

* Make status light un-clickable

* Initialize model#Folder.label

* Rename func to applyCustomRunConditions

* RunConditionMonitor: Make verbose log configurable

* ReceiverManager: Make verbose log configurable

* StatusFragment: Make verbose log configurable

* FolderListFragment: Make verbose log configurable

* DeviceListFragment: Make verbose log configurable

* RestApi: Make verbose log configurable

* SyncthingService: Make verbose log configurable

- Remove duplicate log lines referring to the same logic that happened
- Improved log levels

* RestApi: Log.v => LogV

* SyncthingApp ThreadPolicy - I'll need this later

* RestApi: Reduce logging

* applyCustomRunConditions: Log changes and result in non-verbose level

* EventProcessor: Ignore event "FolderResumed"

* RestApi#readConfigFromRestApi: Change log level to verbose

* SyncthingRunnable: Reduce log, make verbose log configurable
This commit is contained in:
Catfriend1 2019-01-20 14:06:49 +00:00 committed by GitHub
parent f0d72e7a4b
commit 979088f4ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 177 additions and 99 deletions

View file

@ -23,11 +23,21 @@ public class SyncthingApp extends Application {
new Languages(this).setLanguage(this);
// Set VM policy to avoid crash when sending folder URI to file manager.
StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
StrictMode.VmPolicy vmPolicy = new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build();
StrictMode.setVmPolicy(policy);
StrictMode.setVmPolicy(vmPolicy);
/*
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P)
{
StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder()
.permitAll()
.build();
StrictMode.setThreadPolicy(threadPolicy);
}
*/
}
public DaggerComponent component() {

View file

@ -35,6 +35,8 @@ public class DeviceListFragment extends ListFragment implements SyncthingService
private final static String TAG = "DeviceListFragment";
private static final Boolean ENABLE_VERBOSE_LOG = false;
/**
* Compares devices by name, uses the device ID as fallback if the name is empty
*/
@ -86,13 +88,13 @@ public class DeviceListFragment extends ListFragment implements SyncthingService
}
private void startUpdateListHandler() {
Log.v(TAG, "startUpdateListHandler");
LogV("startUpdateListHandler");
mUpdateListHandler.removeCallbacks(mUpdateListRunnable);
mUpdateListHandler.post(mUpdateListRunnable);
}
private void stopUpdateListHandler() {
Log.v(TAG, "stopUpdateListHandler");
LogV("stopUpdateListHandler");
mUpdateListHandler.removeCallbacks(mUpdateListRunnable);
}
@ -122,7 +124,7 @@ public class DeviceListFragment extends ListFragment implements SyncthingService
if (mainActivity.isFinishing()) {
return;
}
Log.v(TAG, "Invoking updateList on UI thread");
LogV("Invoking updateList on UI thread");
mainActivity.runOnUiThread(DeviceListFragment.this::updateList);
}
@ -193,4 +195,9 @@ public class DeviceListFragment extends ListFragment implements SyncthingService
}
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -30,7 +30,9 @@ import java.util.List;
public class FolderListFragment extends ListFragment implements SyncthingService.OnServiceStateChangeListener,
AdapterView.OnItemClickListener {
private final static String TAG = "FolderListFragment";
private static final String TAG = "FolderListFragment";
private static final Boolean ENABLE_VERBOSE_LOG = false;
private Runnable mUpdateListRunnable = new Runnable() {
@Override
@ -74,13 +76,13 @@ public class FolderListFragment extends ListFragment implements SyncthingService
}
private void startUpdateListHandler() {
Log.v(TAG, "startUpdateListHandler");
LogV("startUpdateListHandler");
mUpdateListHandler.removeCallbacks(mUpdateListRunnable);
mUpdateListHandler.post(mUpdateListRunnable);
}
private void stopUpdateListHandler() {
Log.v(TAG, "stopUpdateListHandler");
LogV("stopUpdateListHandler");
mUpdateListHandler.removeCallbacks(mUpdateListRunnable);
}
@ -110,7 +112,7 @@ public class FolderListFragment extends ListFragment implements SyncthingService
if (mainActivity.isFinishing()) {
return;
}
Log.v(TAG, "Invoking updateList on UI thread");
LogV("Invoking updateList on UI thread");
mainActivity.runOnUiThread(FolderListFragment.this::updateList);
}
@ -180,4 +182,9 @@ public class FolderListFragment extends ListFragment implements SyncthingService
}
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -39,6 +39,8 @@ public class StatusFragment extends ListFragment implements SyncthingService.OnS
private static final String TAG = "StatusFragment";
private static final Boolean ENABLE_VERBOSE_LOG = false;
private Runnable mRestApiQueryRunnable = new Runnable() {
@Override
public void run() {
@ -97,13 +99,13 @@ public class StatusFragment extends ListFragment implements SyncthingService.OnS
}
private void startRestApiQueryHandler() {
Log.v(TAG, "startUpdateListHandler");
LogV("startUpdateListHandler");
mRestApiQueryHandler.removeCallbacks(mRestApiQueryRunnable);
mRestApiQueryHandler.post(mRestApiQueryRunnable);
}
private void stopRestApiQueryHandler() {
Log.v(TAG, "stopUpdateListHandler");
LogV("stopUpdateListHandler");
mRestApiQueryHandler.removeCallbacks(mRestApiQueryRunnable);
}
@ -239,7 +241,7 @@ public class StatusFragment extends ListFragment implements SyncthingService.OnS
if (restApi == null) {
return;
}
Log.v(TAG, "Invoking REST status queries");
LogV("Invoking REST status queries");
restApi.getSystemStatus(this::onReceiveSystemStatus);
restApi.getConnections(this::onReceiveConnections);
// onReceiveSystemStatus, onReceiveConnections will call {@link #updateStatus}.
@ -300,4 +302,9 @@ public class StatusFragment extends ListFragment implements SyncthingService.OnS
updateStatus();
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -15,7 +15,7 @@ public class Folder {
// Folder Configuration
public String id;
public String label;
public String label = "";
public String filesystemType = "basic";
public String path;
public String type = Constants.FOLDER_TYPE_SEND_RECEIVE;

View file

@ -84,7 +84,7 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener
public void onDone(long lastId) {
if (lastId < mLastEventId) mLastEventId = 0;
Log.d(TAG, "Reading events starting with id " + mLastEventId);
LogV("Reading events starting with id " + mLastEventId);
mRestApi.getEvents(mLastEventId, 0, EventProcessor.this);
}
@ -99,7 +99,7 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener
switch (event.type) {
case "ConfigSaved":
if (mRestApi != null) {
Log.d(TAG, "Forwarding ConfigSaved event to RestApi to get the updated config.");
LogV("Forwarding ConfigSaved event to RestApi to get the updated config.");
mRestApi.reloadConfig();
}
break;
@ -157,6 +157,7 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener
case "DeviceDiscovered":
case "DownloadProgress":
case "FolderPaused":
case "FolderResumed":
case "FolderScanProgress":
case "FolderSummary":
case "FolderWatchStateChanged":
@ -169,7 +170,7 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener
case "StartupComplete":
case "StateChanged":
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, "Ignored event " + event.type + ", data " + event.data);
LogV("Ignored event " + event.type + ", data " + event.data);
}
break;
default:
@ -339,4 +340,10 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener
}
}
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -13,12 +13,14 @@ public class ReceiverManager {
private static final String TAG = "ReceiverManager";
private static final Boolean ENABLE_VERBOSE_LOG = false;
private static List<BroadcastReceiver> mReceivers = new ArrayList<BroadcastReceiver>();
public static synchronized void registerReceiver(Context context, BroadcastReceiver receiver, IntentFilter intentFilter) {
mReceivers.add(receiver);
context.registerReceiver(receiver, intentFilter);
Log.v(TAG, "Registered receiver: " + receiver + " with filter: " + intentFilter);
LogV("Registered receiver: " + receiver + " with filter: " + intentFilter);
}
public static synchronized boolean isReceiverRegistered(BroadcastReceiver receiver) {
@ -36,7 +38,7 @@ public class ReceiverManager {
if (isReceiverRegistered(receiver)) {
try {
context.unregisterReceiver(receiver);
Log.v(TAG, "Unregistered receiver: " + receiver);
LogV("Unregistered receiver: " + receiver);
} catch(IllegalArgumentException e) {
// We have to catch the race condition a registration is still pending in android
// according to https://stackoverflow.com/a/3568906
@ -46,4 +48,10 @@ public class ReceiverManager {
}
}
}
private static void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -64,6 +64,8 @@ public class RestApi {
private static final String TAG = "RestApi";
private static final Boolean ENABLE_VERBOSE_LOG = false;
/**
* Compares folders by labels, uses the folder ID as fallback if the label is empty
*/
@ -166,7 +168,7 @@ public class RestApi {
* Gets local device ID, syncthing version and config, then calls all OnApiAvailableListeners.
*/
public void readConfigFromRestApi() {
Log.v(TAG, "Reading config from REST ...");
LogV("Querying config from REST ...");
synchronized (mAsyncQueryCompleteLock) {
asyncQueryVersionComplete = false;
asyncQueryConfigComplete = false;
@ -175,7 +177,6 @@ public class RestApi {
new GetRequest(mContext, mUrl, GetRequest.URI_VERSION, mApiKey, null, result -> {
JsonObject json = new JsonParser().parse(result).getAsJsonObject();
mVersion = json.get("version").getAsString();
Log.i(TAG, "Syncthing version is " + mVersion);
updateDebugFacilitiesCache();
synchronized (mAsyncQueryCompleteLock) {
asyncQueryVersionComplete = true;
@ -201,7 +202,8 @@ public class RestApi {
private void checkReadConfigFromRestApiCompleted() {
if (asyncQueryVersionComplete && asyncQueryConfigComplete && asyncQuerySystemStatusComplete) {
Log.v(TAG, "Reading config from REST completed.");
LogV("Reading config from REST completed. Syncthing version is " + mVersion);
// Tell SyncthingService it can transition to State.ACTIVE.
mOnApiAvailableListener.onApiAvailable();
}
}
@ -219,11 +221,9 @@ public class RestApi {
if (!configParseSuccess) {
throw new RuntimeException("config is null: " + result);
}
Log.v(TAG, "onReloadConfigComplete: Successfully parsed configuration.");
if (BuildConfig.DEBUG) {
Log.v(TAG, "mConfig.pendingDevices = " + new Gson().toJson(mConfig.pendingDevices));
Log.v(TAG, "mConfig.remoteIgnoredDevices = " + new Gson().toJson(mConfig.remoteIgnoredDevices));
}
Log.d(TAG, "onReloadConfigComplete: Successfully parsed configuration.");
LogV("mConfig.pendingDevices = " + new Gson().toJson(mConfig.pendingDevices));
LogV("mConfig.remoteIgnoredDevices = " + new Gson().toJson(mConfig.remoteIgnoredDevices));
// Update cached device and folder information stored in the mCompletion model.
mCompletion.updateFromConfig(getDevices(true), getFolders());
@ -340,10 +340,8 @@ public class RestApi {
}
}
device.ignoredFolders.add(ignoredFolder);
if (BuildConfig.DEBUG) {
Log.v(TAG, "device.pendingFolders = " + new Gson().toJson(device.pendingFolders));
Log.v(TAG, "device.ignoredFolders = " + new Gson().toJson(device.ignoredFolders));
}
LogV("device.pendingFolders = " + new Gson().toJson(device.pendingFolders));
LogV("device.ignoredFolders = " + new Gson().toJson(device.ignoredFolders));
sendConfig();
Log.d(TAG, "Ignored folder [" + folderId + "] announced by device [" + deviceId + "]");
@ -521,7 +519,7 @@ public class RestApi {
if (devices.isEmpty()) {
throw new RuntimeException("RestApi.getLocalDevice: devices is empty.");
}
Log.v(TAG, "getLocalDevice: Looking for local device ID " + mLocalDeviceId);
LogV("getLocalDevice: Looking for local device ID " + mLocalDeviceId);
for (Device d : devices) {
if (d.deviceID.equals(mLocalDeviceId)) {
return deepCopy(d, Device.class);
@ -816,21 +814,21 @@ public class RestApi {
/**
* Event triggered by {@link RunConditionMonitor} routed here through {@link SyncthingService}.
*/
public void onSyncPreconditionChanged(RunConditionMonitor runConditionMonitor) {
Log.v(TAG, "onSyncPreconditionChanged: Event fired.");
public void applyCustomRunConditions(RunConditionMonitor runConditionMonitor) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
synchronized (mConfigLock) {
Boolean configChanged = false;
// Check if the config has been loaded.
if (mConfig == null) {
Log.d(TAG, "onSyncPreconditionChanged: mConfig is not ready yet.");
Log.w(TAG, "applyCustomRunConditions: mConfig is not ready yet.");
return;
}
// Check if the folders are available from config.
if (mConfig.folders != null) {
for (Folder folder : mConfig.folders) {
// LogV("applyCustomRunConditions: Processing config of folder(" + folder.label + ")");
Boolean folderCustomSyncConditionsEnabled = sharedPreferences.getBoolean(
Constants.DYN_PREF_OBJECT_CUSTOM_SYNC_CONDITIONS(Constants.PREF_OBJECT_PREFIX_FOLDER + folder.id), false
);
@ -838,22 +836,23 @@ public class RestApi {
Boolean syncConditionsMet = runConditionMonitor.checkObjectSyncConditions(
Constants.PREF_OBJECT_PREFIX_FOLDER + folder.id
);
Log.v(TAG, "onSyncPreconditionChanged: syncFolder(" + folder.id + ")=" + (syncConditionsMet ? "1" : "0"));
LogV("applyCustomRunConditions: f(" + folder.label + ")=" + (syncConditionsMet ? "1" : "0"));
if (folder.paused != !syncConditionsMet) {
folder.paused = !syncConditionsMet;
Log.d(TAG, "onSyncPreconditionChanged: syncFolder(" + folder.id + ")=" + (syncConditionsMet ? ">1" : ">0"));
Log.d(TAG, "applyCustomRunConditions: f(" + folder.label + ")=" + (syncConditionsMet ? ">1" : ">0"));
configChanged = true;
}
}
}
} else {
Log.d(TAG, "onSyncPreconditionChanged: mConfig.folders is not ready yet.");
Log.d(TAG, "applyCustomRunConditions: mConfig.folders is not ready yet.");
return;
}
// Check if the devices are available from config.
if (mConfig.devices != null) {
for (Device device : mConfig.devices) {
// LogV("applyCustomRunConditions: Processing config of device(" + device.name + ")");
Boolean deviceCustomSyncConditionsEnabled = sharedPreferences.getBoolean(
Constants.DYN_PREF_OBJECT_CUSTOM_SYNC_CONDITIONS(Constants.PREF_OBJECT_PREFIX_DEVICE + device.deviceID), false
);
@ -861,23 +860,31 @@ public class RestApi {
Boolean syncConditionsMet = runConditionMonitor.checkObjectSyncConditions(
Constants.PREF_OBJECT_PREFIX_DEVICE + device.deviceID
);
Log.v(TAG, "onSyncPreconditionChanged: syncDevice(" + device.deviceID + ")=" + (syncConditionsMet ? "1" : "0"));
LogV("applyCustomRunConditions: d(" + device.name + ")=" + (syncConditionsMet ? "1" : "0"));
if (device.paused != !syncConditionsMet) {
device.paused = !syncConditionsMet;
Log.d(TAG, "onSyncPreconditionChanged: syncDevice(" + device.deviceID + ")=" + (syncConditionsMet ? ">1" : ">0"));
Log.d(TAG, "applyCustomRunConditions: d(" + device.name + ")=" + (syncConditionsMet ? ">1" : ">0"));
configChanged = true;
}
}
}
} else {
Log.d(TAG, "onSyncPreconditionChanged: mConfig.devices is not ready yet.");
Log.d(TAG, "applyCustomRunConditions: mConfig.devices is not ready yet.");
return;
}
if (configChanged) {
Log.v(TAG, "onSyncPreconditionChanged: Sending changed config ...");
LogV("applyCustomRunConditions: Sending changed config ...");
sendConfig();
} else {
LogV("applyCustomRunConditions: No action was necessary.");
}
}
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -39,6 +39,8 @@ public class RunConditionMonitor {
private static final String TAG = "RunConditionMonitor";
private static final Boolean ENABLE_VERBOSE_LOG = false;
private static final String POWER_SOURCE_CHARGER_BATTERY = "ac_and_battery_power";
private static final String POWER_SOURCE_CHARGER = "ac_power";
private static final String POWER_SOURCE_BATTERY = "battery_power";
@ -109,7 +111,7 @@ public class RunConditionMonitor {
public RunConditionMonitor(Context context,
OnShouldRunChangedListener onShouldRunChangedListener,
OnSyncPreconditionChangedListener onSyncPreconditionChangedListener) {
Log.v(TAG, "Created new instance");
LogV("Created new instance");
((SyncthingApp) context.getApplicationContext()).component().inject(this);
mContext = context;
res = mContext.getResources();
@ -144,7 +146,7 @@ public class RunConditionMonitor {
}
public void shutdown() {
Log.v(TAG, "Shutting down");
LogV("Shutting down");
if (mSyncStatusObserverHandle != null) {
ContentResolver.removeStatusChangeListener(mSyncStatusObserverHandle);
mSyncStatusObserverHandle = null;
@ -303,14 +305,14 @@ public class RunConditionMonitor {
switch (prefPowerSource) {
case POWER_SOURCE_CHARGER:
if (!isCharging()) {
Log.v(TAG, "decideShouldRun: POWER_SOURCE_AC && !isCharging");
LogV("decideShouldRun: POWER_SOURCE_AC && !isCharging");
mRunDecisionExplanation = res.getString(R.string.reason_not_charging);
return false;
}
break;
case POWER_SOURCE_BATTERY:
if (isCharging()) {
Log.v(TAG, "decideShouldRun: POWER_SOURCE_BATTERY && isCharging");
LogV("decideShouldRun: POWER_SOURCE_BATTERY && isCharging");
mRunDecisionExplanation = res.getString(R.string.reason_not_on_battery_power);
return false;
}
@ -323,7 +325,7 @@ public class RunConditionMonitor {
// Power saving
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (prefRespectPowerSaving && isPowerSaving()) {
Log.v(TAG, "decideShouldRun: prefRespectPowerSaving && isPowerSaving");
LogV("decideShouldRun: prefRespectPowerSaving && isPowerSaving");
mRunDecisionExplanation = res.getString(R.string.reason_not_while_power_saving);
return false;
}
@ -331,7 +333,7 @@ public class RunConditionMonitor {
// Android global AutoSync setting.
if (prefRespectMasterSync && !ContentResolver.getMasterSyncAutomatically()) {
Log.v(TAG, "decideShouldRun: prefRespectMasterSync && !getMasterSyncAutomatically");
LogV("decideShouldRun: prefRespectMasterSync && !getMasterSyncAutomatically");
mRunDecisionExplanation = res.getString(R.string.reason_not_while_auto_sync_data_disabled);
return false;
}
@ -341,7 +343,7 @@ public class RunConditionMonitor {
mRunDecisionExplanation += scr.explanation;
if (scr.conditionMet) {
// Mobile data is connected.
Log.v(TAG, "decideShouldRun: checkConditionSyncOnMobileData");
LogV("decideShouldRun: checkConditionSyncOnMobileData");
return true;
}
@ -350,19 +352,19 @@ public class RunConditionMonitor {
mRunDecisionExplanation += scr.explanation;
if (scr.conditionMet) {
// Wifi is connected.
Log.v(TAG, "decideShouldRun: checkConditionSyncOnWifi");
LogV("decideShouldRun: checkConditionSyncOnWifi");
scr = checkConditionSyncOnMeteredWifi(Constants.PREF_RUN_ON_METERED_WIFI);
mRunDecisionExplanation += scr.explanation;
if (scr.conditionMet) {
// Wifi type is allowed.
Log.v(TAG, "decideShouldRun: checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi");
LogV("decideShouldRun: checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi");
scr = checkConditionSyncOnWhitelistedWifi(Constants.PREF_USE_WIFI_SSID_WHITELIST, Constants.PREF_WIFI_SSID_WHITELIST);
mRunDecisionExplanation += scr.explanation;
if (scr.conditionMet) {
// Wifi is whitelisted.
Log.v(TAG, "decideShouldRun: checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi && checkConditionSyncOnWhitelistedWifi");
LogV("decideShouldRun: checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi && checkConditionSyncOnWhitelistedWifi");
return true;
}
}
@ -370,7 +372,7 @@ public class RunConditionMonitor {
// Run in flight mode.
if (prefRunInFlightMode && isFlightMode()) {
Log.v(TAG, "decideShouldRun: prefRunInFlightMode && isFlightMode");
LogV("decideShouldRun: prefRunInFlightMode && isFlightMode");
mRunDecisionExplanation += "\n" + res.getString(R.string.reason_on_flight_mode);
return true;
}
@ -378,7 +380,7 @@ public class RunConditionMonitor {
/**
* If none of the above run conditions matched, don't run.
*/
Log.v(TAG, "decideShouldRun: return false");
LogV("decideShouldRun: return false");
return false;
}
@ -390,7 +392,7 @@ public class RunConditionMonitor {
SyncConditionResult scr = checkConditionSyncOnMobileData(Constants.DYN_PREF_OBJECT_SYNC_ON_MOBILE_DATA(objectPrefixAndId));
if (scr.conditionMet) {
// Mobile data is connected.
Log.v(TAG, "checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnMobileData");
LogV("checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnMobileData");
return true;
}
@ -398,12 +400,12 @@ public class RunConditionMonitor {
scr = checkConditionSyncOnWifi(Constants.DYN_PREF_OBJECT_SYNC_ON_WIFI(objectPrefixAndId));
if (scr.conditionMet) {
// Wifi is connected.
Log.v(TAG, "checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnWifi");
LogV("checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnWifi");
scr = checkConditionSyncOnMeteredWifi(Constants.DYN_PREF_OBJECT_SYNC_ON_METERED_WIFI(objectPrefixAndId));
if (scr.conditionMet) {
// Wifi type is allowed.
Log.v(TAG, "checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi");
LogV("checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi");
scr = checkConditionSyncOnWhitelistedWifi(
Constants.DYN_PREF_OBJECT_USE_WIFI_SSID_WHITELIST(objectPrefixAndId),
@ -411,7 +413,7 @@ public class RunConditionMonitor {
);
if (scr.conditionMet) {
// Wifi is whitelisted.
Log.v(TAG, "checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi && checkConditionSyncOnWhitelistedWifi");
LogV("checkObjectSyncConditions(" + objectPrefixAndId + "): checkConditionSyncOnWifi && checkConditionSyncOnMeteredWifi && checkConditionSyncOnWhitelistedWifi");
return true;
}
}
@ -426,11 +428,11 @@ public class RunConditionMonitor {
private boolean wifiWhitelistConditionMet (boolean prefWifiWhitelistEnabled,
Set<String> whitelistedWifiSsids) throws LocationUnavailableException {
if (!prefWifiWhitelistEnabled) {
Log.v(TAG, "handleWifiWhitelist: !prefWifiWhitelistEnabled");
LogV("handleWifiWhitelist: !prefWifiWhitelistEnabled");
return true;
}
if (isWifiConnectionWhitelisted(whitelistedWifiSsids)) {
Log.v(TAG, "handleWifiWhitelist: isWifiConnectionWhitelisted");
LogV("handleWifiWhitelist: isWifiConnectionWhitelisted");
return true;
}
return false;
@ -572,4 +574,9 @@ public class RunConditionMonitor {
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -52,6 +52,8 @@ public class SyncthingRunnable implements Runnable {
private static final String TAG = "SyncthingRunnable";
private static final String TAG_NATIVE = "SyncthingNativeCode";
private static final String TAG_NICE = "SyncthingRunnableIoNice";
private static final Boolean ENABLE_VERBOSE_LOG = false;
private static final int LOG_FILE_MAX_LINES = 10;
private static final AtomicReference<Process> mSyncthing = new AtomicReference<>();
@ -178,7 +180,7 @@ public class SyncthingRunnable implements Runnable {
br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charsets.UTF_8));
String line;
while ((line = br.readLine()) != null) {
Log.println(Log.INFO, TAG_NATIVE, line);
Log.i(TAG_NATIVE, line);
capturedStdOut = capturedStdOut + line + "\n";
}
} catch (IOException e) {
@ -195,7 +197,7 @@ public class SyncthingRunnable implements Runnable {
niceSyncthing();
exitCode = process.waitFor();
Log.i(TAG, "Syncthing exited with code " + exitCode);
LogV("Syncthing exited with code " + exitCode);
mSyncthing.set(null);
if (lInfo != null) {
lInfo.join();
@ -381,13 +383,13 @@ public class SyncthingRunnable implements Runnable {
int exitCode;
List<String> syncthingPIDs = getSyncthingPIDs(true);
if (syncthingPIDs.isEmpty()) {
Log.d(TAG, "killSyncthing: Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY);
LogV("killSyncthing: Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY);
return;
}
for (String syncthingPID : syncthingPIDs) {
exitCode = Util.runShellCommand("kill -SIGINT " + syncthingPID + "\n", mUseRoot);
if (exitCode == 0) {
Log.d(TAG, "Sent kill SIGINT to process " + syncthingPID);
LogV("Sent kill SIGINT to process " + syncthingPID);
} else {
Log.w(TAG, "Failed to send kill SIGINT to process " + syncthingPID +
" exit code " + Integer.toString(exitCode));
@ -397,11 +399,11 @@ public class SyncthingRunnable implements Runnable {
/**
* Wait for the syncthing instance to end.
*/
Log.v(TAG, "Waiting for all syncthing instances to end ...");
LogV("Waiting for all syncthing instances to end ...");
while (!getSyncthingPIDs(false).isEmpty()) {
SystemClock.sleep(50);
}
Log.v(TAG, "killSyncthing: Complete.");
Log.d(TAG, "killSyncthing: Complete.");
}
/**
@ -556,4 +558,10 @@ public class SyncthingRunnable implements Runnable {
}
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -48,6 +48,8 @@ public class SyncthingService extends Service {
private static final String TAG = "SyncthingService";
private static final Boolean ENABLE_VERBOSE_LOG = false;
/**
* Intent action to perform a Syncthing restart.
*/
@ -217,7 +219,7 @@ public class SyncthingService extends Service {
*/
@Override
public void onCreate() {
Log.v(TAG, "onCreate");
LogV("onCreate");
super.onCreate();
PRNGFixes.apply();
((SyncthingApp) getApplication()).component().inject(this);
@ -245,7 +247,7 @@ public class SyncthingService extends Service {
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "onStartCommand");
Log.d(TAG, "onStartCommand");
if (!mStoragePermissionGranted) {
Log.e(TAG, "User revoked storage permission. Stopping service.");
if (mNotificationHandler != null) {
@ -286,7 +288,7 @@ public class SyncthingService extends Service {
*/
mRunConditionMonitor = new RunConditionMonitor(SyncthingService.this,
this::onShouldRunDecisionChanged,
this::onSyncPreconditionChanged
this::applyCustomRunConditions
);
}
}
@ -400,7 +402,6 @@ public class SyncthingService extends Service {
if (mCurrentState == State.DISABLED) {
return;
}
Log.v(TAG, "Stopping syncthing");
shutdown(State.DISABLED);
}
}
@ -410,16 +411,15 @@ public class SyncthingService extends Service {
* After sync preconditions changed, we need to inform {@link RestApi} to pause or
* unpause devices and folders as defined in per-object sync preferences.
*/
private void onSyncPreconditionChanged(RunConditionMonitor runConditionMonitor) {
private void applyCustomRunConditions(RunConditionMonitor runConditionMonitor) {
synchronized (mStateLock) {
if (mRestApi != null && mCurrentState == State.ACTIVE) {
// Forward event because syncthing is running.
mRestApi.onSyncPreconditionChanged(runConditionMonitor);
mRestApi.applyCustomRunConditions(runConditionMonitor);
return;
}
}
Log.v(TAG, "onSyncPreconditionChanged: Event fired while syncthing is not running.");
Boolean configChanged = false;
ConfigXml configXml;
@ -428,7 +428,7 @@ public class SyncthingService extends Service {
try {
configXml.loadConfig();
} catch (ConfigXml.OpenConfigException e) {
mNotificationHandler.showCrashedNotification(R.string.config_read_failed, "onSyncPreconditionChanged:ConfigXml.OpenConfigException");
mNotificationHandler.showCrashedNotification(R.string.config_read_failed, "applyCustomRunConditions:ConfigXml.OpenConfigException");
synchronized (mStateLock) {
onServiceStateChange(State.ERROR);
}
@ -439,7 +439,7 @@ public class SyncthingService extends Service {
List<Folder> folders = configXml.getFolders();
if (folders != null) {
for (Folder folder : folders) {
// Log.v(TAG, "onSyncPreconditionChanged: Processing config of folder.id=" + folder.id);
// LogV("applyCustomRunConditions: Processing config of folder(" + folder.label + ")");
Boolean folderCustomSyncConditionsEnabled = mPreferences.getBoolean(
Constants.DYN_PREF_OBJECT_CUSTOM_SYNC_CONDITIONS(Constants.PREF_OBJECT_PREFIX_FOLDER + folder.id), false
);
@ -447,16 +447,16 @@ public class SyncthingService extends Service {
Boolean syncConditionsMet = runConditionMonitor.checkObjectSyncConditions(
Constants.PREF_OBJECT_PREFIX_FOLDER + folder.id
);
Log.v(TAG, "onSyncPreconditionChanged: syncFolder(" + folder.id + ")=" + (syncConditionsMet ? "1" : "0"));
LogV("applyCustomRunConditions: f(" + folder.label + ")=" + (syncConditionsMet ? "1" : "0"));
if (folder.paused != !syncConditionsMet) {
configXml.setFolderPause(folder.id, !syncConditionsMet);
Log.d(TAG, "onSyncPreconditionChanged: syncFolder(" + folder.id + ")=" + (syncConditionsMet ? ">1" : ">0"));
Log.d(TAG, "applyCustomRunConditions: f(" + folder.label + ")=" + (syncConditionsMet ? ">1" : ">0"));
configChanged = true;
}
}
}
} else {
Log.d(TAG, "onSyncPreconditionChanged: folders == null");
Log.d(TAG, "applyCustomRunConditions: folders == null");
return;
}
@ -464,7 +464,7 @@ public class SyncthingService extends Service {
List<Device> devices = configXml.getDevices(false);
if (devices != null) {
for (Device device : devices) {
// Log.v(TAG, "onSyncPreconditionChanged: Processing config of device.id=" + device.deviceID);
// LogV("applyCustomRunConditions: Processing config of device(" + device.name + ")");
Boolean deviceCustomSyncConditionsEnabled = mPreferences.getBoolean(
Constants.DYN_PREF_OBJECT_CUSTOM_SYNC_CONDITIONS(Constants.PREF_OBJECT_PREFIX_DEVICE + device.deviceID), false
);
@ -472,22 +472,24 @@ public class SyncthingService extends Service {
Boolean syncConditionsMet = runConditionMonitor.checkObjectSyncConditions(
Constants.PREF_OBJECT_PREFIX_DEVICE + device.deviceID
);
Log.v(TAG, "onSyncPreconditionChanged: syncDevice(" + device.deviceID + ")=" + (syncConditionsMet ? "1" : "0"));
LogV("applyCustomRunConditions: d(" + device.name + ")=" + (syncConditionsMet ? "1" : "0"));
if (device.paused != !syncConditionsMet) {
configXml.setDevicePause(device.deviceID, !syncConditionsMet);
Log.d(TAG, "onSyncPreconditionChanged: syncDevice(" + device.deviceID + ")=" + (syncConditionsMet ? ">1" : ">0"));
Log.d(TAG, "applyCustomRunConditions: d(" + device.name + ")=" + (syncConditionsMet ? ">1" : ">0"));
configChanged = true;
}
}
}
} else {
Log.d(TAG, "onSyncPreconditionChanged: devices == null");
Log.d(TAG, "applyCustomRunConditions: devices == null");
return;
}
if (configChanged) {
Log.v(TAG, "onSyncPreconditionChanged: Saving changed config to disk ...");
LogV("applyCustomRunConditions: Saving changed config ...");
configXml.saveChanges();
} else {
LogV("applyCustomRunConditions: No action was necessary.");
}
}
@ -587,7 +589,7 @@ public class SyncthingService extends Service {
onServiceStateChange(State.ACTIVE);
}
if (mRestApi != null && mRunConditionMonitor != null) {
mRestApi.onSyncPreconditionChanged(mRunConditionMonitor);
mRestApi.applyCustomRunConditions(mRunConditionMonitor);
}
/**
@ -618,7 +620,7 @@ public class SyncthingService extends Service {
*/
@Override
public void onDestroy() {
Log.v(TAG, "onDestroy");
Log.d(TAG, "onDestroy");
if (mRunConditionMonitor != null) {
/**
* Shut down the OnShouldRunChangedListener so we won't get interrupted by run
@ -662,7 +664,6 @@ public class SyncthingService extends Service {
return;
}
Log.i(TAG, "Shutting down");
synchronized (mStateLock) {
onServiceStateChange(newState);
}
@ -685,13 +686,13 @@ public class SyncthingService extends Service {
if (mSyncthingRunnable != null) {
mSyncthingRunnable.killSyncthing();
if (mSyncthingRunnableThread != null) {
Log.v(TAG, "Waiting for mSyncthingRunnableThread to finish after killSyncthing ...");
LogV("Waiting for mSyncthingRunnableThread to finish after killSyncthing ...");
try {
mSyncthingRunnableThread.join();
} catch (InterruptedException e) {
Log.w(TAG, "mSyncthingRunnableThread InterruptedException");
}
Log.v(TAG, "Finished mSyncthingRunnableThread.");
Log.d(TAG, "Finished mSyncthingRunnableThread.");
mSyncthingRunnableThread = null;
}
mSyncthingRunnable = null;
@ -711,7 +712,7 @@ public class SyncthingService extends Service {
if (mRunConditionMonitor == null) {
return;
}
Log.v(TAG, "Forced re-evaluating run conditions ...");
Log.d(TAG, "Forced re-evaluating run conditions ...");
mRunConditionMonitor.updateShouldRunDecision();
}
@ -742,7 +743,7 @@ public class SyncthingService extends Service {
* Called to notify listeners of an API change.
*/
private void onServiceStateChange(State newState) {
Log.v(TAG, "onServiceStateChange: from " + mCurrentState + " to " + newState);
Log.i(TAG, "onServiceStateChange: from " + mCurrentState + " to " + newState);
mCurrentState = newState;
mHandler.post(() -> {
mNotificationHandler.updatePersistentNotification(this);
@ -793,7 +794,7 @@ public class SyncthingService extends Service {
*/
public boolean exportConfig() {
Boolean failSuccess = true;
Log.v(TAG, "exportConfig BEGIN");
Log.d(TAG, "exportConfig BEGIN");
if (mCurrentState != State.DISABLED) {
// Shutdown synchronously.
@ -849,7 +850,7 @@ public class SyncthingService extends Service {
* https://developer.android.com/reference/java/nio/file/package-summary
*/
if (Build.VERSION.SDK_INT >= 26) {
Log.v(TAG, "exportConfig: Exporting index database");
Log.d(TAG, "exportConfig: Exporting index database");
Path databaseSourcePath = Paths.get(this.getFilesDir() + "/" + Constants.INDEX_DB_FOLDER);
Path databaseExportPath = Paths.get(Constants.EXPORT_PATH + "/" + Constants.INDEX_DB_FOLDER);
if (java.nio.file.Files.exists(databaseExportPath)) {
@ -871,7 +872,7 @@ public class SyncthingService extends Service {
Log.e(TAG, "Failed to copy directory '" + databaseSourcePath + "' to '" + databaseExportPath + "'");
}
}
Log.v(TAG, "exportConfig END");
Log.d(TAG, "exportConfig END");
// Start syncthing after export if run conditions apply.
if (mLastDeterminedShouldRun) {
@ -897,7 +898,7 @@ public class SyncthingService extends Service {
*/
public boolean importConfig() {
Boolean failSuccess = true;
Log.v(TAG, "importConfig BEGIN");
Log.d(TAG, "importConfig BEGIN");
if (mCurrentState != State.DISABLED) {
// Shutdown synchronously.
@ -949,17 +950,17 @@ public class SyncthingService extends Service {
case "notification_type":
case "notify_crashes":
case "start_into_web_gui":
Log.v(TAG, "importConfig: Ignoring deprecated pref \"" + prefKey + "\".");
LogV("importConfig: Ignoring deprecated pref \"" + prefKey + "\".");
break;
// Cached information which is not available on SettingsActivity.
case Constants.PREF_DEBUG_FACILITIES_AVAILABLE:
case Constants.PREF_EVENT_PROCESSOR_LAST_SYNC_ID:
case Constants.PREF_LAST_BINARY_VERSION:
case Constants.PREF_LOCAL_DEVICE_ID:
Log.v(TAG, "importConfig: Ignoring cache pref \"" + prefKey + "\".");
LogV("importConfig: Ignoring cache pref \"" + prefKey + "\".");
break;
default:
Log.v(TAG, "importConfig: Adding pref \"" + prefKey + "\" to commit ...");
Log.i(TAG, "importConfig: Adding pref \"" + prefKey + "\" to commit ...");
// The editor only provides typed setters.
if (e.getValue() instanceof Boolean) {
@ -975,7 +976,7 @@ public class SyncthingService extends Service {
} else if (e.getValue() instanceof Set) {
editor.putStringSet(prefKey, (Set<String>) e.getValue());
} else {
Log.v(TAG, "importConfig: SharedPref type " + e.getValue().getClass().getName() + " is unknown");
Log.w(TAG, "importConfig: SharedPref type " + e.getValue().getClass().getName() + " is unknown");
}
break;
}
@ -1017,7 +1018,7 @@ public class SyncthingService extends Service {
if (Build.VERSION.SDK_INT >= 26) {
Path databaseImportPath = Paths.get(Constants.EXPORT_PATH + "/" + Constants.INDEX_DB_FOLDER);
if (java.nio.file.Files.exists(databaseImportPath)) {
Log.v(TAG, "importConfig: Importing index database");
Log.d(TAG, "importConfig: Importing index database");
Path databaseTargetPath = Paths.get(this.getFilesDir() + "/" + Constants.INDEX_DB_FOLDER);
try {
FileUtils.deleteDirectoryRecursively(databaseTargetPath);
@ -1037,7 +1038,7 @@ public class SyncthingService extends Service {
}
}
}
Log.v(TAG, "importConfig END");
Log.d(TAG, "importConfig END");
// Start syncthing after import if run conditions apply.
if (mLastDeterminedShouldRun) {
@ -1052,4 +1053,10 @@ public class SyncthingService extends Service {
}
return failSuccess;
}
private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage);
}
}
}

View file

@ -22,6 +22,7 @@
android:layout_height="wrap_content"
android:checked="true"
android:focusable="false"
android:clickable="false"
android:visibility="gone"
app:buttonTint="#76ff03"/>
@ -31,6 +32,7 @@
android:layout_height="wrap_content"
android:checked="true"
android:focusable="false"
android:clickable="false"
android:visibility="gone"
app:buttonTint="#fff103"/>
@ -40,6 +42,7 @@
android:layout_height="wrap_content"
android:checked="true"
android:focusable="false"
android:clickable="false"
android:visibility="gone"
app:buttonTint="#ffae03"/>