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 android.app.Activity;
19 import android.content.ActivityNotFoundException;
20 import android.content.Intent;
21 import android.util.Log;
22 import android.widget.Toast;
23 
24 import com.android.wallpaper.R;
25 
26 /**
27  * Various utilities pertaining to activities.
28  */
29 public final class ActivityUtils {
30 
31     /**
32      * Starts an activity with the given intent "safely" - i.e., catches exceptions that may occur
33      * and displays a toast to the user in response to such issues.
34      *
35      * @param activity
36      * @param intent
37      * @param requestCode
38      */
startActivityForResultSafely( Activity activity, Intent intent, int requestCode)39     public static void startActivityForResultSafely(
40             Activity activity, Intent intent, int requestCode) {
41         try {
42             activity.startActivityForResult(intent, requestCode);
43         } catch (ActivityNotFoundException e) {
44             Toast.makeText(activity, R.string.app_not_found, Toast.LENGTH_SHORT).show();
45         } catch (SecurityException e) {
46             Toast.makeText(activity, R.string.app_not_found, Toast.LENGTH_SHORT).show();
47             Log.e("Wallpaper", "Wallpaper does not have the permission to launch " + intent
48                     + ". Make sure to create a MAIN intent-filter for the corresponding activity "
49                     + "or use the exported attribute for this activity.", e);
50         }
51     }
52 }
53