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