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

Commit 72be2e11 authored by Abhijeet Kaur's avatar Abhijeet Kaur
Browse files

Remove unused broadcasts/functions/variables from Shell

Bugreport is now triggered using API and not via broadcasts from
dumpstate. As migration to API flow is stable, we can remove methods and
broadcasts that were used in non-API bugreport flow. Now, callbacks are used
for communication between dumpsate and Shell instead of broadcasts.

* Remove BugreportReceiver.java as it used to handle BUGREPORT_STARTED and
  BUGREPORT_FINISHED intents which are not required by Shell anymore to
  track bugreports.
* Remove RemoteBugreportReceiver.java as it used to handle
  REMOTE_BUGREPORT_FINISHED intent which is not required by Shell
  anymore.
* Remove methods that were being used when processing these broadcasts.
* Remove pid as that is not used anymore.
* Since Shell owns the file now, don't need mTempName, mSavedName,
  onFocusListener and disable file name after the bugreport is finished.
  File name can be sanitized and updated even after bugreport finished.

Bug: 136066578
Test: Build and flash. Interactive/Full bugreports from Settings/Power button.
Test: Rename workflow for interactive bugreports works as expected.
Test: * Build and flash
      * Install TestDPC
      * Make it device owner
      * Take a remote bugreport. Works as expected

Change-Id: I5e0f829631cb63074c41c914236e78abe95ba162
parent f6fb7063
Loading
Loading
Loading
Loading
+0 −17
Original line number Diff line number Diff line
@@ -246,23 +246,6 @@
            android:excludeFromRecents="true"
            android:exported="false" />

        <receiver
            android:name=".BugreportReceiver"
            android:permission="android.permission.DUMP">
            <intent-filter>
                <action android:name="com.android.internal.intent.action.BUGREPORT_STARTED" />
                <action android:name="com.android.internal.intent.action.BUGREPORT_FINISHED" />
            </intent-filter>
        </receiver>

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

        <receiver
            android:name=".BugreportRequestedReceiver"
            android:permission="android.permission.TRIGGER_SHELL_BUGREPORT">
+73 −430

File changed.

Preview size limit exceeded, changes collapsed.

+0 −88
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 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.EXTRA_ORIGINAL_INTENT;
import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED;
import static com.android.shell.BugreportProgressService.getFileExtra;
import static com.android.shell.BugreportProgressService.dumpIntent;

import java.io.File;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.FileUtils;
import android.text.format.DateUtils;
import android.util.Log;

/**
 * Receiver that handles finished bugreports, usually by attaching them to an
 * {@link Intent#ACTION_SEND_MULTIPLE}.
 */
public class BugreportReceiver extends BroadcastReceiver {
    private static final String TAG = "BugreportReceiver";

    /**
     * Always keep the newest 8 bugreport files.
     */
    private static final int MIN_KEEP_COUNT = 8;

    /**
     * Always keep bugreports taken in the last week.
     */
    private static final long MIN_KEEP_AGE = DateUtils.WEEK_IN_MILLIS;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive(): " + dumpIntent(intent));
        // Clean up older bugreports in background
        cleanupOldFiles(this, intent, INTENT_BUGREPORT_FINISHED, MIN_KEEP_COUNT, MIN_KEEP_AGE);

        // Delegate intent handling to service.
        Intent serviceIntent = new Intent(context, BugreportProgressService.class);
        serviceIntent.putExtra(EXTRA_ORIGINAL_INTENT, intent);
        context.startService(serviceIntent);
    }

    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);
        if (bugreportFile == null || !bugreportFile.exists()) {
            Log.e(TAG, "Not deleting old files because file " + bugreportFile + " doesn't exist");
            return;
        }
        final PendingResult result = br.goAsync();
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    FileUtils.deleteOlderFiles(bugreportFile.getParentFile(), minCount, minAge);
                } catch (RuntimeException e) {
                    Log.e(TAG, "RuntimeException deleting old files", e);
                }
                result.finish();
                return null;
            }
        }.execute();
    }
}
+0 −67
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.getFileExtra;
import static com.android.shell.BugreportProgressService.getUri;
import static com.android.shell.BugreportReceiver.cleanupOldFiles;

import java.io.File;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.UserHandle;
import android.text.format.DateUtils;

/**
 * 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";

    /** Always keep just the last remote bugreport's files around. */
    private static final int REMOTE_BUGREPORT_FILES_AMOUNT = 3;

    /** Always keep remote bugreport files created in the last day. */
    private static final long MIN_KEEP_AGE = DateUtils.DAY_IN_MILLIS;

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

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

        final Intent newIntent = new Intent(DevicePolicyManager.ACTION_REMOTE_BUGREPORT_DISPATCH);
        newIntent.setDataAndType(bugreportUri, BUGREPORT_MIMETYPE);
        newIntent.putExtra(DevicePolicyManager.EXTRA_REMOTE_BUGREPORT_HASH, bugreportHash);
        context.sendBroadcastAsUser(newIntent, UserHandle.SYSTEM,
                android.Manifest.permission.DUMP);
    }
}
+4 −2
Original line number Diff line number Diff line
@@ -29,10 +29,8 @@ import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT;
import static com.android.shell.BugreportProgressService.EXTRA_ID;
import static com.android.shell.BugreportProgressService.EXTRA_MAX;
import static com.android.shell.BugreportProgressService.EXTRA_NAME;
import static com.android.shell.BugreportProgressService.EXTRA_PID;
import static com.android.shell.BugreportProgressService.EXTRA_SCREENSHOT;
import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED;
import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_STARTED;
import static com.android.shell.BugreportProgressService.SCREENSHOT_DELAY_SECONDS;

import static org.junit.Assert.assertEquals;
@@ -145,6 +143,10 @@ public class BugreportReceiverTest {
    private static final String TITLE2 = "Master of the Universe";
    private static final String DESCRIPTION = "One's description...";
    private static final String DESCRIPTION2 = "...is another's treasure.";
    // TODO(b/143130523): Fix (update) tests and add to presubmit
    private static final String EXTRA_PID = "android.intent.extra.PID";
    private static final String INTENT_BUGREPORT_STARTED =
            "com.android.internal.intent.action.BUGREPORT_STARTED";

    private static final String NO_DESCRIPTION = null;
    private static final String NO_NAME = null;