IncidentManager.java revision 1754d744a7a34731ffc07af1bc3dbfcb06864ab0
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
19import android.annotation.SystemApi;
20import android.annotation.TestApi;
21import android.content.Context;
22import android.os.IIncidentManager;
23import android.os.ServiceManager;
24import android.provider.Settings;
25import android.util.Slog;
26
27/**
28 * Class to take an incident report.
29 *
30 * @hide
31 */
32@SystemApi
33@TestApi
34public class IncidentManager {
35    private static final String TAG = "incident";
36
37    private Context mContext;
38
39    /**
40     * @hide
41     */
42    public IncidentManager(Context context) {
43        mContext = context;
44    }
45
46    /**
47     * Take an incident report and put it in dropbox.
48     */
49    public void reportIncident(IncidentReportArgs args) {
50        final IIncidentManager service = IIncidentManager.Stub.asInterface(
51                ServiceManager.getService("incident"));
52        if (service == null) {
53            Slog.e(TAG, "reportIncident can't find incident binder service");
54            return;
55        }
56
57        try {
58            service.reportIncident(args);
59        } catch (RemoteException ex) {
60            Slog.e(TAG, "reportIncident failed", ex);
61        }
62    }
63
64    /**
65     * Convenience method to trigger an incident report and put it in dropbox.
66     * <p>
67     * The fields that are reported will be looked up in the system setting named by
68     * the settingName parameter.  The setting must match one of these patterns:
69     *      The string "disabled": The report will not be taken.
70     *      The string "all": The report will taken with all sections.
71     *      The string "none": The report will taken with no sections, but with the header.
72     *      A comma separated list of field numbers: The report will have these fields.
73     * <p>
74     * The header parameter will be added as a header for the incident report.  Fill in a
75     * {@link android.util.proto.ProtoOutputStream ProtoOutputStream}, and then call the
76     * {@link android.util.proto.ProtoOutputStream#bytes bytes()} method to retrieve
77     * the encoded data for the header.
78     */
79    public void reportIncident(String settingName, byte[] headerProto) {
80        // Sections
81        String setting = Settings.System.getString(mContext.getContentResolver(), settingName);
82        IncidentReportArgs args;
83        try {
84            args = IncidentReportArgs.parseSetting(setting);
85        } catch (IllegalArgumentException ex) {
86            Slog.w(TAG, "Bad value for incident report setting '" + settingName + "'", ex);
87            return;
88        }
89        if (args == null) {
90            Slog.i(TAG, "Incident report requested but disabled: " + settingName);
91            return;
92        }
93
94        // Header
95        args.addHeader(headerProto);
96
97        // Look up the service
98        final IIncidentManager service = IIncidentManager.Stub.asInterface(
99                ServiceManager.getService("incident"));
100        if (service == null) {
101            Slog.e(TAG, "reportIncident can't find incident binder service");
102            return;
103        }
104
105        // Call the service
106        Slog.i(TAG, "Taking incident report: " + settingName);
107        try {
108            service.reportIncident(args);
109        } catch (RemoteException ex) {
110            Slog.e(TAG, "reportIncident failed", ex);
111        }
112    }
113}
114
115