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