blob: 1336c66b713333437400fd3019a8c24b704ddb41 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060019import android.annotation.RequiresPermission;
Joe Onorato1754d742016-11-21 17:51:35 -080020import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060021import android.annotation.SystemService;
Joe Onorato1754d742016-11-21 17:51:35 -080022import android.annotation.TestApi;
23import android.content.Context;
Joe Onorato1754d742016-11-21 17:51:35 -080024import android.provider.Settings;
25import android.util.Slog;
26
27/**
28 * Class to take an incident report.
29 *
30 * @hide
31 */
32@SystemApi
33@TestApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060034@SystemService(Context.INCIDENT_SERVICE)
Joe Onorato1754d742016-11-21 17:51:35 -080035public class IncidentManager {
36 private static final String TAG = "incident";
37
Yi Jinf32af482017-08-11 15:00:49 -070038 private final Context mContext;
Joe Onorato1754d742016-11-21 17:51:35 -080039
40 /**
41 * @hide
42 */
43 public IncidentManager(Context context) {
44 mContext = context;
45 }
46
47 /**
48 * Take an incident report and put it in dropbox.
49 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060050 @RequiresPermission(allOf = {
51 android.Manifest.permission.DUMP,
52 android.Manifest.permission.PACKAGE_USAGE_STATS
53 })
Joe Onorato1754d742016-11-21 17:51:35 -080054 public void reportIncident(IncidentReportArgs args) {
Yi Jinf32af482017-08-11 15:00:49 -070055 reportIncidentInternal(args);
Joe Onorato1754d742016-11-21 17:51:35 -080056 }
57
58 /**
59 * Convenience method to trigger an incident report and put it in dropbox.
60 * <p>
61 * The fields that are reported will be looked up in the system setting named by
62 * the settingName parameter. The setting must match one of these patterns:
63 * The string "disabled": The report will not be taken.
64 * The string "all": The report will taken with all sections.
65 * The string "none": The report will taken with no sections, but with the header.
66 * A comma separated list of field numbers: The report will have these fields.
67 * <p>
68 * The header parameter will be added as a header for the incident report. Fill in a
69 * {@link android.util.proto.ProtoOutputStream ProtoOutputStream}, and then call the
70 * {@link android.util.proto.ProtoOutputStream#bytes bytes()} method to retrieve
71 * the encoded data for the header.
72 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060073 @RequiresPermission(allOf = {
74 android.Manifest.permission.DUMP,
75 android.Manifest.permission.PACKAGE_USAGE_STATS
76 })
Joe Onorato1754d742016-11-21 17:51:35 -080077 public void reportIncident(String settingName, byte[] headerProto) {
78 // Sections
Yi Jinf32af482017-08-11 15:00:49 -070079 String setting = Settings.Global.getString(mContext.getContentResolver(), settingName);
Joe Onorato1754d742016-11-21 17:51:35 -080080 IncidentReportArgs args;
81 try {
82 args = IncidentReportArgs.parseSetting(setting);
83 } catch (IllegalArgumentException ex) {
84 Slog.w(TAG, "Bad value for incident report setting '" + settingName + "'", ex);
85 return;
86 }
87 if (args == null) {
Yi Jinf32af482017-08-11 15:00:49 -070088 Slog.i(TAG, String.format("Incident report requested but disabled with "
89 + "settings [name: %s, value: %s]", settingName, setting));
Joe Onorato1754d742016-11-21 17:51:35 -080090 return;
91 }
92
Joe Onorato1754d742016-11-21 17:51:35 -080093 args.addHeader(headerProto);
94
Yi Jinf32af482017-08-11 15:00:49 -070095 Slog.i(TAG, "Taking incident report: " + settingName);
96 reportIncidentInternal(args);
97 }
98
99 private void reportIncidentInternal(IncidentReportArgs args) {
Joe Onorato1754d742016-11-21 17:51:35 -0800100 final IIncidentManager service = IIncidentManager.Stub.asInterface(
Yi Jinf32af482017-08-11 15:00:49 -0700101 ServiceManager.getService(Context.INCIDENT_SERVICE));
Joe Onorato1754d742016-11-21 17:51:35 -0800102 if (service == null) {
103 Slog.e(TAG, "reportIncident can't find incident binder service");
104 return;
105 }
106
Joe Onorato1754d742016-11-21 17:51:35 -0800107 try {
108 service.reportIncident(args);
109 } catch (RemoteException ex) {
110 Slog.e(TAG, "reportIncident failed", ex);
111 }
112 }
113}
114