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 static android.provider.Settings.ACTION_WEBVIEW_SETTINGS;
20 
21 import android.app.Activity;
22 import android.app.settings.SettingsEnums;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageInfo;
27 import android.content.pm.PackageItemInfo;
28 import android.content.pm.PackageManager;
29 import android.text.TextUtils;
30 import android.webkit.UserPackage;
31 
32 import androidx.annotation.VisibleForTesting;
33 
34 import com.android.settings.R;
35 import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
36 import com.android.settingslib.applications.DefaultAppInfo;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 public class WebViewAppPicker extends DefaultAppPickerFragment {
42     private WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
43 
getWebViewUpdateServiceWrapper()44     private WebViewUpdateServiceWrapper getWebViewUpdateServiceWrapper() {
45         if (mWebViewUpdateServiceWrapper == null) {
46             setWebViewUpdateServiceWrapper(createDefaultWebViewUpdateServiceWrapper());
47         }
48         return mWebViewUpdateServiceWrapper;
49     }
50 
51     @Override
onAttach(Context context)52     public void onAttach(Context context) {
53         super.onAttach(context);
54 
55         if (!mUserManager.isAdminUser()) {
56             getActivity().finish();
57         }
58     }
59 
60     @Override
getPreferenceScreenResId()61     protected int getPreferenceScreenResId() {
62         return R.xml.webview_app_settings;
63     }
64 
65     @Override
getCandidates()66     protected List<DefaultAppInfo> getCandidates() {
67         final List<DefaultAppInfo> packageInfoList = new ArrayList<DefaultAppInfo>();
68         final Context context = getContext();
69         final WebViewUpdateServiceWrapper webViewUpdateService = getWebViewUpdateServiceWrapper();
70         final List<ApplicationInfo> pkgs =
71                 webViewUpdateService.getValidWebViewApplicationInfos(context);
72         for (ApplicationInfo ai : pkgs) {
73             packageInfoList.add(createDefaultAppInfo(context, mPm, ai,
74                     getDisabledReason(webViewUpdateService, context, ai.packageName)));
75         }
76         return packageInfoList;
77     }
78 
79     @Override
getDefaultKey()80     protected String getDefaultKey() {
81         PackageInfo currentPackage = getWebViewUpdateServiceWrapper().getCurrentWebViewPackage();
82         return currentPackage == null ? null : currentPackage.packageName;
83     }
84 
setDefaultKey(String key)85     protected boolean setDefaultKey(String key) {
86         boolean success = getWebViewUpdateServiceWrapper().setWebViewProvider(key);
87         return success;
88     }
89 
90     @Override
onSelectionPerformed(boolean success)91     protected void onSelectionPerformed(boolean success) {
92         if (success) {
93             Activity activity = getActivity();
94             Intent intent = activity == null ? null : activity.getIntent();
95             if (intent != null && ACTION_WEBVIEW_SETTINGS.equals(intent.getAction())) {
96                 // If this was started through ACTION_WEBVIEW_SETTINGS then return once we have
97                 // chosen a new package.
98                 getActivity().finish();
99             }
100         } else {
101             getWebViewUpdateServiceWrapper().showInvalidChoiceToast(getActivity());
102             updateCandidates();
103         }
104     }
105 
createDefaultWebViewUpdateServiceWrapper()106     private WebViewUpdateServiceWrapper createDefaultWebViewUpdateServiceWrapper() {
107         return new WebViewUpdateServiceWrapper();
108     }
109 
110     @VisibleForTesting
setWebViewUpdateServiceWrapper(WebViewUpdateServiceWrapper wvusWrapper)111     void setWebViewUpdateServiceWrapper(WebViewUpdateServiceWrapper wvusWrapper) {
112         mWebViewUpdateServiceWrapper = wvusWrapper;
113     }
114 
115     @Override
getMetricsCategory()116     public int getMetricsCategory() {
117         return SettingsEnums.WEBVIEW_IMPLEMENTATION;
118     }
119 
120     private static class WebViewAppInfo extends DefaultAppInfo {
WebViewAppInfo(Context context, PackageManager pm, int userId, PackageItemInfo packageItemInfo, String summary, boolean enabled)121         public WebViewAppInfo(Context context, PackageManager pm, int userId,
122                 PackageItemInfo packageItemInfo, String summary, boolean enabled) {
123             super(context, pm, userId, packageItemInfo, summary, enabled);
124         }
125 
126         @Override
loadLabel()127         public CharSequence loadLabel() {
128             String versionName = "";
129             try {
130                 versionName = mPm.getPackageInfo(packageItemInfo.packageName, 0).versionName;
131             } catch (PackageManager.NameNotFoundException e) {
132             }
133             return String.format("%s %s", super.loadLabel(), versionName);
134         }
135     }
136 
137 
138     @VisibleForTesting
createDefaultAppInfo(Context context, PackageManager pm, PackageItemInfo packageItemInfo, String disabledReason)139     DefaultAppInfo createDefaultAppInfo(Context context, PackageManager pm,
140             PackageItemInfo packageItemInfo, String disabledReason) {
141         return new WebViewAppInfo(context, pm, mUserId, packageItemInfo, disabledReason,
142                 TextUtils.isEmpty(disabledReason) /* enabled */);
143     }
144 
145     /**
146      * Returns the reason why a package cannot be used as WebView implementation.
147      * This is either because of it being disabled, uninstalled, or hidden for any user.
148      */
149     @VisibleForTesting
getDisabledReason(WebViewUpdateServiceWrapper webviewUpdateServiceWrapper, Context context, String packageName)150     String getDisabledReason(WebViewUpdateServiceWrapper webviewUpdateServiceWrapper,
151             Context context, String packageName) {
152         List<UserPackage> userPackages =
153                 webviewUpdateServiceWrapper.getPackageInfosAllUsers(context, packageName);
154         for (UserPackage userPackage : userPackages) {
155             if (!userPackage.isInstalledPackage()) {
156                 // Package uninstalled/hidden
157                 return context.getString(
158                         R.string.webview_uninstalled_for_user, userPackage.getUserInfo().name);
159             } else if (!userPackage.isEnabledPackage()) {
160                 // Package disabled
161                 return context.getString(
162                         R.string.webview_disabled_for_user, userPackage.getUserInfo().name);
163             }
164         }
165         return null;
166     }
167 }
168