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