mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-23 04:41:16 +00:00
Show notification if new folders is shared to local device (fixes #295).
This commit is contained in:
parent
0891109c42
commit
f9c8a7dc8c
6 changed files with 53 additions and 57 deletions
|
@ -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();
|
||||
|
|
|
@ -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.
|
||||
* <p/>
|
||||
* 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";
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@
|
|||
|
||||
<string name="device_rejected">Device %1$s wants to connect</string>
|
||||
|
||||
<string name="folder_rejected">Device %1$s wants to share folder %2$s</string>
|
||||
|
||||
<string name="ignore">Ignore</string>
|
||||
|
||||
<!-- FoldersFragment -->
|
||||
|
|
Loading…
Reference in a new issue