1 /* 2 * Copyright 2018 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 com.example.androidx.webkit; 18 19 import android.app.Activity; 20 import android.content.pm.PackageInfo; 21 import android.widget.TextView; 22 23 import androidx.annotation.StringRes; 24 import androidx.webkit.WebViewCompat; 25 26 import org.jspecify.annotations.NonNull; 27 28 /** 29 * Static utility methods for the Webkit demo app. 30 */ 31 public final class WebkitHelpers { 32 /** 33 * Inserts the {@link android.webkit.WebView} version in the current Activity title. This 34 * assumes the title has already been set to something interesting, and we want to append the 35 * WebView version to the end of the title. 36 */ appendWebViewVersionToTitle(@onNull Activity activity)37 public static void appendWebViewVersionToTitle(@NonNull Activity activity) { 38 PackageInfo webViewPackage = WebViewCompat.getCurrentWebViewPackage(activity); 39 40 final String webViewVersion = webViewPackage != null 41 ? webViewPackage.versionName 42 : activity.getResources().getString(R.string.not_updateable_webview); 43 44 final String oldTitle = activity.getTitle().toString(); 45 final String newTitle = oldTitle + " (" + webViewVersion + ")"; 46 activity.setTitle(newTitle); 47 } 48 49 /** 50 * Replaces the entire view hierarchy of this {@link Activity} to show an error message. 51 * 52 * <p>Returns the {@link TextView} holding the error message, so callers can optionally add more 53 * functionality (ex. {@code setOnClickListener()}). 54 * 55 * @param activity the Activity to show the message in. 56 * @param messageResourceId the resource ID of the message to show. 57 * @return the {@link TextView} holding the error message. 58 */ showMessageInActivity(@onNull Activity activity, @StringRes int messageResourceId)59 public static @NonNull TextView showMessageInActivity(@NonNull Activity activity, 60 @StringRes int messageResourceId) { 61 TextView errorMessage = new TextView(activity); 62 errorMessage.setText(messageResourceId); 63 activity.setContentView(errorMessage); 64 return errorMessage; 65 } 66 67 // Do not instantiate this class. WebkitHelpers()68 private WebkitHelpers() {} 69 } 70