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