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 29 /** 30 * Awaits the deletion of all the non-required apps. 31 */ 32 final class NonRequiredPackageDeleteObserver extends IPackageDeleteObserver.Stub { 33 private static final int PACKAGE_DELETE_TIMEOUT_SEC = 30; 34 35 private final CountDownLatch mLatch; 36 private boolean mFailed = false; 37 NonRequiredPackageDeleteObserver(int packageCount)38 NonRequiredPackageDeleteObserver(int packageCount) { 39 this.mLatch = new CountDownLatch(packageCount); 40 } 41 42 @Override packageDeleted(String packageName, int returnCode)43 public void packageDeleted(String packageName, int returnCode) { 44 if (returnCode != PackageManager.DELETE_SUCCEEDED) { 45 Slog.e(LOG_TAG, "Failed to delete package: " + packageName); 46 mFailed = true; 47 } 48 mLatch.countDown(); 49 } 50 awaitPackagesDeletion()51 public boolean awaitPackagesDeletion() { 52 try { 53 if (mLatch.await(PACKAGE_DELETE_TIMEOUT_SEC, TimeUnit.SECONDS)) { 54 if (!mFailed) { 55 Slog.i(LOG_TAG, "All non-required system apps with launcher icon, " 56 + "and all disallowed apps have been uninstalled."); 57 } 58 return !mFailed; 59 } else { 60 Slog.i(LOG_TAG, "Waiting time elapsed before all package deletion finished"); 61 return false; 62 } 63 } catch (InterruptedException e) { 64 Log.w(LOG_TAG, "Interrupted while waiting for package deletion", e); 65 Thread.currentThread().interrupt(); 66 return false; 67 } 68 } 69 } 70