• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.car;
18 
19 import android.annotation.FloatRange;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.RequiresPermission;
23 import android.content.Context;
24 import android.os.Handler;
25 import android.os.IBinder;
26 import android.os.ParcelFileDescriptor;
27 import android.os.RemoteException;
28 
29 import com.android.internal.util.Preconditions;
30 
31 import libcore.io.IoUtils;
32 
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 import java.lang.ref.WeakReference;
36 
37 /**
38  * Car specific bugreport manager. Only available for userdebug and eng builds.
39  *
40  * @hide
41  */
42 public final class CarBugreportManager implements CarManagerBase {
43 
44     private final ICarBugreportService mService;
45     private Handler mHandler;
46 
47     /**
48      * Callback from carbugreport manager. Callback methods are always called on the main thread.
49      */
50     public abstract static class CarBugreportManagerCallback {
51 
52         @Retention(RetentionPolicy.SOURCE)
53         @IntDef(prefix = {"CAR_BUGREPORT_ERROR_"}, value = {
54                 CAR_BUGREPORT_DUMPSTATE_FAILED,
55                 CAR_BUGREPORT_IN_PROGRESS,
56                 CAR_BUGREPORT_DUMPSTATE_CONNECTION_FAILED,
57                 CAR_BUGREPORT_SERVICE_NOT_AVAILABLE
58         })
59 
60         public @interface CarBugreportErrorCode {
61         }
62 
63         /** Dumpstate failed to generate bugreport. */
64         public static final int CAR_BUGREPORT_DUMPSTATE_FAILED = 1;
65 
66         /**
67          * Another bugreport is in progress.
68          */
69         public static final int CAR_BUGREPORT_IN_PROGRESS = 2;
70 
71         /** Cannot connect to dumpstate */
72         public static final int CAR_BUGREPORT_DUMPSTATE_CONNECTION_FAILED = 3;
73 
74         /** Car bugreport service is not available (true for user builds) */
75         public static final int CAR_BUGREPORT_SERVICE_NOT_AVAILABLE = 4;
76 
77         /**
78          * Called when bugreport progress changes.
79          *
80          * <p>It's never called after {@link #onError} or {@link #onFinished}.
81          *
82          * @param progress - a number in [0.0, 100.0].
83          */
onProgress(@loatRangefrom = 0f, to = 100f) float progress)84         public void onProgress(@FloatRange(from = 0f, to = 100f) float progress) {
85         }
86 
87         /**
88          * Called on an error condition with one of the error codes listed above.
89          *
90          * @param errorCode the error code that defines failure reason.
91          */
onError(@arBugreportErrorCode int errorCode)92         public void onError(@CarBugreportErrorCode int errorCode) {
93         }
94 
95         /**
96          * Called when taking bugreport finishes successfully.
97          */
onFinished()98         public void onFinished() {
99         }
100     }
101 
102     /**
103      * Internal wrapper class to service.
104      */
105     private static final class CarBugreportManagerCallbackWrapper extends
106             ICarBugreportCallback.Stub {
107 
108         private final WeakReference<CarBugreportManagerCallback> mWeakCallback;
109         private final WeakReference<Handler> mWeakHandler;
110 
111         /**
112          * Create a new callback wrapper.
113          *
114          * @param callback the callback passed from app
115          * @param handler  the handler to execute callbacks on
116          */
CarBugreportManagerCallbackWrapper(CarBugreportManagerCallback callback, Handler handler)117         CarBugreportManagerCallbackWrapper(CarBugreportManagerCallback callback,
118                 Handler handler) {
119             mWeakCallback = new WeakReference<>(callback);
120             mWeakHandler = new WeakReference<>(handler);
121         }
122 
123         @Override
onProgress(@loatRangefrom = 0f, to = 100f) float progress)124         public void onProgress(@FloatRange(from = 0f, to = 100f) float progress) {
125             CarBugreportManagerCallback callback = mWeakCallback.get();
126             Handler handler = mWeakHandler.get();
127             if (handler != null && callback != null) {
128                 handler.post(() -> callback.onProgress(progress));
129             }
130         }
131 
132         @Override
onError(@arBugreportManagerCallback.CarBugreportErrorCode int errorCode)133         public void onError(@CarBugreportManagerCallback.CarBugreportErrorCode int errorCode) {
134             CarBugreportManagerCallback callback = mWeakCallback.get();
135             Handler handler = mWeakHandler.get();
136             if (handler != null && callback != null) {
137                 handler.post(() -> callback.onError(errorCode));
138             }
139         }
140 
141         @Override
onFinished()142         public void onFinished() {
143             CarBugreportManagerCallback callback = mWeakCallback.get();
144             Handler handler = mWeakHandler.get();
145             if (handler != null && callback != null) {
146                 handler.post(callback::onFinished);
147             }
148         }
149     }
150 
151     /**
152      * Get an instance of the CarBugreportManager
153      *
154      * Should not be obtained directly by clients, use {@link Car#getCarManager(String)} instead.
155      */
CarBugreportManager(IBinder service, Context context)156     public CarBugreportManager(IBinder service, Context context) {
157         mService = ICarBugreportService.Stub.asInterface(service);
158         mHandler = new Handler(context.getMainLooper());
159     }
160 
161     /**
162      * Request a bug report. A zipped (i.e. legacy) bugreport is generated in the background
163      * using dumpstate. This API also generates extra files that does not exist in the legacy
164      * bugreport and makes them available through a extra output file. Currently the extra
165      * output contains the screenshots for all the physical displays.
166      *
167      * <p>The file descriptor is closed when bugreport is written or if an exception happens.
168      *
169      * <p>This method is enabled only for one bug reporting app. It can be configured using
170      * {@code config_car_bugreport_application} string that is defined in
171      * {@code packages/services/Car/service/res/values/config.xml}. To learn more please
172      * see {@code packages/services/Car/tests/BugReportApp/README.md}.
173      *
174      * @param output the zipped bugreport file
175      * @param extraOutput a zip file that contains extra files generated for automotive.
176      * @param callback  the callback for reporting dump status
177      */
178     @RequiresPermission(android.Manifest.permission.DUMP)
requestBugreport( @onNull ParcelFileDescriptor output, @NonNull ParcelFileDescriptor extraOutput, @NonNull CarBugreportManagerCallback callback)179     public void requestBugreport(
180             @NonNull ParcelFileDescriptor output,
181             @NonNull ParcelFileDescriptor extraOutput,
182             @NonNull CarBugreportManagerCallback callback) {
183         Preconditions.checkNotNull(output);
184         Preconditions.checkNotNull(extraOutput);
185         Preconditions.checkNotNull(callback);
186         try {
187             CarBugreportManagerCallbackWrapper wrapper =
188                     new CarBugreportManagerCallbackWrapper(callback, mHandler);
189             mService.requestBugreport(output, extraOutput, wrapper);
190         } catch (RemoteException e) {
191             throw e.rethrowFromSystemServer();
192         } finally {
193             IoUtils.closeQuietly(output);
194             IoUtils.closeQuietly(extraOutput);
195         }
196     }
197 
198     @Override
onCarDisconnected()199     public void onCarDisconnected() {
200     }
201 }
202