OSDN Git Service

Improve copy/move performance with nio and reintroduce cancel.
[android-x86/packages-apps-CMFileManager.git] / src / com / cyanogenmod / filemanager / util / AIDHelper.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod 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.cyanogenmod.filemanager.util;
18
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;
25
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;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Properties;
36
37 /**
38  * A helper class with useful methods for deal with AID (Android IDs).
39  */
40 public final class AIDHelper {
41
42     private static final String TAG = "AIDHelper"; //$NON-NLS-1$
43
44     private static SparseArray<AID> sAids;
45
46     /**
47      * Constructor of <code>AIDHelper</code>.
48      */
49     private AIDHelper() {
50         super();
51     }
52
53     /**
54      * Method that returns the Android IDs (system + application AID)
55      *
56      * @param context The current context
57      * @param force Force the reload of the AIDs
58      * @return SparseArray<AID> The array of {@link AID}
59      */
60     public synchronized static SparseArray<AID> getAIDs(Context context, boolean force) {
61         if (sAids == null || force) {
62             Properties systemAIDs = null;
63             try {
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$
69                 return null;
70             }
71
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));
80             }
81
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);
89                 int uid = info.uid;
90                 if (aids.indexOfKey(uid) < 0) {
91                     String name = pm.getNameForUid(uid);
92                     aids.put(uid, new AID(uid, name));
93                 }
94             }
95
96             // Save to cached aids
97             sAids = aids;
98         }
99
100         // Return the list of AIDs found
101         return sAids;
102     }
103
104     /**
105      * Method that returns the AID from its identifier.
106      *
107      * @param id The id
108      * @return AID The AID, or null if not found
109      */
110     public static AID getAID(int id) {
111         return sAids.get(id);
112     }
113
114     /**
115      * Method that return AID from its user name.
116      *
117      * @param name The user identifier
118      * @return AID The AID
119      */
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) {
125                 return aid;
126             }
127         }
128         return new AID(-1, ""); //$NON-NLS-1$
129     }
130
131     /**
132      * Method that returns the name in safe way
133      *
134      * @param id The id
135      * @return String The name of the AID of null if not found
136      */
137     public static String getNullSafeName(int id) {
138         AID aid = getAID(id);
139         if (aid != null) {
140             return aid.getName();
141         }
142         return null;
143     }
144
145     /**
146      * Method that return a virtual identity composed by the name of the current process
147      *
148      * @return Identity The virtual identity
149      */
150     public static Identity createVirtualIdentity() {
151         AID aid = AIDHelper.getAID(Process.myUid());
152         if (aid == null) return null;
153         return new Identity(
154                 new User(aid.getId(), aid.getName()),
155                 new Group(aid.getId(), aid.getName()),
156                 new ArrayList<Group>());
157     }
158
159 }