| /* |
| * Copyright (C) 2017 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package com.android.wallpaper.util; |
| |
| import android.app.Activity; |
| import android.content.ActivityNotFoundException; |
| import android.content.Intent; |
| import android.util.Log; |
| import android.widget.Toast; |
| |
| import com.android.wallpaper.R; |
| |
| /** |
| * Various utilities pertaining to activities. |
| */ |
| public final class ActivityUtils { |
| |
| /** |
| * Starts an activity with the given intent "safely" - i.e., catches exceptions that may occur |
| * and displays a toast to the user in response to such issues. |
| * |
| * @param activity |
| * @param intent |
| * @param requestCode |
| */ |
| public static void startActivityForResultSafely( |
| Activity activity, Intent intent, int requestCode) { |
| try { |
| activity.startActivityForResult(intent, requestCode); |
| } catch (ActivityNotFoundException e) { |
| Toast.makeText(activity, R.string.app_not_found, Toast.LENGTH_SHORT).show(); |
| } catch (SecurityException e) { |
| Toast.makeText(activity, R.string.app_not_found, Toast.LENGTH_SHORT).show(); |
| Log.e("Wallpaper", "Wallpaper does not have the permission to launch " + intent |
| + ". Make sure to create a MAIN intent-filter for the corresponding activity " |
| + "or use the exported attribute for this activity.", e); |
| } |
| } |
| } |