use of android.os.ParcelFormatException in project android_frameworks_base by crdroidandroid.
the class BatteryStatsService 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 BatteryStats from from pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() + " without permission " + android.Manifest.permission.DUMP);
return;
}
int flags = 0;
boolean useCheckinFormat = false;
boolean isRealCheckin = false;
boolean noOutput = false;
boolean writeData = false;
long historyStart = -1;
int reqUid = -1;
if (args != null) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--checkin".equals(arg)) {
useCheckinFormat = true;
isRealCheckin = true;
} else if ("--history".equals(arg)) {
flags |= BatteryStats.DUMP_HISTORY_ONLY;
} else if ("--history-start".equals(arg)) {
flags |= BatteryStats.DUMP_HISTORY_ONLY;
i++;
if (i >= args.length) {
pw.println("Missing time argument for --history-since");
dumpHelp(pw);
return;
}
historyStart = Long.parseLong(args[i]);
writeData = true;
} else if ("-c".equals(arg)) {
useCheckinFormat = true;
flags |= BatteryStats.DUMP_INCLUDE_HISTORY;
} else if ("--charged".equals(arg)) {
flags |= BatteryStats.DUMP_CHARGED_ONLY;
} else if ("--daily".equals(arg)) {
flags |= BatteryStats.DUMP_DAILY_ONLY;
} else if ("--reset".equals(arg)) {
synchronized (mStats) {
mStats.resetAllStatsCmdLocked();
pw.println("Battery stats reset.");
noOutput = true;
}
updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
} else if ("--write".equals(arg)) {
updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
synchronized (mStats) {
mStats.writeSyncLocked();
pw.println("Battery stats written.");
noOutput = true;
}
} else if ("--new-daily".equals(arg)) {
synchronized (mStats) {
mStats.recordDailyStatsLocked();
pw.println("New daily stats written.");
noOutput = true;
}
} else if ("--read-daily".equals(arg)) {
synchronized (mStats) {
mStats.readDailyStatsLocked();
pw.println("Last daily stats read.");
noOutput = true;
}
} else if ("--enable".equals(arg) || "enable".equals(arg)) {
i = doEnableOrDisable(pw, i, args, true);
if (i < 0) {
return;
}
pw.println("Enabled: " + args[i]);
return;
} else if ("--disable".equals(arg) || "disable".equals(arg)) {
i = doEnableOrDisable(pw, i, args, false);
if (i < 0) {
return;
}
pw.println("Disabled: " + args[i]);
return;
} else if ("-h".equals(arg)) {
dumpHelp(pw);
return;
} else if ("-a".equals(arg)) {
flags |= BatteryStats.DUMP_VERBOSE;
} else if (arg.length() > 0 && arg.charAt(0) == '-') {
pw.println("Unknown option: " + arg);
dumpHelp(pw);
return;
} else {
// Not an option, last argument must be a package name.
try {
reqUid = mContext.getPackageManager().getPackageUidAsUser(arg, UserHandle.getCallingUserId());
} catch (PackageManager.NameNotFoundException e) {
pw.println("Unknown package: " + arg);
dumpHelp(pw);
return;
}
}
}
}
if (noOutput) {
return;
}
long ident = Binder.clearCallingIdentity();
try {
if (BatteryStatsHelper.checkWifiOnly(mContext)) {
flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY;
}
// Fetch data from external sources and update the BatteryStatsImpl object with them.
updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
} finally {
Binder.restoreCallingIdentity(ident);
}
if (reqUid >= 0) {
// we only dump the aggregated data since charged.
if ((flags & (BatteryStats.DUMP_HISTORY_ONLY | BatteryStats.DUMP_CHARGED_ONLY)) == 0) {
flags |= BatteryStats.DUMP_CHARGED_ONLY;
// Also if they are doing -c, we don't want history.
flags &= ~BatteryStats.DUMP_INCLUDE_HISTORY;
}
}
if (useCheckinFormat) {
List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_ALL);
if (isRealCheckin) {
// file if there is one.
synchronized (mStats.mCheckinFile) {
if (mStats.mCheckinFile.exists()) {
try {
byte[] raw = mStats.mCheckinFile.readFully();
if (raw != null) {
Parcel in = Parcel.obtain();
in.unmarshall(raw, 0, raw.length);
in.setDataPosition(0);
BatteryStatsImpl checkinStats = new BatteryStatsImpl(null, mStats.mHandler, null);
checkinStats.readSummaryFromParcel(in);
in.recycle();
checkinStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
mStats.mCheckinFile.delete();
return;
}
} catch (IOException | ParcelFormatException e) {
Slog.w(TAG, "Failure reading checkin file " + mStats.mCheckinFile.getBaseFile(), e);
}
}
}
}
synchronized (mStats) {
mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
if (writeData) {
mStats.writeAsyncLocked();
}
}
} else {
synchronized (mStats) {
mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart);
if (writeData) {
mStats.writeAsyncLocked();
}
}
}
}
use of android.os.ParcelFormatException in project android_frameworks_base by DirtyUnicorns.
the class BatteryStatsService 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 BatteryStats from from pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() + " without permission " + android.Manifest.permission.DUMP);
return;
}
int flags = 0;
boolean useCheckinFormat = false;
boolean isRealCheckin = false;
boolean noOutput = false;
boolean writeData = false;
long historyStart = -1;
int reqUid = -1;
if (args != null) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--checkin".equals(arg)) {
useCheckinFormat = true;
isRealCheckin = true;
} else if ("--history".equals(arg)) {
flags |= BatteryStats.DUMP_HISTORY_ONLY;
} else if ("--history-start".equals(arg)) {
flags |= BatteryStats.DUMP_HISTORY_ONLY;
i++;
if (i >= args.length) {
pw.println("Missing time argument for --history-since");
dumpHelp(pw);
return;
}
historyStart = Long.parseLong(args[i]);
writeData = true;
} else if ("-c".equals(arg)) {
useCheckinFormat = true;
flags |= BatteryStats.DUMP_INCLUDE_HISTORY;
} else if ("--charged".equals(arg)) {
flags |= BatteryStats.DUMP_CHARGED_ONLY;
} else if ("--daily".equals(arg)) {
flags |= BatteryStats.DUMP_DAILY_ONLY;
} else if ("--reset".equals(arg)) {
synchronized (mStats) {
mStats.resetAllStatsCmdLocked();
pw.println("Battery stats reset.");
noOutput = true;
}
updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
} else if ("--write".equals(arg)) {
updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
synchronized (mStats) {
mStats.writeSyncLocked();
pw.println("Battery stats written.");
noOutput = true;
}
} else if ("--new-daily".equals(arg)) {
synchronized (mStats) {
mStats.recordDailyStatsLocked();
pw.println("New daily stats written.");
noOutput = true;
}
} else if ("--read-daily".equals(arg)) {
synchronized (mStats) {
mStats.readDailyStatsLocked();
pw.println("Last daily stats read.");
noOutput = true;
}
} else if ("--enable".equals(arg) || "enable".equals(arg)) {
i = doEnableOrDisable(pw, i, args, true);
if (i < 0) {
return;
}
pw.println("Enabled: " + args[i]);
return;
} else if ("--disable".equals(arg) || "disable".equals(arg)) {
i = doEnableOrDisable(pw, i, args, false);
if (i < 0) {
return;
}
pw.println("Disabled: " + args[i]);
return;
} else if ("-h".equals(arg)) {
dumpHelp(pw);
return;
} else if ("-a".equals(arg)) {
flags |= BatteryStats.DUMP_VERBOSE;
} else if (arg.length() > 0 && arg.charAt(0) == '-') {
pw.println("Unknown option: " + arg);
dumpHelp(pw);
return;
} else {
// Not an option, last argument must be a package name.
try {
reqUid = mContext.getPackageManager().getPackageUidAsUser(arg, UserHandle.getCallingUserId());
} catch (PackageManager.NameNotFoundException e) {
pw.println("Unknown package: " + arg);
dumpHelp(pw);
return;
}
}
}
}
if (noOutput) {
return;
}
long ident = Binder.clearCallingIdentity();
try {
if (BatteryStatsHelper.checkWifiOnly(mContext)) {
flags |= BatteryStats.DUMP_DEVICE_WIFI_ONLY;
}
// Fetch data from external sources and update the BatteryStatsImpl object with them.
updateExternalStatsSync("dump", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
} finally {
Binder.restoreCallingIdentity(ident);
}
if (reqUid >= 0) {
// we only dump the aggregated data since charged.
if ((flags & (BatteryStats.DUMP_HISTORY_ONLY | BatteryStats.DUMP_CHARGED_ONLY)) == 0) {
flags |= BatteryStats.DUMP_CHARGED_ONLY;
// Also if they are doing -c, we don't want history.
flags &= ~BatteryStats.DUMP_INCLUDE_HISTORY;
}
}
if (useCheckinFormat) {
List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_ALL);
if (isRealCheckin) {
// file if there is one.
synchronized (mStats.mCheckinFile) {
if (mStats.mCheckinFile.exists()) {
try {
byte[] raw = mStats.mCheckinFile.readFully();
if (raw != null) {
Parcel in = Parcel.obtain();
in.unmarshall(raw, 0, raw.length);
in.setDataPosition(0);
BatteryStatsImpl checkinStats = new BatteryStatsImpl(null, mStats.mHandler, null);
checkinStats.readSummaryFromParcel(in);
in.recycle();
checkinStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
mStats.mCheckinFile.delete();
return;
}
} catch (IOException | ParcelFormatException e) {
Slog.w(TAG, "Failure reading checkin file " + mStats.mCheckinFile.getBaseFile(), e);
}
}
}
}
synchronized (mStats) {
mStats.dumpCheckinLocked(mContext, pw, apps, flags, historyStart);
if (writeData) {
mStats.writeAsyncLocked();
}
}
} else {
synchronized (mStats) {
mStats.dumpLocked(mContext, pw, flags, reqUid, historyStart);
if (writeData) {
mStats.writeAsyncLocked();
}
}
}
}
use of android.os.ParcelFormatException in project android_frameworks_base by AOSPA.
the class BatteryStatsImpl method readHistory.
void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
final long historyBaseTime = in.readLong();
mHistoryBuffer.setDataSize(0);
mHistoryBuffer.setDataPosition(0);
mHistoryTagPool.clear();
mNextHistoryTagIdx = 0;
mNumHistoryTagChars = 0;
int numTags = in.readInt();
for (int i = 0; i < numTags; i++) {
int idx = in.readInt();
String str = in.readString();
if (str == null) {
throw new ParcelFormatException("null history tag string");
}
int uid = in.readInt();
HistoryTag tag = new HistoryTag();
tag.string = str;
tag.uid = uid;
tag.poolIdx = idx;
mHistoryTagPool.put(tag, idx);
if (idx >= mNextHistoryTagIdx) {
mNextHistoryTagIdx = idx + 1;
}
mNumHistoryTagChars += tag.string.length() + 1;
}
int bufSize = in.readInt();
int curPos = in.dataPosition();
if (bufSize >= (MAX_MAX_HISTORY_BUFFER * 3)) {
throw new ParcelFormatException("File corrupt: history data buffer too large " + bufSize);
} else if ((bufSize & ~3) != bufSize) {
throw new ParcelFormatException("File corrupt: history data buffer not aligned " + bufSize);
} else {
if (DEBUG_HISTORY)
Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize + " bytes at " + curPos);
mHistoryBuffer.appendFrom(in, curPos, bufSize);
in.setDataPosition(curPos + bufSize);
}
if (andOldHistory) {
readOldHistory(in);
}
if (DEBUG_HISTORY) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** OLD mHistoryBaseTime: ");
TimeUtils.formatDuration(mHistoryBaseTime, sb);
Slog.i(TAG, sb.toString());
}
mHistoryBaseTime = historyBaseTime;
if (DEBUG_HISTORY) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** NEW mHistoryBaseTime: ");
TimeUtils.formatDuration(mHistoryBaseTime, sb);
Slog.i(TAG, sb.toString());
}
// the last run until samples in this run.
if (mHistoryBaseTime > 0) {
long oldnow = mClocks.elapsedRealtime();
mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
if (DEBUG_HISTORY) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** ADJUSTED mHistoryBaseTime: ");
TimeUtils.formatDuration(mHistoryBaseTime, sb);
Slog.i(TAG, sb.toString());
}
}
}
use of android.os.ParcelFormatException in project android_frameworks_base by DirtyUnicorns.
the class BatteryStatsImpl method readHistory.
void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
final long historyBaseTime = in.readLong();
mHistoryBuffer.setDataSize(0);
mHistoryBuffer.setDataPosition(0);
mHistoryTagPool.clear();
mNextHistoryTagIdx = 0;
mNumHistoryTagChars = 0;
int numTags = in.readInt();
for (int i = 0; i < numTags; i++) {
int idx = in.readInt();
String str = in.readString();
if (str == null) {
throw new ParcelFormatException("null history tag string");
}
int uid = in.readInt();
HistoryTag tag = new HistoryTag();
tag.string = str;
tag.uid = uid;
tag.poolIdx = idx;
mHistoryTagPool.put(tag, idx);
if (idx >= mNextHistoryTagIdx) {
mNextHistoryTagIdx = idx + 1;
}
mNumHistoryTagChars += tag.string.length() + 1;
}
int bufSize = in.readInt();
int curPos = in.dataPosition();
if (bufSize >= (MAX_MAX_HISTORY_BUFFER * 3)) {
throw new ParcelFormatException("File corrupt: history data buffer too large " + bufSize);
} else if ((bufSize & ~3) != bufSize) {
throw new ParcelFormatException("File corrupt: history data buffer not aligned " + bufSize);
} else {
if (DEBUG_HISTORY)
Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize + " bytes at " + curPos);
mHistoryBuffer.appendFrom(in, curPos, bufSize);
in.setDataPosition(curPos + bufSize);
}
if (andOldHistory) {
readOldHistory(in);
}
if (DEBUG_HISTORY) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** OLD mHistoryBaseTime: ");
TimeUtils.formatDuration(mHistoryBaseTime, sb);
Slog.i(TAG, sb.toString());
}
mHistoryBaseTime = historyBaseTime;
if (DEBUG_HISTORY) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** NEW mHistoryBaseTime: ");
TimeUtils.formatDuration(mHistoryBaseTime, sb);
Slog.i(TAG, sb.toString());
}
// the last run until samples in this run.
if (mHistoryBaseTime > 0) {
long oldnow = mClocks.elapsedRealtime();
mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
if (DEBUG_HISTORY) {
StringBuilder sb = new StringBuilder(128);
sb.append("****************** ADJUSTED mHistoryBaseTime: ");
TimeUtils.formatDuration(mHistoryBaseTime, sb);
Slog.i(TAG, sb.toString());
}
}
}
Aggregations