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.development;
18 
19 import android.content.Context;
20 import android.content.pm.PackageInfo;
21 import android.content.pm.PackageManager;
22 import android.os.UserHandle;
23 import android.text.TextUtils;
24 import android.util.Log;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settings.webview.WebViewUpdateServiceWrapper;
32 import com.android.settingslib.applications.DefaultAppInfo;
33 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
34 
35 public class WebViewAppPreferenceController extends DeveloperOptionsPreferenceController implements
36         PreferenceControllerMixin {
37 
38     private static final String TAG = "WebViewAppPrefCtrl";
39     private static final String WEBVIEW_APP_KEY = "select_webview_provider";
40 
41     private final PackageManager mPackageManager;
42     private final WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
43 
WebViewAppPreferenceController(Context context)44     public WebViewAppPreferenceController(Context context) {
45         super(context);
46 
47         mPackageManager = context.getPackageManager();
48         mWebViewUpdateServiceWrapper = new WebViewUpdateServiceWrapper();
49     }
50 
51     @Override
getPreferenceKey()52     public String getPreferenceKey() {
53         return WEBVIEW_APP_KEY;
54     }
55 
56     @Override
updateState(Preference preference)57     public void updateState(Preference preference) {
58         final CharSequence defaultAppLabel = getDefaultAppLabel();
59         if (!TextUtils.isEmpty(defaultAppLabel)) {
60             mPreference.setSummary(defaultAppLabel);
61         } else {
62             Log.d(TAG, "No default app");
63             mPreference.setSummary(R.string.app_list_preference_none);
64         }
65     }
66 
67     @VisibleForTesting
getDefaultAppInfo()68     DefaultAppInfo getDefaultAppInfo() {
69         final PackageInfo currentPackage = mWebViewUpdateServiceWrapper.getCurrentWebViewPackage();
70         return new DefaultAppInfo(mContext, mPackageManager, UserHandle.myUserId(),
71                 currentPackage == null ? null : currentPackage.applicationInfo);
72     }
73 
getDefaultAppLabel()74     private CharSequence getDefaultAppLabel() {
75         final DefaultAppInfo app = getDefaultAppInfo();
76         return app.loadLabel();
77     }
78 }
79