1 /* 2 * Copyright 2024 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 17 package androidx.pdf.util; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 23 import androidx.annotation.RestrictTo; 24 25 import org.jspecify.annotations.NonNull; 26 import org.jspecify.annotations.Nullable; 27 28 /** 29 * Utility functions for dealing with intents. 30 */ 31 @RestrictTo(RestrictTo.Scope.LIBRARY) 32 public class Intents { 33 34 /** 35 * Private constructor to prevent instantiation 36 */ Intents()37 private Intents() {} 38 private static final String TAG = Intents.class.getSimpleName(); 39 40 /** A safe version of {@link Context#startActivity} that handles exceptions. */ startActivity(@onNull Context context, @NonNull String logTag, @NonNull Intent intent)41 public static boolean startActivity(@NonNull Context context, @NonNull String logTag, 42 @NonNull Intent intent) { 43 try { 44 context.startActivity(intent); 45 return true; 46 } catch (Exception e) { 47 return false; 48 } 49 } 50 51 /** 52 * 53 */ toLongString(@ullable Intent intent)54 public static @NonNull String toLongString(@Nullable Intent intent) { 55 StringBuilder builder = new StringBuilder(); 56 String separator = ", "; 57 if (intent != null) { 58 builder.append(intent.getAction()); 59 builder.append(separator); 60 ComponentName component = intent.getComponent(); 61 if (component != null) { 62 builder.append(intent.getComponent().getShortClassName()); 63 builder.append(separator); 64 builder.append(intent.getComponent().getPackageName()); 65 } else { 66 builder.append("component null"); 67 } 68 } 69 return builder.toString(); 70 } 71 } 72