1 /*
2  * Copyright (C) 2018 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.applications.managedomainurls;
18 
19 import android.content.Context;
20 import android.content.pm.verify.domain.DomainVerificationManager;
21 import android.content.pm.verify.domain.DomainVerificationUserState;
22 import android.util.IconDrawableFactory;
23 
24 import com.android.settings.R;
25 import com.android.settings.applications.intentpicker.IntentPickerUtils;
26 import com.android.settingslib.applications.ApplicationsState.AppEntry;
27 import com.android.settingslib.widget.AppPreference;
28 
29 public class DomainAppPreference extends AppPreference {
30 
31     private final AppEntry mEntry;
32     private final DomainVerificationManager mDomainVerificationManager;
33     private final IconDrawableFactory mIconDrawableFactory;
34 
DomainAppPreference(final Context context, IconDrawableFactory iconFactory, AppEntry entry)35     public DomainAppPreference(final Context context, IconDrawableFactory iconFactory,
36             AppEntry entry) {
37         super(context);
38         mIconDrawableFactory = iconFactory;
39         mDomainVerificationManager = context.getSystemService(DomainVerificationManager.class);
40         mEntry = entry;
41         mEntry.ensureLabel(getContext());
42 
43         setState();
44     }
45 
reuse()46     public void reuse() {
47         setState();
48         notifyChanged();
49     }
50 
getEntry()51     public AppEntry getEntry() {
52         return mEntry;
53     }
54 
setState()55     private void setState() {
56         setTitle(mEntry.label);
57         setIcon(mIconDrawableFactory.getBadgedIcon(mEntry.info));
58         setSummary(getDomainsSummary(mEntry.info.packageName));
59     }
60 
getDomainsSummary(String packageName)61     private CharSequence getDomainsSummary(String packageName) {
62         return getContext().getText(isLinkHandlingAllowed(packageName)
63                 ? R.string.app_link_open_always : R.string.app_link_open_never);
64     }
65 
isLinkHandlingAllowed(String packageName)66     private boolean isLinkHandlingAllowed(String packageName) {
67         final DomainVerificationUserState userState =
68                 IntentPickerUtils.getDomainVerificationUserState(mDomainVerificationManager,
69                         packageName);
70         return userState == null ? false : userState.isLinkHandlingAllowed();
71     }
72 }
73