From f9c8a7dc8cb2ef676d37bece6b32908be190be69 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 19 Mar 2016 21:50:26 +0100 Subject: [PATCH] Show notification if new folders is shared to local device (fixes #295). --- .../activities/MainActivity.java | 34 ----------- .../activities/SettingsActivity.java | 3 - .../fragments/DeviceFragment.java | 2 +- .../fragments/FolderFragment.java | 11 ++-- .../syncthing/EventProcessor.java | 58 ++++++++++++++----- src/main/res/values/strings.xml | 2 + 6 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index 21e6ee26..9f469612 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -70,9 +70,6 @@ public class MainActivity extends SyncthingActivity private static final int REQUEST_WRITE_STORAGE = 142; - public static final String ACTION_ADD_DEVICE = "add_device"; - public static final String EXTRA_DEVICE_ID = "device_id"; - private AlertDialog mLoadingDialog; private AlertDialog mDisabledDialog; @@ -278,37 +275,6 @@ public class MainActivity extends SyncthingActivity onNewIntent(getIntent()); } - /** - * Shows dialog to add rejected devices. - */ - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - - // intent's action will be null if notification is selected - if (intent.getAction() == null) { - return; - } - switch (intent.getAction()) { - case ACTION_ADD_DEVICE: - final String deviceId = intent.getStringExtra(EXTRA_DEVICE_ID); - new AlertDialog.Builder(this) - .setMessage(getString(R.string.device_rejected, deviceId)) - .setPositiveButton(R.string.add_device, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - RestApi.Device device = new RestApi.Device(); - device.deviceID = deviceId; - device.addresses = DeviceFragment.DYNAMIC_ADDRESS; - getApi().editDevice(device, MainActivity.this, null); - } - }) - .setNegativeButton(R.string.ignore, null) - .show(); - - } - } - @Override public void onDestroy() { super.onDestroy(); diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 951d9188..918bc542 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -24,9 +24,6 @@ public class SettingsActivity extends SyncthingActivity { * Must be set for {@link #ACTION_NODE_SETTINGS_FRAGMENT} and * {@link #ACTION_REPO_SETTINGS_FRAGMENT} to determine if an existing folder/device should be * edited or a new one created. - *

- * If this is false, {@link FolderFragment#EXTRA_REPO_ID} or - * {@link DeviceFragment#EXTRA_NODE_ID} must be set (according to the selected fragment). */ public static final String EXTRA_IS_CREATE = "create"; diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java index 676de214..72c7ff3e 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceFragment.java @@ -375,7 +375,7 @@ public class DeviceFragment extends Fragment implements private void initDevice() { mDevice = new RestApi.Device(); mDevice.name = ""; - mDevice.deviceID = ""; + mDevice.deviceID = getActivity().getIntent().getStringExtra(EXTRA_NODE_ID); mDevice.addresses = DYNAMIC_ADDRESS; mDevice.compression = METADATA.getValue(getActivity()); mDevice.introducer = false; diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java index bb989051..edc69705 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java @@ -52,12 +52,10 @@ import static java.lang.String.valueOf; public class FolderFragment extends Fragment implements SyncthingActivity.OnServiceConnectedListener, SyncthingService.OnApiChangeListener { - /** - * The ID of the folder to be edited. To be used with {@link com.nutomic.syncthingandroid.activities.SettingsActivity#EXTRA_IS_CREATE} - * set to false. - */ public static final String EXTRA_REPO_ID = "folder_id"; + public static final String EXTRA_DEVICE_ID = "device_id"; + private static final int DIRECTORY_REQUEST_CODE = 234; private static final String TAG = "EditFolderFragment"; @@ -349,11 +347,14 @@ public class FolderFragment extends Fragment private void initFolder() { mFolder = new RestApi.Folder(); - mFolder.id = ""; + mFolder.id = getActivity().getIntent().getStringExtra(EXTRA_REPO_ID); mFolder.path = ""; mFolder.rescanIntervalS = 259200; // Scan every 3 days (in case inotify dropped some changes) mFolder.deviceIds = new ArrayList<>(); mFolder.versioning = new Versioning(); + String deviceId = getActivity().getIntent().getStringExtra(EXTRA_DEVICE_ID); + if (deviceId != null) + mFolder.deviceIds.add(deviceId); } private void prepareEditMode() { diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java index a12a681f..35b9e07d 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/EventProcessor.java @@ -14,7 +14,9 @@ import android.support.v4.app.NotificationCompat; import android.util.Log; import com.nutomic.syncthingandroid.R; -import com.nutomic.syncthingandroid.activities.MainActivity; +import com.nutomic.syncthingandroid.activities.SettingsActivity; +import com.nutomic.syncthingandroid.fragments.DeviceFragment; +import com.nutomic.syncthingandroid.fragments.FolderFragment; import org.json.JSONException; import org.json.JSONObject; @@ -94,25 +96,38 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene String deviceId = data.getString("device"); Log.d(TAG, "Unknwon device " + deviceId + " wants to connect"); - Intent intent = new Intent(mContext, MainActivity.class); - intent.setAction(MainActivity.ACTION_ADD_DEVICE); - intent.putExtra(MainActivity.EXTRA_DEVICE_ID, deviceId); + Intent intent = new Intent(mContext, SettingsActivity.class) + .setAction(SettingsActivity.ACTION_NODE_SETTINGS_FRAGMENT) + .putExtra(SettingsActivity.EXTRA_IS_CREATE, true) + .putExtra(DeviceFragment.EXTRA_NODE_ID, deviceId); PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); String title = mContext.getString(R.string.device_rejected, deviceId.substring(0, 7)); - Notification n = new NotificationCompat.Builder(mContext) - .setContentTitle(title) - .setContentIntent(pi) - .setSmallIcon(R.drawable.ic_stat_notify) - .setAutoCancel(true) - .build(); - NotificationManager nm = (NotificationManager) - mContext.getSystemService(Context.NOTIFICATION_SERVICE); + notify(title, pi); + break; + case "FolderRejected": + deviceId = data.getString("device"); + String folderId = data.getString("folder"); + Log.d(TAG, "Device " + deviceId + " wants to share folder " + folderId); - // Use random ID so previous notifications are not replaced. - nm.notify(new Random().nextInt(), n); + intent = new Intent(mContext, SettingsActivity.class) + .setAction(SettingsActivity.ACTION_REPO_SETTINGS_FRAGMENT) + .putExtra(SettingsActivity.EXTRA_IS_CREATE, true) + .putExtra(FolderFragment.EXTRA_DEVICE_ID, deviceId) + .putExtra(FolderFragment.EXTRA_REPO_ID, folderId); + pi = PendingIntent.getActivity(mContext, 0, intent, 0); + + String deviceName = null; + for (RestApi.Device d : mApi.getDevices(false)) { + if (d.deviceID.equals(deviceId)) + deviceName = RestApi.getDeviceDisplayName(d); + } + title = mContext.getString(R.string.folder_rejected, deviceName, folderId); + + notify(title, pi); + break; case "ItemFinished": File updatedFile = new File(data.getString("folderpath"), data.getString("item")); Log.i(TAG, "Notified media scanner about " + updatedFile.toString()); @@ -167,4 +182,19 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene mMainThreadHandler.removeCallbacks(this); } } + + private void notify(String title, PendingIntent pi) { + NotificationManager nm = (NotificationManager) + mContext.getSystemService(Context.NOTIFICATION_SERVICE); + Notification n = new NotificationCompat.Builder(mContext) + .setContentTitle(title) + .setContentIntent(pi) + .setSmallIcon(R.drawable.ic_stat_notify) + .setAutoCancel(true) + .build(); + // HACK: Use a random, deterministic ID between 1000 and 2000 to avoid duplicate + // notifications. + int notificationId = 1000 + title.hashCode() % 1000; + nm.notify(notificationId, n); + } } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 080ddd48..8f5495c3 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -36,6 +36,8 @@ Device %1$s wants to connect + Device %1$s wants to share folder %2$s + Ignore