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);
157c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot        if (intent == null) {
158c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot            Log.i(TAG, String.format("Activity %s/%s is disabled, skipping",
159c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot                    app.activityInfo.packageName, app.activityInfo.name));
160c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot            return Collections.EMPTY_LIST;
161c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot        }
1625ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        getContext().startActivity(intent);
1635ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        try {
1645ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            Thread.sleep(appLaunchWait);
1655ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        } catch (InterruptedException e) {
1665ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            // ignore
1675ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        }
1685ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
1695ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        // Send the "home" intent and wait 2 seconds for us to get there
1705ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        getContext().startActivity(mHomeIntent);
1715ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        try {
1725ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            Thread.sleep(homeLaunchWait);
1735ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        } catch (InterruptedException e) {
1745ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens            // ignore
1755ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        }
1765ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
177bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        // See if there are any errors.  We wait until down here to give ANRs as much time as
178bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        // possible to occur.
179bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        final Collection<ProcessError> errProcs =
180bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                ProcessError.fromCollection(mActivityManager.getProcessesInErrorState());
181a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
182a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // Distinguish the asynchronous crashes/ANRs from the synchronous ones by checking the
183a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // crash package name against the package name for {@code app}
184a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        if (errProcs != null) {
185a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            Iterator<ProcessError> errIter = errProcs.iterator();
186a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            while (errIter.hasNext()) {
187a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                ProcessError err = errIter.next();
188a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                if (!packageMatches(app, err)) {
189a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                    // async!  Drop into mAsyncErrors and don't report now
190a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                    mAsyncErrors.add(err);
191a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                    errIter.remove();
192a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                }
193a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens            }
194a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        }
195a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // Take the difference between the remaining current error processes and the ones that were
196a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // present when we started.  The result is guaranteed to be:
197a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // 1) Errors that are pertinent to this app's package
198a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // 2) Errors that are pertinent to this particular app invocation
199bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        if (errProcs != null && preErrProcs != null) {
200bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            errProcs.removeAll(preErrProcs);
201bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens        }
202bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens
2035ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens        return errProcs;
2045ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    }
2055ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens
206a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    private String checkForProcessErrors() throws Exception {
207a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        List<ProcessErrorStateInfo> errList;
208a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        errList = mActivityManager.getProcessesInErrorState();
209a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
210a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // note: this contains information about each process that is currently in an error
211a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // condition.  if the list is empty (null) then "we're good".
212a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
213a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        // if the list is non-empty, then it's useful to report the contents of the list
214a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final String reportMsg = reportListContents(errList);
215a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return reportMsg;
216a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    }
217a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens
2185ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    /**
219a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * A helper function that checks whether the specified error could have been caused by the
220a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * specified app.
221a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     *
222a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * @param app The app to check against
223a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * @param err The error that we're considering
2249f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     */
225a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    private static boolean packageMatches(ResolveInfo app, ProcessError err) {
226a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final String appPkg = app.activityInfo.packageName;
227a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final String errPkg = err.info.processName;
228a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        Log.d(TAG, String.format("packageMatches(%s, %s)", appPkg, errPkg));
229a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return appPkg.equals(errPkg);
230a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    }
2319f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
232a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
233a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * A helper function to query the provided {@link PackageManager} for a list of Activities that
234a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * can be launched from Launcher.
235a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
236a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    static List<ResolveInfo> getLauncherActivities(PackageManager pm) {
237a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final Intent launchable = new Intent(Intent.ACTION_MAIN);
238a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        launchable.addCategory(Intent.CATEGORY_LAUNCHER);
239a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final List<ResolveInfo> activities = pm.queryIntentActivities(launchable, 0);
240a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return activities;
241a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    }
2429f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
243a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
244a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * A helper function to create an {@link Intent} to run, given a {@link ResolveInfo} specifying
245a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * an activity to be launched.
246c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot     *
247c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot     * @return the {@link Intent} or <code>null</code> if given app is disabled
248a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
249c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot    Intent intentForActivity(ResolveInfo app) {
250a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final ComponentName component = new ComponentName(app.activityInfo.packageName,
251a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens                app.activityInfo.name);
252c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot        if (getContext().getPackageManager().getComponentEnabledSetting(component) ==
253c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot                PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
254c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot            return null;
255c0104148fab184efce25c3bcb9aad596545256eaBrett Chabot        }
256a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        final Intent intent = new Intent(Intent.ACTION_MAIN);
257a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        intent.setComponent(component);
258a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
259a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
260a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens        return intent;
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2629f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
263a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    /**
264a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * Report error reports for {@link ProcessErrorStateInfo} instances that are wrapped inside of
265a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * {@link ProcessError} instances.  Just unwraps and calls
266a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     * {@see reportListContents(Collection<ProcessErrorStateInfo>)}.
267a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens     */
268a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    static String reportWrappedListContents(Collection<ProcessError> errList) {
2699f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        List<ProcessErrorStateInfo> newList = new ArrayList<ProcessErrorStateInfo>(errList.size());
2709f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        for (ProcessError err : errList) {
2719f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            newList.add(err.info);
2729f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
2739f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        return reportListContents(newList);
2749f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    }
2759f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This helper function will dump the actual error reports.
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param errList The error report containing one or more error records.
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return Returns a string containing all of the errors.
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
282a6a353653d9147a96cd21543448d9be7631cb424Omari Stephens    private static String reportListContents(Collection<ProcessErrorStateInfo> errList) {
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (errList == null) return null;
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        StringBuilder builder = new StringBuilder();
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2879f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        Iterator<ProcessErrorStateInfo> iter = errList.iterator();
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while (iter.hasNext()) {
2899f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            ProcessErrorStateInfo entry = iter.next();
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String condition;
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            switch (entry.condition) {
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case ActivityManager.ProcessErrorStateInfo.CRASHED:
294bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                condition = "a CRASH";
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING:
297bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                condition = "an ANR";
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                break;
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            default:
300bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                condition = "an unknown error";
301b7f0367cec1c744aa66ef397b0244e25d507491cDan Egnor                break;
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
304bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            builder.append(String.format("Process %s encountered %s (%s)", entry.processName,
305bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                    condition, entry.shortMsg));
306bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            if (entry.condition == ActivityManager.ProcessErrorStateInfo.CRASHED) {
307bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens                builder.append(String.format(" with stack trace:\n%s\n", entry.stackTrace));
308bf29121c215b30bed8cb886f1b6c7d71eb36a49dOmari Stephens            }
3099f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            builder.append("\n");
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return builder.toString();
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3139f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3149f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    /**
3159f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     * A {@link ProcessErrorStateInfo} wrapper class that hashes how we want (so that equivalent
3169f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     * crashes are considered equal).
3179f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens     */
3185ae3dfe59d22457fb232853990ba4131385ff3f6Omari Stephens    static class ProcessError {
3199f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public final ProcessErrorStateInfo info;
3209f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3219f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public ProcessError(ProcessErrorStateInfo newInfo) {
3229f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            info = newInfo;
3239f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3249f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3259f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public static Collection<ProcessError> fromCollection(Collection<ProcessErrorStateInfo> in)
3269f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                {
327ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens            if (in == null) {
328ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens                return null;
329ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens            }
330ad8037e3a9b306bae6cdc8927c35946696bf40f6Omari Stephens
3319f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            List<ProcessError> out = new ArrayList<ProcessError>(in.size());
3329f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            for (ProcessErrorStateInfo info : in) {
3339f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                out.add(new ProcessError(info));
3349f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            }
3359f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            return out;
3369f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3379f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3389f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        private boolean strEquals(String a, String b) {
3399f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if ((a == null) && (b == null)) {
3409f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return true;
3419f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            } else if ((a == null) || (b == null)) {
3429f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return false;
3439f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            } else {
3449f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return a.equals(b);
3459f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            }
3469f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3479f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3489f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        @Override
3499f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public boolean equals(Object other) {
3509f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if (other == null) return false;
3519f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if (!(other instanceof ProcessError)) return false;
3529f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            ProcessError peOther = (ProcessError) other;
3539f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3549f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            return (info.condition == peOther.info.condition)
3559f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.longMsg, peOther.info.longMsg)
3569f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && (info.pid == peOther.info.pid)
3579f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.processName, peOther.info.processName)
3589f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.shortMsg, peOther.info.shortMsg)
3599f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.stackTrace, peOther.info.stackTrace)
3609f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && strEquals(info.tag, peOther.info.tag)
3619f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                    && (info.uid == peOther.info.uid);
3629f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3639f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3649f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        private int hash(Object obj) {
3659f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            if (obj == null) {
3669f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return 13;
3679f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            } else {
3689f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens                return obj.hashCode();
3699f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            }
3709f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3719f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens
3729f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        @Override
3739f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        public int hashCode() {
3749f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            int code = 17;
3759f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code += info.condition;
3769f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.longMsg);
3779f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code += info.pid;
3789f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.processName);
3799f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.shortMsg);
3809f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.stackTrace);
3819f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code *= hash(info.tag);
3829f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            code += info.uid;
3839f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens            return code;
3849f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens        }
3859f5a511a08c56906edac40230ac97d670d7ad0c6Omari Stephens    }
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
387