Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 226940ed authored by Michal Karpinski's avatar Michal Karpinski
Browse files

Modifying Shell to accomodate remote bugreports

After receiving android.intent.action.REMOTE_BUGREPORT_FINISHED
in newly created RemoteBugreportReceiver, Shell will generate URI
to the bugreport zip file and send the broadcast
android.intent.action.REMOTE_BUGREPORT_DISPATCH.

Bug: 26152603
Change-Id: I058d626e021b488c9347b45467a4e3505134e79c
parent 8c04e18c
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -277,8 +277,10 @@
    <protected-broadcast android:name="android.intent.action.AIRPLANE_MODE" />
    <protected-broadcast android:name="android.intent.action.ADVANCED_SETTINGS" />
    <protected-broadcast android:name="android.intent.action.APPLICATION_RESTRICTIONS_CHANGED" />
    <protected-broadcast android:name="android.intent.action.BUGREPORT_FINISHED" />
    <protected-broadcast android:name="android.intent.action.BUGREPORT_STARTED" />
    <protected-broadcast android:name="android.intent.action.BUGREPORT_FINISHED" />
    <protected-broadcast android:name="android.intent.action.REMOTE_BUGREPORT_FINISHED" />
    <protected-broadcast android:name="android.intent.action.REMOTE_BUGREPORT_DISPATCH" />

    <protected-broadcast android:name="android.intent.action.ACTION_IDLE_MAINTENANCE_START" />
    <protected-broadcast android:name="android.intent.action.ACTION_IDLE_MAINTENANCE_END" />
+8 −0
Original line number Diff line number Diff line
@@ -151,6 +151,14 @@
            </intent-filter>
        </receiver>

        <receiver
            android:name=".RemoteBugreportReceiver"
            android:permission="android.permission.DUMP">
            <intent-filter>
                <action android:name="android.intent.action.REMOTE_BUGREPORT_FINISHED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".BugreportProgressService"
            android:exported="false"/>
+5 −1
Original line number Diff line number Diff line
@@ -116,6 +116,10 @@ public class BugreportProgressService extends Service {
    // External intents sent by dumpstate.
    static final String INTENT_BUGREPORT_STARTED = "android.intent.action.BUGREPORT_STARTED";
    static final String INTENT_BUGREPORT_FINISHED = "android.intent.action.BUGREPORT_FINISHED";
    static final String INTENT_REMOTE_BUGREPORT_FINISHED =
            "android.intent.action.REMOTE_BUGREPORT_FINISHED";
    static final String INTENT_REMOTE_BUGREPORT_DISPATCH =
            "android.intent.action.REMOTE_BUGREPORT_DISPATCH";

    // Internal intents used on notification actions.
    static final String INTENT_BUGREPORT_CANCEL = "android.intent.action.BUGREPORT_CANCEL";
@@ -1005,7 +1009,7 @@ public class BugreportProgressService extends Service {
        return foundAccount;
    }

    private static Uri getUri(Context context, File file) {
    static Uri getUri(Context context, File file) {
        return file != null ? FileProvider.getUriForFile(context, AUTHORITY, file) : null;
    }

+6 −6
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ public class BugreportReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Clean up older bugreports in background
        cleanupOldFiles(intent);
        cleanupOldFiles(this, intent, INTENT_BUGREPORT_FINISHED, MIN_KEEP_COUNT, MIN_KEEP_AGE);

        // Delegate intent handling to service.
        Intent serviceIntent = new Intent(context, BugreportProgressService.class);
@@ -60,8 +60,9 @@ public class BugreportReceiver extends BroadcastReceiver {
        context.startService(serviceIntent);
    }

    private void cleanupOldFiles(Intent intent) {
        if (!INTENT_BUGREPORT_FINISHED.equals(intent.getAction())) {
    static void cleanupOldFiles(BroadcastReceiver br, Intent intent, String expectedAction,
            final int minCount, final long minAge) {
        if (!expectedAction.equals(intent.getAction())) {
            return;
        }
        final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
@@ -69,12 +70,11 @@ public class BugreportReceiver extends BroadcastReceiver {
            Log.e(TAG, "Not deleting old files because file " + bugreportFile + " doesn't exist");
            return;
        }
        final PendingResult result = goAsync();
        final PendingResult result = br.goAsync();
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                FileUtils.deleteOlderFiles(
                        bugreportFile.getParentFile(), MIN_KEEP_COUNT, MIN_KEEP_AGE);
                FileUtils.deleteOlderFiles(bugreportFile.getParentFile(), minCount, minAge);
                result.finish();
                return null;
            }
+63 −0
Original line number Diff line number Diff line
/*
 * 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.shell;

import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT;
import static com.android.shell.BugreportProgressService.INTENT_REMOTE_BUGREPORT_FINISHED;
import static com.android.shell.BugreportProgressService.INTENT_REMOTE_BUGREPORT_DISPATCH;
import static com.android.shell.BugreportProgressService.getFileExtra;
import static com.android.shell.BugreportProgressService.getUri;
import static com.android.shell.BugreportReceiver.cleanupOldFiles;

import java.io.File;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.UserHandle;

/**
 * Receiver that handles finished remote bugreports, by re-sending
 * the intent with appended bugreport zip file URI.
 *
 * <p> Remote bugreport never contains a screenshot.
 */
public class RemoteBugreportReceiver extends BroadcastReceiver {

    private static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport";
    private static final String EXTRA_REMOTE_BUGREPORT_HASH =
            "android.intent.extra.REMOTE_BUGREPORT_HASH";

    /** Always keep just the last remote bugreport zip file */
    private static final int MIN_KEEP_COUNT = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        cleanupOldFiles(this, intent, INTENT_REMOTE_BUGREPORT_FINISHED, MIN_KEEP_COUNT, 0);

        final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
        final Uri bugreportUri = getUri(context, bugreportFile);
        final String bugreportHash = intent.getStringExtra(EXTRA_REMOTE_BUGREPORT_HASH);

        final Intent newIntent = new Intent(INTENT_REMOTE_BUGREPORT_DISPATCH);
        newIntent.setDataAndType(bugreportUri, BUGREPORT_MIMETYPE);
        newIntent.putExtra(EXTRA_REMOTE_BUGREPORT_HASH, bugreportHash);
        context.sendBroadcastAsUser(newIntent, UserHandle.SYSTEM,
                android.Manifest.permission.DUMP);
    }
}