• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.ActivityManager;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager.NameNotFoundException;
22 import android.os.AsyncTask;
23 import android.os.Bundle;
24 import android.os.Process;
25 import android.os.RemoteException;
26 import android.util.Log;
27 
28 import com.android.cts.net.hostside.INetworkStateObserver;
29 
30 public final class Common {
31 
32     static final String TAG = "CtsNetApp2";
33 
34     // Constants below must match values defined on app's
35     // AbstractRestrictBackgroundNetworkTestCase.java
36     static final String MANIFEST_RECEIVER = "ManifestReceiver";
37     static final String DYNAMIC_RECEIVER = "DynamicReceiver";
38 
39     static final String ACTION_RECEIVER_READY =
40             "com.android.cts.net.hostside.app2.action.RECEIVER_READY";
41     static final String ACTION_FINISH_ACTIVITY =
42             "com.android.cts.net.hostside.app2.action.FINISH_ACTIVITY";
43     static final String ACTION_FINISH_JOB =
44             "com.android.cts.net.hostside.app2.action.FINISH_JOB";
45     static final String ACTION_SHOW_TOAST =
46             "com.android.cts.net.hostside.app2.action.SHOW_TOAST";
47     // Copied from com.android.server.net.NetworkPolicyManagerService class
48     static final String ACTION_SNOOZE_WARNING =
49             "com.android.server.net.action.SNOOZE_WARNING";
50 
51     static final String NOTIFICATION_TYPE_CONTENT = "CONTENT";
52     static final String NOTIFICATION_TYPE_DELETE = "DELETE";
53     static final String NOTIFICATION_TYPE_FULL_SCREEN = "FULL_SCREEN";
54     static final String NOTIFICATION_TYPE_BUNDLE = "BUNDLE";
55     static final String NOTIFICATION_TYPE_ACTION = "ACTION";
56     static final String NOTIFICATION_TYPE_ACTION_BUNDLE = "ACTION_BUNDLE";
57     static final String NOTIFICATION_TYPE_ACTION_REMOTE_INPUT = "ACTION_REMOTE_INPUT";
58 
59     static final String TEST_PKG = "com.android.cts.net.hostside";
60     static final String KEY_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer";
61     static final String KEY_SKIP_VALIDATION_CHECKS = TEST_PKG + ".skip_validation_checks";
62 
63     static final int TYPE_COMPONENT_ACTIVTY = 0;
64     static final int TYPE_COMPONENT_FOREGROUND_SERVICE = 1;
65     static final int TYPE_COMPONENT_EXPEDITED_JOB = 2;
66 
getUid(Context context)67     static int getUid(Context context) {
68         final String packageName = context.getPackageName();
69         try {
70             return context.getPackageManager().getPackageUid(packageName, 0);
71         } catch (NameNotFoundException e) {
72             throw new IllegalStateException("Could not get UID for " + packageName, e);
73         }
74     }
75 
validateComponentState(Context context, int componentType, INetworkStateObserver observer)76     private static boolean validateComponentState(Context context, int componentType,
77             INetworkStateObserver observer) throws RemoteException {
78         final ActivityManager activityManager = context.getSystemService(ActivityManager.class);
79         switch (componentType) {
80             case TYPE_COMPONENT_ACTIVTY: {
81                 final int procState = activityManager.getUidProcessState(Process.myUid());
82                 if (procState != ActivityManager.PROCESS_STATE_TOP) {
83                     observer.onNetworkStateChecked(
84                             INetworkStateObserver.RESULT_ERROR_UNEXPECTED_PROC_STATE,
85                             "Unexpected procstate: " + procState);
86                     return false;
87                 }
88                 return true;
89             }
90             case TYPE_COMPONENT_FOREGROUND_SERVICE: {
91                 final int procState = activityManager.getUidProcessState(Process.myUid());
92                 if (procState != ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
93                     observer.onNetworkStateChecked(
94                             INetworkStateObserver.RESULT_ERROR_UNEXPECTED_PROC_STATE,
95                             "Unexpected procstate: " + procState);
96                     return false;
97                 }
98                 return true;
99             }
100             case TYPE_COMPONENT_EXPEDITED_JOB: {
101                 final int capabilities = activityManager.getUidProcessCapabilities(Process.myUid());
102                 if ((capabilities
103                         & ActivityManager.PROCESS_CAPABILITY_POWER_RESTRICTED_NETWORK) == 0) {
104                     observer.onNetworkStateChecked(
105                             INetworkStateObserver.RESULT_ERROR_UNEXPECTED_CAPABILITIES,
106                             "Unexpected capabilities: " + capabilities);
107                     return false;
108                 }
109                 return true;
110             }
111             default: {
112                 observer.onNetworkStateChecked(INetworkStateObserver.RESULT_ERROR_OTHER,
113                         "Unknown component type: " + componentType);
114                 return false;
115             }
116         }
117     }
118 
notifyNetworkStateObserver(Context context, Intent intent, int componentType)119     static void notifyNetworkStateObserver(Context context, Intent intent, int componentType) {
120         if (intent == null) {
121             return;
122         }
123         final Bundle extras = intent.getExtras();
124         notifyNetworkStateObserver(context, extras, componentType);
125     }
126 
notifyNetworkStateObserver(Context context, Bundle extras, int componentType)127     static void notifyNetworkStateObserver(Context context, Bundle extras, int componentType) {
128         if (extras == null) {
129             return;
130         }
131         final INetworkStateObserver observer = INetworkStateObserver.Stub.asInterface(
132                 extras.getBinder(KEY_NETWORK_STATE_OBSERVER));
133         if (observer != null) {
134             try {
135                 final boolean skipValidation = extras.getBoolean(KEY_SKIP_VALIDATION_CHECKS);
136                 if (!skipValidation && !validateComponentState(context, componentType, observer)) {
137                     return;
138                 }
139             } catch (RemoteException e) {
140                 Log.e(TAG, "Error occurred while informing the validation result: " + e);
141             }
142             AsyncTask.execute(() -> {
143                 try {
144                     observer.onNetworkStateChecked(
145                             INetworkStateObserver.RESULT_SUCCESS_NETWORK_STATE_CHECKED,
146                             MyBroadcastReceiver.checkNetworkStatus(context));
147                 } catch (RemoteException e) {
148                     Log.e(TAG, "Error occurred while notifying the observer: " + e);
149                 }
150             });
151         }
152     }
153 }
154