2 * Copyright (C) 2012 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.cyanogenmod.filemanager.util;
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 import android.os.Process;
23 import android.util.Log;
24 import android.util.SparseArray;
26 import com.cyanogenmod.filemanager.R;
27 import com.cyanogenmod.filemanager.model.AID;
28 import com.cyanogenmod.filemanager.model.Group;
29 import com.cyanogenmod.filemanager.model.Identity;
30 import com.cyanogenmod.filemanager.model.User;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Properties;
38 * A helper class with useful methods for deal with AID (Android IDs).
40 public final class AIDHelper {
42 private static final String TAG = "AIDHelper"; //$NON-NLS-1$
44 private static SparseArray<AID> sAids;
47 * Constructor of <code>AIDHelper</code>.
54 * Method that returns the Android IDs (system + application AID)
56 * @param context The current context
57 * @param force Force the reload of the AIDs
58 * @return SparseArray<AID> The array of {@link AID}
60 public synchronized static SparseArray<AID> getAIDs(Context context, boolean force) {
61 if (sAids == null || force) {
62 Properties systemAIDs = null;
64 // Load the default known system identifiers
65 systemAIDs = new Properties();
66 systemAIDs.load(context.getResources().openRawResource(R.raw.aid));
67 } catch (Exception e) {
68 Log.e(TAG, "Fail to load AID raw file.", e); //$NON-NLS-1$
72 // Add the default known system identifiers
73 SparseArray<AID> aids = new SparseArray<AID>();
74 Iterator<Object> it = systemAIDs.keySet().iterator();
75 while (it.hasNext()) {
76 String key = (String)it.next();
77 String value = systemAIDs.getProperty(key);
78 int uid = Integer.parseInt(key);
79 aids.put(uid, new AID(uid, value));
82 // Now, retrieve all AID of installed applications
83 final PackageManager pm = context.getPackageManager();
84 List<ApplicationInfo> packages =
85 pm.getInstalledApplications(PackageManager.GET_META_DATA);
86 int cc = packages.size();
87 for (int i = 0; i < cc; i++) {
88 ApplicationInfo info = packages.get(i);
90 if (aids.indexOfKey(uid) < 0) {
91 String name = pm.getNameForUid(uid);
92 aids.put(uid, new AID(uid, name));
96 // Save to cached aids
100 // Return the list of AIDs found
105 * Method that returns the AID from its identifier.
108 * @return AID The AID, or null if not found
110 public static AID getAID(int id) {
111 return sAids.get(id);
115 * Method that return AID from its user name.
117 * @param name The user identifier
118 * @return AID The AID
120 public static AID getAIDFromName(String name) {
121 int len = sAids.size();
122 for (int i = 0; i < len; i++) {
123 AID aid = sAids.valueAt(i);
124 if (aid.getName().compareTo(name) == 0) {
128 return new AID(-1, ""); //$NON-NLS-1$
132 * Method that returns the name in safe way
135 * @return String The name of the AID of null if not found
137 public static String getNullSafeName(int id) {
138 AID aid = getAID(id);
140 return aid.getName();
146 * Method that return a virtual identity composed by the name of the current process
148 * @return Identity The virtual identity
150 public static Identity createVirtualIdentity() {
151 AID aid = AIDHelper.getAID(Process.myUid());
152 if (aid == null) return null;
154 new User(aid.getId(), aid.getName()),
155 new Group(aid.getId(), aid.getName()),
156 new ArrayList<Group>());