Home | History | Annotate | Download | only in hostside
      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;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.ServiceConnection;
     23 import android.os.ConditionVariable;
     24 import android.os.IBinder;
     25 import android.os.ParcelFileDescriptor;
     26 import android.os.RemoteException;
     27 import android.system.ErrnoException;
     28 import android.system.Os;
     29 
     30 import com.android.cts.net.hostside.IRemoteSocketFactory;
     31 
     32 import java.io.FileDescriptor;
     33 import java.io.IOException;
     34 
     35 public class RemoteSocketFactoryClient {
     36     private static final int TIMEOUT_MS = 5000;
     37     private static final String PACKAGE = RemoteSocketFactoryClient.class.getPackage().getName();
     38     private static final String APP2_PACKAGE = PACKAGE + ".app2";
     39     private static final String SERVICE_NAME = APP2_PACKAGE + ".RemoteSocketFactoryService";
     40 
     41     private Context mContext;
     42     private ServiceConnection mServiceConnection;
     43     private IRemoteSocketFactory mService;
     44 
     45     public RemoteSocketFactoryClient(Context context) {
     46         mContext = context;
     47     }
     48 
     49     public void bind() {
     50         if (mService != null) {
     51             throw new IllegalStateException("Already bound");
     52         }
     53 
     54         final ConditionVariable cv = new ConditionVariable();
     55         mServiceConnection = new ServiceConnection() {
     56             @Override
     57             public void onServiceConnected(ComponentName name, IBinder service) {
     58                 mService = IRemoteSocketFactory.Stub.asInterface(service);
     59                 cv.open();
     60             }
     61             @Override
     62             public void onServiceDisconnected(ComponentName name) {
     63                 mService = null;
     64             }
     65         };
     66 
     67         final Intent intent = new Intent();
     68         intent.setComponent(new ComponentName(APP2_PACKAGE, SERVICE_NAME));
     69         mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
     70         cv.block(TIMEOUT_MS);
     71         if (mService == null) {
     72             throw new IllegalStateException(
     73                     "Could not bind to RemoteSocketFactory service after " + TIMEOUT_MS + "ms");
     74         }
     75     }
     76 
     77     public void unbind() {
     78         if (mService != null) {
     79             mContext.unbindService(mServiceConnection);
     80         }
     81     }
     82 
     83     public FileDescriptor openSocketFd(String host, int port, int timeoutMs)
     84             throws RemoteException, ErrnoException, IOException {
     85         // Dup the filedescriptor so ParcelFileDescriptor's finalizer doesn't garbage collect it
     86         // and cause our fd to become invalid. http://b/35927643 .
     87         ParcelFileDescriptor pfd = mService.openSocketFd(host, port, timeoutMs);
     88         FileDescriptor fd = Os.dup(pfd.getFileDescriptor());
     89         pfd.close();
     90         return fd;
     91     }
     92 
     93     public String getPackageName() throws RemoteException {
     94         return mService.getPackageName();
     95     }
     96 
     97     public int getUid() throws RemoteException {
     98         return mService.getUid();
     99     }
    100 }
    101