2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-27 20:37:20 +00:00

Use lambdas where possible

Summary: Let Android Studio replace anonymous types with lambdas. No manual code change.

Test Plan: Compile and superficial behaviour test

Reviewers: #kde_connect, philipc

Reviewed By: #kde_connect, philipc

Subscribers: philipc, #kde_connect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D12229
This commit is contained in:
Nicolas Fella 2018-05-09 14:02:56 +02:00
parent 7536eb7427
commit e712c69e15
31 changed files with 1077 additions and 1615 deletions

View File

@ -23,10 +23,8 @@ android {
javaMaxHeapSize "2g"
}
compileOptions {
// Use Java 1.7, requires minSdk 8
//SSHD requires mina when running on JDK < 7
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main {

View File

@ -89,9 +89,7 @@ public class LanLink extends BaseLink {
//Log.e("LanLink", "Start listening");
//Create a thread to take care of incoming data for the new socket
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), StringsHelper.UTF8));
while (true) {
@ -118,7 +116,6 @@ public class LanLink extends BaseLink {
callback.linkDisconnected(LanLink.this);
}
}
}
}).start();
return oldSocket;

View File

@ -221,24 +221,19 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
if (isDeviceTrusted && !SslHelper.isCertificateStored(context, deviceId)) {
//Device paired with and old version, we can't use it as we lack the certificate
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(context, service -> {
Device device = service.getDevice(deviceId);
if (device == null) return;
device.unpair();
//Retry as unpaired
identityPacketReceived(identityPacket, socket, connectionStarted);
}
});
}
Log.i("KDE/LanLinkProvider", "Starting SSL handshake with " + identityPacket.getString("deviceName") + " trusted:" + isDeviceTrusted);
final SSLSocket sslsocket = SslHelper.convertToSslSocket(context, socket, deviceId, isDeviceTrusted, clientMode);
sslsocket.addHandshakeCompletedListener(new HandshakeCompletedListener() {
@Override
public void handshakeCompleted(HandshakeCompletedEvent event) {
sslsocket.addHandshakeCompletedListener(event -> {
String mode = clientMode ? "client" : "server";
try {
Certificate certificate = event.getPeerCertificates()[0];
@ -248,21 +243,15 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
} catch (Exception e) {
Log.e("KDE/LanLinkProvider", "Handshake as " + mode + " failed with " + identityPacket.getString("deviceName"));
e.printStackTrace();
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(context, service -> {
Device device = service.getDevice(deviceId);
if (device == null) return;
device.unpair();
}
});
}
}
});
//Handshake is blocking, so do it on another thread and free this thread to keep receiving new connection
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
try {
sslsocket.startHandshake();
} catch (Exception e) {
@ -274,7 +263,6 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
// Log.i("SupportedCiphers","cipher: " + cipher);
//}
}
}
}).start();
} else {
addLink(identityPacket, socket, connectionStarted);
@ -331,9 +319,7 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
e.printStackTrace();
return null;
}
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
while (listening) {
final int bufferSize = 1024 * 512;
byte[] data = new byte[bufferSize];
@ -347,7 +333,6 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
}
}
Log.w("UdpListener", "Stopping UDP listener");
}
}).start();
return server;
}
@ -356,9 +341,7 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
try {
tcpServer = openServerSocketOnFreePort(MIN_PORT);
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
while (listening) {
try {
Socket socket = tcpServer.accept();
@ -370,7 +353,6 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
}
}
Log.w("TcpListener", "Stopping TCP listener");
}
}).start();
} catch (Exception e) {
@ -401,9 +383,7 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
return;
}
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
String deviceListPrefs = PreferenceManager.getDefaultSharedPreferences(context).getString(CustomDevicesActivity.KEY_CUSTOM_DEVLIST_PREFERENCE, "");
ArrayList<String> iplist = new ArrayList<>();
@ -445,7 +425,6 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
socket.close();
}
}
}).start();
}

View File

@ -87,24 +87,18 @@ public class BackgroundService extends Service {
}
public static void addGuiInUseCounter(final Context activity, final boolean forceNetworkRefresh) {
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(activity, service -> {
boolean refreshed = service.acquireDiscoveryMode(activity);
if (!refreshed && forceNetworkRefresh) {
service.onNetworkChange();
}
}
});
}
public static void removeGuiInUseCounter(final Context activity) {
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(activity, service -> {
//If no user interface is open, close the connections open to other devices
service.releaseDiscoveryMode(activity);
}
});
}
@ -167,15 +161,12 @@ public class BackgroundService extends Service {
}
private void cleanDevices() {
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
for (Device d : devices.values()) {
if (!d.isPaired() && !d.isPairRequested() && !d.isPairRequestedByPeer() && !d.deviceShouldBeKeptAlive()) {
d.disconnect();
}
}
}
}).start();
}
@ -328,9 +319,7 @@ public class BackgroundService extends Service {
}
public static void RunCommand(final Context c, final InstanceCallback callback) {
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
if (callback != null) {
mutex.lock();
try {
@ -341,7 +330,6 @@ public class BackgroundService extends Service {
}
Intent serviceIntent = new Intent(c, BackgroundService.class);
c.startService(serviceIntent);
}
}).start();
}

View File

@ -638,12 +638,7 @@ public class Device implements BaseLink.PacketReceiver {
//Async
public void sendPacket(final NetworkPacket np, final SendPacketStatusCallback callback) {
new Thread(new Runnable() {
@Override
public void run() {
sendPacketBlocking(np, callback);
}
}).start();
new Thread(() -> sendPacketBlocking(np, callback)).start();
}
public boolean sendPacketBlocking(final NetworkPacket np, final SendPacketStatusCallback callback) {

View File

@ -43,41 +43,27 @@ public class KdeConnectBroadcastReceiver extends BroadcastReceiver {
Log.i("KdeConnect", "Ignoring, it's not me!");
return;
}
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(context, service -> {
}
});
break;
case Intent.ACTION_BOOT_COMPLETED:
Log.i("KdeConnect", "KdeConnectBroadcastReceiver");
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(context, service -> {
}
});
break;
case WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION:
case WifiManager.WIFI_STATE_CHANGED_ACTION:
case ConnectivityManager.CONNECTIVITY_ACTION:
Log.i("KdeConnect", "Connection state changed, trying to connect");
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(context, service -> {
service.onDeviceListChanged();
service.onNetworkChange();
}
});
break;
case Intent.ACTION_SCREEN_ON:
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.onNetworkChange();
}
});
BackgroundService.RunCommand(context, BackgroundService::onNetworkChange);
break;
default:
Log.i("BroadcastReceiver", "Ignoring broadcast event: " + intent.getAction());

View File

@ -69,13 +69,9 @@ public class ClipboardListener {
return;
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).post(() -> {
cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
listener = new ClipboardManager.OnPrimaryClipChangedListener() {
@Override
public void onPrimaryClipChanged() {
listener = () -> {
try {
ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
@ -94,10 +90,8 @@ public class ClipboardListener {
} catch (Exception e) {
//Probably clipboard was not text
}
}
};
cm.addPrimaryClipChangedListener(listener);
}
});
}

View File

@ -53,13 +53,10 @@ public class ClipboardPlugin extends Plugin {
return true;
}
private ClipboardListener.ClipboardObserver observer = new ClipboardListener.ClipboardObserver() {
@Override
public void clipboardChanged(String content) {
private ClipboardListener.ClipboardObserver observer = content -> {
NetworkPacket np = new NetworkPacket(ClipboardPlugin.PACKET_TYPE_CLIPBOARD);
np.set("content", content);
device.sendPacket(np);
}
};
@Override

View File

@ -67,12 +67,7 @@ public class FindMyPhoneActivity extends Activity {
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
findViewById(R.id.bFindMyPhone).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
findViewById(R.id.bFindMyPhone).setOnClickListener(view -> finish());
}
@Override

View File

@ -109,14 +109,11 @@ public class KeyListenerView extends View {
}
private void sendKeyPressPacket(final NetworkPacket np) {
BackgroundService.RunCommand(getContext(), new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(getContext(), service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendKeyboardPacket(np);
}
});
}

View File

@ -135,9 +135,7 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
final View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
decorView.setOnSystemUiVisibilityChangeListener(visibility -> {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
int fullscreenType = 0;
@ -156,7 +154,6 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
getWindow().getDecorView().setSystemUiVisibility(fullscreenType);
}
}
});
}
@ -214,16 +211,13 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
case MotionEvent.ACTION_MOVE:
mCurrentX = event.getX();
mCurrentY = event.getY();
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendMouseDelta(mCurrentX - mPrevX, mCurrentY - mPrevY, mCurrentSensitivity);
mPrevX = mCurrentX;
mPrevY = mCurrentY;
}
});
break;
}
@ -287,14 +281,11 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
getWindow().getDecorView().performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendSingleHold();
}
});
}
@ -305,28 +296,22 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendSingleClick();
}
});
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendDoubleClick();
}
});
return true;
}
@ -366,50 +351,38 @@ public class MousePadActivity extends AppCompatActivity implements GestureDetect
private void sendMiddleClick() {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendMiddleClick();
}
});
}
private void sendRightClick() {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendRightClick();
}
});
}
private void sendSingleHold() {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendSingleHold();
}
});
}
private void sendScroll(final float y) {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class);
if (mousePadPlugin == null) return;
mousePadPlugin.sendScroll(0, y);
}
});
}

View File

@ -78,9 +78,7 @@ public class MprisActivity extends AppCompatActivity {
protected void connectToPlugin(final String targetPlayerName) {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
final Device device = service.getDevice(deviceId);
final MprisPlugin mpris = device.getPlugin(MprisPlugin.class);
@ -93,12 +91,7 @@ public class MprisActivity extends AppCompatActivity {
mpris.setPlayerStatusUpdatedHandler("activity", new Handler() {
@Override
public void handleMessage(Message msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
updatePlayerStatus(mpris);
}
});
runOnUiThread(() -> updatePlayerStatus(mpris));
}
});
@ -112,9 +105,7 @@ public class MprisActivity extends AppCompatActivity {
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
runOnUiThread(new Runnable() {
@Override
public void run() {
runOnUiThread(() -> {
Spinner spinner = (Spinner) findViewById(R.id.player_spinner);
//String prevPlayer = (String)spinner.getSelectedItem();
spinner.setAdapter(adapter);
@ -171,12 +162,10 @@ public class MprisActivity extends AppCompatActivity {
spinner.setSelection(0);
}
updatePlayerStatus(mpris);
}
});
}
});
}
});
}
@ -196,12 +185,7 @@ public class MprisActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
super.onDestroy();
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.removeConnectionListener(connectionReceiver);
}
});
BackgroundService.RunCommand(MprisActivity.this, service -> service.removeConnectionListener(connectionReceiver));
}
private void updatePlayerStatus(MprisPlugin mpris) {
@ -327,78 +311,33 @@ public class MprisActivity extends AppCompatActivity {
getString(R.string.mpris_time_default));
final int interval_time = Integer.parseInt(interval_time_str);
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.addConnectionListener(connectionReceiver);
}
});
BackgroundService.RunCommand(MprisActivity.this, service -> service.addConnectionListener(connectionReceiver));
connectToPlugin(targetPlayerName);
findViewById(R.id.play_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
findViewById(R.id.play_button).setOnClickListener(view -> BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer == null) return;
targetPlayer.playPause();
}
});
}
});
}));
findViewById(R.id.prev_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
findViewById(R.id.prev_button).setOnClickListener(view -> BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer == null) return;
targetPlayer.previous();
}
});
}
});
}));
findViewById(R.id.rew_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
findViewById(R.id.rew_button).setOnClickListener(view -> BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer == null) return;
targetPlayer.seek(interval_time * -1);
}
});
}
});
}));
findViewById(R.id.ff_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
findViewById(R.id.ff_button).setOnClickListener(view -> BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer == null) return;
targetPlayer.seek(interval_time);
}
});
}
});
}));
findViewById(R.id.next_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
findViewById(R.id.next_button).setOnClickListener(view -> BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer == null) return;
targetPlayer.next();
}
});
}
});
}));
((SeekBar) findViewById(R.id.volume_seek)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
@ -411,33 +350,23 @@ public class MprisActivity extends AppCompatActivity {
@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer == null) return;
targetPlayer.setVolume(seekBar.getProgress());
}
});
}
});
positionSeekUpdateRunnable = new Runnable() {
@Override
public void run() {
positionSeekUpdateRunnable = () -> {
final SeekBar positionSeek = (SeekBar) findViewById(R.id.positionSeek);
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer != null) {
positionSeek.setProgress((int) (targetPlayer.getPosition()));
}
positionSeekUpdateHandler.removeCallbacks(positionSeekUpdateRunnable);
positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 1000);
}
});
}
};
positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200);
@ -454,14 +383,11 @@ public class MprisActivity extends AppCompatActivity {
@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(MprisActivity.this, service -> {
if (targetPlayer != null) {
targetPlayer.setPosition(seekBar.getProgress());
}
positionSeekUpdateHandler.postDelayed(positionSeekUpdateRunnable, 200);
}
});
}

View File

@ -211,9 +211,7 @@ public class MprisMediaSession implements SharedPreferences.OnSharedPreferenceCh
* Update the media control notification
*/
private void updateMediaNotification() {
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(context, service -> {
//If the user disabled the media notification, do not show it
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!prefs.getBoolean(context.getString(R.string.mpris_notification_key), true)) {
@ -394,7 +392,6 @@ public class MprisMediaSession implements SharedPreferences.OnSharedPreferenceCh
mediaSession.setActive(true);
final NotificationManager nm = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(MPRIS_MEDIA_NOTIFICATION_ID, notification.build());
}
});
}

View File

@ -98,9 +98,7 @@ public class NotificationFilterActivity extends AppCompatActivity {
setContentView(R.layout.activity_notification_filter);
appDatabase = new AppDatabase(NotificationFilterActivity.this, false);
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
PackageManager packageManager = getPackageManager();
List<ApplicationInfo> appList = packageManager.getInstalledApplications(0);
@ -116,20 +114,9 @@ public class NotificationFilterActivity extends AppCompatActivity {
apps[i].isEnabled = appDatabase.isEnabled(appInfo.packageName);
}
Arrays.sort(apps, new Comparator<AppListInfo>() {
@Override
public int compare(AppListInfo lhs, AppListInfo rhs) {
return StringsHelper.compare(lhs.name, rhs.name);
}
});
Arrays.sort(apps, (lhs, rhs) -> StringsHelper.compare(lhs.name, rhs.name));
runOnUiThread(new Runnable() {
@Override
public void run() {
displayAppList();
}
});
}
runOnUiThread(this::displayAppList);
}).start();
}
@ -140,13 +127,10 @@ public class NotificationFilterActivity extends AppCompatActivity {
AppListAdapter adapter = new AppListAdapter();
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listView.setOnItemClickListener((adapterView, view, i, l) -> {
boolean checked = listView.isItemChecked(i);
appDatabase.setEnabled(apps[i].pkg, checked);
apps[i].isEnabled = checked;
}
});
for (int i = 0; i < apps.length; i++) {

View File

@ -108,9 +108,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
appDatabase = new AppDatabase(context, true);
NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() {
@Override
public void onServiceStart(NotificationReceiver service) {
NotificationReceiver.RunCommand(context, service -> {
service.addListener(NotificationsPlugin.this);
@ -119,7 +117,6 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
if (serviceReady) {
sendCurrentNotifications(service);
}
}
});
return true;
@ -128,12 +125,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
@Override
public void onDestroy() {
NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() {
@Override
public void onServiceStart(NotificationReceiver service) {
service.removeListener(NotificationsPlugin.this);
}
});
NotificationReceiver.RunCommand(context, service -> service.removeListener(NotificationsPlugin.this));
}
@Override
@ -461,22 +453,14 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
if (np.getBoolean("request")) {
if (serviceReady) {
NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() {
@Override
public void onServiceStart(NotificationReceiver service) {
sendCurrentNotifications(service);
}
});
NotificationReceiver.RunCommand(context, this::sendCurrentNotifications);
}
} else if (np.has("cancel")) {
NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() {
@Override
public void onServiceStart(NotificationReceiver service) {
NotificationReceiver.RunCommand(context, service -> {
String dismissedId = np.getString("cancel");
cancelNotificationCompat(service, dismissedId);
}
});
} else if (np.has("requestReplyId") && np.has("message")) {
@ -497,18 +481,12 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
return new AlertDialog.Builder(deviceActivity)
.setTitle(R.string.pref_plugin_notifications)
.setMessage(R.string.no_permissions)
.setPositiveButton(R.string.open_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
.setPositiveButton(R.string.open_settings, (dialogInterface, i) -> {
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
deviceActivity.startActivityForResult(intent, MainActivity.RESULT_NEEDS_RELOAD);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
//Do nothing
}
})
.create();

View File

@ -202,12 +202,7 @@ public abstract class Plugin {
if (!hasMainActivity()) return null;
Button b = new Button(activity);
b.setText(getActionName());
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startMainActivity(activity);
}
});
b.setOnClickListener(view -> startMainActivity(activity));
return b;
}
@ -242,17 +237,9 @@ public abstract class Plugin {
return new AlertDialog.Builder(activity)
.setTitle(getDisplayName())
.setMessage(reason)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(activity, permissions, 0);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
.setPositiveButton(R.string.ok, (dialogInterface, i) -> ActivityCompat.requestPermissions(activity, permissions, 0))
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
//Do nothing
}
})
.create();
}

View File

@ -121,12 +121,7 @@ public class RemoteKeyboardPlugin extends Plugin {
releaseInstances();
}
if (RemoteKeyboardService.instance != null)
RemoteKeyboardService.instance.handler.post(new Runnable() {
@Override
public void run() {
RemoteKeyboardService.instance.updateInputView();
}
});
RemoteKeyboardService.instance.handler.post(() -> RemoteKeyboardService.instance.updateInputView());
return true;
}
@ -137,12 +132,7 @@ public class RemoteKeyboardPlugin extends Plugin {
if (instances.contains(this)) {
instances.remove(this);
if (instances.size() < 1 && RemoteKeyboardService.instance != null)
RemoteKeyboardService.instance.handler.post(new Runnable() {
@Override
public void run() {
RemoteKeyboardService.instance.updateInputView();
}
});
RemoteKeyboardService.instance.handler.post(() -> RemoteKeyboardService.instance.updateInputView());
}
} finally {
releaseInstances();

View File

@ -35,8 +35,7 @@ public class AddCommandDialog extends DialogFragment {
builder.setView(view);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
builder.setPositiveButton(R.string.ok, (dialog, id) -> {
if (getActivity() instanceof RunCommandActivity) {
@ -45,11 +44,8 @@ public class AddCommandDialog extends DialogFragment {
((RunCommandActivity) getActivity()).dialogResult(name, command);
}
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
builder.setNegativeButton(R.string.cancel, (dialog, id) -> {
});
return builder.create();

View File

@ -45,17 +45,10 @@ import java.util.Comparator;
public class RunCommandActivity extends AppCompatActivity {
private String deviceId;
private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = new RunCommandPlugin.CommandsChangedCallback() {
@Override
public void update() {
updateView();
}
};
private final RunCommandPlugin.CommandsChangedCallback commandsChangedCallback = this::updateView;
private void updateView() {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
final Device device = service.getDevice(deviceId);
final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
@ -64,9 +57,7 @@ public class RunCommandActivity extends AppCompatActivity {
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
runOnUiThread(() -> {
ListView view = (ListView) findViewById(R.id.runcommandslist);
final ArrayList<ListAdapter.Item> commandItems = new ArrayList<>();
@ -79,24 +70,18 @@ public class RunCommandActivity extends AppCompatActivity {
}
}
Collections.sort(commandItems, new Comparator<ListAdapter.Item>() {
@Override
public int compare(ListAdapter.Item lhs, ListAdapter.Item rhs) {
Collections.sort(commandItems, (lhs, rhs) -> {
String lName = ((CommandEntry) lhs).getName();
String rName = ((CommandEntry) rhs).getName();
return lName.compareTo(rName);
}
});
ListAdapter adapter = new ListAdapter(RunCommandActivity.this, commandItems);
view.setAdapter(adapter);
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
view.setOnItemClickListener((adapterView, view1, i, l) -> {
CommandEntry entry = (CommandEntry) commandItems.get(i);
plugin.runCommand(entry.getKey());
}
});
@ -107,9 +92,7 @@ public class RunCommandActivity extends AppCompatActivity {
}
explanation.setText(text);
explanation.setVisibility(commandItems.isEmpty() ? View.VISIBLE : View.GONE);
}
});
}
});
}
@ -125,12 +108,7 @@ public class RunCommandActivity extends AppCompatActivity {
FloatingActionButton addCommandButton = (FloatingActionButton) findViewById(R.id.add_command_button);
addCommandButton.setVisibility(canAddCommands ? View.VISIBLE : View.GONE);
addCommandButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AddCommandDialog().show(getSupportFragmentManager(), "addcommanddialog");
}
});
addCommandButton.setOnClickListener(view -> new AddCommandDialog().show(getSupportFragmentManager(), "addcommanddialog"));
updateView();
}
@ -139,9 +117,7 @@ public class RunCommandActivity extends AppCompatActivity {
protected void onResume() {
super.onResume();
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
final Device device = service.getDevice(deviceId);
final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
@ -150,7 +126,6 @@ public class RunCommandActivity extends AppCompatActivity {
return;
}
plugin.addCommandsUpdatedCallback(commandsChangedCallback);
}
});
}
@ -158,9 +133,7 @@ public class RunCommandActivity extends AppCompatActivity {
protected void onPause() {
super.onPause();
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
final Device device = service.getDevice(deviceId);
final RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
@ -169,20 +142,16 @@ public class RunCommandActivity extends AppCompatActivity {
return;
}
plugin.removeCommandsUpdatedCallback(commandsChangedCallback);
}
});
}
public void dialogResult(final String cmdName, final String cmdCmd) {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(deviceId);
RunCommandPlugin plugin = device.getPlugin(RunCommandPlugin.class);
if(!cmdName.isEmpty() && !cmdCmd.isEmpty()) {
plugin.addCommand(cmdName, cmdCmd);
}
}
});
}
}

View File

@ -89,9 +89,7 @@ public class SendFileActivity extends AppCompatActivity {
if (uris.isEmpty()) {
Log.w("SendFileActivity", "No files to send?");
} else {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(mDeviceId);
if (device == null) {
Log.e("SendFileActivity", "Device is null");
@ -99,7 +97,6 @@ public class SendFileActivity extends AppCompatActivity {
return;
}
SharePlugin.queuedSendUriList(getApplicationContext(), device, uris);
}
});
}
}

View File

@ -70,28 +70,15 @@ public class ShareActivity extends AppCompatActivity {
private void updateComputerListAction() {
updateComputerList();
BackgroundService.RunCommand(ShareActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.onNetworkChange();
}
});
BackgroundService.RunCommand(ShareActivity.this, BackgroundService::onNetworkChange);
mSwipeRefreshLayout.setRefreshing(true);
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
try {
Thread.sleep(1500);
} catch (InterruptedException ignored) {
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
runOnUiThread(() -> mSwipeRefreshLayout.setRefreshing(false));
}).start();
}
@ -105,9 +92,7 @@ public class ShareActivity extends AppCompatActivity {
return;
}
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Collection<Device> devices = service.getDevices().values();
final ArrayList<Device> devicesList = new ArrayList<>();
@ -122,24 +107,17 @@ public class ShareActivity extends AppCompatActivity {
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
runOnUiThread(() -> {
ListView list = (ListView) findViewById(R.id.devices_list);
list.setAdapter(new ListAdapter(ShareActivity.this, items));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
list.setOnItemClickListener((adapterView, view, i, l) -> {
Device device = devicesList.get(i - 1); //NOTE: -1 because of the title!
SharePlugin.share(intent, device);
finish();
}
});
}
});
}
});
}
@ -154,12 +132,7 @@ public class ShareActivity extends AppCompatActivity {
ActionBar actionBar = getSupportActionBar();
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_list_layout);
mSwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateComputerListAction();
}
}
this::updateComputerListAction
);
if (actionBar != null) {
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
@ -176,32 +149,20 @@ public class ShareActivity extends AppCompatActivity {
if (deviceId != null) {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Log.d("DirectShare", "sharing to " + service.getDevice(deviceId).getName());
Device device = service.getDevice(deviceId);
if (device.isReachable() && device.isPaired()) {
SharePlugin.share(intent, device);
}
finish();
}
});
} else {
BackgroundService.addGuiInUseCounter(this);
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
service.onNetworkChange();
service.addDeviceListChangedCallback("ShareActivity", new BackgroundService.DeviceListChangedCallback() {
@Override
public void onDeviceListChanged() {
updateComputerList();
}
});
}
service.addDeviceListChangedCallback("ShareActivity", this::updateComputerList);
});
updateComputerList();
}
@ -210,12 +171,7 @@ public class ShareActivity extends AppCompatActivity {
@Override
protected void onStop() {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.removeDeviceListChangedCallback("ShareActivity");
}
});
BackgroundService.RunCommand(this, service -> service.removeDeviceListChangedCallback("ShareActivity"));
BackgroundService.removeGuiInUseCounter(this);
super.onStop();
}

View File

@ -219,9 +219,7 @@ public class SharePlugin extends Plugin {
final ShareNotification notification = new ShareNotification(device, filename);
notification.show();
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
try {
byte data[] = new byte[4096];
long progress = 0, prevProgressPercentage = -1;
@ -278,7 +276,6 @@ public class SharePlugin extends Plugin {
} catch (Exception e) {
}
}
}
}).start();
}
@ -302,9 +299,7 @@ public class SharePlugin extends Plugin {
final NotificationUpdateCallback notificationUpdateCallback = new NotificationUpdateCallback(context, device, toSend);
//Do the sending in background
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
//Actually send the files
try {
for (NetworkPacket np : toSend) {
@ -317,7 +312,6 @@ public class SharePlugin extends Plugin {
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}

View File

@ -36,20 +36,14 @@ public class ShareSettingsActivity extends PluginSettingsActivity {
filePicker = findPreference("share_destination_folder_preference");
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
customDownloads.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
customDownloads.setOnPreferenceChangeListener((preference, newValue) -> {
updateFilePickerStatus((Boolean) newValue);
return true;
}
});
filePicker.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
filePicker.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, RESULT_PICKER);
return true;
}
});
} else {
customDownloads.setEnabled(false);

View File

@ -65,38 +65,27 @@ public class CustomDevicesActivity extends AppCompatActivity {
list.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, ipAddressList));
findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewDevice();
}
});
findViewById(android.R.id.button1).setOnClickListener(v -> addNewDevice());
EditText ipEntryBox = (EditText) findViewById(R.id.ip_edittext);
ipEntryBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
ipEntryBox.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_SEND) {
addNewDevice();
return true;
}
return false;
}
});
}
boolean dialogAlreadyShown = false;
private AdapterView.OnItemClickListener onClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
private AdapterView.OnItemClickListener onClickListener = (parent, view, position, id) -> {
if (dialogAlreadyShown) {
return;
}
// remove touched item after confirmation
DialogInterface.OnClickListener confirmationListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogInterface.OnClickListener confirmationListener = (dialog, which) -> {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
ipAddressList.remove(position);
@ -105,7 +94,6 @@ public class CustomDevicesActivity extends AppCompatActivity {
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(CustomDevicesActivity.this);
@ -115,16 +103,10 @@ public class CustomDevicesActivity extends AppCompatActivity {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { //DismissListener
dialogAlreadyShown = true;
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialogAlreadyShown = false;
}
});
builder.setOnDismissListener(dialog -> dialogAlreadyShown = false);
}
builder.show();
}
};
private void addNewDevice() {

View File

@ -110,9 +110,7 @@ public class DeviceFragment extends Fragment {
//Log.e("DeviceFragment", "device: " + deviceId);
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(mActivity, service -> {
device = service.getDevice(mDeviceId);
if (device == null) {
Log.e("DeviceFragment", "Trying to display a device fragment but the device is not present");
@ -127,48 +125,28 @@ public class DeviceFragment extends Fragment {
refreshUI();
}
});
final Button pairButton = (Button) rootView.findViewById(R.id.pair_button);
pairButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pairButton.setOnClickListener(view -> {
pairButton.setVisibility(View.GONE);
((TextView) rootView.findViewById(R.id.pair_message)).setText("");
rootView.findViewById(R.id.pair_progress).setVisibility(View.VISIBLE);
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(mActivity, service -> {
device = service.getDevice(deviceId);
if (device == null) return;
device.requestPairing();
}
});
}
});
rootView.findViewById(R.id.accept_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
rootView.findViewById(R.id.accept_button).setOnClickListener(view -> BackgroundService.RunCommand(mActivity, service -> {
if (device != null) {
device.acceptPairing();
rootView.findViewById(R.id.pairing_buttons).setVisibility(View.GONE);
}
}
});
}
});
}));
rootView.findViewById(R.id.reject_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
rootView.findViewById(R.id.reject_button).setOnClickListener(view -> BackgroundService.RunCommand(mActivity, service -> {
if (device != null) {
//Remove listener so buttons don't show for a while before changing the view
device.removePluginsChangedListener(pluginsChangedListener);
@ -176,31 +154,20 @@ public class DeviceFragment extends Fragment {
device.rejectPairing();
}
mActivity.onDeviceSelected(null);
}
});
}
});
}));
return rootView;
}
final Device.PluginsChangedListener pluginsChangedListener = new Device.PluginsChangedListener() {
@Override
public void onPluginsChanged(final Device device) {
refreshUI();
}
};
final Device.PluginsChangedListener pluginsChangedListener = device -> refreshUI();
@Override
public void onDestroyView() {
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(mActivity, service -> {
Device device = service.getDevice(mDeviceId);
if (device == null) return;
device.removePluginsChangedListener(pluginsChangedListener);
device.removePairingCallback(pairingCallback);
}
});
super.onDestroyView();
}
@ -223,38 +190,26 @@ public class DeviceFragment extends Fragment {
if (!p.displayInContextMenu()) {
continue;
}
menu.add(p.getActionName()).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
menu.add(p.getActionName()).setOnMenuItemClickListener(item -> {
p.startMainActivity(mActivity);
return true;
}
});
}
menu.add(R.string.device_menu_plugins).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
menu.add(R.string.device_menu_plugins).setOnMenuItemClickListener(menuItem -> {
Intent intent = new Intent(mActivity, SettingsActivity.class);
intent.putExtra("deviceId", mDeviceId);
startActivity(intent);
return true;
}
});
if (device.isReachable()) {
menu.add(R.string.encryption_info_title).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
menu.add(R.string.encryption_info_title).setOnMenuItemClickListener(menuItem -> {
Context context = mActivity;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getResources().getString(R.string.encryption_info_title));
builder.setPositiveButton(context.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.setPositiveButton(context.getResources().getString(R.string.ok), (dialog, id) -> dialog.dismiss());
if (device.certificate == null) {
builder.setMessage(R.string.encryption_info_msg_no_ssl);
@ -264,22 +219,18 @@ public class DeviceFragment extends Fragment {
}
builder.create().show();
return true;
}
});
}
if (device.isPaired()) {
menu.add(R.string.device_menu_unpair).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
menu.add(R.string.device_menu_unpair).setOnMenuItemClickListener(menuItem -> {
//Remove listener so buttons don't show for a while before changing the view
device.removePluginsChangedListener(pluginsChangedListener);
device.removePairingCallback(pairingCallback);
device.unpair();
mActivity.onDeviceSelected(null);
return true;
}
});
}
@ -291,9 +242,7 @@ public class DeviceFragment extends Fragment {
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
getView().setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
boolean fromDeviceList = getArguments().getBoolean(ARG_FROM_DEVICE_LIST, false);
// Handle back button so we go to the list of devices in case we came from there
@ -303,7 +252,6 @@ public class DeviceFragment extends Fragment {
}
}
return false;
}
});
}
@ -347,12 +295,7 @@ public class DeviceFragment extends Fragment {
if (!p.hasMainActivity()) continue;
if (p.displayInContextMenu()) continue;
pluginListItems.add(new PluginItem(p, new View.OnClickListener() {
@Override
public void onClick(View v) {
p.startMainActivity(mActivity);
}
}));
pluginListItems.add(new PluginItem(p, v -> p.startMainActivity(mActivity)));
}
createPluginsList(device.getFailedPlugins(), R.string.plugins_failed_to_load, new PluginClickListener() {
@ -408,31 +351,25 @@ public class DeviceFragment extends Fragment {
@Override
public void pairingFailed(final String error) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mActivity.runOnUiThread(() -> {
if (rootView == null) return;
((TextView) rootView.findViewById(R.id.pair_message)).setText(error);
rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE);
rootView.findViewById(R.id.pair_button).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.pair_request).setVisibility(View.GONE);
refreshUI();
}
});
}
@Override
public void unpaired() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mActivity.runOnUiThread(() -> {
if (rootView == null) return;
((TextView) rootView.findViewById(R.id.pair_message)).setText(R.string.device_not_paired);
rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE);
rootView.findViewById(R.id.pair_button).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.pair_request).setVisibility(View.GONE);
refreshUI();
}
});
}
@ -440,8 +377,7 @@ public class DeviceFragment extends Fragment {
public static void acceptPairing(final String devId, final MainActivity activity) {
final DeviceFragment frag = new DeviceFragment(devId, activity);
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(activity, service -> {
Device dev = service.getDevice(devId);
if (dev == null) {
Log.w("rejectPairing", "Device no longer exists: " + devId);
@ -457,14 +393,12 @@ public class DeviceFragment extends Fragment {
frag.refreshUI();
}
});
}
public static void rejectPairing(final String devId, final MainActivity activity) {
final DeviceFragment frag = new DeviceFragment(devId, activity);
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(activity, service -> {
Device dev = service.getDevice(devId);
if (dev == null) {
Log.w("rejectPairing", "Device no longer exists: " + devId);
@ -484,7 +418,6 @@ public class DeviceFragment extends Fragment {
activity.onDeviceSelected(null);
frag.refreshUI();
}
});
}

View File

@ -74,12 +74,7 @@ public class PairingDeviceItem implements ListAdapter.Item {
v.findViewById(R.id.list_item_entry_summary).setVisibility(View.GONE);
}
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.pairingClicked(device);
}
});
v.setOnClickListener(v1 -> callback.pairingClicked(device));
return v;
}

View File

@ -92,12 +92,7 @@ public class MainActivity extends AppCompatActivity {
TextView nameView = (TextView) mDrawerHeader.findViewById(R.id.device_name);
nameView.setText(deviceName);
View.OnClickListener renameListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
renameDevice();
}
};
View.OnClickListener renameListener = v -> renameDevice();
mDrawerHeader.findViewById(R.id.kdeconnect_label).setOnClickListener(renameListener);
mDrawerHeader.findViewById(R.id.device_name).setOnClickListener(renameListener);
@ -105,9 +100,7 @@ public class MainActivity extends AppCompatActivity {
addDarkModeSwitch((ViewGroup) mDrawerHeader);
}
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mNavigationView.setNavigationItemSelectedListener(menuItem -> {
String deviceId = mMapMenuToDeviceId.get(menuItem);
onDeviceSelected(deviceId);
@ -115,7 +108,6 @@ public class MainActivity extends AppCompatActivity {
mDrawerLayout.closeDrawer(mNavigationView);
return true;
}
});
preferences = getSharedPreferences(STATE_SELECTED_DEVICE, Context.MODE_PRIVATE);
@ -218,9 +210,7 @@ public class MainActivity extends AppCompatActivity {
//Log.e("MainActivity", "UpdateComputerList");
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
BackgroundService.RunCommand(MainActivity.this, service -> {
Menu menu = mNavigationView.getMenu();
@ -244,7 +234,6 @@ public class MainActivity extends AppCompatActivity {
item.setCheckable(true);
item.setChecked(mCurrentDevice == null);
mMapMenuToDeviceId.put(item, null);
}
});
}
@ -252,29 +241,14 @@ public class MainActivity extends AppCompatActivity {
protected void onStart() {
super.onStart();
BackgroundService.addGuiInUseCounter(this, true);
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.addDeviceListChangedCallback("MainActivity", new BackgroundService.DeviceListChangedCallback() {
@Override
public void onDeviceListChanged() {
updateComputerList();
}
});
}
});
BackgroundService.RunCommand(this, service -> service.addDeviceListChangedCallback("MainActivity", this::updateComputerList));
updateComputerList();
}
@Override
protected void onStop() {
BackgroundService.removeGuiInUseCounter(this);
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.removeDeviceListChangedCallback("MainActivity");
}
});
BackgroundService.RunCommand(this, service -> service.removeDeviceListChangedCallback("MainActivity"));
super.onStop();
}
@ -326,12 +300,9 @@ public class MainActivity extends AppCompatActivity {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_NEEDS_RELOAD:
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(mCurrentDevice);
device.reloadPluginsFromSettings();
}
});
break;
default:
@ -344,12 +315,9 @@ public class MainActivity extends AppCompatActivity {
for (int result : grantResults) {
if (result == PackageManager.PERMISSION_GRANTED) {
//New permission granted, reload plugins
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(this, service -> {
Device device = service.getDevice(mCurrentDevice);
device.reloadPluginsFromSettings();
}
});
}
}
@ -368,24 +336,13 @@ public class MainActivity extends AppCompatActivity {
);
new AlertDialog.Builder(MainActivity.this)
.setView(deviceNameEdit)
.setPositiveButton(R.string.device_rename_confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String deviceName = deviceNameEdit.getText().toString();
DeviceHelper.setDeviceName(MainActivity.this, deviceName);
nameView.setText(deviceName);
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
service.onNetworkChange();
}
});
}
.setPositiveButton(R.string.device_rename_confirm, (dialog, which) -> {
String deviceName1 = deviceNameEdit.getText().toString();
DeviceHelper.setDeviceName(MainActivity.this, deviceName1);
nameView.setText(deviceName1);
BackgroundService.RunCommand(MainActivity.this, BackgroundService::onNetworkChange);
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
.setNegativeButton(R.string.cancel, (dialog, which) -> {
})
.setTitle(R.string.device_rename_title)
.show();

View File

@ -79,12 +79,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
View listRootView = rootView.findViewById(R.id.devices_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_list_layout);
mSwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateComputerListAction();
}
}
this::updateComputerListAction
);
headerText = new TextView(inflater.getContext());
headerText.setText(getString(R.string.pairing_description));
@ -102,37 +97,19 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
private void updateComputerListAction() {
updateComputerList();
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.onNetworkChange();
}
});
BackgroundService.RunCommand(mActivity, BackgroundService::onNetworkChange);
mSwipeRefreshLayout.setRefreshing(true);
new Thread(new Runnable() {
@Override
public void run() {
new Thread(() -> {
try {
Thread.sleep(1500);
} catch (InterruptedException ignored) {
}
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
mActivity.runOnUiThread(() -> mSwipeRefreshLayout.setRefreshing(false));
}).start();
}
private void updateComputerList() {
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
BackgroundService.RunCommand(mActivity, service -> mActivity.runOnUiThread(() -> {
if (!isAdded()) {
//Fragment is not attached to an activity. We will crash if we try to do anything here.
@ -212,28 +189,14 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
} finally {
listRefreshCalledThisFrame = false;
}
}
});
}));
}
});
}
@Override
public void onStart() {
super.onStart();
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.addDeviceListChangedCallback("PairingFragment", new BackgroundService.DeviceListChangedCallback() {
@Override
public void onDeviceListChanged() {
updateComputerList();
}
});
}
});
BackgroundService.RunCommand(mActivity, service -> service.addDeviceListChangedCallback("PairingFragment", this::updateComputerList));
updateComputerList();
}
@ -241,12 +204,7 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
public void onStop() {
super.onStop();
mSwipeRefreshLayout.setEnabled(false);
BackgroundService.RunCommand(mActivity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.removeDeviceListChangedCallback("PairingFragment");
}
});
BackgroundService.RunCommand(mActivity, service -> service.removeDeviceListChangedCallback("PairingFragment"));
}
@Override

View File

@ -29,16 +29,13 @@ public class PluginPreference extends CheckBoxPreference {
Plugin plugin = device.getPlugin(pluginKey, true);
if (info.hasSettings() && plugin != null) {
this.listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Plugin plugin = device.getPlugin(pluginKey, true);
if (plugin != null) {
plugin.startPreferencesActivity(activity);
this.listener = v -> {
Plugin plugin1 = device.getPlugin(pluginKey, true);
if (plugin1 != null) {
plugin1.startPreferencesActivity(activity);
} else { //Could happen if the device is not connected anymore
activity.finish(); //End this activity so we go to the "device not reachable" screen
}
}
};
} else {
this.listener = null;
@ -58,14 +55,11 @@ public class PluginPreference extends CheckBoxPreference {
button.setOnClickListener(listener);
}
root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
root.setOnClickListener(v -> {
boolean newState = !device.isPluginEnabled(pluginKey);
setChecked(newState); //It actually works on API<14
button.setEnabled(newState);
device.setPluginEnabled(pluginKey, newState);
}
});
}

View File

@ -44,17 +44,10 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
deviceId = getIntent().getStringExtra("deviceId");
}
BackgroundService.RunCommand(getApplicationContext(), new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
BackgroundService.RunCommand(getApplicationContext(), service -> {
final Device device = service.getDevice(deviceId);
if (device == null) {
SettingsActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
SettingsActivity.this.finish();
}
});
SettingsActivity.this.runOnUiThread(SettingsActivity.this::finish);
return;
}
List<String> plugins = device.getSupportedPlugins();
@ -62,7 +55,6 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
PluginPreference pref = new PluginPreference(SettingsActivity.this, pluginKey, device);
preferenceScreen.addPreference(pref);
}
}
});
}

View File

@ -204,33 +204,18 @@ public class LanLinkTest extends AndroidTestCase {
Mockito.when(sharePacket.getType()).thenReturn("kdeconnect.share");
Mockito.when(sharePacket.hasPayload()).thenReturn(true);
Mockito.when(sharePacket.hasPayloadTransferInfo()).thenReturn(true);
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
return sharePacketJson.toString();
}
}).when(sharePacket).serialize();
Mockito.doAnswer(invocationOnMock -> sharePacketJson.toString()).when(sharePacket).serialize();
Mockito.when(sharePacket.getPayload()).thenReturn(new ByteArrayInputStream(data));
Mockito.when(sharePacket.getPayloadSize()).thenReturn((long) data.length);
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
return sharePacketJson.getJSONObject("payloadTransferInfo");
}
}).when(sharePacket).getPayloadTransferInfo();
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Mockito.doAnswer(invocationOnMock -> sharePacketJson.getJSONObject("payloadTransferInfo")).when(sharePacket).getPayloadTransferInfo();
Mockito.doAnswer(invocationOnMock -> {
JSONObject object = (JSONObject) invocationOnMock.getArguments()[0];
sharePacketJson.put("payloadTransferInfo", object);
return null;
}
}).when(sharePacket).setPayloadTransferInfo(Mockito.any(JSONObject.class));
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Mockito.doAnswer(invocationOnMock -> {
Log.e("LanLinkTest", "Write to stream");
String stringNetworkPacket = new String((byte[]) invocationOnMock.getArguments()[0]);
@ -240,7 +225,6 @@ public class LanLinkTest extends AndroidTestCase {
downloader.start();
return stringNetworkPacket.length();
}
}).when(goodOutputStream).write(Mockito.any(byte[].class));
goodLanLink.sendPacket(sharePacket, callback);