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
17package android.mtp;
18
19import com.android.internal.util.Preconditions;
20
21/**
22 * Java wrapper for MTP/PTP support as USB responder.
23 * {@hide}
24 */
25public class MtpServer implements Runnable {
26
27    private long mNativeContext; // accessed by native methods
28    private final MtpDatabase mDatabase;
29    private final Runnable mOnTerminate;
30
31    static {
32        System.loadLibrary("media_jni");
33    }
34
35    public MtpServer(
36            MtpDatabase database,
37            boolean usePtp,
38            Runnable onTerminate,
39            String deviceInfoManufacturer,
40            String deviceInfoModel,
41            String deviceInfoDeviceVersion,
42            String deviceInfoSerialNumber) {
43        mDatabase = Preconditions.checkNotNull(database);
44        mOnTerminate = Preconditions.checkNotNull(onTerminate);
45        native_setup(
46                database,
47                usePtp,
48                deviceInfoManufacturer,
49                deviceInfoModel,
50                deviceInfoDeviceVersion,
51                deviceInfoSerialNumber);
52        database.setServer(this);
53    }
54
55    public void start() {
56        Thread thread = new Thread(this, "MtpServer");
57        thread.start();
58    }
59
60    @Override
61    public void run() {
62        native_run();
63        native_cleanup();
64        mDatabase.close();
65        mOnTerminate.run();
66    }
67
68    public void sendObjectAdded(int handle) {
69        native_send_object_added(handle);
70    }
71
72    public void sendObjectRemoved(int handle) {
73        native_send_object_removed(handle);
74    }
75
76    public void sendDevicePropertyChanged(int property) {
77        native_send_device_property_changed(property);
78    }
79
80    public void addStorage(MtpStorage storage) {
81        native_add_storage(storage);
82    }
83
84    public void removeStorage(MtpStorage storage) {
85        native_remove_storage(storage.getStorageId());
86    }
87
88    public static void configure(boolean usePtp) {
89        native_configure(usePtp);
90    }
91
92    public static native final void native_configure(boolean usePtp);
93    private native final void native_setup(
94            MtpDatabase database,
95            boolean usePtp,
96            String deviceInfoManufacturer,
97            String deviceInfoModel,
98            String deviceInfoDeviceVersion,
99            String deviceInfoSerialNumber);
100    private native final void native_run();
101    private native final void native_cleanup();
102    private native final void native_send_object_added(int handle);
103    private native final void native_send_object_removed(int handle);
104    private native final void native_send_device_property_changed(int property);
105    private native final void native_add_storage(MtpStorage storage);
106    private native final void native_remove_storage(int storageId);
107}
108