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

Commit 57cb73f2 authored by Chris Göllner's avatar Chris Göllner Committed by Android (Google) Code Review
Browse files

Merge "[MediaProjection] Create MediaProjectionSessionIdGenerator" into main

parents 55f12409 c464ed27
Loading
Loading
Loading
Loading
+91 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.server.media.projection;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.io.File;

public class MediaProjectionSessionIdGenerator {

    private static final String PREFERENCES_FILE_NAME = "media_projection_session_id";
    private static final String SESSION_ID_PREF_KEY = "media_projection_session_id_key";
    private static final int SESSION_ID_DEFAULT_VALUE = 0;

    private static final Object sInstanceLock = new Object();

    @GuardedBy("sInstanceLock")
    private static MediaProjectionSessionIdGenerator sInstance;

    private final Object mSessionIdLock = new Object();

    @GuardedBy("mSessionIdLock")
    private final SharedPreferences mSharedPreferences;

    /** Creates or returns an existing instance of {@link MediaProjectionSessionIdGenerator}. */
    public static MediaProjectionSessionIdGenerator getInstance(Context context) {
        synchronized (sInstanceLock) {
            if (sInstance == null) {
                File preferencesFile =
                        new File(Environment.getDataSystemDirectory(), PREFERENCES_FILE_NAME);
                SharedPreferences preferences =
                        context.getSharedPreferences(preferencesFile, Context.MODE_PRIVATE);
                sInstance = new MediaProjectionSessionIdGenerator(preferences);
            }
            return sInstance;
        }
    }

    @VisibleForTesting
    public MediaProjectionSessionIdGenerator(SharedPreferences sharedPreferences) {
        this.mSharedPreferences = sharedPreferences;
    }

    /** Returns the current session ID. This value is persisted across reboots. */
    public int getCurrentSessionId() {
        synchronized (mSessionIdLock) {
            return getCurrentSessionIdInternal();
        }
    }

    /**
     * Creates and returns a new session ID. This value will be persisted as the new current session
     * ID, and will be persisted across reboots.
     */
    public int createAndGetNewSessionId() {
        synchronized (mSessionIdLock) {
            int newSessionId = getCurrentSessionId() + 1;
            setSessionIdInternal(newSessionId);
            return newSessionId;
        }
    }

    @GuardedBy("mSessionIdLock")
    private void setSessionIdInternal(int value) {
        mSharedPreferences.edit().putInt(SESSION_ID_PREF_KEY, value).apply();
    }

    @GuardedBy("mSessionIdLock")
    private int getCurrentSessionIdInternal() {
        return mSharedPreferences.getInt(SESSION_ID_PREF_KEY, SESSION_ID_DEFAULT_VALUE);
    }
}
+103 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.server.media.projection;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.content.SharedPreferences;
import android.platform.test.annotations.Presubmit;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;

/**
 * Tests for the {@link MediaProjectionSessionIdGenerator} class.
 *
 * <p>Build/Install/Run: atest FrameworksServicesTests:MediaProjectionSessionIdGeneratorTest
 */
@SmallTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class MediaProjectionSessionIdGeneratorTest {

    private static final String TEST_PREFS_FILE = "media-projection-session-id-test";

    private final Context mContext =
            InstrumentationRegistry.getInstrumentation().getTargetContext();
    private final File mSharedPreferencesFile = new File(mContext.getCacheDir(), TEST_PREFS_FILE);
    private final SharedPreferences mSharedPreferences = createSharePreferences();
    private final MediaProjectionSessionIdGenerator mGenerator =
            createGenerator(mSharedPreferences);

    @Before
    public void setUp() {
        mSharedPreferences.edit().clear().commit();
    }

    @After
    public void tearDown() {
        mSharedPreferences.edit().clear().commit();
        mSharedPreferencesFile.delete();
    }

    @Test
    public void getCurrentSessionId_byDefault_returns0() {
        assertThat(mGenerator.getCurrentSessionId()).isEqualTo(0);
    }

    @Test
    public void getCurrentSessionId_multipleTimes_returnsSameValue() {
        assertThat(mGenerator.getCurrentSessionId()).isEqualTo(0);
        assertThat(mGenerator.getCurrentSessionId()).isEqualTo(0);
        assertThat(mGenerator.getCurrentSessionId()).isEqualTo(0);
    }

    @Test
    public void createAndGetNewSessionId_returnsIncrementedId() {
        int previousValue = mGenerator.getCurrentSessionId();

        int newValue = mGenerator.createAndGetNewSessionId();

        assertThat(newValue).isEqualTo(previousValue + 1);
    }

    @Test
    public void createAndGetNewSessionId_persistsNewValue() {
        int newValue = mGenerator.createAndGetNewSessionId();

        MediaProjectionSessionIdGenerator newInstance = createGenerator(createSharePreferences());

        assertThat(newInstance.getCurrentSessionId()).isEqualTo(newValue);
    }

    private SharedPreferences createSharePreferences() {
        return mContext.getSharedPreferences(mSharedPreferencesFile, Context.MODE_PRIVATE);
    }

    private MediaProjectionSessionIdGenerator createGenerator(SharedPreferences sharedPreferences) {
        return new MediaProjectionSessionIdGenerator(sharedPreferences);
    }
}