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.R;
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 = context.getString(R.string.explore);
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     /**
60      * Loads the explore Intent from the actionUrl
61      */
loadExploreIntent( Context context, @Nullable String actionUrl, @NonNull ExploreIntentReceiver callback)62     public static void loadExploreIntent(
63             Context context,
64             @Nullable String actionUrl,
65             @NonNull ExploreIntentReceiver callback) {
66         CharSequence actionLabel = context.getString(R.string.explore);
67 
68         if (!TextUtils.isEmpty(actionUrl)) {
69             Uri exploreUri = Uri.parse(actionUrl);
70             ExploreIntentChecker intentChecker =
71                     InjectorProvider.getInjector().getExploreIntentChecker(context);
72             intentChecker.fetchValidActionViewIntent(exploreUri,
73                     intent -> callback.onReceiveExploreIntent(actionLabel, intent));
74         } else {
75             callback.onReceiveExploreIntent(actionLabel, null);
76         }
77     }
78 
79     /** Indicates if the explore button should show up in the wallpaper info view. */
shouldShowExploreButton(Context context, @Nullable Intent exploreIntent)80     public static boolean shouldShowExploreButton(Context context, @Nullable Intent exploreIntent) {
81         return exploreIntent != null && !ActivityUtils.isSUWMode(context);
82     }
83 
WallpaperInfoHelper()84     private WallpaperInfoHelper() {}
85 }
86