| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 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; |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 21 | import android.text.TextUtils; |
| 22 | import android.util.Log; |
| 23 | |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 24 | import androidx.annotation.VisibleForTesting; |
| 25 | import androidx.preference.Preference; |
| 26 | |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 27 | import com.android.settings.R; |
| jeffreyhuang | 37df3d6 | 2017-10-06 11:37:56 -0700 | [diff] [blame] | 28 | import com.android.settings.core.PreferenceControllerMixin; |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 29 | import com.android.settings.webview.WebViewUpdateServiceWrapper; |
| Dake Gu | fef14ca | 2018-02-20 17:02:12 -0800 | [diff] [blame] | 30 | import com.android.settingslib.applications.DefaultAppInfo; |
| jeffreyhuang | 37df3d6 | 2017-10-06 11:37:56 -0700 | [diff] [blame] | 31 | import com.android.settingslib.development.DeveloperOptionsPreferenceController; |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 32 | import com.android.settingslib.wrapper.PackageManagerWrapper; |
| 33 | |
| Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 34 | public class WebViewAppPreferenceController extends DeveloperOptionsPreferenceController implements |
| 35 | PreferenceControllerMixin { |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 36 | |
| 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 | |
| jeffreyhuang | e89dc36 | 2017-11-22 16:44:51 -0800 | [diff] [blame] | 43 | public WebViewAppPreferenceController(Context context) { |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 44 | 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 |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 56 | public void updateState(Preference preference) { |
| 57 | final CharSequence defaultAppLabel = getDefaultAppLabel(); |
| 58 | if (!TextUtils.isEmpty(defaultAppLabel)) { |
| 59 | mPreference.setSummary(defaultAppLabel); |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 60 | } else { |
| 61 | Log.d(TAG, "No default app"); |
| 62 | mPreference.setSummary(R.string.app_list_preference_none); |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 66 | @VisibleForTesting |
| 67 | DefaultAppInfo getDefaultAppInfo() { |
| 68 | final PackageInfo currentPackage = mWebViewUpdateServiceWrapper.getCurrentWebViewPackage(); |
| Fan Zhang | efa7636 | 2017-10-17 11:35:37 -0700 | [diff] [blame] | 69 | return new DefaultAppInfo(mContext, mPackageManager, |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 70 | currentPackage == null ? null : currentPackage.applicationInfo); |
| 71 | } |
| 72 | |
| jeffreyhuang | aa4bf8c | 2017-10-02 18:18:28 -0700 | [diff] [blame] | 73 | private CharSequence getDefaultAppLabel() { |
| 74 | final DefaultAppInfo app = getDefaultAppInfo(); |
| 75 | return app.loadLabel(); |
| 76 | } |
| 77 | } |