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

Accept or reject pair requests from the notification

This commit is contained in:
Okoko Michaels 2017-02-05 15:19:46 +01:00 committed by Albert Vaca
parent 1179807bcf
commit 89ecdfc363
13 changed files with 107 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 474 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 624 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 786 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 894 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -368,7 +368,23 @@ public class Device implements BaseLink.PackageReceiver {
Intent intent = new Intent(getContext(), MaterialActivity.class);
intent.putExtra("deviceId", getDeviceId());
intent.putExtra("notificationId", notificationId);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent acceptIntent = new Intent(getContext(), MaterialActivity.class);
Intent rejectIntent = new Intent(getContext(), MaterialActivity.class);
acceptIntent.putExtra("deviceId", getDeviceId());
acceptIntent.putExtra("notificationId", notificationId);
acceptIntent.setAction("action "+System.currentTimeMillis());
acceptIntent.putExtra(MaterialActivity.PAIR_REQUEST_STATUS, MaterialActivity.PAIRING_ACCEPTED);
rejectIntent.putExtra("deviceId", getDeviceId());
rejectIntent.putExtra("notificationId", notificationId);
rejectIntent.setAction("action "+System.currentTimeMillis());
rejectIntent.putExtra(MaterialActivity.PAIR_REQUEST_STATUS, MaterialActivity.PAIRING_REJECTED);
PendingIntent acceptedPendingIntent = PendingIntent.getActivity(getContext(), 2, acceptIntent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent rejectedPendingIntent = PendingIntent.getActivity(getContext(), 4, rejectIntent, PendingIntent.FLAG_ONE_SHOT);
Resources res = getContext().getResources();
@ -378,6 +394,8 @@ public class Device implements BaseLink.PackageReceiver {
.setContentIntent(pendingIntent)
.setTicker(res.getString(R.string.pair_requested))
.setSmallIcon(R.drawable.ic_notification)
.addAction(R.drawable.ic_accept_pairing, res.getString(R.string.pairing_accept), acceptedPendingIntent)
.addAction(R.drawable.ic_reject_pairing, res.getString(R.string.pairing_reject), rejectedPendingIntent)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.build();

View File

@ -84,6 +84,13 @@ public class DeviceFragment extends Fragment {
this.setArguments(args);
}
public DeviceFragment(String deviceId, MaterialActivity activity){
this.mActivity = activity;
Bundle args = new Bundle();
args.putString(ARG_DEVICE_ID, deviceId);
this.setArguments(args);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
@ -183,7 +190,6 @@ public class DeviceFragment extends Fragment {
return rootView;
}
private final Device.PluginsChangedListener pluginsChangedListener = new Device.PluginsChangedListener() {
@Override
public void onPluginsChanged(final Device device) {
@ -460,4 +466,54 @@ public class DeviceFragment extends Fragment {
};
public static void acceptPairing(final String devId, final MaterialActivity activity){
final DeviceFragment frag = new DeviceFragment(devId, activity);
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
public void onServiceStart(BackgroundService service) {
Device dev = service.getDevice(devId);
activity.getSupportActionBar().setTitle(dev.getName());
dev.addPairingCallback(frag.pairingCallback);
dev.addPluginsChangedListener(frag.pluginsChangedListener);
frag.refreshUI();
frag.device = dev;
}
});
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
frag.device.acceptPairing();
}
});
}
public static void rejectPairing(final String devId, final MaterialActivity activity){
final DeviceFragment frag = new DeviceFragment(devId, activity);
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
public void onServiceStart(BackgroundService service) {
Device dev = service.getDevice(devId);
activity.getSupportActionBar().setTitle(dev.getName());
dev.addPairingCallback(frag.pairingCallback);
dev.addPluginsChangedListener(frag.pluginsChangedListener);
frag.refreshUI();
frag.device = dev;
}
});
BackgroundService.RunCommand(activity, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
//Remove listener so buttons don't show for a while before changing the view
frag.device.removePluginsChangedListener(frag.pluginsChangedListener);
frag.device.removePairingCallback(frag.pairingCallback);
frag.device.rejectPairing();
activity.onDeviceSelected(null);
}
});
}
}

View File

@ -38,6 +38,10 @@ public class MaterialActivity extends AppCompatActivity {
public static final int RESULT_NEEDS_RELOAD = Activity.RESULT_FIRST_USER;
public static final String PAIR_REQUEST_STATUS = "pair_req_status";
public static final String PAIRING_ACCEPTED = "accepted";
public static final String PAIRING_REJECTED = "rejected";
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
@ -102,12 +106,16 @@ public class MaterialActivity extends AppCompatActivity {
preferences = getSharedPreferences(STATE_SELECTED_DEVICE, Context.MODE_PRIVATE);
String savedDevice;
String pairStatus = "";
if (getIntent().hasExtra("forceOverview")) {
Log.i("MaterialActivity", "Requested to start main overview");
savedDevice = null;
} else if (getIntent().hasExtra("deviceId")) {
Log.i("MaterialActivity", "Loading selected device from parameter");
savedDevice = getIntent().getStringExtra("deviceId");
if(getIntent().hasExtra(PAIR_REQUEST_STATUS)){
pairStatus = getIntent().getStringExtra(PAIR_REQUEST_STATUS);
}
} else if (savedInstanceState != null) {
Log.i("MaterialActivity", "Loading selected device from saved activity state");
savedDevice = savedInstanceState.getString(STATE_SELECTED_DEVICE);
@ -115,8 +123,31 @@ public class MaterialActivity extends AppCompatActivity {
Log.i("MaterialActivity", "Loading selected device from persistent storage");
savedDevice = preferences.getString(STATE_SELECTED_DEVICE, null);
}
//if pairStatus is not empty, then the decision has been made...
if (!pairStatus.equals("")) {
Log.i("MaterialActivity", "pair status is "+pairStatus);
onNewDeviceSelected(savedDevice, pairStatus);
}
onDeviceSelected(savedDevice);
}
//like onNewDeviceSelected but assumes that the new device is simply requesting to be paired
//and can't be null
private void onNewDeviceSelected(String deviceId, String pairStatus){
mCurrentDevice = deviceId;
preferences.edit().putString(STATE_SELECTED_DEVICE, mCurrentDevice).apply();
for (HashMap.Entry<MenuItem, String> entry : mMapMenuToDeviceId.entrySet()) {
boolean selected = TextUtils.equals(entry.getValue(), deviceId); //null-safe
entry.getKey().setChecked(selected);
}
if (pairStatus.equals(PAIRING_ACCEPTED)) {
DeviceFragment.acceptPairing(deviceId, this);
} else {
DeviceFragment.rejectPairing(deviceId, this);
}
}
@Override