mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-23 04:41:16 +00:00
Added notification for connection from unknown device
This commit is contained in:
parent
c656c45081
commit
c280657861
3 changed files with 67 additions and 1 deletions
|
@ -69,6 +69,9 @@ 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;
|
||||
|
||||
|
@ -270,6 +273,35 @@ public class MainActivity extends SyncthingActivity
|
|||
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
|
||||
mDrawerLayout.setDrawerListener(mDrawerToggle);
|
||||
setOptimalDrawerWidth(findViewById(R.id.drawer));
|
||||
|
||||
onNewIntent(getIntent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows dialog to add rejected devices.
|
||||
*/
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
|
||||
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 = "";
|
||||
getApi().editDevice(device, MainActivity.this, null);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.ignore, null)
|
||||
.show();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package com.nutomic.syncthingandroid.syncthing;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
|
@ -7,12 +10,17 @@ import android.net.Uri;
|
|||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
|
||||
import com.nutomic.syncthingandroid.R;
|
||||
import com.nutomic.syncthingandroid.activities.MainActivity;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
|
@ -82,6 +90,29 @@ public class EventProcessor implements SyncthingService.OnWebGuiAvailableListene
|
|||
@Override
|
||||
public void onEvent(long id, String type, JSONObject data) throws JSONException {
|
||||
switch (type) {
|
||||
case "DeviceRejected":
|
||||
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);
|
||||
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);
|
||||
|
||||
// Use random ID so previous notifications are not replaced.
|
||||
nm.notify(new Random().nextInt(), n);
|
||||
case "ItemFinished":
|
||||
File updatedFile = new File(data.getString("folderpath"), data.getString("item"));
|
||||
Log.i(TAG, "Notified media scanner about " + updatedFile.toString());
|
||||
|
|
|
@ -34,6 +34,10 @@
|
|||
|
||||
<string name="toast_write_storage_permission_required">Write storage permission is required for this app</string>
|
||||
|
||||
<string name="device_rejected">Device %1$s wants to connect</string>
|
||||
|
||||
<string name="ignore">Ignore</string>
|
||||
|
||||
<!-- FoldersFragment -->
|
||||
|
||||
|
||||
|
@ -175,7 +179,6 @@
|
|||
<!-- ActionBar item -->
|
||||
<string name="delete_device">Delete Device</string>
|
||||
|
||||
<!-- Title for DeviceSettingsFragment in create mode -->
|
||||
<string name="add_device">Add Device</string>
|
||||
|
||||
<!-- Menu item to confirm adding a device -->
|
||||
|
|
Loading…
Reference in a new issue