• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.systemui.util.leak;
18 
19 import static com.android.systemui.Dependency.LEAK_REPORT_EMAIL_NAME;
20 
21 import android.annotation.Nullable;
22 import android.app.Notification;
23 import android.app.NotificationChannel;
24 import android.app.NotificationManager;
25 import android.app.PendingIntent;
26 import android.content.ClipData;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.net.Uri;
30 import android.os.Debug;
31 import android.os.SystemProperties;
32 import android.os.UserHandle;
33 import android.util.Log;
34 
35 import androidx.core.content.FileProvider;
36 
37 import com.android.systemui.dagger.SysUISingleton;
38 
39 import com.google.android.collect.Lists;
40 
41 import java.io.File;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.io.PrintWriter;
45 import java.util.ArrayList;
46 
47 import javax.inject.Inject;
48 import javax.inject.Named;
49 
50 /**
51  * Dumps data to debug leaks and posts a notification to share the data.
52  */
53 @SysUISingleton
54 public class LeakReporter {
55 
56     static final String TAG = "LeakReporter";
57 
58     public static final String FILEPROVIDER_AUTHORITY = "com.android.systemui.fileprovider";
59 
60     static final String LEAK_DIR = "leak";
61     static final String LEAK_HPROF = "leak.hprof";
62     static final String LEAK_DUMP = "leak.dump";
63 
64     private final Context mContext;
65     private final LeakDetector mLeakDetector;
66     private final String mLeakReportEmail;
67 
68     @Inject
LeakReporter(Context context, LeakDetector leakDetector, @Nullable @Named(LEAK_REPORT_EMAIL_NAME) String leakReportEmail)69     public LeakReporter(Context context, LeakDetector leakDetector,
70             @Nullable @Named(LEAK_REPORT_EMAIL_NAME) String leakReportEmail) {
71         mContext = context;
72         mLeakDetector = leakDetector;
73         mLeakReportEmail = leakReportEmail;
74     }
75 
dumpLeak(int garbageCount)76     public void dumpLeak(int garbageCount) {
77         try {
78             File leakDir = new File(mContext.getCacheDir(), LEAK_DIR);
79             leakDir.mkdir();
80 
81             File hprofFile = new File(leakDir, LEAK_HPROF);
82             Debug.dumpHprofData(hprofFile.getAbsolutePath());
83 
84             File dumpFile = new File(leakDir, LEAK_DUMP);
85             try (FileOutputStream fos = new FileOutputStream(dumpFile)) {
86                 PrintWriter w = new PrintWriter(fos);
87                 w.print("Build: "); w.println(SystemProperties.get("ro.build.description"));
88                 w.println();
89                 w.flush();
90                 mLeakDetector.dump(fos.getFD(), w, new String[0]);
91                 w.close();
92             }
93 
94             NotificationManager notiMan = mContext.getSystemService(NotificationManager.class);
95 
96             NotificationChannel channel = new NotificationChannel("leak", "Leak Alerts",
97                     NotificationManager.IMPORTANCE_HIGH);
98             channel.enableVibration(true);
99 
100             notiMan.createNotificationChannel(channel);
101             Notification.Builder builder = new Notification.Builder(mContext, channel.getId())
102                     .setAutoCancel(true)
103                     .setShowWhen(true)
104                     .setContentTitle("Memory Leak Detected")
105                     .setContentText(String.format(
106                             "SystemUI has detected %d leaked objects. Tap to send", garbageCount))
107                     .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
108                     .setContentIntent(PendingIntent.getActivityAsUser(
109                             mContext,
110                             0,
111                             getIntent(hprofFile, dumpFile),
112                             PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE,
113                             null,
114                             UserHandle.CURRENT));
115             notiMan.notify(TAG, 0, builder.build());
116         } catch (IOException e) {
117             Log.e(TAG, "Couldn't dump heap for leak", e);
118         }
119     }
120 
getIntent(File hprofFile, File dumpFile)121     private Intent getIntent(File hprofFile, File dumpFile) {
122         Uri dumpUri = FileProvider.getUriForFile(mContext, FILEPROVIDER_AUTHORITY, dumpFile);
123         Uri hprofUri = FileProvider.getUriForFile(mContext, FILEPROVIDER_AUTHORITY, hprofFile);
124 
125         Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
126         String mimeType = "application/vnd.android.leakreport";
127 
128         intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
129         intent.addCategory(Intent.CATEGORY_DEFAULT);
130         intent.setType(mimeType);
131 
132         final String subject = "SystemUI leak report";
133         intent.putExtra(Intent.EXTRA_SUBJECT, subject);
134 
135         // EXTRA_TEXT should be an ArrayList, but some clients are expecting a single String.
136         // So, to avoid an exception on Intent.migrateExtraStreamToClipData(), we need to manually
137         // create the ClipData object with the attachments URIs.
138         final StringBuilder messageBody = new StringBuilder("Build info: ")
139                 .append(SystemProperties.get("ro.build.description"));
140         intent.putExtra(Intent.EXTRA_TEXT, messageBody.toString());
141         final ClipData clipData = new ClipData(null, new String[] { mimeType },
142                 new ClipData.Item(null, null, null, dumpUri));
143         final ArrayList<Uri> attachments = Lists.newArrayList(dumpUri);
144 
145         clipData.addItem(new ClipData.Item(null, null, null, hprofUri));
146         attachments.add(hprofUri);
147 
148         intent.setClipData(clipData);
149         intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
150 
151         String leakReportEmail = mLeakReportEmail;
152         if (leakReportEmail != null) {
153             intent.putExtra(Intent.EXTRA_EMAIL, new String[] { leakReportEmail });
154         }
155 
156         return intent;
157     }
158 }
159