blob: 7329fd24d3f572c06180ae315356be5d57d6383c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.libreoffice.impressremote.communication;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Build;
import org.libreoffice.impressremote.util.Preferences;
final class PairingProvider {
private final Context mContext;
private PairingProvider(Context aContext) {
mContext = aContext;
}
public static boolean isPairingNecessary(Server aServer) {
return aServer.getProtocol() == Server.Protocol.TCP;
}
public static String getPairingPin(Context aContext, Server aServer) {
return new PairingProvider(aContext).getPairingPin(aServer);
}
private String getPairingPin(Server aServer) {
if (isPinSaved(aServer)) {
return getSavedPin(aServer);
}
String aPin = Protocol.Pin.generate();
savePin(aServer, aPin);
return aPin;
}
private boolean isPinSaved(Server aServer) {
return getSavedPin(aServer) != null;
}
private String getSavedPin(Server aServer) {
String aLocation = Preferences.Locations.AUTHORIZED_REMOTES;
String aServerAddress = aServer.getAddress();
return Preferences.getString(mContext, aLocation, aServerAddress);
}
private void savePin(Server aServer, String aPin) {
String aLocation = Preferences.Locations.AUTHORIZED_REMOTES;
String aServerAddress = aServer.getAddress();
Preferences.set(mContext, aLocation, aServerAddress, aPin);
}
public static String getPairingDeviceName(Context aContext) {
return new PairingProvider(aContext).getPairingDeviceName();
}
private String getPairingDeviceName() {
if (BluetoothAdapter.getDefaultAdapter() == null) {
return Build.MODEL;
}
if (BluetoothAdapter.getDefaultAdapter().getName() == null) {
return Build.MODEL;
}
return BluetoothAdapter.getDefaultAdapter().getName();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|