Skip to content

Commit 49397ac

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Always bind to DefaultContainerService as OWNER." into jb-mr1-dev
2 parents 6b3292c + 752cd92 commit 49397ac

File tree

4 files changed

+65
-100
lines changed

4 files changed

+65
-100
lines changed

core/java/android/content/pm/PackageCleanItem.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121

2222
/** @hide */
2323
public class PackageCleanItem {
24+
public final int userId;
2425
public final String packageName;
2526
public final boolean andCode;
2627

27-
public PackageCleanItem(String packageName, boolean andCode) {
28+
public PackageCleanItem(int userId, String packageName, boolean andCode) {
29+
this.userId = userId;
2830
this.packageName = packageName;
2931
this.andCode = andCode;
3032
}
@@ -37,7 +39,8 @@ public boolean equals(Object obj) {
3739
try {
3840
if (obj != null) {
3941
PackageCleanItem other = (PackageCleanItem)obj;
40-
return packageName.equals(other.packageName) && andCode == other.andCode;
42+
return userId == other.userId && packageName.equals(other.packageName)
43+
&& andCode == other.andCode;
4144
}
4245
} catch (ClassCastException e) {
4346
}
@@ -47,6 +50,7 @@ public boolean equals(Object obj) {
4750
@Override
4851
public int hashCode() {
4952
int result = 17;
53+
result = 31 * result + userId;
5054
result = 31 * result + packageName.hashCode();
5155
result = 31 * result + (andCode ? 1 : 0);
5256
return result;
@@ -57,6 +61,7 @@ public int describeContents() {
5761
}
5862

5963
public void writeToParcel(Parcel dest, int parcelableFlags) {
64+
dest.writeInt(userId);
6065
dest.writeString(packageName);
6166
dest.writeInt(andCode ? 1 : 0);
6267
}
@@ -73,6 +78,7 @@ public PackageCleanItem[] newArray(int size) {
7378
};
7479

7580
private PackageCleanItem(Parcel source) {
81+
userId = source.readInt();
7682
packageName = source.readString();
7783
andCode = source.readInt() != 0;
7884
}

packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import android.content.res.ObbScanner;
3636
import android.net.Uri;
3737
import android.os.Environment;
38+
import android.os.Environment.UserEnvironment;
3839
import android.os.FileUtils;
3940
import android.os.IBinder;
4041
import android.os.ParcelFileDescriptor;
@@ -268,15 +269,16 @@ public DefaultContainerService() {
268269
@Override
269270
protected void onHandleIntent(Intent intent) {
270271
if (PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE.equals(intent.getAction())) {
271-
IPackageManager pm = IPackageManager.Stub.asInterface(
272+
final IPackageManager pm = IPackageManager.Stub.asInterface(
272273
ServiceManager.getService("package"));
273-
PackageCleanItem pkg = null;
274+
PackageCleanItem item = null;
274275
try {
275-
while ((pkg=pm.nextPackageToClean(pkg)) != null) {
276-
eraseFiles(Environment.getExternalStorageAppDataDirectory(pkg.packageName));
277-
eraseFiles(Environment.getExternalStorageAppMediaDirectory(pkg.packageName));
278-
if (pkg.andCode) {
279-
eraseFiles(Environment.getExternalStorageAppObbDirectory(pkg.packageName));
276+
while ((item = pm.nextPackageToClean(item)) != null) {
277+
final UserEnvironment userEnv = new UserEnvironment(item.userId);
278+
eraseFiles(userEnv.getExternalStorageAppDataDirectory(item.packageName));
279+
eraseFiles(userEnv.getExternalStorageAppMediaDirectory(item.packageName));
280+
if (item.andCode) {
281+
eraseFiles(userEnv.getExternalStorageAppObbDirectory(item.packageName));
280282
}
281283
}
282284
} catch (RemoteException e) {

services/java/com/android/server/pm/PackageManagerService.java

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ public class PackageManagerService extends IPackageManager.Stub {
413413
// package uri's from external media onto secure containers
414414
// or internal storage.
415415
private IMediaContainerService mContainerService = null;
416-
private int mContainerServiceUserId;
417416

418417
static final int SEND_PENDING_BROADCAST = 1;
419418
static final int MCS_BOUND = 3;
@@ -482,15 +481,8 @@ private boolean connectToService() {
482481
" DefaultContainerService");
483482
Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
484483
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
485-
mContainerServiceUserId = 0;
486-
if (mPendingInstalls.size() > 0) {
487-
mContainerServiceUserId = mPendingInstalls.get(0).getUser().getIdentifier();
488-
if (mContainerServiceUserId == UserHandle.USER_ALL) {
489-
mContainerServiceUserId = 0;
490-
}
491-
}
492484
if (mContext.bindService(service, mDefContainerConn,
493-
Context.BIND_AUTO_CREATE, mContainerServiceUserId)) {
485+
Context.BIND_AUTO_CREATE, UserHandle.USER_OWNER)) {
494486
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
495487
mBound = true;
496488
return true;
@@ -567,15 +559,6 @@ void doHandleMessage(Message msg) {
567559
} else if (mPendingInstalls.size() > 0) {
568560
HandlerParams params = mPendingInstalls.get(0);
569561
if (params != null) {
570-
// Check if we're connected to the correct service, if it's an install
571-
// request.
572-
final int installFor = params.getUser().getIdentifier();
573-
if (installFor != mContainerServiceUserId
574-
&& (installFor == UserHandle.USER_ALL
575-
&& mContainerServiceUserId != 0)) {
576-
mHandler.sendEmptyMessage(MCS_RECONNECT);
577-
return;
578-
}
579562
if (params.startCopy()) {
580563
// We are done... look for more work or to
581564
// go idle.
@@ -693,20 +676,23 @@ void doHandleMessage(Message msg) {
693676
}
694677
case START_CLEANING_PACKAGE: {
695678
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
696-
PackageCleanItem item = new PackageCleanItem((String)msg.obj,
697-
msg.arg2 != 0);
679+
final String packageName = (String)msg.obj;
680+
final int userId = msg.arg1;
681+
final boolean andCode = msg.arg2 != 0;
698682
synchronized (mPackages) {
699-
if (msg.arg1 == UserHandle.USER_ALL) {
683+
if (userId == UserHandle.USER_ALL) {
700684
int[] users = sUserManager.getUserIds();
701685
for (int user : users) {
702-
mSettings.addPackageToCleanLPw(user, item);
686+
mSettings.addPackageToCleanLPw(
687+
new PackageCleanItem(user, packageName, andCode));
703688
}
704689
} else {
705-
mSettings.addPackageToCleanLPw(msg.arg1, item);
690+
mSettings.addPackageToCleanLPw(
691+
new PackageCleanItem(userId, packageName, andCode));
706692
}
707693
}
708694
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
709-
startCleaningPackages(-1);
695+
startCleaningPackages();
710696
} break;
711697
case POST_INSTALL: {
712698
if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1);
@@ -4193,10 +4179,14 @@ private PackageParser.Package scanPackageLI(PackageParser.Package pkg,
41934179
// Add the new setting to mPackages
41944180
mPackages.put(pkg.applicationInfo.packageName, pkg);
41954181
// Make sure we don't accidentally delete its data.
4196-
for (int i=0; i<mSettings.mPackagesToBeCleaned.size(); i++) {
4197-
mSettings.mPackagesToBeCleaned.valueAt(i).remove(pkgName);
4182+
final Iterator<PackageCleanItem> iter = mSettings.mPackagesToBeCleaned.iterator();
4183+
while (iter.hasNext()) {
4184+
PackageCleanItem item = iter.next();
4185+
if (pkgName.equals(item.packageName)) {
4186+
iter.remove();
4187+
}
41984188
}
4199-
4189+
42004190
// Take care of first install / last update times.
42014191
if (currentTime != 0) {
42024192
if (pkgSetting.firstInstallTime == 0) {
@@ -5443,31 +5433,20 @@ private boolean isExternalMediaAvailable() {
54435433

54445434
public PackageCleanItem nextPackageToClean(PackageCleanItem lastPackage) {
54455435
// writer
5446-
final int userId = UserHandle.getCallingUserId();
54475436
synchronized (mPackages) {
54485437
if (!isExternalMediaAvailable()) {
54495438
// If the external storage is no longer mounted at this point,
54505439
// the caller may not have been able to delete all of this
54515440
// packages files and can not delete any more. Bail.
54525441
return null;
54535442
}
5454-
ArrayList<PackageCleanItem> pkgs = mSettings.mPackagesToBeCleaned.get(userId);
5455-
if (pkgs != null) {
5456-
if (lastPackage != null) {
5457-
pkgs.remove(lastPackage);
5458-
}
5459-
if (pkgs.size() > 0) {
5460-
return pkgs.get(0);
5461-
}
5443+
final ArrayList<PackageCleanItem> pkgs = mSettings.mPackagesToBeCleaned;
5444+
if (lastPackage != null) {
5445+
pkgs.remove(lastPackage);
5446+
}
5447+
if (pkgs.size() > 0) {
5448+
return pkgs.get(0);
54625449
}
5463-
mSettings.mPackagesToBeCleaned.remove(userId);
5464-
}
5465-
// Move on to the next user to clean.
5466-
long ident = Binder.clearCallingIdentity();
5467-
try {
5468-
startCleaningPackages(userId);
5469-
} finally {
5470-
Binder.restoreCallingIdentity(ident);
54715450
}
54725451
return null;
54735452
}
@@ -5483,34 +5462,22 @@ void schedulePackageCleaning(String packageName, int userId, boolean andCode) {
54835462
userId, andCode ? 1 : 0, packageName));
54845463
}
54855464

5486-
void startCleaningPackages(int lastUser) {
5465+
void startCleaningPackages() {
54875466
// reader
5488-
int nextUser = -1;
54895467
synchronized (mPackages) {
54905468
if (!isExternalMediaAvailable()) {
54915469
return;
54925470
}
5493-
final int N = mSettings.mPackagesToBeCleaned.size();
5494-
if (N <= 0) {
5471+
if (mSettings.mPackagesToBeCleaned.isEmpty()) {
54955472
return;
54965473
}
5497-
for (int i=0; i<N; i++) {
5498-
int user = mSettings.mPackagesToBeCleaned.keyAt(i);
5499-
if (user > lastUser) {
5500-
nextUser = user;
5501-
break;
5502-
}
5503-
}
5504-
if (nextUser < 0) {
5505-
nextUser = mSettings.mPackagesToBeCleaned.keyAt(0);
5506-
}
55075474
}
55085475
Intent intent = new Intent(PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE);
55095476
intent.setComponent(DEFAULT_CONTAINER_COMPONENT);
55105477
IActivityManager am = ActivityManagerNative.getDefault();
55115478
if (am != null) {
55125479
try {
5513-
am.startService(null, intent, null, nextUser);
5480+
am.startService(null, intent, null, UserHandle.USER_OWNER);
55145481
} catch (RemoteException e) {
55155482
}
55165483
}
@@ -8399,10 +8366,11 @@ private void clearExternalStorageDataSync(String packageName, int userId, boolea
83998366
} else {
84008367
users = new int[] { userId };
84018368
}
8402-
for (int curUser : users) {
8403-
ClearStorageConnection conn = new ClearStorageConnection();
8404-
if (mContext.bindService(containerIntent, conn, Context.BIND_AUTO_CREATE, curUser)) {
8405-
try {
8369+
final ClearStorageConnection conn = new ClearStorageConnection();
8370+
if (mContext.bindService(
8371+
containerIntent, conn, Context.BIND_AUTO_CREATE, UserHandle.USER_OWNER)) {
8372+
try {
8373+
for (int curUser : users) {
84068374
long timeout = SystemClock.uptimeMillis() + 5000;
84078375
synchronized (conn) {
84088376
long now = SystemClock.uptimeMillis();
@@ -8438,9 +8406,9 @@ private void clearExternalStorageDataSync(String packageName, int userId, boolea
84388406
} catch (RemoteException e) {
84398407
}
84408408
}
8441-
} finally {
8442-
mContext.unbindService(conn);
84438409
}
8410+
} finally {
8411+
mContext.unbindService(conn);
84448412
}
84458413
}
84468414
}
@@ -9596,7 +9564,7 @@ private void updateExternalMediaStatusInner(boolean isMounted, boolean reportSta
95969564
if (DEBUG_SD_INSTALL)
95979565
Log.i(TAG, "Loading packages");
95989566
loadMediaPackages(processCids, uidArr, removeCids);
9599-
startCleaningPackages(-1);
9567+
startCleaningPackages();
96009568
} else {
96019569
if (DEBUG_SD_INSTALL)
96029570
Log.i(TAG, "Unloading packages");

services/java/com/android/server/pm/Settings.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ protected void dumpFilter(PrintWriter out, String prefix,
159159

160160
// Packages that have been uninstalled and still need their external
161161
// storage data deleted.
162-
final SparseArray<ArrayList<PackageCleanItem>> mPackagesToBeCleaned
163-
= new SparseArray<ArrayList<PackageCleanItem>>();
162+
final ArrayList<PackageCleanItem> mPackagesToBeCleaned = new ArrayList<PackageCleanItem>();
164163

165164
// Packages that have been renamed since they were first installed.
166165
// Keys are the new names of the packages, values are the original
@@ -1257,18 +1256,13 @@ void writeLPr() {
12571256
}
12581257

12591258
if (mPackagesToBeCleaned.size() > 0) {
1260-
for (int i=0; i<mPackagesToBeCleaned.size(); i++) {
1261-
final int userId = mPackagesToBeCleaned.keyAt(i);
1262-
final String userStr = Integer.toString(userId);
1263-
final ArrayList<PackageCleanItem> pkgs = mPackagesToBeCleaned.valueAt(i);
1264-
for (int j=0; j<pkgs.size(); j++) {
1265-
serializer.startTag(null, "cleaning-package");
1266-
PackageCleanItem item = pkgs.get(j);
1267-
serializer.attribute(null, ATTR_NAME, item.packageName);
1268-
serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false");
1269-
serializer.attribute(null, ATTR_USER, userStr);
1270-
serializer.endTag(null, "cleaning-package");
1271-
}
1259+
for (PackageCleanItem item : mPackagesToBeCleaned) {
1260+
final String userStr = Integer.toString(item.userId);
1261+
serializer.startTag(null, "cleaning-package");
1262+
serializer.attribute(null, ATTR_NAME, item.packageName);
1263+
serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false");
1264+
serializer.attribute(null, ATTR_USER, userStr);
1265+
serializer.endTag(null, "cleaning-package");
12721266
}
12731267
}
12741268

@@ -1524,14 +1518,9 @@ ArrayList<PackageSetting> getListOfIncompleteInstallPackagesLPr() {
15241518
return ret;
15251519
}
15261520

1527-
void addPackageToCleanLPw(int userId, PackageCleanItem pkg) {
1528-
ArrayList<PackageCleanItem> pkgs = mPackagesToBeCleaned.get(userId);
1529-
if (pkgs == null) {
1530-
pkgs = new ArrayList<PackageCleanItem>();
1531-
mPackagesToBeCleaned.put(userId, pkgs);
1532-
}
1533-
if (!pkgs.contains(pkg)) {
1534-
pkgs.add(pkg);
1521+
void addPackageToCleanLPw(PackageCleanItem pkg) {
1522+
if (!mPackagesToBeCleaned.contains(pkg)) {
1523+
mPackagesToBeCleaned.add(pkg);
15351524
}
15361525
}
15371526

@@ -1615,18 +1604,18 @@ boolean readLPw(List<UserInfo> users) {
16151604
String userStr = parser.getAttributeValue(null, ATTR_USER);
16161605
String codeStr = parser.getAttributeValue(null, ATTR_CODE);
16171606
if (name != null) {
1618-
int user = 0;
1607+
int userId = 0;
16191608
boolean andCode = true;
16201609
try {
16211610
if (userStr != null) {
1622-
user = Integer.parseInt(userStr);
1611+
userId = Integer.parseInt(userStr);
16231612
}
16241613
} catch (NumberFormatException e) {
16251614
}
16261615
if (codeStr != null) {
16271616
andCode = Boolean.parseBoolean(codeStr);
16281617
}
1629-
addPackageToCleanLPw(user, new PackageCleanItem(name, andCode));
1618+
addPackageToCleanLPw(new PackageCleanItem(userId, name, andCode));
16301619
}
16311620
} else if (tagName.equals("renamed-package")) {
16321621
String nname = parser.getAttributeValue(null, "new");

0 commit comments

Comments
 (0)