1 /* 2 * Copyright (C) 2017 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.normalapp; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.os.Binder; 24 import android.os.IBinder; 25 26 import com.android.cts.util.TestResult; 27 28 public class ExposedService extends Service { 29 30 @Override 31 public IBinder onBind(Intent intent) { 32 boolean canAccessInstantApp = false; 33 String exception = null; 34 try { 35 canAccessInstantApp = tryAccessingInstantApp(); 36 } catch (Throwable t) { 37 exception = t.getClass().getName(); 38 } 39 TestResult.getBuilder() 40 .setPackageName("com.android.cts.normalapp") 41 .setComponentName("ExposedService") 42 .setMethodName("onBind") 43 .setStatus("PASS") 44 .setException(exception) 45 .setEphemeralPackageInfoExposed(canAccessInstantApp) 46 .build() 47 .broadcast(this); 48 return new Binder(); 49 } 50 51 @Override 52 public int onStartCommand(Intent intent, int flags, int startId) { 53 boolean canAccessInstantApp = false; 54 String exception = null; 55 try { 56 canAccessInstantApp = tryAccessingInstantApp(); 57 } catch (Throwable t) { 58 exception = t.getClass().getName(); 59 } 60 TestResult.getBuilder() 61 .setPackageName("com.android.cts.normalapp") 62 .setComponentName("ExposedService") 63 .setMethodName("onStartCommand") 64 .setStatus("PASS") 65 .setException(exception) 66 .setEphemeralPackageInfoExposed(canAccessInstantApp) 67 .build() 68 .broadcast(this); 69 return super.onStartCommand(intent, flags, startId); 70 } 71 72 private boolean tryAccessingInstantApp() throws NameNotFoundException { 73 final PackageInfo info = getPackageManager() 74 .getPackageInfo("com.android.cts.ephemeralapp1", 0 /*flags*/); 75 return (info != null); 76 } 77 } 78