1 /* 2 * Copyright (C) 2021 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 package com.android.settings.applications.intentpicker; 17 18 import android.content.Context; 19 import android.content.pm.PackageManager; 20 import android.content.pm.verify.domain.DomainOwner; 21 import android.text.TextUtils; 22 import android.util.Log; 23 24 import com.android.settings.R; 25 26 import java.util.List; 27 import java.util.SortedSet; 28 import java.util.stream.Collectors; 29 30 /** 31 * A buffer of the supported link data. This {@link SupportedLinkWrapper} wraps the host, enabled 32 * and a list of {@link DomainOwner}. 33 */ 34 public class SupportedLinkWrapper implements Comparable<SupportedLinkWrapper> { 35 private static final String TAG = "SupportedLinkWrapper"; 36 37 private String mHost; 38 private SortedSet<DomainOwner> mOwnerSet; 39 private boolean mIsEnabled; 40 private String mLastOwnerName; 41 private boolean mIsChecked; 42 SupportedLinkWrapper(Context context, String host, SortedSet<DomainOwner> ownerSet)43 public SupportedLinkWrapper(Context context, String host, SortedSet<DomainOwner> ownerSet) { 44 mHost = host; 45 mOwnerSet = ownerSet; 46 mIsEnabled = true; 47 mLastOwnerName = ""; 48 mIsChecked = false; 49 init(context); 50 } 51 init(Context context)52 private void init(Context context) { 53 if (mOwnerSet.size() > 0) { 54 final long nonOverirideableNo = mOwnerSet.stream() 55 .filter(it -> !it.isOverrideable()) 56 .count(); 57 mIsEnabled = (nonOverirideableNo == 0L); 58 if (nonOverirideableNo > 0L) { 59 mLastOwnerName = getLastPackageLabel(context, false); 60 } else { 61 mLastOwnerName = getLastPackageLabel(context, true); 62 } 63 } 64 } 65 getLastPackageLabel(Context context, boolean isOverrideable)66 private String getLastPackageLabel(Context context, boolean isOverrideable) { 67 final List<String> labelList = mOwnerSet.stream() 68 .filter(it -> it.isOverrideable() == isOverrideable) 69 .map(it -> getLabel(context, it.getPackageName())) 70 .filter(label -> label != null) 71 .collect(Collectors.toList()); 72 return labelList.get(labelList.size() - 1); 73 } 74 getLabel(Context context, String pkg)75 private String getLabel(Context context, String pkg) { 76 try { 77 final PackageManager pm = context.getPackageManager(); 78 return pm.getApplicationInfo(pkg, /* flags= */ 0).loadLabel(pm).toString(); 79 } catch (PackageManager.NameNotFoundException e) { 80 Log.w(TAG, "getLabel error : " + e.getMessage()); 81 return null; 82 } 83 } 84 85 /** Returns the enabled/disabled value for list item. */ isEnabled()86 public boolean isEnabled() { 87 return mIsEnabled; 88 } 89 90 /** Returns the display format of list item in the Supported Links dialog */ getDisplayTitle(Context context)91 public String getDisplayTitle(Context context) { 92 if (TextUtils.isEmpty(mLastOwnerName) || context == null) { 93 return mHost; 94 } 95 return mHost + System.lineSeparator() + context.getString( 96 R.string.app_launch_supported_links_subtext, mLastOwnerName); 97 } 98 99 /** Returns the host name. */ getHost()100 public String getHost() { 101 return mHost; 102 } 103 104 /** Returns the checked value for list item. */ isChecked()105 public boolean isChecked() { 106 return mIsChecked; 107 } 108 109 /** Set the checked value. */ setChecked(boolean isChecked)110 public void setChecked(boolean isChecked) { 111 mIsChecked = isChecked; 112 } 113 114 @Override compareTo(SupportedLinkWrapper that)115 public int compareTo(SupportedLinkWrapper that) { 116 if (this.mIsEnabled != that.mIsEnabled) { 117 return this.mIsEnabled ? -1 : 1; 118 } 119 if (TextUtils.isEmpty(this.mLastOwnerName) != TextUtils.isEmpty(that.mLastOwnerName)) { 120 return TextUtils.isEmpty(this.mLastOwnerName) ? -1 : 1; 121 } 122 return 0; 123 } 124 } 125