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

Commit b331d1f1 authored by Bernardo Rufino's avatar Bernardo Rufino Committed by Android (Google) Code Review
Browse files

Merge "Add local transport parameters for transport flags GTS test"

parents e99445f8 eaa78b92
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -163,6 +163,16 @@ public abstract class BackupAgent extends ContextWrapper {
     */
    public static final int FLAG_DEVICE_TO_DEVICE_TRANSFER = 2;

    /**
     * Flag for {@link BackupDataOutput#getTransportFlags()} and
     * {@link FullBackupDataOutput#getTransportFlags()} only.
     *
     * <p>Used for internal testing only. Do not check this flag in production code.
     *
     * @hide
     */
    public static final int FLAG_FAKE_CLIENT_SIDE_ENCRYPTION_ENABLED = 1 << 31;

    Handler mHandler = null;

    Handler getHandler() {
+18 −0
Original line number Diff line number Diff line
@@ -7662,6 +7662,24 @@ public final class Settings {
         */
        public static final String BACKUP_MANAGER_CONSTANTS = "backup_manager_constants";


        /**
         * Local transport parameters so we can configure it for tests.
         * This is encoded as a key=value list, separated by commas.
         *
         * The following keys are supported:
         *
         * <pre>
         * fake_encryption_flag  (boolean)
         * </pre>
         *
         * <p>
         * Type: string
         * @hide
         */
        public static final String BACKUP_LOCAL_TRANSPORT_PARAMETERS =
                "backup_local_transport_parameters";

        /**
         * Flag to set if the system should predictively attempt to re-enable Bluetooth while
         * the user is driving.
+19 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.internal.backup;

import android.app.backup.BackupAgent;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.BackupTransport;
@@ -98,6 +99,7 @@ public class LocalTransport extends BackupTransport {
    private FileInputStream mCurFullRestoreStream;
    private FileOutputStream mFullRestoreSocketStream;
    private byte[] mFullRestoreBuffer;
    private final LocalTransportParameters mParameters;

    private void makeDataDirs() {
        mCurrentSetDir.mkdirs();
@@ -105,11 +107,16 @@ public class LocalTransport extends BackupTransport {
        mCurrentSetIncrementalDir.mkdir();
    }

    public LocalTransport(Context context) {
    public LocalTransport(Context context, LocalTransportParameters parameters) {
        mContext = context;
        mParameters = parameters;
        makeDataDirs();
    }

    LocalTransportParameters getParameters() {
        return mParameters;
    }

    @Override
    public String name() {
        return new ComponentName(mContext, this.getClass()).flattenToShortString();
@@ -142,6 +149,17 @@ public class LocalTransport extends BackupTransport {
        return TRANSPORT_DIR_NAME;
    }

    @Override
    public int getTransportFlags() {
        int flags = super.getTransportFlags();
        // Testing for a fake flag and having it set as a boolean in settings prevents anyone from
        // using this it to pull data from the agent
        if (mParameters.isFakeEncryptionFlag()) {
            flags |= BackupAgent.FLAG_FAKE_CLIENT_SIDE_ENCRYPTION_ENABLED;
        }
        return flags;
    }

    @Override
    public long requestBackupTime() {
        // any time is a good time for local backup
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.backup;

import android.content.ContentResolver;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;
import android.util.KeyValueListParser;
import android.util.Slog;

class LocalTransportParameters {
    private static final String TAG = "LocalTransportParams";
    private static final String SETTING = Settings.Secure.BACKUP_LOCAL_TRANSPORT_PARAMETERS;
    private static final String KEY_FAKE_ENCRYPTION_FLAG = "fake_encryption_flag";

    private final KeyValueListParser mParser = new KeyValueListParser(',');
    private final ContentObserver mObserver;
    private final ContentResolver mResolver;
    private boolean mFakeEncryptionFlag;

    LocalTransportParameters(Handler handler, ContentResolver resolver) {
        mObserver = new Observer(handler);
        mResolver = resolver;
    }

    /** Observes for changes in the setting. This method MUST be paired with {@link #stop()}. */
    void start() {
        mResolver.registerContentObserver(Settings.Secure.getUriFor(SETTING), false, mObserver);
        update();
    }

    /** Stop observing for changes in the setting. */
    void stop() {
        mResolver.unregisterContentObserver(mObserver);
    }

    boolean isFakeEncryptionFlag() {
        return mFakeEncryptionFlag;
    }

    private void update() {
        String parameters = "";
        try {
            parameters = Settings.Secure.getString(mResolver, SETTING);
        } catch (IllegalArgumentException e) {
            Slog.e(TAG, "Malformed " + SETTING + " setting: " + e.getMessage());
        }
        mParser.setString(parameters);
        mFakeEncryptionFlag = mParser.getBoolean(KEY_FAKE_ENCRYPTION_FLAG, false);
    }

    private class Observer extends ContentObserver {
        private Observer(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            update();
        }
    }
}
+9 −1
Original line number Diff line number Diff line
@@ -26,8 +26,16 @@ public class LocalTransportService extends Service {
    @Override
    public void onCreate() {
        if (sTransport == null) {
            sTransport = new LocalTransport(this);
            LocalTransportParameters parameters =
                    new LocalTransportParameters(getMainThreadHandler(), getContentResolver());
            sTransport = new LocalTransport(this, parameters);
        }
        sTransport.getParameters().start();
    }

    @Override
    public void onDestroy() {
        sTransport.getParameters().stop();
    }

    @Override
Loading