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.RemoteException; 26 27 import com.android.cts.net.hostside.IMyService; 28 29 import java.io.FileDescriptor; 30 31 public class MyServiceClient { 32 private static final int TIMEOUT_MS = 5000; 33 private static final String PACKAGE = MyServiceClient.class.getPackage().getName(); 34 private static final String APP2_PACKAGE = PACKAGE + ".app2"; 35 private static final String SERVICE_NAME = APP2_PACKAGE + ".MyService"; 36 37 private Context mContext; 38 private ServiceConnection mServiceConnection; 39 private IMyService mService; 40 41 public MyServiceClient(Context context) { 42 mContext = context; 43 } 44 45 public void bind() { 46 if (mService != null) { 47 throw new IllegalStateException("Already bound"); 48 } 49 50 final ConditionVariable cv = new ConditionVariable(); 51 mServiceConnection = new ServiceConnection() { 52 @Override 53 public void onServiceConnected(ComponentName name, IBinder service) { 54 mService = IMyService.Stub.asInterface(service); 55 cv.open(); 56 } 57 @Override 58 public void onServiceDisconnected(ComponentName name) { 59 mService = null; 60 } 61 }; 62 63 final Intent intent = new Intent(); 64 intent.setComponent(new ComponentName(APP2_PACKAGE, SERVICE_NAME)); 65 // Needs to use BIND_ALLOW_OOM_MANAGEMENT and BIND_NOT_FOREGROUND so app2 does not run in 66 // the same process state as app 67 mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE 68 | Context.BIND_ALLOW_OOM_MANAGEMENT | Context.BIND_NOT_FOREGROUND); 69 cv.block(TIMEOUT_MS); 70 if (mService == null) { 71 throw new IllegalStateException( 72 "Could not bind to MyService service after " + TIMEOUT_MS + "ms"); 73 } 74 } 75 76 public void unbind() { 77 if (mService != null) { 78 mContext.unbindService(mServiceConnection); 79 } 80 } 81 82 public void registerBroadcastReceiver() throws RemoteException { 83 mService.registerBroadcastReceiver(); 84 } 85 86 public int getCounters(String receiverName, String action) throws RemoteException { 87 return mService.getCounters(receiverName, action); 88 } 89 90 public String checkNetworkStatus() throws RemoteException { 91 return mService.checkNetworkStatus(); 92 } 93 94 public String getRestrictBackgroundStatus() throws RemoteException { 95 return mService.getRestrictBackgroundStatus(); 96 } 97 98 public void sendNotification(int notificationId, String notificationType) throws RemoteException { 99 mService.sendNotification(notificationId, notificationType); 100 } 101 } 102