Search in sources :

Example 21 with Comparator

use of java.util.Comparator in project android_frameworks_base by ParanoidAndroid.

the class AlarmManagerService 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 AlarmManager from from pid=" + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
        return;
    }
    synchronized (mLock) {
        pw.println("Current Alarm Manager state:");
        if (mRtcWakeupAlarms.size() > 0 || mRtcAlarms.size() > 0) {
            final long now = System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            pw.println(" ");
            pw.print("  Realtime wakeup (now=");
            pw.print(sdf.format(new Date(now)));
            pw.println("):");
            if (mRtcWakeupAlarms.size() > 0) {
                dumpAlarmList(pw, mRtcWakeupAlarms, "  ", "RTC_WAKEUP", now);
            }
            if (mRtcAlarms.size() > 0) {
                dumpAlarmList(pw, mRtcAlarms, "  ", "RTC", now);
            }
        }
        if (mElapsedRealtimeWakeupAlarms.size() > 0 || mElapsedRealtimeAlarms.size() > 0) {
            final long now = SystemClock.elapsedRealtime();
            pw.println(" ");
            pw.print("  Elapsed realtime wakeup (now=");
            TimeUtils.formatDuration(now, pw);
            pw.println("):");
            if (mElapsedRealtimeWakeupAlarms.size() > 0) {
                dumpAlarmList(pw, mElapsedRealtimeWakeupAlarms, "  ", "ELAPSED_WAKEUP", now);
            }
            if (mElapsedRealtimeAlarms.size() > 0) {
                dumpAlarmList(pw, mElapsedRealtimeAlarms, "  ", "ELAPSED", now);
            }
        }
        pw.println();
        pw.print("  Broadcast ref count: ");
        pw.println(mBroadcastRefCount);
        pw.println();
        if (mLog.dump(pw, "  Recent problems", "    ")) {
            pw.println();
        }
        final FilterStats[] topFilters = new FilterStats[10];
        final Comparator<FilterStats> comparator = new Comparator<FilterStats>() {

            @Override
            public int compare(FilterStats lhs, FilterStats rhs) {
                if (lhs.aggregateTime < rhs.aggregateTime) {
                    return 1;
                } else if (lhs.aggregateTime > rhs.aggregateTime) {
                    return -1;
                }
                return 0;
            }
        };
        int len = 0;
        for (Map.Entry<String, BroadcastStats> be : mBroadcastStats.entrySet()) {
            BroadcastStats bs = be.getValue();
            for (Map.Entry<Pair<String, ComponentName>, FilterStats> fe : bs.filterStats.entrySet()) {
                FilterStats fs = fe.getValue();
                int pos = len > 0 ? Arrays.binarySearch(topFilters, 0, len, fs, comparator) : 0;
                if (pos < 0) {
                    pos = -pos - 1;
                }
                if (pos < topFilters.length) {
                    int copylen = topFilters.length - pos - 1;
                    if (copylen > 0) {
                        System.arraycopy(topFilters, pos, topFilters, pos + 1, copylen);
                    }
                    topFilters[pos] = fs;
                    if (len < topFilters.length) {
                        len++;
                    }
                }
            }
        }
        if (len > 0) {
            pw.println("  Top Alarms:");
            for (int i = 0; i < len; i++) {
                FilterStats fs = topFilters[i];
                pw.print("    ");
                if (fs.nesting > 0)
                    pw.print("*ACTIVE* ");
                TimeUtils.formatDuration(fs.aggregateTime, pw);
                pw.print(" running, ");
                pw.print(fs.numWakeup);
                pw.print(" wakeups, ");
                pw.print(fs.count);
                pw.print(" alarms: ");
                pw.print(fs.mBroadcastStats.mPackageName);
                pw.println();
                pw.print("      ");
                if (fs.mTarget.first != null) {
                    pw.print(" act=");
                    pw.print(fs.mTarget.first);
                }
                if (fs.mTarget.second != null) {
                    pw.print(" cmp=");
                    pw.print(fs.mTarget.second.toShortString());
                }
                pw.println();
            }
        }
        pw.println(" ");
        pw.println("  Alarm Stats:");
        final ArrayList<FilterStats> tmpFilters = new ArrayList<FilterStats>();
        for (Map.Entry<String, BroadcastStats> be : mBroadcastStats.entrySet()) {
            BroadcastStats bs = be.getValue();
            pw.print("  ");
            if (bs.nesting > 0)
                pw.print("*ACTIVE* ");
            pw.print(be.getKey());
            pw.print(" ");
            TimeUtils.formatDuration(bs.aggregateTime, pw);
            pw.print(" running, ");
            pw.print(bs.numWakeup);
            pw.println(" wakeups:");
            tmpFilters.clear();
            for (Map.Entry<Pair<String, ComponentName>, FilterStats> fe : bs.filterStats.entrySet()) {
                tmpFilters.add(fe.getValue());
            }
            Collections.sort(tmpFilters, comparator);
            for (int i = 0; i < tmpFilters.size(); i++) {
                FilterStats fs = tmpFilters.get(i);
                pw.print("    ");
                if (fs.nesting > 0)
                    pw.print("*ACTIVE* ");
                TimeUtils.formatDuration(fs.aggregateTime, pw);
                pw.print(" ");
                pw.print(fs.numWakeup);
                pw.print(" wakes ");
                pw.print(fs.count);
                pw.print(" alarms:");
                if (fs.mTarget.first != null) {
                    pw.print(" act=");
                    pw.print(fs.mTarget.first);
                }
                if (fs.mTarget.second != null) {
                    pw.print(" cmp=");
                    pw.print(fs.mTarget.second.toShortString());
                }
                pw.println();
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) Comparator(java.util.Comparator) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) Pair(android.util.Pair)

Example 22 with Comparator

use of java.util.Comparator in project android_frameworks_base by ParanoidAndroid.

the class ActivityManagerService method dumpProcessOomList.

private static final boolean dumpProcessOomList(PrintWriter pw, ActivityManagerService service, List<ProcessRecord> origList, String prefix, String normalLabel, String persistentLabel, boolean inclDetails, String dumpPackage) {
    ArrayList<Pair<ProcessRecord, Integer>> list = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size());
    for (int i = 0; i < origList.size(); i++) {
        ProcessRecord r = origList.get(i);
        if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
            continue;
        }
        list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i));
    }
    if (list.size() <= 0) {
        return false;
    }
    Comparator<Pair<ProcessRecord, Integer>> comparator = new Comparator<Pair<ProcessRecord, Integer>>() {

        @Override
        public int compare(Pair<ProcessRecord, Integer> object1, Pair<ProcessRecord, Integer> object2) {
            if (object1.first.setAdj != object2.first.setAdj) {
                return object1.first.setAdj > object2.first.setAdj ? -1 : 1;
            }
            if (object1.second.intValue() != object2.second.intValue()) {
                return object1.second.intValue() > object2.second.intValue() ? -1 : 1;
            }
            return 0;
        }
    };
    Collections.sort(list, comparator);
    final long curRealtime = SystemClock.elapsedRealtime();
    final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
    final long curUptime = SystemClock.uptimeMillis();
    final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
    for (int i = list.size() - 1; i >= 0; i--) {
        ProcessRecord r = list.get(i).first;
        String oomAdj;
        if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
            oomAdj = buildOomTag("bak", "  ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ);
        } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) {
            oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ);
        } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) {
            oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ);
        } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) {
            oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ);
        } else if (r.setAdj >= ProcessList.SERVICE_ADJ) {
            oomAdj = buildOomTag("svc  ", null, r.setAdj, ProcessList.SERVICE_ADJ);
        } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) {
            oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ);
        } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
            oomAdj = buildOomTag("hvy  ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ);
        } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
            oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ);
        } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) {
            oomAdj = buildOomTag("vis  ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ);
        } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) {
            oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ);
        } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) {
            oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ);
        } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) {
            oomAdj = buildOomTag("sys  ", null, r.setAdj, ProcessList.SYSTEM_ADJ);
        } else {
            oomAdj = Integer.toString(r.setAdj);
        }
        String schedGroup;
        switch(r.setSchedGroup) {
            case Process.THREAD_GROUP_BG_NONINTERACTIVE:
                schedGroup = "B";
                break;
            case Process.THREAD_GROUP_DEFAULT:
                schedGroup = "F";
                break;
            default:
                schedGroup = Integer.toString(r.setSchedGroup);
                break;
        }
        String foreground;
        if (r.foregroundActivities) {
            foreground = "A";
        } else if (r.foregroundServices) {
            foreground = "S";
        } else {
            foreground = " ";
        }
        pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)", prefix, (r.persistent ? persistentLabel : normalLabel), (origList.size() - 1) - list.get(i).second, oomAdj, schedGroup, foreground, r.trimMemoryLevel, r.toShortString(), r.adjType));
        if (r.adjSource != null || r.adjTarget != null) {
            pw.print(prefix);
            pw.print("    ");
            if (r.adjTarget instanceof ComponentName) {
                pw.print(((ComponentName) r.adjTarget).flattenToShortString());
            } else if (r.adjTarget != null) {
                pw.print(r.adjTarget.toString());
            } else {
                pw.print("{null}");
            }
            pw.print("<=");
            if (r.adjSource instanceof ProcessRecord) {
                pw.print("Proc{");
                pw.print(((ProcessRecord) r.adjSource).toShortString());
                pw.println("}");
            } else if (r.adjSource != null) {
                pw.println(r.adjSource.toString());
            } else {
                pw.println("{null}");
            }
        }
        if (inclDetails) {
            pw.print(prefix);
            pw.print("    ");
            pw.print("oom: max=");
            pw.print(r.maxAdj);
            pw.print(" hidden=");
            pw.print(r.hiddenAdj);
            pw.print(" client=");
            pw.print(r.clientHiddenAdj);
            pw.print(" empty=");
            pw.print(r.emptyAdj);
            pw.print(" curRaw=");
            pw.print(r.curRawAdj);
            pw.print(" setRaw=");
            pw.print(r.setRawAdj);
            pw.print(" cur=");
            pw.print(r.curAdj);
            pw.print(" set=");
            pw.println(r.setAdj);
            pw.print(prefix);
            pw.print("    ");
            pw.print("keeping=");
            pw.print(r.keeping);
            pw.print(" hidden=");
            pw.print(r.hidden);
            pw.print(" empty=");
            pw.print(r.empty);
            pw.print(" hasAboveClient=");
            pw.println(r.hasAboveClient);
            if (!r.keeping) {
                if (r.lastWakeTime != 0) {
                    long wtime;
                    BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics();
                    synchronized (stats) {
                        wtime = stats.getProcessWakeTime(r.info.uid, r.pid, curRealtime);
                    }
                    long timeUsed = wtime - r.lastWakeTime;
                    pw.print(prefix);
                    pw.print("    ");
                    pw.print("keep awake over ");
                    TimeUtils.formatDuration(realtimeSince, pw);
                    pw.print(" used ");
                    TimeUtils.formatDuration(timeUsed, pw);
                    pw.print(" (");
                    pw.print((timeUsed * 100) / realtimeSince);
                    pw.println("%)");
                }
                if (r.lastCpuTime != 0) {
                    long timeUsed = r.curCpuTime - r.lastCpuTime;
                    pw.print(prefix);
                    pw.print("    ");
                    pw.print("run cpu over ");
                    TimeUtils.formatDuration(uptimeSince, pw);
                    pw.print(" used ");
                    TimeUtils.formatDuration(timeUsed, pw);
                    pw.print(" (");
                    pw.print((timeUsed * 100) / uptimeSince);
                    pw.println("%)");
                }
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) BatteryStatsImpl(com.android.internal.os.BatteryStatsImpl) Comparator(java.util.Comparator) ComponentName(android.content.ComponentName) Pair(android.util.Pair)

Example 23 with Comparator

use of java.util.Comparator in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundUpdateCardBrowserList.

private TaskData doInBackgroundUpdateCardBrowserList(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundSortCards");
    if (params.length == 1) {
        Comparator comparator = params[0].getComparator();
        ArrayList<HashMap<String, String>> card = params[0].getCards();
        Collections.sort(card, comparator);
    } else {
        ArrayList<HashMap<String, String>> allCard = params[0].getCards();
        ArrayList<HashMap<String, String>> cards = params[1].getCards();
        cards.clear();
        HashSet<String> tags = new HashSet<String>();
        for (String s : (HashSet<String>) params[2].getObjArray()[0]) {
            tags.add(s.toLowerCase());
        }
        for (int i = 0; i < allCard.size(); i++) {
            HashMap<String, String> card = allCard.get(i);
            if (Arrays.asList(card.get("tags").toLowerCase().trim().split("\\s")).containsAll(tags)) {
                cards.add(allCard.get(i));
            }
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) Comparator(java.util.Comparator) HashSet(java.util.HashSet)

Example 24 with Comparator

use of java.util.Comparator in project Reader by TheKeeperOfPie.

the class ControllerSearch method setSort.

public Observable<Listing> setSort(Sort sort) {
    Listing listingSort = getCurrentPage() == PAGE_SUBREDDITS_RECOMMENDED ? subredditsRecommended : subreddits;
    if (Sort.ALPHABETICAL.equals(sort)) {
        if (Sort.ALPHABETICAL.equals(sortSubreddits)) {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    return ((Subreddit) rhs).getDisplayName().compareToIgnoreCase(((Subreddit) lhs).getDisplayName());
                }
            });
            sortSubreddits = null;
        } else {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    return ((Subreddit) lhs).getDisplayName().compareToIgnoreCase(((Subreddit) rhs).getDisplayName());
                }
            });
            sortSubreddits = Sort.ALPHABETICAL;
        }
        if (listingSort == subredditsSubscribed) {
            saveSubscriptions();
        } else if (listingSort == subredditsRecommended) {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
            }
        } else {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubreddits().notifyDataSetChanged();
            }
        }
    } else if (Sort.SUBSCRIBERS.equals(sort)) {
        if (Sort.SUBSCRIBERS.equals(sortSubreddits)) {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    long subscribersFirst = ((Subreddit) lhs).getSubscribers();
                    long subscribersSecond = ((Subreddit) rhs).getSubscribers();
                    return subscribersFirst < subscribersSecond ? -1 : (subscribersFirst == subscribersSecond ? 0 : 1);
                }
            });
            sortSubreddits = null;
        } else {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    long subscribersFirst = ((Subreddit) lhs).getSubscribers();
                    long subscribersSecond = ((Subreddit) rhs).getSubscribers();
                    return subscribersSecond < subscribersFirst ? -1 : (subscribersFirst == subscribersSecond ? 0 : 1);
                }
            });
            sortSubreddits = Sort.SUBSCRIBERS;
        }
        if (listingSort == subredditsSubscribed) {
            saveSubscriptions();
        } else if (listingSort == subredditsRecommended) {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
            }
        } else {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubreddits().notifyDataSetChanged();
            }
        }
    } else if (this.sort != sort) {
        this.sort = sort;
        eventHolder.getSort().call(sort);
        return reloadCurrentPage();
    }
    return Observable.empty();
}
Also used : Listing(com.winsonchiu.reader.data.reddit.Listing) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) Thing(com.winsonchiu.reader.data.reddit.Thing) Comparator(java.util.Comparator)

Example 25 with Comparator

use of java.util.Comparator in project CoCoin by Nightonke.

the class CoCoinUtil method SortTreeMapByValues.

public static <K, V extends Comparable<V>> Map<K, V> SortTreeMapByValues(final Map<K, V> map) {
    Comparator<K> valueComparator = new Comparator<K>() {

        public int compare(K k1, K k2) {
            int compare = map.get(k1).compareTo(map.get(k2));
            if (compare == 0)
                return 1;
            else
                return compare;
        }
    };
    TreeMap<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    sortedByValues.putAll(map);
    return sortedByValues;
}
Also used : TreeMap(java.util.TreeMap) Point(android.graphics.Point) Comparator(java.util.Comparator)

Aggregations

Comparator (java.util.Comparator)322 ArrayList (java.util.ArrayList)123 List (java.util.List)58 Test (org.junit.Test)58 HashMap (java.util.HashMap)50 IOException (java.io.IOException)36 Map (java.util.Map)35 File (java.io.File)24 HashSet (java.util.HashSet)23 TreeSet (java.util.TreeSet)20 Set (java.util.Set)18 Iterator (java.util.Iterator)15 Method (java.lang.reflect.Method)14 Collections (java.util.Collections)14 Date (java.util.Date)14 TreeMap (java.util.TreeMap)14 ArrayMap (android.util.ArrayMap)12 Collection (java.util.Collection)11 LinkedList (java.util.LinkedList)11 SimpleDateFormat (java.text.SimpleDateFormat)10