Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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 | |
| 17 | package android.os; |
| 18 | |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 19 | import android.annotation.RequiresPermission; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 21 | import android.annotation.SystemService; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | import android.annotation.TestApi; |
| 23 | import android.content.Context; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 24 | import android.provider.Settings; |
| 25 | import android.util.Slog; |
| 26 | |
| 27 | /** |
| 28 | * Class to take an incident report. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | @SystemApi |
| 33 | @TestApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 34 | @SystemService(Context.INCIDENT_SERVICE) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 35 | public class IncidentManager { |
| 36 | private static final String TAG = "incident"; |
| 37 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 38 | private final Context mContext; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | |
| 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 Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 50 | @RequiresPermission(allOf = { |
| 51 | android.Manifest.permission.DUMP, |
| 52 | android.Manifest.permission.PACKAGE_USAGE_STATS |
| 53 | }) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 54 | public void reportIncident(IncidentReportArgs args) { |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 55 | reportIncidentInternal(args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 56 | } |
| 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 Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 73 | @RequiresPermission(allOf = { |
| 74 | android.Manifest.permission.DUMP, |
| 75 | android.Manifest.permission.PACKAGE_USAGE_STATS |
| 76 | }) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 77 | public void reportIncident(String settingName, byte[] headerProto) { |
| 78 | // Sections |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 79 | String setting = Settings.Global.getString(mContext.getContentResolver(), settingName); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 80 | 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 Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 88 | Slog.i(TAG, String.format("Incident report requested but disabled with " |
| 89 | + "settings [name: %s, value: %s]", settingName, setting)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 93 | args.addHeader(headerProto); |
| 94 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 95 | Slog.i(TAG, "Taking incident report: " + settingName); |
| 96 | reportIncidentInternal(args); |
| 97 | } |
| 98 | |
| 99 | private void reportIncidentInternal(IncidentReportArgs args) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 100 | final IIncidentManager service = IIncidentManager.Stub.asInterface( |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 101 | ServiceManager.getService(Context.INCIDENT_SERVICE)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 102 | if (service == null) { |
| 103 | Slog.e(TAG, "reportIncident can't find incident binder service"); |
| 104 | return; |
| 105 | } |
| 106 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 107 | try { |
| 108 | service.reportIncident(args); |
| 109 | } catch (RemoteException ex) { |
| 110 | Slog.e(TAG, "reportIncident failed", ex); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |