Differential D10905 Diff 28488 src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothPairingHandler.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothPairingHandler.java
Show All 15 Lines | |||||
16 | * | 16 | * | ||
17 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | package org.kde.kdeconnect.Backends.BluetoothBackend; | 21 | package org.kde.kdeconnect.Backends.BluetoothBackend; | ||
22 | 22 | | |||
23 | import android.util.Log; | 23 | import android.util.Log; | ||
24 | | ||||
24 | import org.kde.kdeconnect.Backends.BasePairingHandler; | 25 | import org.kde.kdeconnect.Backends.BasePairingHandler; | ||
25 | import org.kde.kdeconnect.Device; | 26 | import org.kde.kdeconnect.Device; | ||
26 | import org.kde.kdeconnect.NetworkPackage; | 27 | import org.kde.kdeconnect.NetworkPackage; | ||
27 | import org.kde.kdeconnect_tp.R; | 28 | import org.kde.kdeconnect_tp.R; | ||
28 | 29 | | |||
29 | import java.util.Timer; | 30 | import java.util.Timer; | ||
30 | import java.util.TimerTask; | 31 | import java.util.TimerTask; | ||
31 | 32 | | |||
32 | public class BluetoothPairingHandler extends BasePairingHandler { | 33 | public class BluetoothPairingHandler extends BasePairingHandler { | ||
33 | 34 | | |||
34 | Timer mPairingTimer; | 35 | Timer mPairingTimer; | ||
36 | | ||||
35 | public BluetoothPairingHandler(Device device, final PairingHandlerCallback callback) { | 37 | public BluetoothPairingHandler(Device device, final PairingHandlerCallback callback) { | ||
36 | super(device, callback); | 38 | super(device, callback); | ||
37 | 39 | | |||
38 | if (device.isPaired()) { | 40 | if (device.isPaired()) { | ||
39 | mPairStatus = PairStatus.Paired; | 41 | mPairStatus = PairStatus.Paired; | ||
40 | } else { | 42 | } else { | ||
41 | mPairStatus = PairStatus.NotPaired; | 43 | mPairStatus = PairStatus.NotPaired; | ||
42 | } | 44 | } | ||
43 | } | 45 | } | ||
44 | 46 | | |||
45 | // @Override | 47 | // @Override | ||
46 | public NetworkPackage createPairPackage() { | 48 | public NetworkPackage createPairPackage() { | ||
47 | NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_PAIR); | 49 | NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_PAIR); | ||
48 | np.set("pair", true); | 50 | np.set("pair", true); | ||
49 | return np; | 51 | return np; | ||
50 | } | 52 | } | ||
51 | 53 | | |||
52 | @Override | 54 | @Override | ||
53 | public void packageReceived(NetworkPackage np) throws Exception{ | 55 | public void packageReceived(NetworkPackage np) throws Exception { | ||
54 | 56 | | |||
55 | boolean wantsPair = np.getBoolean("pair"); | 57 | boolean wantsPair = np.getBoolean("pair"); | ||
56 | 58 | | |||
57 | if (wantsPair == isPaired()) { | 59 | if (wantsPair == isPaired()) { | ||
58 | if (mPairStatus == PairStatus.Requested) { | 60 | if (mPairStatus == PairStatus.Requested) { | ||
59 | //Log.e("Device","Unpairing (pair rejected)"); | 61 | //Log.e("Device","Unpairing (pair rejected)"); | ||
60 | mPairStatus = PairStatus.NotPaired; | 62 | mPairStatus = PairStatus.NotPaired; | ||
61 | hidePairingNotification(); | 63 | hidePairingNotification(); | ||
62 | mCallback.pairingFailed(mDevice.getContext().getString(R.string.error_canceled_by_other_peer)); | 64 | mCallback.pairingFailed(mDevice.getContext().getString(R.string.error_canceled_by_other_peer)); | ||
63 | } | 65 | } | ||
64 | return; | 66 | return; | ||
65 | } | 67 | } | ||
66 | 68 | | |||
67 | if (wantsPair) { | 69 | if (wantsPair) { | ||
68 | 70 | | |||
69 | if (mPairStatus == PairStatus.Requested) { //We started pairing | 71 | if (mPairStatus == PairStatus.Requested) { //We started pairing | ||
70 | hidePairingNotification(); | 72 | hidePairingNotification(); | ||
71 | pairingDone(); | 73 | pairingDone(); | ||
72 | } else { | 74 | } else { | ||
73 | 75 | | |||
74 | // If device is already paired, accept pairing silently | 76 | // If device is already paired, accept pairing silently | ||
75 | if (mDevice.isPaired()) { | 77 | if (mDevice.isPaired()) { | ||
76 | acceptPairing(); | 78 | acceptPairing(); | ||
77 | return; | 79 | return; | ||
78 | } | 80 | } | ||
79 | 81 | | |||
80 | // Pairing notifications are still managed by device as there is no other way to | 82 | // Pairing notifications are still managed by device as there is no other way to | ||
81 | // know about notificationId to cancel notification when PairActivity is started | 83 | // know about notificationId to cancel notification when PairActivity is started | ||
82 | // Even putting notificationId in intent does not work because PairActivity can be | 84 | // Even putting notificationId in intent does not work because PairActivity can be | ||
83 | // started from MainActivity too, so then notificationId cannot be set | 85 | // started from MainActivity too, so then notificationId cannot be set | ||
84 | hidePairingNotification(); | 86 | hidePairingNotification(); | ||
85 | mDevice.displayPairingNotification(); | 87 | mDevice.displayPairingNotification(); | ||
86 | 88 | | |||
87 | mPairingTimer = new Timer(); | 89 | mPairingTimer = new Timer(); | ||
88 | 90 | | |||
89 | mPairingTimer.schedule(new TimerTask() { | 91 | mPairingTimer.schedule(new TimerTask() { | ||
90 | @Override | 92 | @Override | ||
91 | public void run() { | 93 | public void run() { | ||
92 | Log.w("KDE/Device","Unpairing (timeout B)"); | 94 | Log.w("KDE/Device", "Unpairing (timeout B)"); | ||
93 | mPairStatus = PairStatus.NotPaired; | 95 | mPairStatus = PairStatus.NotPaired; | ||
94 | hidePairingNotification(); | 96 | hidePairingNotification(); | ||
95 | } | 97 | } | ||
96 | }, 25*1000); //Time to show notification, waiting for user to accept (peer will timeout in 30 seconds) | 98 | }, 25 * 1000); //Time to show notification, waiting for user to accept (peer will timeout in 30 seconds) | ||
97 | mPairStatus = PairStatus.RequestedByPeer; | 99 | mPairStatus = PairStatus.RequestedByPeer; | ||
98 | mCallback.incomingRequest(); | 100 | mCallback.incomingRequest(); | ||
99 | 101 | | |||
100 | } | 102 | } | ||
101 | } else { | 103 | } else { | ||
102 | Log.i("KDE/Pairing", "Unpair request"); | 104 | Log.i("KDE/Pairing", "Unpair request"); | ||
103 | 105 | | |||
104 | if (mPairStatus == PairStatus.Requested) { | 106 | if (mPairStatus == PairStatus.Requested) { | ||
Show All 16 Lines | 122 | Device.SendPackageStatusCallback statusCallback = new Device.SendPackageStatusCallback() { | |||
121 | @Override | 123 | @Override | ||
122 | public void onSuccess() { | 124 | public void onSuccess() { | ||
123 | hidePairingNotification(); //Will stop the pairingTimer if it was running | 125 | hidePairingNotification(); //Will stop the pairingTimer if it was running | ||
124 | mPairingTimer = new Timer(); | 126 | mPairingTimer = new Timer(); | ||
125 | mPairingTimer.schedule(new TimerTask() { | 127 | mPairingTimer.schedule(new TimerTask() { | ||
126 | @Override | 128 | @Override | ||
127 | public void run() { | 129 | public void run() { | ||
128 | mCallback.pairingFailed(mDevice.getContext().getString(R.string.error_timed_out)); | 130 | mCallback.pairingFailed(mDevice.getContext().getString(R.string.error_timed_out)); | ||
129 | Log.w("KDE/Device","Unpairing (timeout A)"); | 131 | Log.w("KDE/Device", "Unpairing (timeout A)"); | ||
130 | mPairStatus = PairStatus.NotPaired; | 132 | mPairStatus = PairStatus.NotPaired; | ||
131 | } | 133 | } | ||
132 | }, 30*1000); //Time to wait for the other to accept | 134 | }, 30 * 1000); //Time to wait for the other to accept | ||
133 | mPairStatus = PairStatus.Requested; | 135 | mPairStatus = PairStatus.Requested; | ||
134 | } | 136 | } | ||
135 | 137 | | |||
136 | @Override | 138 | @Override | ||
137 | public void onFailure(Throwable e) { | 139 | public void onFailure(Throwable e) { | ||
138 | mCallback.pairingFailed(mDevice.getContext().getString(R.string.error_could_not_send_package)); | 140 | mCallback.pairingFailed(mDevice.getContext().getString(R.string.error_could_not_send_package)); | ||
139 | } | 141 | } | ||
140 | }; | 142 | }; | ||
141 | mDevice.sendPackage(createPairPackage(), statusCallback); | 143 | mDevice.sendPackage(createPairPackage(), statusCallback); | ||
142 | } | 144 | } | ||
143 | 145 | | |||
144 | public void hidePairingNotification() { | 146 | public void hidePairingNotification() { | ||
145 | mDevice.hidePairingNotification(); | 147 | mDevice.hidePairingNotification(); | ||
146 | if (mPairingTimer != null) { | 148 | if (mPairingTimer != null) { | ||
147 | mPairingTimer .cancel(); | 149 | mPairingTimer.cancel(); | ||
148 | } | 150 | } | ||
149 | } | 151 | } | ||
150 | 152 | | |||
151 | @Override | 153 | @Override | ||
152 | public void acceptPairing() { | 154 | public void acceptPairing() { | ||
153 | hidePairingNotification(); | 155 | hidePairingNotification(); | ||
154 | Device.SendPackageStatusCallback statusCallback = new Device.SendPackageStatusCallback() { | 156 | Device.SendPackageStatusCallback statusCallback = new Device.SendPackageStatusCallback() { | ||
155 | @Override | 157 | @Override | ||
Show All 38 Lines |