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.PackageManager;
21 import android.os.UserHandle;
22 import android.util.ArraySet;
23 import android.util.IconDrawableFactory;
24 import android.view.View;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settingslib.applications.ApplicationsState.AppEntry;
31 import com.android.settingslib.widget.apppreference.AppPreference;
32 
33 public class DomainAppPreference extends AppPreference {
34 
35     private final AppEntry mEntry;
36     private final PackageManager mPm;
37     private final IconDrawableFactory mIconDrawableFactory;
38 
DomainAppPreference(final Context context, IconDrawableFactory iconFactory, AppEntry entry)39     public DomainAppPreference(final Context context, IconDrawableFactory iconFactory,
40             AppEntry entry) {
41         super(context);
42         mIconDrawableFactory = iconFactory;
43         mPm = context.getPackageManager();
44         mEntry = entry;
45         mEntry.ensureLabel(getContext());
46 
47         setState();
48     }
49 
reuse()50     public void reuse() {
51         setState();
52         notifyChanged();
53     }
54 
getEntry()55     public AppEntry getEntry() {
56         return mEntry;
57     }
58 
setState()59     private void setState() {
60         setTitle(mEntry.label);
61         setIcon(mIconDrawableFactory.getBadgedIcon(mEntry.info));
62         setSummary(getDomainsSummary(mEntry.info.packageName));
63     }
64 
getDomainsSummary(String packageName)65     private CharSequence getDomainsSummary(String packageName) {
66         // If the user has explicitly said "no" for this package, that's the
67         // string we should show.
68         int domainStatus =
69                 mPm.getIntentVerificationStatusAsUser(packageName, UserHandle.myUserId());
70         if (domainStatus == PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER) {
71             return getContext().getText(R.string.domain_urls_summary_none);
72         }
73         // Otherwise, ask package manager for the domains for this package,
74         // and show the first one (or none if there aren't any).
75         final ArraySet<String> result = Utils.getHandledDomains(mPm, packageName);
76         if (result.isEmpty()) {
77             return getContext().getText(R.string.domain_urls_summary_none);
78         } else if (result.size() == 1) {
79             return getContext().getString(R.string.domain_urls_summary_one, result.valueAt(0));
80         } else {
81             return getContext().getString(R.string.domain_urls_summary_some, result.valueAt(0));
82         }
83     }
84 }
85