1 /*
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 
17 package android.mtp;
18 
19 /**
20  * Java wrapper for MTP/PTP support as USB responder.
21  * {@hide}
22  */
23 public class MtpServer implements Runnable {
24 
25     private long mNativeContext; // accessed by native methods
26     private final MtpDatabase mDatabase;
27 
28     static {
29         System.loadLibrary("media_jni");
30     }
31 
MtpServer(MtpDatabase database, boolean usePtp)32     public MtpServer(MtpDatabase database, boolean usePtp) {
33         mDatabase = database;
34         native_setup(database, usePtp);
35         database.setServer(this);
36     }
37 
start()38     public void start() {
39         Thread thread = new Thread(this, "MtpServer");
40         thread.start();
41     }
42 
43     @Override
run()44     public void run() {
45         native_run();
46         native_cleanup();
47         mDatabase.close();
48     }
49 
sendObjectAdded(int handle)50     public void sendObjectAdded(int handle) {
51         native_send_object_added(handle);
52     }
53 
sendObjectRemoved(int handle)54     public void sendObjectRemoved(int handle) {
55         native_send_object_removed(handle);
56     }
57 
sendDevicePropertyChanged(int property)58     public void sendDevicePropertyChanged(int property) {
59         native_send_device_property_changed(property);
60     }
61 
addStorage(MtpStorage storage)62     public void addStorage(MtpStorage storage) {
63         native_add_storage(storage);
64     }
65 
removeStorage(MtpStorage storage)66     public void removeStorage(MtpStorage storage) {
67         native_remove_storage(storage.getStorageId());
68     }
69 
native_setup(MtpDatabase database, boolean usePtp)70     private native final void native_setup(MtpDatabase database, boolean usePtp);
native_run()71     private native final void native_run();
native_cleanup()72     private native final void native_cleanup();
native_send_object_added(int handle)73     private native final void native_send_object_added(int handle);
native_send_object_removed(int handle)74     private native final void native_send_object_removed(int handle);
native_send_device_property_changed(int property)75     private native final void native_send_device_property_changed(int property);
native_add_storage(MtpStorage storage)76     private native final void native_add_storage(MtpStorage storage);
native_remove_storage(int storageId)77     private native final void native_remove_storage(int storageId);
78 }
79