Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide notification handler in device and folder activities #2055

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.os.StrictMode;

import com.google.android.material.color.DynamicColors;
import com.nutomic.syncthingandroid.di.DaggerComponent;
import com.nutomic.syncthingandroid.di.DaggerDaggerComponent;
import com.nutomic.syncthingandroid.di.SyncthingModule;
import com.nutomic.syncthingandroid.util.Languages;

import javax.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

import com.google.gson.Gson;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.databinding.ActivityDeviceBinding;
import com.nutomic.syncthingandroid.model.Connections;
import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.service.NotificationHandler;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Compression;
import com.nutomic.syncthingandroid.util.TextWatcherAdapter;
Expand All @@ -40,6 +42,8 @@
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

/**
* Shows device details and allows changing them.
*/
Expand Down Expand Up @@ -136,9 +140,13 @@ public void onCheckedChanged(CompoundButton view, boolean isChecked) {
}
};

@Inject
NotificationHandler mNotificationHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((SyncthingApp) getApplication()).component().inject(this);
binding = ActivityDeviceBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

Expand Down Expand Up @@ -186,7 +194,7 @@ public void onDestroy() {
super.onDestroy();
SyncthingService syncthingService = getService();
if (syncthingService != null) {
syncthingService.getNotificationHandler().cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
mNotificationHandler.cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
syncthingService.unregisterOnServiceStateChangeListener(this::onServiceStateChange);
}
binding.id.removeTextChangedListener(mIdTextWatcher);
Expand Down Expand Up @@ -227,7 +235,7 @@ public void onSaveInstanceState(Bundle outState) {
private void onServiceConnected() {
Log.v(TAG, "onServiceConnected");
SyncthingService syncthingService = (SyncthingService) getService();
syncthingService.getNotificationHandler().cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
mNotificationHandler.cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
syncthingService.registerOnServiceStateChangeListener(this::onServiceStateChange);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.databinding.ActivityFirstStartBinding;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.util.PermissionUtil;
import com.nutomic.syncthingandroid.util.Util;
Expand Down Expand Up @@ -75,6 +76,7 @@ private enum Slide {
private ActivityFirstStartBinding binding;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

@SuppressLint("ClickableViewAccessibility")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.gson.Gson;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.databinding.FragmentFolderBinding;
import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.NotificationHandler;
import com.nutomic.syncthingandroid.service.RestApi;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.FileUtils;
Expand All @@ -51,6 +53,8 @@
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static com.nutomic.syncthingandroid.service.SyncthingService.State.ACTIVE;

import javax.inject.Inject;

/**
* Shows folder details and allows changing them.
*/
Expand Down Expand Up @@ -132,9 +136,13 @@ public void onCheckedChanged(CompoundButton view, boolean isChecked) {
}
};

@Inject
NotificationHandler mNotificationHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((SyncthingApp) getApplication()).component().inject(this);
binding = FragmentFolderBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

Expand Down Expand Up @@ -283,7 +291,7 @@ public void onDestroy() {
super.onDestroy();
SyncthingService syncthingService = getService();
if (syncthingService != null) {
syncthingService.getNotificationHandler().cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
mNotificationHandler.cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
syncthingService.unregisterOnServiceStateChangeListener(this::onServiceStateChange);
}
binding.label.removeTextChangedListener(mTextWatcher);
Expand Down Expand Up @@ -320,7 +328,7 @@ protected void onSaveInstanceState(Bundle outState) {
public void onServiceConnected() {
Log.v(TAG, "onServiceConnected");
SyncthingService syncthingService = (SyncthingService) getService();
syncthingService.getNotificationHandler().cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
mNotificationHandler.cancelConsentNotification(getIntent().getIntExtra(EXTRA_NOTIFICATION_ID, 0));
syncthingService.registerOnServiceStateChangeListener(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.annimon.stream.function.Consumer;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.fragments.DeviceListFragment;
import com.nutomic.syncthingandroid.fragments.DrawerFragment;
import com.nutomic.syncthingandroid.fragments.FolderListFragment;
Expand Down Expand Up @@ -102,7 +103,10 @@ public class MainActivity extends StateDialogActivity

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
@Inject SharedPreferences mPreferences;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

/**
* Handles various dialogs based on current state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.Iterables;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.model.Config;
import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.model.Options;
Expand Down Expand Up @@ -103,7 +104,10 @@ public static class SettingsFragment extends PreferenceFragment
private static final String KEY_ST_RESET_DELTAS = "st_reset_deltas";

@Inject NotificationHandler mNotificationHandler;
@Inject SharedPreferences mPreferences;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

private PreferenceGroup mCategoryRunConditions;
private CheckBoxPreference mRunConditions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.nutomic.syncthingandroid;
package com.nutomic.syncthingandroid.di;

import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.activities.DeviceActivity;
import com.nutomic.syncthingandroid.activities.FirstStartActivity;
import com.nutomic.syncthingandroid.activities.FolderActivity;
import com.nutomic.syncthingandroid.activities.FolderPickerActivity;
import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.activities.SettingsActivity;
Expand All @@ -23,6 +26,10 @@ public interface DaggerComponent {

void inject(SyncthingApp app);
void inject(MainActivity activity);

void inject(DeviceActivity activity);

void inject(FolderActivity activity);
void inject(FirstStartActivity activity);
void inject(FolderPickerActivity activity);
void inject(Languages languages);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.nutomic.syncthingandroid.di;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;

import javax.inject.Qualifier;

@Qualifier
@Retention(RUNTIME)
public @interface DefaultSharedPreferences {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.nutomic.syncthingandroid;
package com.nutomic.syncthingandroid.di;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.service.NotificationHandler;

import javax.inject.Singleton;
Expand All @@ -21,6 +22,7 @@ public SyncthingModule(SyncthingApp app) {

@Provides
@Singleton
@DefaultSharedPreferences
public SharedPreferences getPreferences() {
return PreferenceManager.getDefaultSharedPreferences(mApp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.activities.DeviceActivity;
import com.nutomic.syncthingandroid.activities.FolderActivity;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.model.CompletionInfo;
import com.nutomic.syncthingandroid.model.Device;
import com.nutomic.syncthingandroid.model.Event;
Expand Down Expand Up @@ -60,9 +61,12 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener

private final Context mContext;
private final RestApi mApi;
@Inject SharedPreferences mPreferences;
@Inject NotificationHandler mNotificationHandler;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

public EventProcessor(Context context, RestApi api) {
((SyncthingApp) context.getApplicationContext()).component().inject(this);
mContext = context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nutomic.syncthingandroid.service;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
Expand All @@ -18,7 +17,7 @@
import com.nutomic.syncthingandroid.activities.FirstStartActivity;
import com.nutomic.syncthingandroid.activities.LogActivity;
import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.service.SyncthingService.State;

import javax.inject.Inject;
Expand All @@ -37,7 +36,6 @@ public class NotificationHandler {
private static final String CHANNEL_PERSISTENT_WAITING = "03_syncthing_persistent_waiting";

private final Context mContext;
@Inject SharedPreferences mPreferences;
private final NotificationManager mNotificationManager;
private final NotificationChannel mPersistentChannel;
private final NotificationChannel mPersistentChannelWaiting;
Expand All @@ -46,6 +44,10 @@ public class NotificationHandler {
private Boolean lastStartForegroundService = false;
private Boolean appShutdownInProgress = false;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

public NotificationHandler(Context context) {
((SyncthingApp) context.getApplicationContext()).component().inject(this);
mContext = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.util.Log;

import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.model.RunConditionCheckResult;

import java.util.ArrayList;
Expand Down Expand Up @@ -59,9 +60,12 @@ public interface OnRunConditionChangedListener {
}

private final Context mContext;
@Inject SharedPreferences mPreferences;
private ReceiverManager mReceiverManager;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

/**
* Sending callback notifications through {@link OnRunConditionChangedListener} is enabled if not null.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.google.common.io.Files;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.util.Util;

Expand Down Expand Up @@ -56,10 +57,13 @@ public class SyncthingRunnable implements Runnable {
private final File mSyncthingBinary;
private String[] mCommand;
private final File mLogFile;
@Inject SharedPreferences mPreferences;
private final boolean mUseRoot;
@Inject NotificationHandler mNotificationHandler;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

public enum Command {
deviceid, // Output the device ID to the command line.
generate, // Generate keys, a config file and immediately exit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.io.Files;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.SyncthingApp;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.http.PollWebGuiAvailableTask;
import com.nutomic.syncthingandroid.model.RunConditionCheckResult;
import com.nutomic.syncthingandroid.util.ConfigXml;
Expand Down Expand Up @@ -159,7 +160,10 @@ public enum State {
private final SyncthingServiceBinder mBinder = new SyncthingServiceBinder(this);

@Inject NotificationHandler mNotificationHandler;
@Inject SharedPreferences mPreferences;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

/**
* Object that must be locked upon accessing mCurrentState
Expand Down Expand Up @@ -649,10 +653,6 @@ public RunConditionCheckResult getCurrentRunConditionCheckResult() {
return mCurrentCheckResult.get();
}

public NotificationHandler getNotificationHandler() {
return mNotificationHandler;
}

/**
* Exports the local config and keys to {@link Constants#EXPORT_PATH}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.util.Log;

import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.di.DefaultSharedPreferences;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.SyncthingRunnable;

Expand Down Expand Up @@ -54,7 +55,10 @@ public class OpenConfigException extends RuntimeException {
private static final int FOLDER_ID_APPENDIX_LENGTH = 4;

private final Context mContext;
@Inject SharedPreferences mPreferences;

@Inject
@DefaultSharedPreferences
SharedPreferences mPreferences;

private final File mConfigFile;

Expand Down
Loading
Loading