Search in sources :

Example 66 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 67 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 68 with Comparator

use of java.util.Comparator in project KeepScore by nolanlawson.

the class MainActivity method showLoadBackupDialog.

private void showLoadBackupDialog() {
    if (!SdcardHelper.isAvailable()) {
        ToastHelper.showLong(this, R.string.toast_no_sdcard);
        return;
    }
    final List<String> backups = SdcardHelper.list(Location.Backups);
    if (backups.isEmpty()) {
        ToastHelper.showShort(this, R.string.toast_no_backups);
        return;
    }
    final ProgressDialog progressDialog = showProgressDialog(R.string.text_loading_generic, backups.size());
    // show progress dialog to avoid jankiness
    new AsyncTask<Void, Void, List<GamesBackupSummary>>() {

        @Override
        protected List<GamesBackupSummary> doInBackground(Void... params) {
            List<GamesBackupSummary> summaries = new ArrayList<GamesBackupSummary>();
            // fetch the summaries only, so that we don't have to read the entire XML file for each one
            for (String backup : backups) {
                File file = SdcardHelper.getFile(backup, Location.Backups);
                Uri uri = Uri.fromFile(file);
                Format format = file.getName().endsWith(".gz") ? Format.GZIP : Format.XML;
                GamesBackupSummary summary = GamesBackupSerializer.readGamesBackupSummary(uri, format, getContentResolver());
                summaries.add(summary);
                publishProgress((Void) null);
            }
            // show most recent ones first
            Collections.sort(summaries, new Comparator<GamesBackupSummary>() {

                public int compare(GamesBackupSummary lhs, GamesBackupSummary rhs) {
                    return Long.valueOf(rhs.getDateSaved()).compareTo(lhs.getDateSaved());
                }
            });
            return summaries;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            progressDialog.incrementProgressBy(1);
        }

        @Override
        protected void onPostExecute(List<GamesBackupSummary> result) {
            super.onPostExecute(result);
            progressDialog.dismiss();
            showLoadBackupDialogFinished(result);
        }
    }.execute((Void) null);
}
Also used : GamesBackupSummary(com.nolanlawson.keepscore.serialization.GamesBackupSummary) ProgressDialog(android.app.ProgressDialog) Uri(android.net.Uri) Comparator(java.util.Comparator) Format(com.nolanlawson.keepscore.helper.SdcardHelper.Format) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 69 with Comparator

use of java.util.Comparator in project jersey by jersey.

the class EntityTypesTest method testJAXBListRepresentationJSON.

@Test
public void testJAXBListRepresentationJSON() throws Exception {
    final WebTarget target = target("JAXBListResourceJSON");
    Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new LinkedList<>(a);
    b = target.path("queue").request().post(Entity.entity(new GenericEntity<Queue<JaxbBean>>((Queue<JaxbBean>) a) {
    }, "application/json"), new GenericType<Queue<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new HashSet<>(a);
    b = target.path("set").request().post(Entity.entity(new GenericEntity<Set<JaxbBean>>((Set<JaxbBean>) a) {
    }, "application/json"), new GenericType<Set<JaxbBean>>() {
    });
    final Comparator<JaxbBean> c = new Comparator<JaxbBean>() {

        @Override
        public int compare(final JaxbBean t, final JaxbBean t1) {
            return t.value.compareTo(t1.value);
        }
    };
    final TreeSet<JaxbBean> t1 = new TreeSet<>(c);
    final TreeSet<JaxbBean> t2 = new TreeSet<>(c);
    t1.addAll(a);
    t2.addAll(b);
    assertEquals(t1, t2);
    final Stack<JaxbBean> s = new Stack<>();
    s.addAll(a);
    b = target.path("stack").request().post(Entity.entity(new GenericEntity<Stack<JaxbBean>>(s) {
    }, "application/json"), new GenericType<Stack<JaxbBean>>() {
    });
    assertEquals(s, b);
    a = new MyArrayList<>(a);
    b = target.path("custom").request().post(Entity.entity(new GenericEntity<MyArrayList<JaxbBean>>((MyArrayList<JaxbBean>) a) {
    }, "application/json"), new GenericType<MyArrayList<JaxbBean>>() {
    });
    assertEquals(a, b);
// TODO: would be nice to produce/consume a real JSON array like following
// instead of what we have now:
//        JSONArray a = r.get(JSONArray.class);
//        JSONArray b = new JSONArray().
//                put(new JSONObject().put("value", "one")).
//                put(new JSONObject().put("value", "two")).
//                put(new JSONObject().put("value", "three"));
//        assertEquals(a.toString(), b.toString());
//        JSONArray c = r.post(JSONArray.class, b);
//        assertEquals(a.toString(), c.toString());
}
Also used : GenericType(javax.ws.rs.core.GenericType) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Comparator(java.util.Comparator) Stack(java.util.Stack) GenericEntity(javax.ws.rs.core.GenericEntity) TreeSet(java.util.TreeSet) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Queue(java.util.Queue) Test(org.junit.Test)

Example 70 with Comparator

use of java.util.Comparator in project jersey by jersey.

the class EntityTypesTest method testJAXBListRepresentationFastInfoset.

/**
     * TODO, the unmarshalling fails.
     */
@Test
@Ignore("TODO: unignore once fi support implemented (JERSEY-1190)")
public // TODO: unignore once fi support implemented (JERSEY-1190)
void testJAXBListRepresentationFastInfoset() {
    final WebTarget target = target("JAXBListResourceFastInfoset");
    Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/fastinfoset"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/fastinfoset"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new LinkedList<>(a);
    b = target.path("queue").request().post(Entity.entity(new GenericEntity<Queue<JaxbBean>>((Queue<JaxbBean>) a) {
    }, "application/fastinfoset"), new GenericType<Queue<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new HashSet<>(a);
    b = target.path("set").request().post(Entity.entity(new GenericEntity<Set<JaxbBean>>((Set<JaxbBean>) a) {
    }, "application/fastinfoset"), new GenericType<Set<JaxbBean>>() {
    });
    final Comparator<JaxbBean> c = new Comparator<JaxbBean>() {

        @Override
        public int compare(final JaxbBean t, final JaxbBean t1) {
            return t.value.compareTo(t1.value);
        }
    };
    final TreeSet<JaxbBean> t1 = new TreeSet<>(c);
    final TreeSet<JaxbBean> t2 = new TreeSet<>(c);
    t1.addAll(a);
    t2.addAll(b);
    assertEquals(t1, t2);
    final Stack<JaxbBean> s = new Stack<>();
    s.addAll(a);
    b = target.path("stack").request().post(Entity.entity(new GenericEntity<Stack<JaxbBean>>(s) {
    }, "application/fastinfoset"), new GenericType<Stack<JaxbBean>>() {
    });
    assertEquals(s, b);
    a = new MyArrayList<>(a);
    b = target.path("custom").request().post(Entity.entity(new GenericEntity<MyArrayList<JaxbBean>>((MyArrayList<JaxbBean>) a) {
    }, "application/fastinfoset"), new GenericType<MyArrayList<JaxbBean>>() {
    });
    assertEquals(a, b);
}
Also used : GenericType(javax.ws.rs.core.GenericType) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Comparator(java.util.Comparator) Stack(java.util.Stack) GenericEntity(javax.ws.rs.core.GenericEntity) TreeSet(java.util.TreeSet) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Queue(java.util.Queue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Comparator (java.util.Comparator)372 ArrayList (java.util.ArrayList)139 Test (org.junit.Test)66 List (java.util.List)65 HashMap (java.util.HashMap)57 Map (java.util.Map)39 IOException (java.io.IOException)37 File (java.io.File)27 HashSet (java.util.HashSet)26 TreeSet (java.util.TreeSet)23 Set (java.util.Set)21 Date (java.util.Date)17 TreeMap (java.util.TreeMap)17 Method (java.lang.reflect.Method)16 Iterator (java.util.Iterator)16 LinkedList (java.util.LinkedList)16 Collection (java.util.Collection)15 Collections (java.util.Collections)15 ArrayMap (android.util.ArrayMap)12 SimpleDateFormat (java.text.SimpleDateFormat)10