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

Commit 4327dfc1 authored by George Lin's avatar George Lin
Browse files

Transfer loggers to Kotlin (3/3)

Test: Build succeeded
Bug: 299659307
Change-Id: Ie25e5d005432b455723b4e66f827b5b937d5c27c
parent 469383e2
Loading
Loading
Loading
Loading
+0 −212
Original line number Diff line number Diff line
/*
 * 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.customization.module.logging;

import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__ACTION__APP_LAUNCHED;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_CROP_AND_SET_ACTION;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_DEEP_LINK;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCHER;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCH_ICON;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS_SEARCH;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SUW;
import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_TIPS;
import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SETTINGS_SEARCH;
import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_DEEP_LINK;
import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_LAUNCHER;
import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SETTINGS;
import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SUW;
import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_TIPS;
import static com.android.wallpaper.util.LaunchSourceUtils.WALLPAPER_LAUNCH_SOURCE;

import android.app.WallpaperManager;
import android.content.Intent;
import android.stats.style.StyleEnums;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.android.customization.model.color.ColorOption;
import com.android.customization.model.grid.GridOption;
import com.android.customization.module.SysUiStatsLogger;
import com.android.wallpaper.module.WallpaperPreferences;
import com.android.wallpaper.module.WallpaperStatusChecker;
import com.android.wallpaper.module.logging.NoOpUserEventLogger;

/**
 * StatsLog-backed implementation of {@link ThemesUserEventLogger}.
 */
public class StatsLogUserEventLogger extends NoOpUserEventLogger implements ThemesUserEventLogger {

    private final WallpaperPreferences mPreferences;
    private final WallpaperStatusChecker mWallpaperStatusChecker;

    public StatsLogUserEventLogger(
            WallpaperPreferences preferences,
            WallpaperStatusChecker wallpaperStatusChecker) {
        mPreferences = preferences;
        mWallpaperStatusChecker = wallpaperStatusChecker;
    }

    @Override
    public void logAppLaunched(Intent launchSource) {
        new SysUiStatsLogger(STYLE_UICHANGED__ACTION__APP_LAUNCHED)
                .setLaunchedPreference(getAppLaunchSource(launchSource))
                .log();
    }

    @Override
    public void logActionClicked(String collectionId, int actionLabelResId) {
        new SysUiStatsLogger(StyleEnums.WALLPAPER_EXPLORE)
                .setWallpaperCategoryHash(getIdHashCode(collectionId))
                .log();
    }

    @Override
    public void logIndividualWallpaperSelected(String collectionId) {
        new SysUiStatsLogger(StyleEnums.WALLPAPER_SELECT)
                .setWallpaperCategoryHash(getIdHashCode(collectionId))
                .log();
    }

    @Override
    public void logCategorySelected(String collectionId) {
        new SysUiStatsLogger(StyleEnums.WALLPAPER_OPEN_CATEGORY)
                .setWallpaperCategoryHash(getIdHashCode(collectionId))
                .log();
    }

    @Override
    public void logSnapshot() {
        final boolean isLockWallpaperSet = mWallpaperStatusChecker.isLockWallpaperSet();
        final String homeCollectionId = mPreferences.getHomeWallpaperCollectionId();
        final String homeRemoteId = mPreferences.getHomeWallpaperRemoteId();
        final String effects = mPreferences.getHomeWallpaperEffects();
        String homeWallpaperId = TextUtils.isEmpty(homeRemoteId)
                ? mPreferences.getHomeWallpaperServiceName() : homeRemoteId;
        String lockCollectionId = isLockWallpaperSet ? mPreferences.getLockWallpaperCollectionId()
                : homeCollectionId;
        String lockWallpaperId = isLockWallpaperSet ? mPreferences.getLockWallpaperRemoteId()
                : homeWallpaperId;

        new SysUiStatsLogger(StyleEnums.SNAPSHOT)
                .setWallpaperCategoryHash(getIdHashCode(homeCollectionId))
                .setWallpaperIdHash(getIdHashCode(homeWallpaperId))
                .setLockWallpaperCategoryHash(getIdHashCode(lockCollectionId))
                .setLockWallpaperIdHash(getIdHashCode(lockWallpaperId))
                .setFirstLaunchDateSinceSetup(mPreferences.getFirstLaunchDateSinceSetup())
                .setFirstWallpaperApplyDateSinceSetup(
                        mPreferences.getFirstWallpaperApplyDateSinceSetup())
                .setAppLaunchCount(mPreferences.getAppLaunchCount())
                .setEffectIdHash(getIdHashCode(effects))
                .log();
    }

    @Override
    public void logWallpaperSet(String collectionId, @Nullable String wallpaperId,
            @Nullable String effects) {
        new SysUiStatsLogger(StyleEnums.WALLPAPER_APPLIED)
                .setWallpaperCategoryHash(getIdHashCode(collectionId))
                .setWallpaperIdHash(getIdHashCode(wallpaperId))
                .setEffectIdHash(getIdHashCode(effects))
                .log();
    }

    @Override
    public void logEffectApply(String effect, @EffectStatus int status, long timeElapsedMillis,
            int resultCode) {
        new SysUiStatsLogger(StyleEnums.WALLPAPER_EFFECT_APPLIED)
                .setEffectPreference(status)
                .setEffectIdHash(getIdHashCode(effect))
                .setTimeElapsed(timeElapsedMillis)
                .setEffectResultCode(resultCode)
                .log();
    }

    @Override
    public void logEffectProbe(String effect, @EffectStatus int status) {
        new SysUiStatsLogger(StyleEnums.WALLPAPER_EFFECT_PROBE)
                .setEffectPreference(status)
                .setEffectIdHash(getIdHashCode(effect))
                .log();
    }

    @Override
    public void logEffectForegroundDownload(String effect, @EffectStatus int status,
            long timeElapsedMillis) {
        new SysUiStatsLogger(StyleEnums.WALLPAPER_EFFECT_FG_DOWNLOAD)
                .setEffectPreference(status)
                .setEffectIdHash(getIdHashCode(effect))
                .setTimeElapsed(timeElapsedMillis)
                .log();
    }

    @Override
    public void logColorApplied(int action, ColorOption colorOption) {
        new SysUiStatsLogger(action)
                .setColorPreference(colorOption.getIndex())
                .setColorVariant(colorOption.getStyle().ordinal() + 1)
                .log();
    }

    @Override
    public void logGridSelected(GridOption grid) {
        new SysUiStatsLogger(StyleEnums.PICKER_SELECT)
                .setLauncherGrid(grid.cols)
                .log();
    }

    @Override
    public void logGridApplied(GridOption grid) {
        new SysUiStatsLogger(StyleEnums.PICKER_APPLIED)
                .setLauncherGrid(grid.cols)
                .log();
    }

    private int getAppLaunchSource(Intent launchSource) {
        if (launchSource.hasExtra(WALLPAPER_LAUNCH_SOURCE)) {
            switch (launchSource.getStringExtra(WALLPAPER_LAUNCH_SOURCE)) {
                case LAUNCH_SOURCE_LAUNCHER:
                    return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCHER;
                case LAUNCH_SOURCE_SETTINGS:
                    return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS;
                case LAUNCH_SOURCE_SUW:
                    return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SUW;
                case LAUNCH_SOURCE_TIPS:
                    return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_TIPS;
                case LAUNCH_SOURCE_DEEP_LINK:
                    return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_DEEP_LINK;
                default:
                    return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED;
            }
        } else if (launchSource.hasExtra(LAUNCH_SETTINGS_SEARCH)) {
            return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS_SEARCH;
        } else if (launchSource.getAction() != null && launchSource.getAction().equals(
                WallpaperManager.ACTION_CROP_AND_SET_WALLPAPER)) {
            return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_CROP_AND_SET_ACTION;
        } else if (launchSource.getCategories() != null
                && launchSource.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
            return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCH_ICON;
        } else {
            return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED;
        }
    }

    private int getIdHashCode(String id) {
        return id != null ? id.hashCode() : 0;
    }
}
+183 −0
Original line number Diff line number Diff line
/*
 * 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.customization.module.logging

import android.app.WallpaperManager
import android.content.Intent
import android.stats.style.StyleEnums
import android.text.TextUtils
import com.android.customization.model.color.ColorOption
import com.android.customization.model.grid.GridOption
import com.android.customization.module.SysUiStatsLogger
import com.android.systemui.shared.system.SysUiStatsLog
import com.android.wallpaper.module.WallpaperPreferences
import com.android.wallpaper.module.WallpaperStatusChecker
import com.android.wallpaper.module.logging.NoOpUserEventLogger
import com.android.wallpaper.module.logging.UserEventLogger.Companion.EffectStatus
import com.android.wallpaper.util.LaunchSourceUtils

/** StatsLog-backed implementation of [ThemesUserEventLogger]. */
class StatsLogUserEventLogger(
    private val preferences: WallpaperPreferences,
    private val wallpaperStatusChecker: WallpaperStatusChecker
) : NoOpUserEventLogger(), ThemesUserEventLogger {

    override fun logAppLaunched(launchSource: Intent) {
        SysUiStatsLogger(SysUiStatsLog.STYLE_UICHANGED__ACTION__APP_LAUNCHED)
            .setLaunchedPreference(getAppLaunchSource(launchSource))
            .log()
    }

    override fun logActionClicked(collectionId: String, actionLabelResId: Int) {
        SysUiStatsLogger(StyleEnums.WALLPAPER_EXPLORE)
            .setWallpaperCategoryHash(getIdHashCode(collectionId))
            .log()
    }

    override fun logIndividualWallpaperSelected(collectionId: String) {
        SysUiStatsLogger(StyleEnums.WALLPAPER_SELECT)
            .setWallpaperCategoryHash(getIdHashCode(collectionId))
            .log()
    }

    override fun logCategorySelected(collectionId: String) {
        SysUiStatsLogger(StyleEnums.WALLPAPER_OPEN_CATEGORY)
            .setWallpaperCategoryHash(getIdHashCode(collectionId))
            .log()
    }

    override fun logSnapshot() {
        val isLockWallpaperSet = wallpaperStatusChecker.isLockWallpaperSet()
        val homeCollectionId = preferences.homeWallpaperCollectionId
        val homeRemoteId = preferences.homeWallpaperRemoteId
        val effects = preferences.homeWallpaperEffects
        val homeWallpaperId =
            if (TextUtils.isEmpty(homeRemoteId)) preferences.homeWallpaperServiceName
            else homeRemoteId
        val lockCollectionId =
            if (isLockWallpaperSet) preferences.lockWallpaperCollectionId else homeCollectionId
        val lockWallpaperId =
            if (isLockWallpaperSet) preferences.lockWallpaperRemoteId else homeWallpaperId
        SysUiStatsLogger(StyleEnums.SNAPSHOT)
            .setWallpaperCategoryHash(getIdHashCode(homeCollectionId))
            .setWallpaperIdHash(getIdHashCode(homeWallpaperId))
            .setLockWallpaperCategoryHash(getIdHashCode(lockCollectionId))
            .setLockWallpaperIdHash(getIdHashCode(lockWallpaperId))
            .setFirstLaunchDateSinceSetup(preferences.firstLaunchDateSinceSetup)
            .setFirstWallpaperApplyDateSinceSetup(preferences.firstWallpaperApplyDateSinceSetup)
            .setAppLaunchCount(preferences.appLaunchCount)
            .setEffectIdHash(getIdHashCode(effects))
            .log()
    }

    override fun logWallpaperSet(
        collectionId: String?,
        wallpaperId: String?,
        effects: String?,
    ) {
        SysUiStatsLogger(StyleEnums.WALLPAPER_APPLIED)
            .setWallpaperCategoryHash(getIdHashCode(collectionId))
            .setWallpaperIdHash(getIdHashCode(wallpaperId))
            .setEffectIdHash(getIdHashCode(effects))
            .log()
    }

    override fun logEffectApply(
        effect: String,
        @EffectStatus status: Int,
        timeElapsedMillis: Long,
        resultCode: Int
    ) {
        SysUiStatsLogger(StyleEnums.WALLPAPER_EFFECT_APPLIED)
            .setEffectPreference(status)
            .setEffectIdHash(getIdHashCode(effect))
            .setTimeElapsed(timeElapsedMillis)
            .setEffectResultCode(resultCode)
            .log()
    }

    override fun logEffectProbe(effect: String, @EffectStatus status: Int) {
        SysUiStatsLogger(StyleEnums.WALLPAPER_EFFECT_PROBE)
            .setEffectPreference(status)
            .setEffectIdHash(getIdHashCode(effect))
            .log()
    }

    override fun logEffectForegroundDownload(
        effect: String,
        @EffectStatus status: Int,
        timeElapsedMillis: Long
    ) {
        SysUiStatsLogger(StyleEnums.WALLPAPER_EFFECT_FG_DOWNLOAD)
            .setEffectPreference(status)
            .setEffectIdHash(getIdHashCode(effect))
            .setTimeElapsed(timeElapsedMillis)
            .log()
    }

    override fun logColorApplied(action: Int, colorOption: ColorOption) {
        SysUiStatsLogger(action)
            .setColorPreference(colorOption.index)
            .setColorVariant(colorOption.style.ordinal + 1)
            .log()
    }

    override fun logGridSelected(grid: GridOption) {
        SysUiStatsLogger(StyleEnums.PICKER_SELECT).setLauncherGrid(grid.cols).log()
    }

    override fun logGridApplied(grid: GridOption) {
        SysUiStatsLogger(StyleEnums.PICKER_APPLIED).setLauncherGrid(grid.cols).log()
    }

    private fun getAppLaunchSource(launchSource: Intent): Int {
        return if (launchSource.hasExtra(LaunchSourceUtils.WALLPAPER_LAUNCH_SOURCE)) {
            when (launchSource.getStringExtra(LaunchSourceUtils.WALLPAPER_LAUNCH_SOURCE)) {
                LaunchSourceUtils.LAUNCH_SOURCE_LAUNCHER ->
                    SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCHER
                LaunchSourceUtils.LAUNCH_SOURCE_SETTINGS ->
                    SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS
                LaunchSourceUtils.LAUNCH_SOURCE_SUW ->
                    SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SUW
                LaunchSourceUtils.LAUNCH_SOURCE_TIPS ->
                    SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_TIPS
                LaunchSourceUtils.LAUNCH_SOURCE_DEEP_LINK ->
                    SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_DEEP_LINK
                else ->
                    SysUiStatsLog
                        .STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED
            }
        } else if (launchSource.hasExtra(LaunchSourceUtils.LAUNCH_SETTINGS_SEARCH)) {
            SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS_SEARCH
        } else if (
            launchSource.action != null &&
                launchSource.action == WallpaperManager.ACTION_CROP_AND_SET_WALLPAPER
        ) {
            SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_CROP_AND_SET_ACTION
        } else if (
            launchSource.categories != null &&
                launchSource.categories.contains(Intent.CATEGORY_LAUNCHER)
        ) {
            SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCH_ICON
        } else {
            SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED
        }
    }

    private fun getIdHashCode(id: String?): Int {
        return id?.hashCode() ?: 0
    }
}
+11 −15
Original line number Diff line number Diff line
@@ -13,29 +13,25 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.customization.module.logging;
package com.android.customization.module.logging

import com.android.customization.model.color.ColorOption;
import com.android.customization.model.grid.GridOption;
import com.android.wallpaper.module.logging.UserEventLogger;

/**
 * Extension of {@link UserEventLogger} that adds ThemePicker specific events.
 */
public interface ThemesUserEventLogger extends UserEventLogger {
import com.android.customization.model.color.ColorOption
import com.android.customization.model.grid.GridOption
import com.android.wallpaper.module.logging.UserEventLogger

/** Extension of [UserEventLogger] that adds ThemePicker specific events. */
interface ThemesUserEventLogger : UserEventLogger {
    /**
     * Logs the color usage while color is applied.
     *
     * @param action color applied action.
     * @param colorOption applied color option.
     */
    void logColorApplied(int action, ColorOption colorOption);
    fun logColorApplied(action: Int, colorOption: ColorOption)

    /**  */
    void logGridSelected(GridOption grid);
    fun logGridSelected(grid: GridOption)

    /**  */
    void logGridApplied(GridOption grid);

    fun logGridApplied(grid: GridOption)
}
+31 −0
Original line number Diff line number Diff line
@@ -13,30 +13,19 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.customization.testing;
package com.android.customization.testing

import com.android.customization.model.color.ColorOption;
import com.android.customization.model.grid.GridOption;
import com.android.customization.module.logging.ThemesUserEventLogger;
import com.android.wallpaper.testing.TestUserEventLogger;
import com.android.customization.model.color.ColorOption
import com.android.customization.model.grid.GridOption
import com.android.customization.module.logging.ThemesUserEventLogger
import com.android.wallpaper.testing.TestUserEventLogger

/**
 * Test implementation of {@link ThemesUserEventLogger}.
 */
public class TestThemesUserEventLogger extends TestUserEventLogger
        implements ThemesUserEventLogger {
    @Override
    public void logColorApplied(int action, ColorOption colorOption) {
        // Do nothing.
    }
/** Test implementation of [ThemesUserEventLogger]. */
class TestThemesUserEventLogger : TestUserEventLogger(), ThemesUserEventLogger {

    @Override
    public void logGridSelected(GridOption grid) {
        // Do nothing.
    }
    override fun logColorApplied(action: Int, colorOption: ColorOption) {}

    @Override
    public void logGridApplied(GridOption grid) {
        // Do nothing.
    }
    override fun logGridSelected(grid: GridOption) {}

    override fun logGridApplied(grid: GridOption) {}
}