• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.drawable.Drawable;
23 
24 import androidx.preference.PreferenceViewHolder;
25 
26 import com.android.settings.R;
27 import com.android.settings.applications.intentpicker.IntentPickerUtils;
28 import com.android.settingslib.applications.AppUtils;
29 import com.android.settingslib.applications.ApplicationsState.AppEntry;
30 import com.android.settingslib.utils.ThreadUtils;
31 import com.android.settingslib.widget.AppPreference;
32 
33 public class DomainAppPreference extends AppPreference {
34 
35     private Drawable mCacheIcon;
36 
37     private final AppEntry mEntry;
38     private final DomainVerificationManager mDomainVerificationManager;
39 
DomainAppPreference(final Context context, AppEntry entry)40     public DomainAppPreference(final Context context, AppEntry entry) {
41         super(context);
42         mDomainVerificationManager = context.getSystemService(DomainVerificationManager.class);
43         mEntry = entry;
44         mEntry.ensureLabel(getContext());
45         mCacheIcon = AppUtils.getIconFromCache(mEntry);
46         setState();
47     }
48 
reuse()49     public void reuse() {
50         setState();
51         notifyChanged();
52     }
53 
getEntry()54     public AppEntry getEntry() {
55         return mEntry;
56     }
57 
setState()58     private void setState() {
59         setTitle(mEntry.label);
60 
61         if (mCacheIcon != null) {
62             setIcon(mCacheIcon);
63         } else {
64             setIcon(R.drawable.empty_icon);
65         }
66         setSummary(getDomainsSummary(mEntry.info.packageName));
67     }
68 
getDomainsSummary(String packageName)69     private CharSequence getDomainsSummary(String packageName) {
70         return getContext().getText(isLinkHandlingAllowed(packageName)
71                 ? R.string.app_link_open_always : R.string.app_link_open_never);
72     }
73 
isLinkHandlingAllowed(String packageName)74     private boolean isLinkHandlingAllowed(String packageName) {
75         final DomainVerificationUserState userState =
76                 IntentPickerUtils.getDomainVerificationUserState(mDomainVerificationManager,
77                         packageName);
78         return userState == null ? false : userState.isLinkHandlingAllowed();
79     }
80 
81     @Override
onBindViewHolder(PreferenceViewHolder view)82     public void onBindViewHolder(PreferenceViewHolder view) {
83         if (mCacheIcon == null) {
84             ThreadUtils.postOnBackgroundThread(() -> {
85                 final Drawable icon = AppUtils.getIcon(getContext(), mEntry);
86                 ThreadUtils.postOnMainThread(() -> {
87                     setIcon(icon);
88                     mCacheIcon = icon;
89                 });
90             });
91         }
92         super.onBindViewHolder(view);
93     }
94 }
95