• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.server.devicepolicy;
18 
19 import static com.android.server.devicepolicy.DevicePolicyManagerService.LOG_TAG;
20 
21 import android.content.pm.IPackageDeleteObserver;
22 import android.content.pm.PackageManager;
23 import android.util.Log;
24 import android.util.Slog;
25 
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicInteger;
29 
30 /**
31  * Awaits the deletion of all the non-required apps.
32  */
33 final class NonRequiredPackageDeleteObserver extends IPackageDeleteObserver.Stub {
34     private static final int PACKAGE_DELETE_TIMEOUT_SEC = 30;
35 
36     private final AtomicInteger mPackageCount = new AtomicInteger(/* initialValue= */ 0);
37     private final CountDownLatch mLatch;
38     private boolean mSuccess;
39 
NonRequiredPackageDeleteObserver(int packageCount)40     NonRequiredPackageDeleteObserver(int packageCount) {
41         this.mLatch = new CountDownLatch(packageCount);
42         this.mPackageCount.set(packageCount);
43     }
44 
45     @Override
packageDeleted(String packageName, int returnCode)46     public void packageDeleted(String packageName, int returnCode) {
47         if (returnCode != PackageManager.DELETE_SUCCEEDED) {
48             Slog.e(LOG_TAG, "Failed to delete package: " + packageName);
49             mLatch.notifyAll();
50             return;
51         }
52         int currentPackageCount = mPackageCount.decrementAndGet();
53         if (currentPackageCount == 0) {
54             mSuccess = true;
55             Slog.i(LOG_TAG, "All non-required system apps with launcher icon, "
56                     + "and all disallowed apps have been uninstalled.");
57         }
58         mLatch.countDown();
59     }
60 
awaitPackagesDeletion()61     public boolean awaitPackagesDeletion() {
62         try {
63             mLatch.await(PACKAGE_DELETE_TIMEOUT_SEC, TimeUnit.SECONDS);
64         } catch (InterruptedException e) {
65             Log.w(LOG_TAG, "Interrupted while waiting for package deletion", e);
66             Thread.currentThread().interrupt();
67         }
68         return mSuccess;
69     }
70 }
71