summaryrefslogtreecommitdiff
path: root/packages/Shell/src/com/android/shell/HeapDumpActivity.java
blob: 0ff0d3353041c324ad7ddd10530870dce7fe9ab0 (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
138
139
140
141
142
/*
 * Copyright (C) 2019 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.shell;

import static com.android.shell.HeapDumpProvider.makeUri;
import static com.android.shell.HeapDumpReceiver.ACTION_DELETE_HEAP_DUMP;
import static com.android.shell.HeapDumpReceiver.EXTRA_IS_USER_INITIATED;
import static com.android.shell.HeapDumpReceiver.EXTRA_PROCESS_NAME;
import static com.android.shell.HeapDumpReceiver.EXTRA_REPORT_PACKAGE;
import static com.android.shell.HeapDumpReceiver.EXTRA_SIZE_BYTES;

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

import com.android.internal.R;

/**
 * This activity is displayed when the system has collected a heap dump.
 */
public class HeapDumpActivity extends Activity {
    private static final String TAG = "HeapDumpActivity";

    static final String KEY_URI = "uri";

    private AlertDialog mDialog;
    private Uri mDumpUri;
    private boolean mHandled = false;

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

        String process = getIntent().getStringExtra(EXTRA_PROCESS_NAME);
        long size = getIntent().getLongExtra(EXTRA_SIZE_BYTES, 0);
        final boolean isUserInitiated = getIntent().getBooleanExtra(EXTRA_IS_USER_INITIATED, false);
        final int uid = getIntent().getIntExtra(Intent.EXTRA_UID, 0);
        final boolean isSystemProcess = uid == Process.SYSTEM_UID;
        mDumpUri = makeUri(process);
        final String procDisplayName = isSystemProcess
                ? getString(com.android.internal.R.string.android_system_label)
                : process;

        final Intent sendIntent = new Intent();
        ClipData clip = ClipData.newUri(getContentResolver(), "Heap Dump", mDumpUri);
        sendIntent.setClipData(clip);
        sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        sendIntent.setType(clip.getDescription().getMimeType(0));
        sendIntent.putExtra(Intent.EXTRA_STREAM, mDumpUri);

        String directLaunchPackage = getIntent().getStringExtra(EXTRA_REPORT_PACKAGE);
        if (directLaunchPackage != null) {
            sendIntent.setAction(ActivityManager.ACTION_REPORT_HEAP_LIMIT);
            sendIntent.setPackage(directLaunchPackage);
            try {
                startActivity(sendIntent);
                mHandled = true;
                finish();
                return;
            } catch (ActivityNotFoundException e) {
                Log.e(TAG, "Unable to direct launch to " + directLaunchPackage, e);
            }
        }

        final int messageId;
        if (isUserInitiated) {
            messageId = com.android.internal.R.string.dump_heap_ready_text;
        } else if (isSystemProcess) {
            messageId = com.android.internal.R.string.dump_heap_system_text;
        } else {
            messageId = com.android.internal.R.string.dump_heap_text;
        }
        mDialog = new AlertDialog.Builder(this, android.R.style.Theme_Material_Light_Dialog_Alert)
                .setTitle(com.android.internal.R.string.dump_heap_title)
                .setMessage(getString(messageId, procDisplayName,
                        DebugUtils.sizeValueToString(size, null)))
                .setNegativeButton(android.R.string.cancel, (dialog, which) -> {
                    mHandled = true;
                    finish();
                })
                .setNeutralButton(R.string.delete, (dialog, which) -> {
                    mHandled = true;
                    Intent deleteIntent = new Intent(ACTION_DELETE_HEAP_DUMP);
                    deleteIntent.setClass(getApplicationContext(), HeapDumpReceiver.class);
                    deleteIntent.putExtra(KEY_URI, mDumpUri.toString());
                    sendBroadcast(deleteIntent);
                    finish();
                })
                .setPositiveButton(android.R.string.ok, (dialog, which) -> {
                    mHandled = true;
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.setPackage(null);
                    startActivity(Intent.createChooser(sendIntent,
                            getText(com.android.internal.R.string.dump_heap_title)));
                    finish();
                })
                .show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (!isChangingConfigurations()) {
            if (!mHandled) {
                Intent deleteIntent = new Intent(ACTION_DELETE_HEAP_DUMP);
                deleteIntent.setClass(getApplicationContext(), HeapDumpReceiver.class);
                deleteIntent.putExtra(KEY_URI, mDumpUri.toString());
                sendBroadcast(deleteIntent);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }
}