Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 0cd0136 | 2010-12-30 11:54:33 -0500 | [diff] [blame] | 17 | package android.mtp; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 18 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 19 | /** |
| 20 | * Java wrapper for MTP/PTP support as USB responder. |
| 21 | * {@hide} |
| 22 | */ |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 23 | public class MtpServer implements Runnable { |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 24 | |
Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 25 | private long mNativeContext; // accessed by native methods |
Daichi Hirono | fe25931 | 2016-04-06 13:00:22 +0900 | [diff] [blame] | 26 | private final MtpDatabase mDatabase; |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 27 | |
| 28 | static { |
| 29 | System.loadLibrary("media_jni"); |
| 30 | } |
| 31 | |
Alex Klyubin | abdc2b4 | 2016-12-21 11:19:52 -0800 | [diff] [blame] | 32 | public MtpServer( |
| 33 | MtpDatabase database, |
| 34 | boolean usePtp, |
| 35 | String deviceInfoManufacturer, |
| 36 | String deviceInfoModel, |
| 37 | String deviceInfoDeviceVersion, |
| 38 | String deviceInfoSerialNumber) { |
Daichi Hirono | fe25931 | 2016-04-06 13:00:22 +0900 | [diff] [blame] | 39 | mDatabase = database; |
Alex Klyubin | abdc2b4 | 2016-12-21 11:19:52 -0800 | [diff] [blame] | 40 | native_setup( |
| 41 | database, |
| 42 | usePtp, |
| 43 | deviceInfoManufacturer, |
| 44 | deviceInfoModel, |
| 45 | deviceInfoDeviceVersion, |
| 46 | deviceInfoSerialNumber); |
Mike Lockwood | 56c8524 | 2014-03-07 13:29:08 -0800 | [diff] [blame] | 47 | database.setServer(this); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 50 | public void start() { |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 51 | Thread thread = new Thread(this, "MtpServer"); |
| 52 | thread.start(); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 55 | @Override |
| 56 | public void run() { |
| 57 | native_run(); |
| 58 | native_cleanup(); |
Daichi Hirono | fe25931 | 2016-04-06 13:00:22 +0900 | [diff] [blame] | 59 | mDatabase.close(); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 62 | public void sendObjectAdded(int handle) { |
| 63 | native_send_object_added(handle); |
| 64 | } |
| 65 | |
| 66 | public void sendObjectRemoved(int handle) { |
| 67 | native_send_object_removed(handle); |
| 68 | } |
| 69 | |
Mike Lockwood | 56c8524 | 2014-03-07 13:29:08 -0800 | [diff] [blame] | 70 | public void sendDevicePropertyChanged(int property) { |
| 71 | native_send_device_property_changed(property); |
| 72 | } |
| 73 | |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 74 | public void addStorage(MtpStorage storage) { |
| 75 | native_add_storage(storage); |
Mike Lockwood | 66e57f6 | 2011-02-18 13:24:01 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 78 | public void removeStorage(MtpStorage storage) { |
| 79 | native_remove_storage(storage.getStorageId()); |
| 80 | } |
| 81 | |
Jerry Zhang | bb598ee | 2016-10-24 14:35:08 -0700 | [diff] [blame] | 82 | public static void configure(boolean usePtp) { |
| 83 | native_configure(usePtp); |
| 84 | } |
| 85 | |
| 86 | public static native final void native_configure(boolean usePtp); |
Alex Klyubin | abdc2b4 | 2016-12-21 11:19:52 -0800 | [diff] [blame] | 87 | private native final void native_setup( |
| 88 | MtpDatabase database, |
| 89 | boolean usePtp, |
| 90 | String deviceInfoManufacturer, |
| 91 | String deviceInfoModel, |
| 92 | String deviceInfoDeviceVersion, |
| 93 | String deviceInfoSerialNumber); |
Mike Lockwood | dcc3194 | 2011-07-11 15:04:38 -0400 | [diff] [blame] | 94 | private native final void native_run(); |
| 95 | private native final void native_cleanup(); |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 96 | private native final void native_send_object_added(int handle); |
| 97 | private native final void native_send_object_removed(int handle); |
Mike Lockwood | 56c8524 | 2014-03-07 13:29:08 -0800 | [diff] [blame] | 98 | private native final void native_send_device_property_changed(int property); |
Mike Lockwood | b239b683 | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 99 | private native final void native_add_storage(MtpStorage storage); |
| 100 | private native final void native_remove_storage(int storageId); |
Mike Lockwood | 98ef64e | 2010-06-29 16:42:13 -0400 | [diff] [blame] | 101 | } |