1 /* 2 * Copyright (C) 2017 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.util; 17 18 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SETTINGS; 19 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SETTINGS_HOMEPAGE; 20 import static com.android.wallpaper.util.LaunchSourceUtils.LAUNCH_SOURCE_SETTINGS_SEARCH; 21 import static com.android.wallpaper.util.LaunchSourceUtils.WALLPAPER_LAUNCH_SOURCE; 22 23 import android.app.Activity; 24 import android.content.ActivityNotFoundException; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.provider.Settings; 28 import android.text.TextUtils; 29 import android.util.Log; 30 import android.widget.Toast; 31 32 import androidx.annotation.NonNull; 33 34 import com.android.wallpaper.R; 35 36 /** 37 * Various utilities pertaining to activities. 38 */ 39 public final class ActivityUtils { 40 private static final int SUW_NOT_YET = 0; 41 private static final int SUW_COMPLETE = 1; 42 43 /** 44 * Starts an activity with the given intent "safely" - i.e., catches exceptions that may occur 45 * and displays a toast to the user in response to such issues. 46 * 47 * @param activity 48 * @param intent 49 * @param requestCode 50 */ startActivityForResultSafely( Activity activity, Intent intent, int requestCode)51 public static void startActivityForResultSafely( 52 Activity activity, Intent intent, int requestCode) { 53 try { 54 activity.startActivityForResult(intent, requestCode); 55 } catch (ActivityNotFoundException e) { 56 Toast.makeText(activity, R.string.app_not_found, Toast.LENGTH_SHORT).show(); 57 } catch (SecurityException e) { 58 Toast.makeText(activity, R.string.app_not_found, Toast.LENGTH_SHORT).show(); 59 Log.e("Wallpaper", "Wallpaper does not have the permission to launch " + intent 60 + ". Make sure to create a MAIN intent-filter for the corresponding activity " 61 + "or use the exported attribute for this activity.", e); 62 } 63 } 64 65 /** 66 * Returns true if wallpaper launch source is from Settings related. 67 * 68 * @param intent activity intent. 69 */ isLaunchedFromSettingsRelated(Intent intent)70 public static boolean isLaunchedFromSettingsRelated(Intent intent) { 71 if (intent == null) { 72 return false; 73 } 74 75 return isLaunchedFromSettings(intent) || isLaunchedFromSettingsSearch(intent); 76 } 77 78 /** 79 * Checks if the Activity's launch source is from Settings' trampoline. 80 * 81 * @param intent intent to start the Activity 82 * @return {@code true} if the Activity's launch source is from Settings' trampoline. 83 */ isLaunchedFromSettingsTrampoline(Intent intent)84 public static boolean isLaunchedFromSettingsTrampoline(Intent intent) { 85 return isLaunchedFromSettingsHome(intent); 86 } 87 88 /** 89 * Returns true if wallpaper launch source is from Settings. 90 * 91 * @param intent activity intent. 92 */ isLaunchedFromSettings(@onNull Intent intent)93 private static boolean isLaunchedFromSettings(@NonNull Intent intent) { 94 return TextUtils.equals(LAUNCH_SOURCE_SETTINGS, 95 intent.getStringExtra(WALLPAPER_LAUNCH_SOURCE)); 96 } 97 98 isLaunchedFromSettingsHome(Intent intent)99 private static boolean isLaunchedFromSettingsHome(Intent intent) { 100 return (intent != null && intent.getBooleanExtra(LAUNCH_SOURCE_SETTINGS_HOMEPAGE, false)); 101 } 102 103 /** 104 * Returns true if wallpaper launch source is from Settings Search. 105 * 106 * @param intent activity intent. 107 */ isLaunchedFromSettingsSearch(@onNull Intent intent)108 public static boolean isLaunchedFromSettingsSearch(@NonNull Intent intent) { 109 return TextUtils.equals(LAUNCH_SOURCE_SETTINGS_SEARCH, 110 intent.getStringExtra(WALLPAPER_LAUNCH_SOURCE)); 111 } 112 113 114 /** 115 * Returns true if wallpaper is in SUW mode. 116 * 117 * @param context activity's context. 118 */ isSUWMode(Context context)119 public static boolean isSUWMode(Context context) { 120 return (Settings.Secure.getInt( 121 context.getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, SUW_COMPLETE) 122 == SUW_NOT_YET); 123 } 124 125 /** 126 * Returns true if it's wallpaper only mode. 127 * 128 * @param intent activity intent. 129 */ isWallpaperOnlyMode(Intent intent)130 public static boolean isWallpaperOnlyMode(Intent intent) { 131 return "wallpaper_only".equals( 132 intent.getStringExtra("com.android.launcher3.WALLPAPER_FLAVOR")); 133 } 134 135 /** 136 * Returns {@code true} if the activity was launched from the home screen (launcher); 137 * {@code false} otherwise. 138 */ isLaunchedFromLauncher(Intent intent)139 public static boolean isLaunchedFromLauncher(Intent intent) { 140 return LaunchSourceUtils.LAUNCH_SOURCE_LAUNCHER.equals( 141 intent.getStringExtra(WALLPAPER_LAUNCH_SOURCE)); 142 } 143 }