use of android.util.SparseIntArray in project android_frameworks_base by DirtyUnicorns.
the class AppOpsService method writeState.
void writeState() {
synchronized (mFile) {
FileOutputStream stream;
try {
stream = mFile.startWrite();
} catch (IOException e) {
Slog.w(TAG, "Failed to write state: " + e);
return;
}
SparseArray<UidState> outUidStates = null;
synchronized (this) {
final int uidStateCount = mUidStates.size();
for (int i = 0; i < uidStateCount; i++) {
UidState uidState = mUidStates.valueAt(i);
SparseIntArray opModes = uidState.opModes;
if (opModes != null && opModes.size() > 0) {
UidState outUidState = new UidState(uidState.uid);
outUidState.opModes = opModes.clone();
if (outUidStates == null) {
outUidStates = new SparseArray<>();
}
outUidStates.put(mUidStates.keyAt(i), outUidState);
}
}
}
List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
try {
XmlSerializer out = new FastXmlSerializer();
out.setOutput(stream, StandardCharsets.UTF_8.name());
out.startDocument(null, true);
out.startTag(null, "app-ops");
final int uidStateCount = outUidStates != null ? outUidStates.size() : 0;
for (int i = 0; i < uidStateCount; i++) {
UidState uidState = outUidStates.valueAt(i);
if (uidState.opModes != null && uidState.opModes.size() > 0) {
out.startTag(null, "uid");
out.attribute(null, "n", Integer.toString(uidState.uid));
SparseIntArray uidOpModes = uidState.opModes;
final int opCount = uidOpModes.size();
for (int j = 0; j < opCount; j++) {
final int op = uidOpModes.keyAt(j);
final int mode = uidOpModes.valueAt(j);
out.startTag(null, "op");
out.attribute(null, "n", Integer.toString(op));
out.attribute(null, "m", Integer.toString(mode));
out.endTag(null, "op");
}
out.endTag(null, "uid");
}
}
if (allOps != null) {
String lastPkg = null;
for (int i = 0; i < allOps.size(); i++) {
AppOpsManager.PackageOps pkg = allOps.get(i);
if (!pkg.getPackageName().equals(lastPkg)) {
if (lastPkg != null) {
out.endTag(null, "pkg");
}
lastPkg = pkg.getPackageName();
out.startTag(null, "pkg");
out.attribute(null, "n", lastPkg);
}
out.startTag(null, "uid");
out.attribute(null, "n", Integer.toString(pkg.getUid()));
synchronized (this) {
Ops ops = getOpsRawLocked(pkg.getUid(), pkg.getPackageName(), false);
// from Ops.
if (ops != null) {
out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
} else {
out.attribute(null, "p", Boolean.toString(false));
}
}
List<AppOpsManager.OpEntry> ops = pkg.getOps();
for (int j = 0; j < ops.size(); j++) {
AppOpsManager.OpEntry op = ops.get(j);
out.startTag(null, "op");
out.attribute(null, "n", Integer.toString(op.getOp()));
if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
out.attribute(null, "m", Integer.toString(op.getMode()));
}
long time = op.getTime();
if (time != 0) {
out.attribute(null, "t", Long.toString(time));
}
time = op.getRejectTime();
if (time != 0) {
out.attribute(null, "r", Long.toString(time));
}
int dur = op.getDuration();
if (dur != 0) {
out.attribute(null, "d", Integer.toString(dur));
}
int proxyUid = op.getProxyUid();
if (proxyUid != -1) {
out.attribute(null, "pu", Integer.toString(proxyUid));
}
String proxyPackageName = op.getProxyPackageName();
if (proxyPackageName != null) {
out.attribute(null, "pp", proxyPackageName);
}
out.endTag(null, "op");
}
out.endTag(null, "uid");
}
if (lastPkg != null) {
out.endTag(null, "pkg");
}
}
out.endTag(null, "app-ops");
out.endDocument();
mFile.finishWrite(stream);
} catch (IOException e) {
Slog.w(TAG, "Failed to write state, restoring backup.", e);
mFile.failWrite(stream);
}
}
}
use of android.util.SparseIntArray in project android_frameworks_base by DirtyUnicorns.
the class AppOpsService method dump.
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
pw.println("Permission Denial: can't dump ApOps service from from pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
return;
}
if (args != null) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("-h".equals(arg)) {
dumpHelp(pw);
return;
} else if ("-a".equals(arg)) {
// dump all data
} else if (arg.length() > 0 && arg.charAt(0) == '-') {
pw.println("Unknown option: " + arg);
return;
} else {
pw.println("Unknown command: " + arg);
return;
}
}
}
synchronized (this) {
pw.println("Current AppOps Service state:");
final long now = System.currentTimeMillis();
boolean needSep = false;
if (mOpModeWatchers.size() > 0) {
needSep = true;
pw.println(" Op mode watchers:");
for (int i = 0; i < mOpModeWatchers.size(); i++) {
pw.print(" Op ");
pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
pw.println(":");
ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
for (int j = 0; j < callbacks.size(); j++) {
pw.print(" #");
pw.print(j);
pw.print(": ");
pw.println(callbacks.get(j));
}
}
}
if (mPackageModeWatchers.size() > 0) {
needSep = true;
pw.println(" Package mode watchers:");
for (int i = 0; i < mPackageModeWatchers.size(); i++) {
pw.print(" Pkg ");
pw.print(mPackageModeWatchers.keyAt(i));
pw.println(":");
ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
for (int j = 0; j < callbacks.size(); j++) {
pw.print(" #");
pw.print(j);
pw.print(": ");
pw.println(callbacks.get(j));
}
}
}
if (mModeWatchers.size() > 0) {
needSep = true;
pw.println(" All mode watchers:");
for (int i = 0; i < mModeWatchers.size(); i++) {
pw.print(" ");
pw.print(mModeWatchers.keyAt(i));
pw.print(" -> ");
pw.println(mModeWatchers.valueAt(i));
}
}
if (mClients.size() > 0) {
needSep = true;
pw.println(" Clients:");
for (int i = 0; i < mClients.size(); i++) {
pw.print(" ");
pw.print(mClients.keyAt(i));
pw.println(":");
ClientState cs = mClients.valueAt(i);
pw.print(" ");
pw.println(cs);
if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
pw.println(" Started ops:");
for (int j = 0; j < cs.mStartedOps.size(); j++) {
Op op = cs.mStartedOps.get(j);
pw.print(" ");
pw.print("uid=");
pw.print(op.uid);
pw.print(" pkg=");
pw.print(op.packageName);
pw.print(" op=");
pw.println(AppOpsManager.opToName(op.op));
}
}
}
}
if (mAudioRestrictions.size() > 0) {
boolean printedHeader = false;
for (int o = 0; o < mAudioRestrictions.size(); o++) {
final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o));
final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o);
for (int i = 0; i < restrictions.size(); i++) {
if (!printedHeader) {
pw.println(" Audio Restrictions:");
printedHeader = true;
needSep = true;
}
final int usage = restrictions.keyAt(i);
pw.print(" ");
pw.print(op);
pw.print(" usage=");
pw.print(AudioAttributes.usageToString(usage));
Restriction r = restrictions.valueAt(i);
pw.print(": mode=");
pw.println(r.mode);
if (!r.exceptionPackages.isEmpty()) {
pw.println(" Exceptions:");
for (int j = 0; j < r.exceptionPackages.size(); j++) {
pw.print(" ");
pw.println(r.exceptionPackages.valueAt(j));
}
}
}
}
}
if (needSep) {
pw.println();
}
for (int i = 0; i < mUidStates.size(); i++) {
UidState uidState = mUidStates.valueAt(i);
pw.print(" Uid ");
UserHandle.formatUid(pw, uidState.uid);
pw.println(":");
SparseIntArray opModes = uidState.opModes;
if (opModes != null) {
final int opModeCount = opModes.size();
for (int j = 0; j < opModeCount; j++) {
final int code = opModes.keyAt(j);
final int mode = opModes.valueAt(j);
pw.print(" ");
pw.print(AppOpsManager.opToName(code));
pw.print(": mode=");
pw.println(mode);
}
}
ArrayMap<String, Ops> pkgOps = uidState.pkgOps;
if (pkgOps == null) {
continue;
}
for (Ops ops : pkgOps.values()) {
pw.print(" Package ");
pw.print(ops.packageName);
pw.println(":");
for (int j = 0; j < ops.size(); j++) {
Op op = ops.valueAt(j);
pw.print(" ");
pw.print(AppOpsManager.opToName(op.op));
pw.print(": mode=");
pw.print(op.mode);
if (op.time != 0) {
pw.print("; time=");
TimeUtils.formatDuration(now - op.time, pw);
pw.print(" ago");
}
if (op.rejectTime != 0) {
pw.print("; rejectTime=");
TimeUtils.formatDuration(now - op.rejectTime, pw);
pw.print(" ago");
}
if (op.duration == -1) {
pw.print(" (running)");
} else if (op.duration != 0) {
pw.print("; duration=");
TimeUtils.formatDuration(op.duration, pw);
}
pw.println();
}
}
}
}
}
use of android.util.SparseIntArray in project android_frameworks_base by DirtyUnicorns.
the class AppOpsService method readUidOps.
void readUidOps(XmlPullParser parser) throws NumberFormatException, XmlPullParserException, IOException {
final int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
int outerDepth = parser.getDepth();
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String tagName = parser.getName();
if (tagName.equals("op")) {
final int code = Integer.parseInt(parser.getAttributeValue(null, "n"));
final int mode = Integer.parseInt(parser.getAttributeValue(null, "m"));
UidState uidState = getUidStateLocked(uid, true);
if (uidState.opModes == null) {
uidState.opModes = new SparseIntArray();
}
uidState.opModes.put(code, mode);
} else {
Slog.w(TAG, "Unknown element under <uid-ops>: " + parser.getName());
XmlUtils.skipCurrentTag(parser);
}
}
}
use of android.util.SparseIntArray in project android_frameworks_base by DirtyUnicorns.
the class GsmAlphabet method stringToGsm7BitPacked.
/**
* Converts a String into a byte array containing
* the 7-bit packed GSM Alphabet representation of the string.
*
* Byte 0 in the returned byte array is the count of septets used
* The returned byte array is the minimum size required to store
* the packed septets. The returned array cannot contain more than 255
* septets.
*
* @param data the text to convert to septets
* @param startingSeptetOffset the number of padding septets to put before
* the character data at the beginning of the array
* @param throwException If true, throws EncodeException on invalid char.
* If false, replaces unencodable char with GSM alphabet space char.
* @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
* @param languageShiftTable the 7 bit single shift language table, or 0 for the default
* GSM extension table
* @return the encoded message
*
* @throws EncodeException if String is too large to encode
*/
public static byte[] stringToGsm7BitPacked(String data, int startingSeptetOffset, boolean throwException, int languageTable, int languageShiftTable) throws EncodeException {
int dataLen = data.length();
int septetCount = countGsmSeptetsUsingTables(data, !throwException, languageTable, languageShiftTable);
if (septetCount == -1) {
throw new EncodeException("countGsmSeptetsUsingTables(): unencodable char");
}
septetCount += startingSeptetOffset;
if (septetCount > 255) {
throw new EncodeException("Payload cannot exceed 255 septets");
}
int byteCount = ((septetCount * 7) + 7) / 8;
// Include space for one byte length prefix.
byte[] ret = new byte[byteCount + 1];
SparseIntArray charToLanguageTable = sCharsToGsmTables[languageTable];
SparseIntArray charToShiftTable = sCharsToShiftTables[languageShiftTable];
for (int i = 0, septets = startingSeptetOffset, bitOffset = startingSeptetOffset * 7; i < dataLen && septets < septetCount; i++, bitOffset += 7) {
char c = data.charAt(i);
int v = charToLanguageTable.get(c, -1);
if (v == -1) {
// Lookup the extended char.
v = charToShiftTable.get(c, -1);
if (v == -1) {
if (throwException) {
throw new EncodeException("stringToGsm7BitPacked(): unencodable char");
} else {
// should return ASCII space
v = charToLanguageTable.get(' ', ' ');
}
} else {
packSmsChar(ret, bitOffset, GSM_EXTENDED_ESCAPE);
bitOffset += 7;
septets++;
}
}
packSmsChar(ret, bitOffset, v);
septets++;
}
// Validated by check above.
ret[0] = (byte) (septetCount);
return ret;
}
use of android.util.SparseIntArray in project android_frameworks_base by DirtyUnicorns.
the class GsmAlphabet method countGsmSeptetsUsingTables.
/**
* Returns the count of 7-bit GSM alphabet characters needed
* to represent this string, using the specified 7-bit language table
* and extension table (0 for GSM default tables).
* @param s the Unicode string that will be encoded
* @param use7bitOnly allow using space in place of unencodable character if true,
* otherwise, return -1 if any characters are unencodable
* @param languageTable the 7 bit language table, or 0 for the default GSM alphabet
* @param languageShiftTable the 7 bit single shift language table, or 0 for the default
* GSM extension table
* @return the septet count for s using the specified language tables, or -1 if any
* characters are unencodable and use7bitOnly is false
*/
public static int countGsmSeptetsUsingTables(CharSequence s, boolean use7bitOnly, int languageTable, int languageShiftTable) {
int count = 0;
int sz = s.length();
SparseIntArray charToLanguageTable = sCharsToGsmTables[languageTable];
SparseIntArray charToShiftTable = sCharsToShiftTables[languageShiftTable];
for (int i = 0; i < sz; i++) {
char c = s.charAt(i);
if (c == GSM_EXTENDED_ESCAPE) {
Rlog.w(TAG, "countGsmSeptets() string contains Escape character, skipping.");
continue;
}
if (charToLanguageTable.get(c, -1) != -1) {
count++;
} else if (charToShiftTable.get(c, -1) != -1) {
// escape + shift table index
count += 2;
} else if (use7bitOnly) {
// encode as space
count++;
} else {
// caller must check for this case
return -1;
}
}
return count;
}
Aggregations