• 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 
17 package com.android.storagemanager.deletionhelper;
18 
19 import android.content.pm.IPackageDeleteObserver;
20 import android.content.pm.PackageManager;
21 import android.os.UserHandle;
22 import android.util.Log;
23 
24 import java.util.Set;
25 import java.util.concurrent.atomic.AtomicInteger;
26 
27 /**
28  * Deletes a specified set of apps as a specified user and calls back once done.
29  */
30 public class PackageDeletionTask {
31     private static final String TAG = "PackageDeletionTask";
32     private Set<String> mPackages;
33     private Callback mCallback;
34     private PackageManager mPm;
35     private UserHandle mUser;
36 
PackageDeletionTask(PackageManager pm, Set<String> packageNames, Callback callback)37     public PackageDeletionTask(PackageManager pm, Set<String> packageNames, Callback callback) {
38         mPackages = packageNames;
39         mCallback = callback;
40         mPm = pm;
41         mUser = android.os.Process.myUserHandle();
42     }
43 
44     /**
45      * Runs the deletion task and clears out the given packages. Upon completion, the callback
46      * is run, if it is set.
47      */
run()48     public void run() {
49         PackageDeletionObserver observer = new PackageDeletionObserver(mPackages.size());
50         for (String packageName : mPackages) {
51             try {
52                 mPm.deletePackageAsUser(packageName, observer, 0, mUser.getIdentifier());
53             } catch (IllegalArgumentException e) {
54                 // Couldn't find the package, no need to delete.
55                 Log.w(TAG, "Could not find package, not deleting " + packageName, e);
56             }
57         }
58     }
59 
60     private class PackageDeletionObserver extends IPackageDeleteObserver.Stub {
61         private final AtomicInteger mPackagesRemaining = new AtomicInteger(0);
62 
PackageDeletionObserver(int packages)63         public PackageDeletionObserver(int packages) {
64             mPackagesRemaining.set(packages);
65         }
66 
67         @Override
packageDeleted(String packageName, int returnCode)68         public void packageDeleted(String packageName, int returnCode) {
69             if (returnCode != PackageManager.DELETE_SUCCEEDED) {
70                 mCallback.onError();
71                 return;
72             }
73 
74             int remaining = mPackagesRemaining.decrementAndGet();
75             if (remaining == 0) {
76                 mCallback.onSuccess();
77             }
78         }
79     }
80 
81     public static abstract class Callback {
onSuccess()82         public abstract void onSuccess();
onError()83         public abstract void onError();
84     }
85 }
86