summaryrefslogtreecommitdiffstats
path: root/core/java/com/android/internal/app/DumpHeapActivity.java
blob: 0ce501ec6daaae7adae4b8fddfe69c8a3c48ea6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.app;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DebugUtils;
import android.util.Slog;

/**
 * This activity is displayed when the system has collected a heap dump from
 * a large process and the user has selected to share it.
 */
public class DumpHeapActivity extends Activity {
    /** The process we are reporting */
    public static final String KEY_PROCESS = "process";
    /** The size limit the process reached */
    public static final String KEY_SIZE = "size";
    /** Optional name of package to directly launch */
    public static final String KEY_DIRECT_LAUNCH = "direct_launch";

    // Broadcast action to determine when to delete the current dump heap data.
    public static final String ACTION_DELETE_DUMPHEAP = "com.android.server.am.DELETE_DUMPHEAP";

    // Extra for above: delay delete of data, since the user is in the process of sharing it.
    public static final String EXTRA_DELAY_DELETE = "delay_delete";

    static final public Uri JAVA_URI = Uri.parse("content://com.android.server.heapdump/java");

    String mProcess;
    long mSize;
    AlertDialog mDialog;
    boolean mHandled = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mProcess = getIntent().getStringExtra(KEY_PROCESS);
        mSize = getIntent().getLongExtra(KEY_SIZE, 0);

        String directLaunch = getIntent().getStringExtra(KEY_DIRECT_LAUNCH);
        if (directLaunch != null) {
            Intent intent = new Intent(ActivityManager.ACTION_REPORT_HEAP_LIMIT);
            intent.setPackage(directLaunch);
            ClipData clip = ClipData.newUri(getContentResolver(), "Heap Dump", JAVA_URI);
            intent.setClipData(clip);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setType(clip.getDescription().getMimeType(0));
            intent.putExtra(Intent.EXTRA_STREAM, JAVA_URI);
            try {
                startActivity(intent);
                scheduleDelete();
                mHandled = true;
                finish();
                return;
            } catch (ActivityNotFoundException e) {
                Slog.i("DumpHeapActivity", "Unable to direct launch to " + directLaunch
                        + ": " + e.getMessage());
            }
        }

        AlertDialog.Builder b = new AlertDialog.Builder(this,
                android.R.style.Theme_Material_Light_Dialog_Alert);
        b.setTitle(com.android.internal.R.string.dump_heap_title);
        b.setMessage(getString(com.android.internal.R.string.dump_heap_text,
                mProcess, DebugUtils.sizeValueToString(mSize, null)));
        b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mHandled = true;
                sendBroadcast(new Intent(ACTION_DELETE_DUMPHEAP));
                finish();
            }
        });
        b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mHandled = true;
                scheduleDelete();
                Intent intent = new Intent(Intent.ACTION_SEND);
                ClipData clip = ClipData.newUri(getContentResolver(), "Heap Dump", JAVA_URI);
                intent.setClipData(clip);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setType(clip.getDescription().getMimeType(0));
                intent.putExtra(Intent.EXTRA_STREAM, JAVA_URI);
                startActivity(Intent.createChooser(intent,
                        getText(com.android.internal.R.string.dump_heap_title)));
                finish();
        }
        });
        mDialog = b.show();
    }

    void scheduleDelete() {
        Intent broadcast = new Intent(ACTION_DELETE_DUMPHEAP);
        broadcast.putExtra(EXTRA_DELAY_DELETE, true);
        sendBroadcast(broadcast);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (!isChangingConfigurations()) {
            if (!mHandled) {
                sendBroadcast(new Intent(ACTION_DELETE_DUMPHEAP));
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDialog.dismiss();
    }
}