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 17 package com.android.settings.webview; 18 19 import android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.PackageManager; 23 import android.os.RemoteException; 24 import android.util.Log; 25 import android.webkit.UserPackage; 26 import android.webkit.WebViewFactory; 27 import android.webkit.WebViewProviderInfo; 28 import android.widget.Toast; 29 30 import com.android.settings.R; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 public class WebViewUpdateServiceWrapper { 36 private static final String TAG = "WVUSWrapper"; 37 WebViewUpdateServiceWrapper()38 public WebViewUpdateServiceWrapper() { 39 } 40 41 /** 42 * Fetch the package currently used as WebView implementation. 43 */ getCurrentWebViewPackage()44 public PackageInfo getCurrentWebViewPackage() { 45 try { 46 return WebViewFactory.getUpdateService().getCurrentWebViewPackage(); 47 } catch (RemoteException e) { 48 Log.e(TAG, e.toString()); 49 } 50 return null; 51 } 52 53 /** 54 * Fetches ApplicationInfo objects for all currently valid WebView packages. 55 * A WebView package is considered valid if it can be used as a WebView implementation. The 56 * validity of a package is not dependent on whether the package is installed/enabled. 57 */ getValidWebViewApplicationInfos(Context context)58 public List<ApplicationInfo> getValidWebViewApplicationInfos(Context context) { 59 WebViewProviderInfo[] providers = null; 60 try { 61 providers = WebViewFactory.getUpdateService().getValidWebViewPackages(); 62 } catch (RemoteException e) { 63 } 64 List<ApplicationInfo> pkgs = new ArrayList<>(); 65 for (WebViewProviderInfo provider : providers) { 66 try { 67 pkgs.add(context.getPackageManager().getApplicationInfo( 68 provider.packageName, PACKAGE_FLAGS)); 69 } catch (PackageManager.NameNotFoundException e) { 70 } 71 } 72 return pkgs; 73 } 74 75 /** 76 * Change WebView provider to {@param packageName}. 77 * 78 * @return whether the change succeeded. 79 */ setWebViewProvider(String packageName)80 public boolean setWebViewProvider(String packageName) { 81 try { 82 return packageName.equals( 83 WebViewFactory.getUpdateService().changeProviderAndSetting(packageName)); 84 } catch (RemoteException e) { 85 Log.e(TAG, "RemoteException when trying to change provider to " + packageName, e); 86 } 87 return false; 88 } 89 90 /** 91 * Fetch PackageInfos for the package named {@param packageName} for all users on the device. 92 */ getPackageInfosAllUsers(Context context, String packageName)93 public List<UserPackage> getPackageInfosAllUsers(Context context, String packageName) { 94 return UserPackage.getPackageInfosAllUsers(context, packageName, PACKAGE_FLAGS); 95 } 96 97 /** 98 * Show a toast to explain the chosen package can no longer be chosen. 99 */ showInvalidChoiceToast(Context context)100 public void showInvalidChoiceToast(Context context) { 101 // The user chose a package that became invalid since the list was last updated, 102 // show a Toast to explain the situation. 103 Toast toast = Toast.makeText(context, 104 R.string.select_webview_provider_toast_text, Toast.LENGTH_SHORT); 105 toast.show(); 106 } 107 108 static final int PACKAGE_FLAGS = PackageManager.MATCH_ANY_USER; 109 } 110