| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.mtp; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.content.SharedPreferences; |
| 21 |
|
| 22 |
import com.android.internal.util.Preconditions; |
| 23 |
|
| 24 |
import libcore.util.HexEncoding; |
| 25 |
|
| 26 |
import java.io.FileDescriptor; |
| 27 |
import java.util.Random; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class MtpServer implements Runnable { |
| 34 |
|
| 35 |
private long mNativeContext; |
| 36 |
private final MtpDatabase mDatabase; |
| 37 |
private final Runnable mOnTerminate; |
| 38 |
private final Context mContext; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private static final int sID_LEN_BYTES = 16; |
| 43 |
private static final int sID_LEN_STR = (sID_LEN_BYTES * 2); |
| 44 |
|
| 45 |
static { |
| 46 |
System.loadLibrary("media_jni"); |
| 47 |
} |
| 48 |
|
| 49 |
public MtpServer( |
| 50 |
MtpDatabase database, |
| 51 |
FileDescriptor controlFd, |
| 52 |
boolean usePtp, |
| 53 |
Runnable onTerminate, |
| 54 |
String deviceInfoManufacturer, |
| 55 |
String deviceInfoModel, |
| 56 |
String deviceInfoDeviceVersion) { |
| 57 |
mDatabase = Preconditions.checkNotNull(database); |
| 58 |
mOnTerminate = Preconditions.checkNotNull(onTerminate); |
| 59 |
mContext = mDatabase.getContext(); |
| 60 |
|
| 61 |
final String strID_PREFS_NAME = "mtp-cfg"; |
| 62 |
final String strID_PREFS_KEY = "mtp-id"; |
| 63 |
String strRandomId = null; |
| 64 |
String deviceInfoSerialNumber; |
| 65 |
|
| 66 |
SharedPreferences sharedPref = |
| 67 |
mContext.getSharedPreferences(strID_PREFS_NAME, Context.MODE_PRIVATE); |
| 68 |
if (sharedPref.contains(strID_PREFS_KEY)) { |
| 69 |
strRandomId = sharedPref.getString(strID_PREFS_KEY, null); |
| 70 |
|
| 71 |
|
| 72 |
if (strRandomId.length() != sID_LEN_STR) { |
| 73 |
strRandomId = null; |
| 74 |
} else { |
| 75 |
|
| 76 |
for (int ii = 0; ii < strRandomId.length(); ii++) |
| 77 |
if (Character.digit(strRandomId.charAt(ii), 16) == -1) { |
| 78 |
strRandomId = null; |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (strRandomId == null) { |
| 85 |
strRandomId = getRandId(); |
| 86 |
sharedPref.edit().putString(strID_PREFS_KEY, strRandomId).apply(); |
| 87 |
} |
| 88 |
|
| 89 |
deviceInfoSerialNumber = strRandomId; |
| 90 |
|
| 91 |
native_setup( |
| 92 |
database, |
| 93 |
controlFd, |
| 94 |
usePtp, |
| 95 |
deviceInfoManufacturer, |
| 96 |
deviceInfoModel, |
| 97 |
deviceInfoDeviceVersion, |
| 98 |
deviceInfoSerialNumber); |
| 99 |
database.setServer(this); |
| 100 |
} |
| 101 |
|
| 102 |
private String getRandId() { |
| 103 |
Random randomVal = new Random(); |
| 104 |
byte[] randomBytes = new byte[sID_LEN_BYTES]; |
| 105 |
|
| 106 |
randomVal.nextBytes(randomBytes); |
| 107 |
return HexEncoding.encodeToString(randomBytes); |
| 108 |
} |
| 109 |
|
| 110 |
public void start() { |
| 111 |
Thread thread = new Thread(this, "MtpServer"); |
| 112 |
thread.start(); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void run() { |
| 117 |
native_run(); |
| 118 |
native_cleanup(); |
| 119 |
mDatabase.close(); |
| 120 |
mOnTerminate.run(); |
| 121 |
} |
| 122 |
|
| 123 |
public void sendObjectAdded(int handle) { |
| 124 |
native_send_object_added(handle); |
| 125 |
} |
| 126 |
|
| 127 |
public void sendObjectRemoved(int handle) { |
| 128 |
native_send_object_removed(handle); |
| 129 |
} |
| 130 |
|
| 131 |
public void sendObjectInfoChanged(int handle) { |
| 132 |
native_send_object_info_changed(handle); |
| 133 |
} |
| 134 |
|
| 135 |
public void sendDevicePropertyChanged(int property) { |
| 136 |
native_send_device_property_changed(property); |
| 137 |
} |
| 138 |
|
| 139 |
public void addStorage(MtpStorage storage) { |
| 140 |
native_add_storage(storage); |
| 141 |
} |
| 142 |
|
| 143 |
public void removeStorage(MtpStorage storage) { |
| 144 |
native_remove_storage(storage.getStorageId()); |
| 145 |
} |
| 146 |
|
| 147 |
private native final void native_setup( |
| 148 |
MtpDatabase database, |
| 149 |
FileDescriptor controlFd, |
| 150 |
boolean usePtp, |
| 151 |
String deviceInfoManufacturer, |
| 152 |
String deviceInfoModel, |
| 153 |
String deviceInfoDeviceVersion, |
| 154 |
String deviceInfoSerialNumber); |
| 155 |
private native final void native_run(); |
| 156 |
private native final void native_cleanup(); |
| 157 |
private native final void native_send_object_added(int handle); |
| 158 |
private native final void native_send_object_removed(int handle); |
| 159 |
private native final void native_send_object_info_changed(int handle); |
| 160 |
private native final void native_send_device_property_changed(int property); |
| 161 |
private native final void native_add_storage(MtpStorage storage); |
| 162 |
private native final void native_remove_storage(int storageId); |
| 163 |
} |