1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.settings.webview;
16
17import android.content.Context;
18import android.content.pm.PackageInfo;
19import android.support.v7.preference.Preference;
20import android.support.v7.preference.PreferenceScreen;
21
22import com.android.settings.applications.defaultapps.DefaultAppInfo;
23import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
24
25public class WebViewAppPreferenceController extends DefaultAppPreferenceController {
26
27    private static final String WEBVIEW_APP_KEY = "select_webview_provider";
28
29    private final Context mContext;
30    private final WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
31    private Preference mPreference;
32
33    public WebViewAppPreferenceController(Context context) {
34        this(context, new WebViewUpdateServiceWrapper());
35    }
36
37    public WebViewAppPreferenceController(Context context,
38            WebViewUpdateServiceWrapper webviewUpdateServiceWrapper) {
39        super(context);
40        mContext = context;
41        mWebViewUpdateServiceWrapper = webviewUpdateServiceWrapper;
42    }
43
44    @Override
45    public DefaultAppInfo getDefaultAppInfo() {
46        PackageInfo currentPackage = mWebViewUpdateServiceWrapper.getCurrentWebViewPackage();
47        return new DefaultAppInfo(mPackageManager,
48                currentPackage == null ? null : currentPackage.applicationInfo);
49    }
50
51    @Override
52    public void displayPreference(PreferenceScreen screen) {
53        super.displayPreference(screen);
54        if (isAvailable()) {
55            mPreference = screen.findPreference(WEBVIEW_APP_KEY);
56        }
57    }
58
59    @Override
60    public String getPreferenceKey() {
61        return WEBVIEW_APP_KEY;
62    }
63
64    @Override
65    public boolean isAvailable() {
66        return true;
67    }
68
69    public void enablePreference(boolean enabled) {
70        if (isAvailable()) {
71            mPreference.setEnabled(enabled);
72        }
73    }
74}
75