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 package com.android.cts.net.hostside.app2; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.pm.PackageManager.NameNotFoundException; 21 import android.os.AsyncTask; 22 import android.os.Bundle; 23 import android.os.RemoteException; 24 import android.util.Log; 25 26 import com.android.cts.net.hostside.INetworkStateObserver; 27 28 public final class Common { 29 30 static final String TAG = "CtsNetApp2"; 31 32 // Constants below must match values defined on app's 33 // AbstractRestrictBackgroundNetworkTestCase.java 34 static final String MANIFEST_RECEIVER = "ManifestReceiver"; 35 static final String DYNAMIC_RECEIVER = "DynamicReceiver"; 36 37 static final String ACTION_RECEIVER_READY = 38 "com.android.cts.net.hostside.app2.action.RECEIVER_READY"; 39 static final String ACTION_FINISH_ACTIVITY = 40 "com.android.cts.net.hostside.app2.action.FINISH_ACTIVITY"; 41 static final String ACTION_SHOW_TOAST = 42 "com.android.cts.net.hostside.app2.action.SHOW_TOAST"; 43 44 static final String NOTIFICATION_TYPE_CONTENT = "CONTENT"; 45 static final String NOTIFICATION_TYPE_DELETE = "DELETE"; 46 static final String NOTIFICATION_TYPE_FULL_SCREEN = "FULL_SCREEN"; 47 static final String NOTIFICATION_TYPE_BUNDLE = "BUNDLE"; 48 static final String NOTIFICATION_TYPE_ACTION = "ACTION"; 49 static final String NOTIFICATION_TYPE_ACTION_BUNDLE = "ACTION_BUNDLE"; 50 static final String NOTIFICATION_TYPE_ACTION_REMOTE_INPUT = "ACTION_REMOTE_INPUT"; 51 52 static final String TEST_PKG = "com.android.cts.net.hostside"; 53 static final String KEY_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer"; 54 55 static int getUid(Context context) { 56 final String packageName = context.getPackageName(); 57 try { 58 return context.getPackageManager().getPackageUid(packageName, 0); 59 } catch (NameNotFoundException e) { 60 throw new IllegalStateException("Could not get UID for " + packageName, e); 61 } 62 } 63 64 static void notifyNetworkStateObserver(Context context, Intent intent) { 65 if (intent == null) { 66 return; 67 } 68 final Bundle extras = intent.getExtras(); 69 if (extras == null) { 70 return; 71 } 72 final INetworkStateObserver observer = INetworkStateObserver.Stub.asInterface( 73 extras.getBinder(KEY_NETWORK_STATE_OBSERVER)); 74 if (observer != null) { 75 try { 76 if (!observer.isForeground()) { 77 Log.e(TAG, "App didn't come to foreground"); 78 observer.onNetworkStateChecked(null); 79 return; 80 } 81 } catch (RemoteException e) { 82 Log.e(TAG, "Error occurred while reading the proc state: " + e); 83 } 84 AsyncTask.execute(() -> { 85 try { 86 observer.onNetworkStateChecked( 87 MyBroadcastReceiver.checkNetworkStatus(context)); 88 } catch (RemoteException e) { 89 Log.e(TAG, "Error occurred while notifying the observer: " + e); 90 } 91 }); 92 } 93 } 94 } 95