1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.customization.module;
17 
18 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_COLOR;
19 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_FONT;
20 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_SHAPE;
21 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__ACTION__APP_LAUNCHED;
22 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_CROP_AND_SET_ACTION;
23 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_DEEP_LINK;
24 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCHER;
25 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCH_ICON;
26 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED;
27 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS;
28 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS_SEARCH;
29 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SUW;
30 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_TIPS;
31 import static com.android.systemui.shared.system.SysUiStatsLog.STYLE_UI_CHANGED;
32 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SETTINGS_SEARCH;
33 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_DEEP_LINK;
34 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_LAUNCHER;
35 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SETTINGS;
36 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SUW;
37 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_TIPS;
38 import static com.android.wallpaper.util.LaunchSourceUtils.WALLPAPER_LAUNCH_SOURCE;
39 
40 import android.app.WallpaperManager;
41 import android.content.Intent;
42 import android.stats.style.StyleEnums;
43 
44 import androidx.annotation.Nullable;
45 
46 import com.android.customization.model.clock.Clockface;
47 import com.android.customization.model.grid.GridOption;
48 import com.android.customization.model.theme.ThemeBundle;
49 import com.android.systemui.shared.system.SysUiStatsLog;
50 import com.android.wallpaper.module.NoOpUserEventLogger;
51 
52 import java.util.Map;
53 import java.util.Objects;
54 
55 /**
56  * StatsLog-backed implementation of {@link ThemesUserEventLogger}.
57  */
58 public class StatsLogUserEventLogger extends NoOpUserEventLogger implements ThemesUserEventLogger {
59 
60     private static final String TAG = "StatsLogUserEventLogger";
61 
62     @Override
logAppLaunched(Intent launchSource)63     public void logAppLaunched(Intent launchSource) {
64         SysUiStatsLog.write(STYLE_UI_CHANGED, STYLE_UICHANGED__ACTION__APP_LAUNCHED, 0, 0, 0, 0, 0,
65                 0, 0, 0, 0, 0, getAppLaunchSource(launchSource));
66     }
67 
68     @Override
logResumed(boolean provisioned, boolean wallpaper)69     public void logResumed(boolean provisioned, boolean wallpaper) {
70         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.ONRESUME, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71                 0);
72     }
73 
74     @Override
logStopped()75     public void logStopped() {
76         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.ONSTOP, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
77     }
78 
79     @Override
logActionClicked(String collectionId, int actionLabelResId)80     public void logActionClicked(String collectionId, int actionLabelResId) {
81         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.WALLPAPER_EXPLORE, 0, 0, 0, 0, 0,
82                 getCollectionIdHashCode(collectionId), 0, 0, 0, 0, 0);
83     }
84 
85     @Override
logIndividualWallpaperSelected(String collectionId)86     public void logIndividualWallpaperSelected(String collectionId) {
87         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.WALLPAPER_SELECT, 0, 0, 0, 0, 0,
88                 getCollectionIdHashCode(collectionId), 0, 0, 0, 0, 0);
89     }
90 
91     @Override
logCategorySelected(String collectionId)92     public void logCategorySelected(String collectionId) {
93         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.WALLPAPER_OPEN_CATEGORY,
94                 0, 0, 0, 0, 0,
95                 getCollectionIdHashCode(collectionId),
96                 0, 0, 0, 0, 0);
97     }
98 
99     @Override
logLiveWallpaperInfoSelected(String collectionId, @Nullable String wallpaperId)100     public void logLiveWallpaperInfoSelected(String collectionId, @Nullable String wallpaperId) {
101         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.LIVE_WALLPAPER_INFO_SELECT,
102                 0, 0, 0, 0, 0,
103                 getCollectionIdHashCode(collectionId),
104                 wallpaperId != null ? wallpaperId.hashCode() : 0,
105                 0, 0, 0, 0);
106     }
107 
108     @Override
logLiveWallpaperCustomizeSelected(String collectionId, @Nullable String wallpaperId)109     public void logLiveWallpaperCustomizeSelected(String collectionId,
110             @Nullable String wallpaperId) {
111         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.LIVE_WALLPAPER_CUSTOMIZE_SELECT,
112                 0, 0, 0, 0, 0,
113                 getCollectionIdHashCode(collectionId),
114                 wallpaperId != null ? wallpaperId.hashCode() : 0,
115                 0, 0, 0, 0);
116     }
117 
118     @Override
logWallpaperSet(String collectionId, @Nullable String wallpaperId)119     public void logWallpaperSet(String collectionId, @Nullable String wallpaperId) {
120         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.WALLPAPER_APPLIED,
121                 0, 0, 0, 0, 0,
122                 getCollectionIdHashCode(collectionId),
123                 wallpaperId != null ? wallpaperId.hashCode() : 0,
124                 0, 0, 0, 0);
125     }
126 
127     @Nullable
getThemePackage(ThemeBundle theme, String category)128     private String getThemePackage(ThemeBundle theme, String category) {
129         Map<String, String> packages = theme.getPackagesByCategory();
130         return packages.get(category);
131     }
132 
133     @Override
logThemeSelected(ThemeBundle theme, boolean isCustomTheme)134     public void logThemeSelected(ThemeBundle theme, boolean isCustomTheme) {
135         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.PICKER_SELECT,
136                 Objects.hashCode(getThemePackage(theme, OVERLAY_CATEGORY_COLOR)),
137                 Objects.hashCode(getThemePackage(theme,OVERLAY_CATEGORY_FONT)),
138                 Objects.hashCode(getThemePackage(theme, OVERLAY_CATEGORY_SHAPE)),
139                 0, 0, 0, 0, 0, 0, 0, 0);
140     }
141 
142     @Override
logThemeApplied(ThemeBundle theme, boolean isCustomTheme)143     public void logThemeApplied(ThemeBundle theme, boolean isCustomTheme) {
144         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.PICKER_APPLIED,
145                 Objects.hashCode(getThemePackage(theme, OVERLAY_CATEGORY_COLOR)),
146                 Objects.hashCode(getThemePackage(theme,OVERLAY_CATEGORY_FONT)),
147                 Objects.hashCode(getThemePackage(theme, OVERLAY_CATEGORY_SHAPE)),
148                 0, 0, 0, 0, 0, 0, 0, 0);
149     }
150 
151     @Override
logColorApplied(int action, int colorIndex)152     public void logColorApplied(int action, int colorIndex) {
153         SysUiStatsLog.write(STYLE_UI_CHANGED, action,
154                 0, 0, 0, 0, 0, 0, 0,
155                 colorIndex,
156                 0, 0, 0);
157     }
158 
159     @Override
logClockSelected(Clockface clock)160     public void logClockSelected(Clockface clock) {
161         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.PICKER_SELECT,
162                 0, 0, 0,
163                 Objects.hashCode(clock.getId()),
164                 0, 0, 0, 0, 0, 0, 0);
165     }
166 
167     @Override
logClockApplied(Clockface clock)168     public void logClockApplied(Clockface clock) {
169         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.PICKER_APPLIED,
170                 0, 0, 0,
171                 Objects.hashCode(clock.getId()),
172                 0, 0, 0, 0, 0, 0, 0);
173     }
174 
175     @Override
logGridSelected(GridOption grid)176     public void logGridSelected(GridOption grid) {
177         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.PICKER_SELECT,
178                 0, 0, 0, 0,
179                 grid.cols,
180                 0, 0, 0, 0, 0, 0);
181     }
182 
183     @Override
logGridApplied(GridOption grid)184     public void logGridApplied(GridOption grid) {
185         SysUiStatsLog.write(STYLE_UI_CHANGED, StyleEnums.PICKER_APPLIED,
186                 0, 0, 0, 0,
187                 grid.cols,
188                 0, 0, 0, 0, 0, 0);
189     }
190 
getAppLaunchSource(Intent launchSource)191     private int getAppLaunchSource(Intent launchSource) {
192         if (launchSource.hasExtra(WALLPAPER_LAUNCH_SOURCE)) {
193             switch (launchSource.getStringExtra(WALLPAPER_LAUNCH_SOURCE)) {
194                 case LAUNCH_SOURCE_LAUNCHER:
195                     return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCHER;
196                 case LAUNCH_SOURCE_SETTINGS:
197                     return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS;
198                 case LAUNCH_SOURCE_SUW:
199                     return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SUW;
200                 case LAUNCH_SOURCE_TIPS:
201                     return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_TIPS;
202                 case LAUNCH_SOURCE_DEEP_LINK:
203                     return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_DEEP_LINK;
204                 default:
205                     return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED;
206             }
207         } else if (launchSource.hasExtra(LAUNCH_SETTINGS_SEARCH)) {
208             return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_SETTINGS_SEARCH;
209         } else if (launchSource.getAction() != null && launchSource.getAction().equals(
210                 WallpaperManager.ACTION_CROP_AND_SET_WALLPAPER)) {
211             return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_CROP_AND_SET_ACTION;
212         } else if (launchSource.getCategories() != null
213                 && launchSource.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
214             return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_LAUNCH_ICON;
215         } else {
216             return STYLE_UICHANGED__LAUNCHED_PREFERENCE__LAUNCHED_PREFERENCE_UNSPECIFIED;
217         }
218     }
219 
getCollectionIdHashCode(String collectionId)220     private int getCollectionIdHashCode(String collectionId) {
221         return collectionId != null ? collectionId.hashCode() : 0;
222     }
223 }
224