1 /*
2  * Copyright (C) 2018 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.servicestests.apps.suspendtestapp;
18 
19 import static com.android.server.pm.SuspendPackagesTest.ACTION_REPORT_MY_PACKAGE_SUSPENDED;
20 import static com.android.server.pm.SuspendPackagesTest.ACTION_REPORT_MY_PACKAGE_UNSUSPENDED;
21 import static com.android.server.pm.SuspendPackagesTest.INSTRUMENTATION_PACKAGE;
22 
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.pm.PackageManager;
27 import android.os.Bundle;
28 import android.util.Log;
29 
30 public class SuspendTestReceiver extends BroadcastReceiver {
31     private static final String TAG = SuspendTestReceiver.class.getSimpleName();
32 
33     public static final String PACKAGE_NAME = "com.android.servicestests.apps.suspendtestapp";
34     public static final String ACTION_GET_SUSPENDED_STATE =
35             PACKAGE_NAME + ".action.GET_SUSPENDED_STATE";
36     public static final String EXTRA_SUSPENDED = PACKAGE_NAME + ".extra.SUSPENDED";
37     public static final String EXTRA_SUSPENDED_APP_EXTRAS =
38             PACKAGE_NAME + ".extra.SUSPENDED_APP_EXTRAS";
39 
40     @Override
onReceive(Context context, Intent intent)41     public void onReceive(Context context, Intent intent) {
42         final PackageManager packageManager = context.getPackageManager();
43         Log.d(TAG, "Received action " + intent.getAction());
44         final Bundle appExtras;
45         switch (intent.getAction()) {
46             case ACTION_GET_SUSPENDED_STATE:
47                 final Bundle result = new Bundle();
48                 final boolean suspended = packageManager.isPackageSuspended();
49                 appExtras = packageManager.getSuspendedPackageAppExtras();
50                 result.putBoolean(EXTRA_SUSPENDED, suspended);
51                 result.putBundle(EXTRA_SUSPENDED_APP_EXTRAS, appExtras);
52                 setResult(0, null, result);
53                 break;
54             case Intent.ACTION_MY_PACKAGE_SUSPENDED:
55                 appExtras = intent.getBundleExtra(Intent.EXTRA_SUSPENDED_PACKAGE_EXTRAS);
56                 final Intent reportSuspendIntent = new Intent(ACTION_REPORT_MY_PACKAGE_SUSPENDED)
57                         .putExtra(EXTRA_SUSPENDED_APP_EXTRAS, appExtras)
58                         .setPackage(INSTRUMENTATION_PACKAGE);
59                 context.sendBroadcast(reportSuspendIntent);
60                 break;
61             case Intent.ACTION_MY_PACKAGE_UNSUSPENDED:
62                 final Intent reportUnsuspendIntent =
63                         new Intent(ACTION_REPORT_MY_PACKAGE_UNSUSPENDED)
64                         .setPackage(INSTRUMENTATION_PACKAGE);
65                 context.sendBroadcast(reportUnsuspendIntent);
66                 break;
67             default:
68                 Log.e(TAG, "Unknown action: " + intent.getAction());
69         }
70     }
71 }
72