19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage com.android.smoketest;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.ActivityManager;
209f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport android.app.ActivityManager.ProcessErrorStateInfo;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
229f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport android.content.ComponentName;
239f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport android.content.Intent;
249f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport android.content.pm.PackageManager;
259f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport android.content.pm.ResolveInfo;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.test.AndroidTestCase;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
299f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport java.util.ArrayList;
309f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport java.util.Collection;
31a6a353653d9147a96cd21543448d9be7631cb424Omari Stephensimport java.util.Collections;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Iterator;
33a6a353653d9147a96cd21543448d9be7631cb424Omari Stephensimport java.util.LinkedHashSet;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.List;
359f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephensimport java.util.Set;
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
38a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens * This smoke test is designed to check for crashes and ANRs in an attempt to quickly determine if
39a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens * all minimal functionality in the build is working properly.
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class ProcessErrorsTest extends AndroidTestCase {
42a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
439f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    private static final String TAG = "ProcessErrorsTest";
445ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
455ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    private final Intent mHomeIntent;
465ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected ActivityManager mActivityManager;
489f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    protected PackageManager mPackageManager;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
50a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
51a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * Used to buffer asynchronously-caused crashes and ANRs so that we can have a big fail-party
52a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * in the catch-all testCase.
53a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
54a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    private static final Collection<ProcessError> mAsyncErrors =
55a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            Collections.synchronizedSet(new LinkedHashSet<ProcessError>());
56a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
575ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    public ProcessErrorsTest() {
585ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        mHomeIntent = new Intent(Intent.ACTION_MAIN);
595ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        mHomeIntent.addCategory(Intent.CATEGORY_HOME);
605ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
615ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    }
625ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
63a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
64a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * {@inheritDoc}
65a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setUp() throws Exception {
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        super.setUp();
69a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // First, make sure we have a Context
70a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        assertNotNull("getContext() returned null!", getContext());
71a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
729f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        mActivityManager = (ActivityManager)
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                getContext().getSystemService(Context.ACTIVITY_SERVICE);
749f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        mPackageManager = getContext().getPackageManager();
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void testSetUpConditions() throws Exception {
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        assertNotNull(mActivityManager);
799f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        assertNotNull(mPackageManager);
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
825ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    public void testNoProcessErrorsAfterBoot() throws Exception {
839f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        final String reportMsg = checkForProcessErrors();
849f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        if (reportMsg != null) {
859f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            Log.w(TAG, reportMsg);
869f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
879f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
889f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        // report a non-empty list back to the test framework
899f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        assertNull(reportMsg, reportMsg);
909f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    }
919f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
92a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
93a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * A test that runs all Launcher-launchable activities and verifies that no ANRs or crashes
94a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * happened while doing so.
95a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
96a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    public void testRunAllActivities() throws Exception {
97a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final Set<ProcessError> errSet = new LinkedHashSet<ProcessError>();
985ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
99a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        for (ResolveInfo app : getLauncherActivities(mPackageManager)) {
100a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            final Collection<ProcessError> errProcs = runOneActivity(app);
101a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            if (errProcs != null) {
102a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                errSet.addAll(errProcs);
103a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            }
104a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        }
1055ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
106a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        if (!errSet.isEmpty()) {
107a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            fail(String.format("Got %d errors:\n%s", errSet.size(),
108a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                    reportWrappedListContents(errSet)));
109a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        }
1109f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    }
1119f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
1129f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    /**
113a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * This test checks for asynchronously-caused errors (crashes or ANRs) and fails in case any
114a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * were found.  This prevents us from needing to fail unrelated testcases when, for instance
115a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * a background thread causes a crash or ANR.
116a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * <p />
117a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * Because this behavior depends on the contents of static member {@link mAsyncErrors}, we clear
118a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * that state here as a side-effect so that if two successive runs happen in the same process,
119a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * the asynchronous errors in the second test run won't include errors produced during the first
120a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * run.
1215ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens     */
122a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    public void testZZReportAsyncErrors() throws Exception {
123a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        try {
124a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            if (!mAsyncErrors.isEmpty()) {
125a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                fail(String.format("Got %d asynchronous errors:\n%s", mAsyncErrors.size(),
126a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                        reportWrappedListContents(mAsyncErrors)));
127a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            }
128a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        } finally {
129a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            // Reset state just in case we should get another set of runs in the same process
130a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            mAsyncErrors.clear();
131a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        }
1325ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    }
1335ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
1345ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
1355ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    /**
1365ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens     * A method to run the specified Activity and return a {@link Collection} of the Activities that
1375ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens     * were in an error state, as listed by {@link ActivityManager.getProcessesInErrorState()}.
1385ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens     * <p />
1395ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens     * The method will launch the app, wait for 7 seconds, check for apps in the error state, send
1405ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens     * the Home intent, wait for 2 seconds, and then return.
1415ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens     */
142ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens    public Collection<ProcessError> runOneActivity(ResolveInfo app) {
1435ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        final long appLaunchWait = 7000;
1445ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        final long homeLaunchWait = 2000;
1455ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
1465ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        Log.i(TAG, String.format("Running activity %s/%s", app.activityInfo.packageName,
1475ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens                app.activityInfo.name));
1485ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
149ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens        // We check for any Crash or ANR dialogs that are already up, and we ignore them.  This is
150ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens        // so that we don't report crashes that were caused by prior apps (which those particular
151a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // tests should have caught and reported already).
152ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens        final Collection<ProcessError> preErrProcs =
153ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens                ProcessError.fromCollection(mActivityManager.getProcessesInErrorState());
154ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens
1555ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        // launch app, and wait 7 seconds for it to start/settle
1565ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        final Intent intent = intentForActivity(app);
1575ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        getContext().startActivity(intent);
1585ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        try {
1595ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            Thread.sleep(appLaunchWait);
1605ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        } catch (InterruptedException e) {
1615ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            // ignore
1625ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        }
1635ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
1645ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        // Send the "home" intent and wait 2 seconds for us to get there
1655ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        getContext().startActivity(mHomeIntent);
1665ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        try {
1675ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            Thread.sleep(homeLaunchWait);
1685ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        } catch (InterruptedException e) {
1695ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            // ignore
1705ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        }
1715ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
172bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        // See if there are any errors.  We wait until down here to give ANRs as much time as
173bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        // possible to occur.
174bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        final Collection<ProcessError> errProcs =
175bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                ProcessError.fromCollection(mActivityManager.getProcessesInErrorState());
176a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
177a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // Distinguish the asynchronous crashes/ANRs from the synchronous ones by checking the
178a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // crash package name against the package name for {@code app}
179a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        if (errProcs != null) {
180a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            Iterator<ProcessError> errIter = errProcs.iterator();
181a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            while (errIter.hasNext()) {
182a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                ProcessError err = errIter.next();
183a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                if (!packageMatches(app, err)) {
184a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                    // async!  Drop into mAsyncErrors and don't report now
185a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                    mAsyncErrors.add(err);
186a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                    errIter.remove();
187a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                }
188a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            }
189a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        }
190a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // Take the difference between the remaining current error processes and the ones that were
191a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // present when we started.  The result is guaranteed to be:
192a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // 1) Errors that are pertinent to this app's package
193a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // 2) Errors that are pertinent to this particular app invocation
194bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        if (errProcs != null && preErrProcs != null) {
195bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            errProcs.removeAll(preErrProcs);
196bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        }
197bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens
1985ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        return errProcs;
1995ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    }
2005ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
201a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    private String checkForProcessErrors() throws Exception {
202a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        List<ProcessErrorStateInfo> errList;
203a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        errList = mActivityManager.getProcessesInErrorState();
204a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
205a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // note: this contains information about each process that is currently in an error
206a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // condition.  if the list is empty (null) then "we're good".
207a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
208a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // if the list is non-empty, then it's useful to report the contents of the list
209a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final String reportMsg = reportListContents(errList);
210a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return reportMsg;
211a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    }
212a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
2135ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    /**
214a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * A helper function that checks whether the specified error could have been caused by the
215a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * specified app.
216a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     *
217a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * @param app The app to check against
218a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * @param err The error that we're considering
2199f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     */
220a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    private static boolean packageMatches(ResolveInfo app, ProcessError err) {
221a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final String appPkg = app.activityInfo.packageName;
222a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final String errPkg = err.info.processName;
223a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        Log.d(TAG, String.format("packageMatches(%s, %s)", appPkg, errPkg));
224a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return appPkg.equals(errPkg);
225a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    }
2269f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
227a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
228a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * A helper function to query the provided {@link PackageManager} for a list of Activities that
229a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * can be launched from Launcher.
230a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
231a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    static List<ResolveInfo> getLauncherActivities(PackageManager pm) {
232a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final Intent launchable = new Intent(Intent.ACTION_MAIN);
233a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        launchable.addCategory(Intent.CATEGORY_LAUNCHER);
234a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0);
235a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return activities;
236a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    }
2379f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
238a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
239a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying
240a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * an activity to be launched.
241a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
242a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    static Intent intentForActivity(ResolveInfo app) {
243a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final ComponentName component = new ComponentName(app.activityInfo.packageName,
244a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                app.activityInfo.name);
245a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final Intent intent = new Intent(Intent.ACTION_MAIN);
246a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        intent.setComponent(component);
247a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
248a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
249a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return intent;
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2519f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
252a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
253a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * Report error reports for {@link ProcessErrorStateInfo} instances that are wrapped inside of
254a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * {@link ProcessError} instances.  Just unwraps and calls
255a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * {@see reportListContents(Collection<ProcessErrorStateInfo>)}.
256a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
257a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    static String reportWrappedListContents(Collection<ProcessError> errList) {
2589f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size());
2599f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        for (ProcessError err : errList) {
2609f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            newList.add(err.info);
2619f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
2629f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        return reportListContents(newList);
2639f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    }
2649f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This helper function will dump the actual error reports.
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param errList The error report containing one or more error records.
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return Returns a string containing all of the errors.
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
271a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    private static String reportListContents(Collection<ProcessErrorStateInfo> errList) {
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (errList == null) return null;
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        StringBuilder builder = new StringBuilder();
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2769f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        Iterator<ProcessErrorStateInfo> iter = errList.iterator();
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (iter.hasNext()) {
2789f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            ProcessErrorStateInfo entry = iter.next();
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String condition;
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            switch (entry.condition) {
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case ActivityManager.ProcessErrorStateInfo.CRASHED:
283bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                condition = "a CRASH";
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING:
286bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                condition = "an ANR";
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            default:
289bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                condition = "an unknown error";
290b7f0367cec1c744aa66ef397b0244e25d507491cDan Egnor                break;
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
293bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            builder.append(String.format("Process %s encountered %s (%s)", entry.processName,
294bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                    condition, entry.shortMsg));
295bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            if (entry.condition == ActivityManager.ProcessErrorStateInfo.CRASHED) {
296bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                builder.append(String.format(" with stack trace:\n%s\n", entry.stackTrace));
297bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            }
2989f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            builder.append("\n");
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return builder.toString();
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3029f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3039f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    /**
3049f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     * A {@link ProcessErrorStateInfo} wrapper class that hashes how we want (so that equivalent
3059f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     * crashes are considered equal).
3069f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     */
3075ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    static class ProcessError {
3089f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public final ProcessErrorStateInfo info;
3099f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3109f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public ProcessError(ProcessErrorStateInfo newInfo) {
3119f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            info = newInfo;
3129f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3139f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3149f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public static Collection<ProcessError> fromCollection(Collection<ProcessErrorStateInfo> in)
3159f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                {
316ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens            if (in == null) {
317ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens                return null;
318ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens            }
319ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens
3209f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            List<ProcessError> out = new ArrayList<ProcessError>(in.size());
3219f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            for (ProcessErrorStateInfo info : in) {
3229f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                out.add(new ProcessError(info));
3239f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            }
3249f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            return out;
3259f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3269f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3279f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        private boolean strEquals(String a, String b) {
3289f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if ((a == null) && (b == null)) {
3299f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return true;
3309f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            } else if ((a == null) || (b == null)) {
3319f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return false;
3329f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            } else {
3339f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return a.equals(b);
3349f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            }
3359f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3369f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3379f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        @Override
3389f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public boolean equals(Object other) {
3399f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if (other == null) return false;
3409f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if (!(other instanceof ProcessError)) return false;
3419f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            ProcessError peOther = (ProcessError) other;
3429f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3439f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            return (info.condition == peOther.info.condition)
3449f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.longMsg, peOther.info.longMsg)
3459f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && (info.pid == peOther.info.pid)
3469f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.processName, peOther.info.processName)
3479f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.shortMsg, peOther.info.shortMsg)
3489f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.stackTrace, peOther.info.stackTrace)
3499f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.tag, peOther.info.tag)
3509f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && (info.uid == peOther.info.uid);
3519f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3529f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3539f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        private int hash(Object obj) {
3549f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if (obj == null) {
3559f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return 13;
3569f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            } else {
3579f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return obj.hashCode();
3589f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            }
3599f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3609f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3619f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        @Override
3629f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public int hashCode() {
3639f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            int code = 17;
3649f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code += info.condition;
3659f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.longMsg);
3669f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code += info.pid;
3679f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.processName);
3689f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.shortMsg);
3699f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.stackTrace);
3709f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.tag);
3719f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code += info.uid;
3729f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            return code;
3739f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3749f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    }
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
376