blob: 1d2657a280a227b76a571997c7d6f9c374089bc5 [file] [log] [blame]
Aurimas Liutikas93554f22022-04-19 16:51:35 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.mtp;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21
22import com.android.internal.util.Preconditions;
23
24import libcore.util.HexEncoding;
25
26import java.io.FileDescriptor;
27import java.util.Random;
28
29/**
30 * Java wrapper for MTP/PTP support as USB responder.
31 * {@hide}
32 */
33public class MtpServer implements Runnable {
34
35 private long mNativeContext; // accessed by native methods
36 private final MtpDatabase mDatabase;
37 private final Runnable mOnTerminate;
38 private final Context mContext;
39
40// It requires "exactly 32 characters, including any leading 0s" in MTP spec
41// (5.1.1.14 Serial Number)
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 // Check for format consistence (regenerate upon corruption)
72 if (strRandomId.length() != sID_LEN_STR) {
73 strRandomId = null;
74 } else {
75 // Only accept hex digit
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}