1 /*
2  * Copyright (C) 2020 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.wallpaper.picker;
17 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.net.Uri;
21 import android.text.TextUtils;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 
26 import com.android.wallpaper.model.LiveWallpaperInfo;
27 import com.android.wallpaper.model.WallpaperInfo;
28 import com.android.wallpaper.module.ExploreIntentChecker;
29 import com.android.wallpaper.module.InjectorProvider;
30 import com.android.wallpaper.util.ActivityUtils;
31 
32 /** A helper class for wallpaper info. */
33 public class WallpaperInfoHelper {
34 
35     /** A callback for receiving explore Intent. */
36     public interface ExploreIntentReceiver {
37         /** Gets called when received explore Intent. */
onReceiveExploreIntent(CharSequence actionLabel, @Nullable Intent exploreIntent)38         void onReceiveExploreIntent(CharSequence actionLabel, @Nullable Intent exploreIntent);
39     }
40 
41     /** Loads the explore Intent from the specific wallpaper. */
loadExploreIntent( Context context, @NonNull WallpaperInfo wallpaperInfo, @NonNull ExploreIntentReceiver callback)42     public static void loadExploreIntent(
43             Context context,
44             @NonNull WallpaperInfo wallpaperInfo,
45             @NonNull ExploreIntentReceiver callback) {
46         String actionUrl = wallpaperInfo.getActionUrl(context);
47         CharSequence actionLabel = getActionLabel(context, wallpaperInfo);
48         if (actionUrl != null && !actionUrl.isEmpty()) {
49             Uri exploreUri = Uri.parse(wallpaperInfo.getActionUrl(context));
50             ExploreIntentChecker intentChecker =
51                     InjectorProvider.getInjector().getExploreIntentChecker(context);
52             intentChecker.fetchValidActionViewIntent(exploreUri,
53                     intent -> callback.onReceiveExploreIntent(actionLabel, intent));
54         } else {
55             callback.onReceiveExploreIntent(actionLabel, null);
56         }
57     }
58 
59     /** Indicates if the explore button should show up in the wallpaper info view. */
shouldShowExploreButton(Context context, @Nullable Intent exploreIntent)60     public static boolean shouldShowExploreButton(Context context, @Nullable Intent exploreIntent) {
61         return exploreIntent != null && !ActivityUtils.isSUWMode(context);
62     }
63 
getActionLabel(Context context, WallpaperInfo wallpaperInfo)64     private static CharSequence getActionLabel(Context context, WallpaperInfo wallpaperInfo) {
65         CharSequence exploreLabel = null;
66         if (wallpaperInfo instanceof LiveWallpaperInfo) {
67             exploreLabel = ((LiveWallpaperInfo) wallpaperInfo).getActionDescription(context);
68         }
69         if (TextUtils.isEmpty(exploreLabel)) {
70             exploreLabel = context.getString(wallpaperInfo.getActionLabelRes(context));
71         }
72         return exploreLabel;
73     }
74 
WallpaperInfoHelper()75     private WallpaperInfoHelper() {}
76 }
77