use of android.text.format.Time in project android_frameworks_base by ParanoidAndroid.
the class SyncManager method dumpRecentHistory.
private void dumpRecentHistory(PrintWriter pw) {
final ArrayList<SyncStorageEngine.SyncHistoryItem> items = mSyncStorageEngine.getSyncHistory();
if (items != null && items.size() > 0) {
final Map<String, AuthoritySyncStats> authorityMap = Maps.newHashMap();
long totalElapsedTime = 0;
long totalTimes = 0;
final int N = items.size();
int maxAuthority = 0;
int maxAccount = 0;
for (SyncStorageEngine.SyncHistoryItem item : items) {
SyncStorageEngine.AuthorityInfo authority = mSyncStorageEngine.getAuthority(item.authorityId);
final String authorityName;
final String accountKey;
if (authority != null) {
authorityName = authority.authority;
accountKey = authority.account.name + "/" + authority.account.type + " u" + authority.userId;
} else {
authorityName = "Unknown";
accountKey = "Unknown";
}
int length = authorityName.length();
if (length > maxAuthority) {
maxAuthority = length;
}
length = accountKey.length();
if (length > maxAccount) {
maxAccount = length;
}
final long elapsedTime = item.elapsedTime;
totalElapsedTime += elapsedTime;
totalTimes++;
AuthoritySyncStats authoritySyncStats = authorityMap.get(authorityName);
if (authoritySyncStats == null) {
authoritySyncStats = new AuthoritySyncStats(authorityName);
authorityMap.put(authorityName, authoritySyncStats);
}
authoritySyncStats.elapsedTime += elapsedTime;
authoritySyncStats.times++;
final Map<String, AccountSyncStats> accountMap = authoritySyncStats.accountMap;
AccountSyncStats accountSyncStats = accountMap.get(accountKey);
if (accountSyncStats == null) {
accountSyncStats = new AccountSyncStats(accountKey);
accountMap.put(accountKey, accountSyncStats);
}
accountSyncStats.elapsedTime += elapsedTime;
accountSyncStats.times++;
}
if (totalElapsedTime > 0) {
pw.println();
pw.printf("Detailed Statistics (Recent history): " + "%d (# of times) %ds (sync time)\n", totalTimes, totalElapsedTime / 1000);
final List<AuthoritySyncStats> sortedAuthorities = new ArrayList<AuthoritySyncStats>(authorityMap.values());
Collections.sort(sortedAuthorities, new Comparator<AuthoritySyncStats>() {
@Override
public int compare(AuthoritySyncStats lhs, AuthoritySyncStats rhs) {
// reverse order
int compare = Integer.compare(rhs.times, lhs.times);
if (compare == 0) {
compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime);
}
return compare;
}
});
final int maxLength = Math.max(maxAuthority, maxAccount + 3);
final int padLength = 2 + 2 + maxLength + 2 + 10 + 11;
final char[] chars = new char[padLength];
Arrays.fill(chars, '-');
final String separator = new String(chars);
final String authorityFormat = String.format(" %%-%ds: %%-9s %%-11s\n", maxLength + 2);
final String accountFormat = String.format(" %%-%ds: %%-9s %%-11s\n", maxLength);
pw.println(separator);
for (AuthoritySyncStats authoritySyncStats : sortedAuthorities) {
String name = authoritySyncStats.name;
long elapsedTime;
int times;
String timeStr;
String timesStr;
elapsedTime = authoritySyncStats.elapsedTime;
times = authoritySyncStats.times;
timeStr = String.format("%ds/%d%%", elapsedTime / 1000, elapsedTime * 100 / totalElapsedTime);
timesStr = String.format("%d/%d%%", times, times * 100 / totalTimes);
pw.printf(authorityFormat, name, timesStr, timeStr);
final List<AccountSyncStats> sortedAccounts = new ArrayList<AccountSyncStats>(authoritySyncStats.accountMap.values());
Collections.sort(sortedAccounts, new Comparator<AccountSyncStats>() {
@Override
public int compare(AccountSyncStats lhs, AccountSyncStats rhs) {
// reverse order
int compare = Integer.compare(rhs.times, lhs.times);
if (compare == 0) {
compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime);
}
return compare;
}
});
for (AccountSyncStats stats : sortedAccounts) {
elapsedTime = stats.elapsedTime;
times = stats.times;
timeStr = String.format("%ds/%d%%", elapsedTime / 1000, elapsedTime * 100 / totalElapsedTime);
timesStr = String.format("%d/%d%%", times, times * 100 / totalTimes);
pw.printf(accountFormat, stats.name, timesStr, timeStr);
}
pw.println(separator);
}
}
pw.println();
pw.println("Recent Sync History");
final String format = " %-" + maxAccount + "s %-" + maxAuthority + "s %s\n";
final Map<String, Long> lastTimeMap = Maps.newHashMap();
final PackageManager pm = mContext.getPackageManager();
for (int i = 0; i < N; i++) {
SyncStorageEngine.SyncHistoryItem item = items.get(i);
SyncStorageEngine.AuthorityInfo authority = mSyncStorageEngine.getAuthority(item.authorityId);
final String authorityName;
final String accountKey;
if (authority != null) {
authorityName = authority.authority;
accountKey = authority.account.name + "/" + authority.account.type + " u" + authority.userId;
} else {
authorityName = "Unknown";
accountKey = "Unknown";
}
final long elapsedTime = item.elapsedTime;
final Time time = new Time();
final long eventTime = item.eventTime;
time.set(eventTime);
final String key = authorityName + "/" + accountKey;
final Long lastEventTime = lastTimeMap.get(key);
final String diffString;
if (lastEventTime == null) {
diffString = "";
} else {
final long diff = (lastEventTime - eventTime) / 1000;
if (diff < 60) {
diffString = String.valueOf(diff);
} else if (diff < 3600) {
diffString = String.format("%02d:%02d", diff / 60, diff % 60);
} else {
final long sec = diff % 3600;
diffString = String.format("%02d:%02d:%02d", diff / 3600, sec / 60, sec % 60);
}
}
lastTimeMap.put(key, eventTime);
pw.printf(" #%-3d: %s %8s %5.1fs %8s", i + 1, formatTime(eventTime), SyncStorageEngine.SOURCES[item.source], ((float) elapsedTime) / 1000, diffString);
pw.printf(format, accountKey, authorityName, SyncOperation.reasonToString(pm, item.reason));
if (item.event != SyncStorageEngine.EVENT_STOP || item.upstreamActivity != 0 || item.downstreamActivity != 0) {
pw.printf(" event=%d upstreamActivity=%d downstreamActivity=%d\n", item.event, item.upstreamActivity, item.downstreamActivity);
}
if (item.mesg != null && !SyncStorageEngine.MESG_SUCCESS.equals(item.mesg)) {
pw.printf(" mesg=%s\n", item.mesg);
}
}
pw.println();
pw.println("Recent Sync History Extras");
for (int i = 0; i < N; i++) {
final SyncStorageEngine.SyncHistoryItem item = items.get(i);
final Bundle extras = item.extras;
if (extras == null || extras.size() == 0) {
continue;
}
final SyncStorageEngine.AuthorityInfo authority = mSyncStorageEngine.getAuthority(item.authorityId);
final String authorityName;
final String accountKey;
if (authority != null) {
authorityName = authority.authority;
accountKey = authority.account.name + "/" + authority.account.type + " u" + authority.userId;
} else {
authorityName = "Unknown";
accountKey = "Unknown";
}
final Time time = new Time();
final long eventTime = item.eventTime;
time.set(eventTime);
pw.printf(" #%-3d: %s %8s ", i + 1, formatTime(eventTime), SyncStorageEngine.SOURCES[item.source]);
pw.printf(format, accountKey, authorityName, extras);
}
}
}
use of android.text.format.Time in project android_frameworks_base by ParanoidAndroid.
the class NetworkPolicyManagerServiceTest method formatTime.
private static String formatTime(long millis) {
final Time time = new Time(Time.TIMEZONE_UTC);
time.set(millis);
return time.format3339(false);
}
use of android.text.format.Time in project android_frameworks_base by ParanoidAndroid.
the class NetworkPolicyManagerServiceTest method parseTime.
private static long parseTime(String time) {
final Time result = new Time();
result.parse3339(time);
return result.toMillis(true);
}
use of android.text.format.Time in project android_frameworks_base by ParanoidAndroid.
the class NetworkPolicyManager method computeNextCycleBoundary.
/** {@hide} */
public static long computeNextCycleBoundary(long currentTime, NetworkPolicy policy) {
if (policy.cycleDay == CYCLE_NONE) {
throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
}
final Time now = new Time(policy.cycleTimezone);
now.set(currentTime);
// first, find cycle boundary for current month
final Time cycle = new Time(now);
cycle.hour = cycle.minute = cycle.second = 0;
snapToCycleDay(cycle, policy.cycleDay);
if (Time.compare(cycle, now) <= 0) {
// cycle boundary is before now, use next cycle boundary; start by
// pushing ourselves squarely into next month.
final Time nextMonth = new Time(now);
nextMonth.hour = nextMonth.minute = nextMonth.second = 0;
nextMonth.monthDay = 1;
nextMonth.month += 1;
nextMonth.normalize(true);
cycle.set(nextMonth);
snapToCycleDay(cycle, policy.cycleDay);
}
return cycle.toMillis(true);
}
use of android.text.format.Time in project android_frameworks_base by ParanoidAndroid.
the class TimeTest method testParse33390.
@SmallTest
public void testParse33390() throws Exception {
Time t = new Time(Time.TIMEZONE_UTC);
t.parse3339("1980-05-23");
if (!t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23) {
fail("Did not parse all-day date correctly");
}
t.parse3339("1980-05-23T09:50:50");
if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
fail("Did not parse timezone-offset-less date correctly");
}
t.parse3339("1980-05-23T09:50:50Z");
if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
fail("Did not parse UTC date correctly");
}
t.parse3339("1980-05-23T09:50:50.0Z");
if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
fail("Did not parse UTC date correctly");
}
t.parse3339("1980-05-23T09:50:50.12Z");
if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
fail("Did not parse UTC date correctly");
}
t.parse3339("1980-05-23T09:50:50.123Z");
if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 9 || t.minute != 50 || t.second != 50 || t.gmtoff != 0) {
fail("Did not parse UTC date correctly");
}
// The time should be normalized to UTC
t.parse3339("1980-05-23T09:50:50-01:05");
if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 10 || t.minute != 55 || t.second != 50 || t.gmtoff != 0) {
fail("Did not parse timezone-offset date correctly");
}
// The time should be normalized to UTC
t.parse3339("1980-05-23T09:50:50.123-01:05");
if (t.allDay || t.year != 1980 || t.month != 04 || t.monthDay != 23 || t.hour != 10 || t.minute != 55 || t.second != 50 || t.gmtoff != 0) {
fail("Did not parse timezone-offset date correctly");
}
try {
t.parse3339("1980");
fail("Did not throw error on truncated input length");
} catch (TimeFormatException e) {
// Successful
}
try {
t.parse3339("1980-05-23T09:50:50.123+");
fail("Did not throw error on truncated timezone offset");
} catch (TimeFormatException e1) {
// Successful
}
try {
t.parse3339("1980-05-23T09:50:50.123+05:0");
fail("Did not throw error on truncated timezone offset");
} catch (TimeFormatException e1) {
// Successful
}
}
Aggregations