1 /* 2 * Copyright (C) 2016 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 com.android.cts.net.hostside.app2; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.IBinder; 22 import android.os.ParcelFileDescriptor; 23 import android.os.Process; 24 25 import com.android.cts.net.hostside.IRemoteSocketFactory; 26 27 import java.io.UncheckedIOException; 28 import java.net.DatagramSocket; 29 import java.net.Socket; 30 import java.net.SocketException; 31 32 33 public class RemoteSocketFactoryService extends Service { 34 35 private static final String TAG = RemoteSocketFactoryService.class.getSimpleName(); 36 37 private IRemoteSocketFactory.Stub mBinder = new IRemoteSocketFactory.Stub() { 38 @Override 39 public ParcelFileDescriptor openSocketFd(String host, int port, int timeoutMs) { 40 try { 41 Socket s = new Socket(host, port); 42 s.setSoTimeout(timeoutMs); 43 return ParcelFileDescriptor.fromSocket(s); 44 } catch (Exception e) { 45 throw new IllegalArgumentException(e); 46 } 47 } 48 49 @Override 50 public String getPackageName() { 51 return RemoteSocketFactoryService.this.getPackageName(); 52 } 53 54 @Override 55 public int getUid() { 56 return Process.myUid(); 57 } 58 59 @Override 60 public ParcelFileDescriptor openDatagramSocketFd() { 61 try { 62 final DatagramSocket s = new DatagramSocket(); 63 return ParcelFileDescriptor.fromDatagramSocket(s); 64 } catch (SocketException e) { 65 throw new UncheckedIOException(e); 66 } 67 } 68 }; 69 70 @Override onBind(Intent intent)71 public IBinder onBind(Intent intent) { 72 return mBinder; 73 } 74 } 75