blob: fb48cd54ea1edf395da55856212e92fe00a83dad [file] [log] [blame]
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -07001/*
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
17package com.android.settings.development;
18
19import android.content.Context;
20import android.content.pm.PackageInfo;
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070021import android.text.TextUtils;
22import android.util.Log;
23
Fan Zhangde117042018-09-04 13:52:15 -070024import androidx.annotation.VisibleForTesting;
25import androidx.preference.Preference;
26
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070027import com.android.settings.R;
jeffreyhuang37df3d62017-10-06 11:37:56 -070028import com.android.settings.core.PreferenceControllerMixin;
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070029import com.android.settings.webview.WebViewUpdateServiceWrapper;
Dake Gufef14ca2018-02-20 17:02:12 -080030import com.android.settingslib.applications.DefaultAppInfo;
jeffreyhuang37df3d62017-10-06 11:37:56 -070031import com.android.settingslib.development.DeveloperOptionsPreferenceController;
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070032import com.android.settingslib.wrapper.PackageManagerWrapper;
33
Doris Ling4fbf04c2018-03-01 10:33:14 -080034public class WebViewAppPreferenceController extends DeveloperOptionsPreferenceController implements
35 PreferenceControllerMixin {
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070036
37 private static final String TAG = "WebViewAppPrefCtrl";
38 private static final String WEBVIEW_APP_KEY = "select_webview_provider";
39
40 private final PackageManagerWrapper mPackageManager;
41 private final WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
42
jeffreyhuange89dc362017-11-22 16:44:51 -080043 public WebViewAppPreferenceController(Context context) {
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070044 super(context);
45
46 mPackageManager = new PackageManagerWrapper(context.getPackageManager());
47 mWebViewUpdateServiceWrapper = new WebViewUpdateServiceWrapper();
48 }
49
50 @Override
51 public String getPreferenceKey() {
52 return WEBVIEW_APP_KEY;
53 }
54
55 @Override
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070056 public void updateState(Preference preference) {
57 final CharSequence defaultAppLabel = getDefaultAppLabel();
58 if (!TextUtils.isEmpty(defaultAppLabel)) {
59 mPreference.setSummary(defaultAppLabel);
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070060 } else {
61 Log.d(TAG, "No default app");
62 mPreference.setSummary(R.string.app_list_preference_none);
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070063 }
64 }
65
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070066 @VisibleForTesting
67 DefaultAppInfo getDefaultAppInfo() {
68 final PackageInfo currentPackage = mWebViewUpdateServiceWrapper.getCurrentWebViewPackage();
Fan Zhangefa76362017-10-17 11:35:37 -070069 return new DefaultAppInfo(mContext, mPackageManager,
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070070 currentPackage == null ? null : currentPackage.applicationInfo);
71 }
72
jeffreyhuangaa4bf8c2017-10-02 18:18:28 -070073 private CharSequence getDefaultAppLabel() {
74 final DefaultAppInfo app = getDefaultAppInfo();
75 return app.loadLabel();
76 }
77}